-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtracker.js
More file actions
139 lines (126 loc) · 4.47 KB
/
Copy pathtracker.js
File metadata and controls
139 lines (126 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { expandReferences } from '@openfn/language-common/util';
import * as util from './util.js';
/**
* State object
* @typedef {Object} DHIS2State
* @private
* @property data - The response body (as JSON)
* @property response - The HTTP response from the DHIS2 server (excluding the body)
* @property references - An array of all previous data objects used in the Job
*/
/**
* All options, apart from those listed here, will be appended as query parameters to the URL
* @typedef {Object} TrackerOptions
* @property {string} [parseAs='json'] - The response format to parse (e.g., 'json', 'text', 'stream', or 'base64'. Defaults to `json`
*/
/**
* Import data into DHIS2 using the tracker endpoint.
* @alias import
* @public
* @example <caption>Import some data and pass the `atomicMode` parameter</caption>
* tracker.import('CREATE', $.trackerData, { atomicMode: 'ALL' })
* @example <caption>Import a trackedEntity resource</caption>
* tracker.import(
* 'CREATE',
* {
* trackedEntities: [
* {
* orgUnit: 'TSyzvBiovKh',
* trackedEntityType: 'nEenWmSyUEp',
* attributes: [
* {
* attribute: 'w75KJ2mc4zz',
* value: 'Gigiwe',
* },
* ],
* },
* ],
* },
* {
* atomicMode: 'ALL',
* }
* );
* @function
* @param {string} strategy - The effect the import should have. Can either be CREATE, UPDATE, CREATE_AND_UPDATE and DELETE.
* @param {object} payload - The data to be imported.
* @param {TrackerOptions} [options] - An optional object containing parseAs, and apiVersion, and queries for the request
* @param {boolean} [options.async=false] - Whether to perform the import asynchronously. Defaults to false. See [Sync and async imports](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/tracker.html#sync-and-async)
* @state {DHIS2State}
* @returns {Operation}
*/
function _import(strategy, payload, options = {}) {
return async state => {
console.log('Preparing tracker import operation...');
const [resolvedStrategy, resolvedPayload, resolvedOptions] =
expandReferences(state, strategy, payload, options);
const { apiVersion, parseAs, async = false, ...query } = resolvedOptions;
const response = await util.request(state.configuration, {
method: 'POST',
path: util.prefixVersionToPath(
state.configuration,
{
...resolvedOptions,
resolvedStrategy,
},
'tracker',
),
options: {
apiVersion,
parseAs,
query: {
...query,
async,
},
},
data: resolvedPayload,
});
return util.handleHttpResponse(response, state);
};
}
export { _import as import };
/**
* Export data from DHIS2.
* @alias export
* @public
* @example <caption>Export a trackedEntity resource using the id</caption>
* tracker.export('trackedEntities/Gu5UKnIFnJf')
* @example <caption>Export all enrollment resources</caption>
* tracker.export('enrollments', {orgUnit: 'TSyzvBiovKh'});
* @example <caption>Export all events</caption>
* tracker.export('events', { paging: false})
* @example <caption>Export the first page of events with pagination metadata</caption>
* tracker.export('events', { totalPages: true, pageSize: 1000, page: 1 })
* @function
* @param {string} path - Path to the resource, relative to the /tracker endpoint
* @param {object} query - An object of query parameters to be encoded into the URL. Can include [pagination parameters](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/tracker.html#request-parameters-for-pagination), filters, etc.
* @param {TrackerOptions} [options] - An optional object containing parseAs, and apiVersion for the request
* @state {DHIS2State}
* @returns {Operation}
*/
function _export(path, query = {}, options = {}) {
return async state => {
console.log('Preparing tracker export operation...');
const [resolvedPath, resolvedQuery, resolvedOptions] = expandReferences(
state,
path,
query,
options,
);
const response = await util.request(state.configuration, {
method: 'GET',
path: util.prefixVersionToPath(
state.configuration,
resolvedOptions,
`tracker/${resolvedPath}`,
),
options: {
...resolvedOptions,
query: {
...resolvedQuery,
},
},
});
return util.handleHttpResponse(response, state);
};
}
export { _export as export };