@openfn/language-dhis2 5.0.8 → 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ast.json +234 -43
- package/dist/index.cjs +172 -73
- package/dist/index.js +171 -76
- package/package.json +1 -1
- package/types/Adaptor.d.ts +102 -53
- package/types/Utils.d.ts +4 -1
package/dist/index.cjs
CHANGED
|
@@ -28,6 +28,7 @@ __export(src_exports, {
|
|
|
28
28
|
alterState: () => import_language_common3.alterState,
|
|
29
29
|
attr: () => attr,
|
|
30
30
|
create: () => create,
|
|
31
|
+
cursor: () => import_language_common3.cursor,
|
|
31
32
|
dataPath: () => import_language_common3.dataPath,
|
|
32
33
|
dataValue: () => import_language_common3.dataValue,
|
|
33
34
|
dateFns: () => import_language_common3.dateFns,
|
|
@@ -49,6 +50,7 @@ __export(src_exports, {
|
|
|
49
50
|
merge: () => import_language_common3.merge,
|
|
50
51
|
metadata: () => metadata_default,
|
|
51
52
|
patch: () => patch,
|
|
53
|
+
post: () => post,
|
|
52
54
|
sourceValue: () => import_language_common3.sourceValue,
|
|
53
55
|
update: () => update,
|
|
54
56
|
upsert: () => upsert
|
|
@@ -63,6 +65,11 @@ var import_axios = __toESM(require("axios"), 1);
|
|
|
63
65
|
|
|
64
66
|
// src/Utils.js
|
|
65
67
|
var import_language_common = require("@openfn/language-common");
|
|
68
|
+
function shouldUseNewTracker(resourceType) {
|
|
69
|
+
return /^(enrollments|relationships|events|trackedEntities)$/.test(
|
|
70
|
+
resourceType
|
|
71
|
+
);
|
|
72
|
+
}
|
|
66
73
|
var CONTENT_TYPES = {
|
|
67
74
|
xml: "application/xml",
|
|
68
75
|
json: "application/json",
|
|
@@ -91,9 +98,8 @@ function handleResponse(result, state, callback) {
|
|
|
91
98
|
function prettyJson(data) {
|
|
92
99
|
return JSON.stringify(data, null, 2);
|
|
93
100
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return isArray(data) ? { [key]: data } : data;
|
|
101
|
+
function ensureArray(data, key) {
|
|
102
|
+
return Array.isArray(data) ? { [key]: data } : { [key]: [data] };
|
|
97
103
|
}
|
|
98
104
|
function generateUrl(configuration, options, resourceType, path = null) {
|
|
99
105
|
let { hostUrl, apiVersion } = configuration;
|
|
@@ -204,6 +210,7 @@ __export(Adaptor_exports, {
|
|
|
204
210
|
alterState: () => import_language_common3.alterState,
|
|
205
211
|
attr: () => attr,
|
|
206
212
|
create: () => create,
|
|
213
|
+
cursor: () => import_language_common3.cursor,
|
|
207
214
|
dataPath: () => import_language_common3.dataPath,
|
|
208
215
|
dataValue: () => import_language_common3.dataValue,
|
|
209
216
|
dateFns: () => import_language_common3.dateFns,
|
|
@@ -223,13 +230,15 @@ __export(Adaptor_exports, {
|
|
|
223
230
|
lastReferenceValue: () => import_language_common3.lastReferenceValue,
|
|
224
231
|
merge: () => import_language_common3.merge,
|
|
225
232
|
patch: () => patch,
|
|
233
|
+
post: () => post,
|
|
226
234
|
sourceValue: () => import_language_common3.sourceValue,
|
|
227
235
|
update: () => update,
|
|
228
236
|
upsert: () => upsert
|
|
229
237
|
});
|
|
230
238
|
var import_axios3 = __toESM(require("axios"), 1);
|
|
231
|
-
var import_lodash = __toESM(require("lodash"), 1);
|
|
232
239
|
var import_language_common2 = require("@openfn/language-common");
|
|
240
|
+
var import_util = require("@openfn/language-common/util");
|
|
241
|
+
var import_lodash = __toESM(require("lodash"), 1);
|
|
233
242
|
|
|
234
243
|
// src/Client.js
|
|
235
244
|
var import_axios2 = __toESM(require("axios"), 1);
|
|
@@ -263,10 +272,11 @@ function execute(...operations) {
|
|
|
263
272
|
return (state) => {
|
|
264
273
|
var _a;
|
|
265
274
|
const version = (_a = state.configuration) == null ? void 0 : _a.apiVersion;
|
|
266
|
-
if (+version
|
|
275
|
+
if (+version < 36) {
|
|
267
276
|
console.warn(
|
|
268
|
-
`WARNING: This adaptor is
|
|
277
|
+
`WARNING: This adaptor is INCOMPATIBLE with DHIS2 tracker API versions before v36. Some functionality may break. See https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/tracker.html`
|
|
269
278
|
);
|
|
279
|
+
}
|
|
270
280
|
return (0, import_language_common2.execute)(
|
|
271
281
|
configMigrationHelper,
|
|
272
282
|
...operations
|
|
@@ -326,21 +336,31 @@ import_axios3.default.interceptors.response.use(
|
|
|
326
336
|
});
|
|
327
337
|
}
|
|
328
338
|
);
|
|
329
|
-
function create(resourceType, data, options = {}, callback =
|
|
339
|
+
function create(resourceType, data, options = {}, callback = (s) => s) {
|
|
330
340
|
return (state) => {
|
|
331
341
|
console.log(`Preparing create operation...`);
|
|
332
|
-
const resolvedResourceType = (0,
|
|
333
|
-
const resolvedData = (0, import_language_common2.expandReferences)(data)(state);
|
|
334
|
-
const resolvedOptions = (0, import_language_common2.expandReferences)(options)(state);
|
|
342
|
+
const [resolvedResourceType, resolvedData, resolvedOptions] = (0, import_util.expandReferences)(state, resourceType, data, options);
|
|
335
343
|
const { params, requestConfig } = resolvedOptions;
|
|
336
344
|
const { configuration } = state;
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
345
|
+
let promise;
|
|
346
|
+
if (shouldUseNewTracker(resolvedResourceType)) {
|
|
347
|
+
promise = callNewTracker(
|
|
348
|
+
"create",
|
|
349
|
+
configuration,
|
|
350
|
+
resolvedOptions,
|
|
351
|
+
resolvedResourceType,
|
|
352
|
+
resolvedData
|
|
353
|
+
);
|
|
354
|
+
} else {
|
|
355
|
+
promise = request(configuration, {
|
|
356
|
+
method: "post",
|
|
357
|
+
url: generateUrl(configuration, resolvedOptions, resolvedResourceType),
|
|
358
|
+
params,
|
|
359
|
+
data: resolvedData,
|
|
360
|
+
...requestConfig
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
return promise.then((result) => {
|
|
344
364
|
const details = `with response ${JSON.stringify(result.data, null, 2)}`;
|
|
345
365
|
console.log(`Created ${resolvedResourceType} ${details}`);
|
|
346
366
|
const { location } = result.headers;
|
|
@@ -350,27 +370,36 @@ function create(resourceType, data, options = {}, callback = false) {
|
|
|
350
370
|
});
|
|
351
371
|
};
|
|
352
372
|
}
|
|
353
|
-
function update(resourceType, path, data, options = {}, callback =
|
|
373
|
+
function update(resourceType, path, data, options = {}, callback = (s) => s) {
|
|
354
374
|
return (state) => {
|
|
355
375
|
console.log(`Preparing update operation...`);
|
|
356
|
-
const resolvedResourceType = (0,
|
|
357
|
-
const
|
|
358
|
-
const resolvedData = (0, import_language_common2.expandReferences)(data)(state);
|
|
359
|
-
const resolvedOptions = (0, import_language_common2.expandReferences)(options)(state);
|
|
360
|
-
const { params, requestConfig } = resolvedOptions;
|
|
376
|
+
const [resolvedResourceType, resolvedPath, resolvedData, resolvedOptions] = (0, import_util.expandReferences)(state, resourceType, path, data, options);
|
|
377
|
+
const { requestConfig } = resolvedOptions;
|
|
361
378
|
const { configuration } = state;
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
379
|
+
let promise;
|
|
380
|
+
if (shouldUseNewTracker(resolvedResourceType)) {
|
|
381
|
+
promise = callNewTracker(
|
|
382
|
+
"update",
|
|
365
383
|
configuration,
|
|
366
384
|
resolvedOptions,
|
|
367
385
|
resolvedResourceType,
|
|
368
|
-
|
|
369
|
-
)
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
386
|
+
resolvedData
|
|
387
|
+
);
|
|
388
|
+
} else {
|
|
389
|
+
promise = request(configuration, {
|
|
390
|
+
method: "put",
|
|
391
|
+
url: generateUrl(
|
|
392
|
+
configuration,
|
|
393
|
+
resolvedOptions,
|
|
394
|
+
resolvedResourceType,
|
|
395
|
+
resolvedPath
|
|
396
|
+
),
|
|
397
|
+
options,
|
|
398
|
+
data: resolvedData,
|
|
399
|
+
...requestConfig
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
return promise.then((result) => {
|
|
374
403
|
console.log(`Updated ${resolvedResourceType} at ${resolvedPath}`);
|
|
375
404
|
return handleResponse(result, state, callback);
|
|
376
405
|
});
|
|
@@ -379,9 +408,7 @@ function update(resourceType, path, data, options = {}, callback = false) {
|
|
|
379
408
|
function get(resourceType, query, options = {}, callback = false) {
|
|
380
409
|
return (state) => {
|
|
381
410
|
console.log("Preparing get operation...");
|
|
382
|
-
const resolvedResourceType = (0,
|
|
383
|
-
const resolvedQuery = (0, import_language_common2.expandReferences)(query)(state);
|
|
384
|
-
const resolvedOptions = (0, import_language_common2.expandReferences)(options)(state);
|
|
411
|
+
const [resolvedResourceType, resolvedQuery, resolvedOptions] = (0, import_util.expandReferences)(state, resourceType, query, options);
|
|
385
412
|
const { params, requestConfig } = resolvedOptions;
|
|
386
413
|
const { configuration } = state;
|
|
387
414
|
return request(configuration, {
|
|
@@ -396,26 +423,59 @@ function get(resourceType, query, options = {}, callback = false) {
|
|
|
396
423
|
});
|
|
397
424
|
};
|
|
398
425
|
}
|
|
399
|
-
function
|
|
426
|
+
function post(resourceType, data, query, options = {}, callback = (s) => s) {
|
|
400
427
|
return (state) => {
|
|
401
|
-
console.log(
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
} else if (resources.length <= 0) {
|
|
413
|
-
return create(resourceType, data, options)(state);
|
|
414
|
-
} else {
|
|
415
|
-
const path = resources[0][selectId(resourceType)];
|
|
416
|
-
return update(resourceType, path, data, options)(state);
|
|
417
|
-
}
|
|
428
|
+
console.log("Preparing post operation...");
|
|
429
|
+
const [resolvedResourceType, resolvedQuery, resolvedOptions, resolvedData] = (0, import_util.expandReferences)(state, resourceType, query, options, data);
|
|
430
|
+
const { params, requestConfig } = resolvedOptions;
|
|
431
|
+
const { configuration } = state;
|
|
432
|
+
return request(configuration, {
|
|
433
|
+
method: "post",
|
|
434
|
+
url: generateUrl(configuration, resolvedOptions, resolvedResourceType),
|
|
435
|
+
params: { ...resolvedQuery, ...params },
|
|
436
|
+
responseType: "json",
|
|
437
|
+
data: resolvedData,
|
|
438
|
+
...requestConfig
|
|
418
439
|
}).then((result) => {
|
|
440
|
+
console.log(`Created ${resolvedResourceType}`);
|
|
441
|
+
return handleResponse(result, state, callback);
|
|
442
|
+
});
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
function upsert(resourceType, query, data, options = {}, callback = (s) => s) {
|
|
446
|
+
return (state) => {
|
|
447
|
+
const [resolvedResourceType, resolvedOptions, resolvedData] = (0, import_util.expandReferences)(state, resourceType, options, data);
|
|
448
|
+
let promise;
|
|
449
|
+
if (shouldUseNewTracker(resolvedResourceType)) {
|
|
450
|
+
const { configuration } = state;
|
|
451
|
+
promise = callNewTracker(
|
|
452
|
+
"create_and_update",
|
|
453
|
+
configuration,
|
|
454
|
+
resolvedOptions,
|
|
455
|
+
resolvedResourceType,
|
|
456
|
+
resolvedData
|
|
457
|
+
);
|
|
458
|
+
} else {
|
|
459
|
+
console.log(`Preparing upsert via 'get' then 'create' OR 'update'...`);
|
|
460
|
+
promise = get(
|
|
461
|
+
resourceType,
|
|
462
|
+
query,
|
|
463
|
+
options
|
|
464
|
+
)(state).then((resp) => {
|
|
465
|
+
const resources = resp.data[resourceType];
|
|
466
|
+
if (resources.length > 1) {
|
|
467
|
+
throw new RangeError(
|
|
468
|
+
`Cannot upsert on Non-unique attribute. The operation found more than one records for your request.`
|
|
469
|
+
);
|
|
470
|
+
} else if (resources.length <= 0) {
|
|
471
|
+
return create(resourceType, data, options)(state);
|
|
472
|
+
} else {
|
|
473
|
+
const path = resources[0][selectId(resourceType)];
|
|
474
|
+
return update(resourceType, path, data, options)(state);
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
return promise.then((result) => {
|
|
419
479
|
console.log(`Performed a "composed upsert" on ${resourceType}`);
|
|
420
480
|
return handleResponse(result, state, callback);
|
|
421
481
|
});
|
|
@@ -487,13 +547,10 @@ ${prettyJson(
|
|
|
487
547
|
});
|
|
488
548
|
};
|
|
489
549
|
}
|
|
490
|
-
function patch(resourceType, path, data, options = {}, callback =
|
|
550
|
+
function patch(resourceType, path, data, options = {}, callback = (s) => s) {
|
|
491
551
|
return (state) => {
|
|
492
552
|
console.log("Preparing patch operation...");
|
|
493
|
-
const resolvedResourceType = (0,
|
|
494
|
-
const resolvedPath = (0, import_language_common2.expandReferences)(path)(state);
|
|
495
|
-
const resolvedData = (0, import_language_common2.expandReferences)(data)(state);
|
|
496
|
-
const resolvedOptions = (0, import_language_common2.expandReferences)(options)(state);
|
|
553
|
+
const [resolvedResourceType, resolvedPath, resolvedData, resolvedOptions] = (0, import_util.expandReferences)(state, resourceType, path, data, options);
|
|
497
554
|
const { params, requestConfig } = resolvedOptions;
|
|
498
555
|
const { configuration } = state;
|
|
499
556
|
return request(configuration, {
|
|
@@ -516,32 +573,41 @@ function patch(resourceType, path, data, options = {}, callback = false) {
|
|
|
516
573
|
function destroy(resourceType, path, data = null, options = {}, callback = false) {
|
|
517
574
|
return (state) => {
|
|
518
575
|
console.log("Preparing destroy operation...");
|
|
519
|
-
const resolvedResourceType = (0,
|
|
520
|
-
const resolvedPath = (0, import_language_common2.expandReferences)(path)(state);
|
|
521
|
-
const resolvedData = (0, import_language_common2.expandReferences)(data)(state);
|
|
522
|
-
const resolvedOptions = (0, import_language_common2.expandReferences)(options)(state);
|
|
576
|
+
const [resolvedResourceType, resolvedPath, resolvedData, resolvedOptions] = (0, import_util.expandReferences)(state, resourceType, path, data, options);
|
|
523
577
|
const { params, requestConfig } = resolvedOptions;
|
|
524
578
|
const { configuration } = state;
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
579
|
+
let promise;
|
|
580
|
+
if (shouldUseNewTracker(resolvedResourceType)) {
|
|
581
|
+
promise = callNewTracker(
|
|
582
|
+
"delete",
|
|
528
583
|
configuration,
|
|
529
584
|
resolvedOptions,
|
|
530
585
|
resolvedResourceType,
|
|
531
|
-
|
|
532
|
-
)
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
586
|
+
resolvedData
|
|
587
|
+
);
|
|
588
|
+
} else {
|
|
589
|
+
promise = request({
|
|
590
|
+
method: "delete",
|
|
591
|
+
url: generateUrl(
|
|
592
|
+
configuration,
|
|
593
|
+
resolvedOptions,
|
|
594
|
+
resolvedResourceType,
|
|
595
|
+
resolvedPath
|
|
596
|
+
),
|
|
597
|
+
params,
|
|
598
|
+
resolvedData,
|
|
599
|
+
...requestConfig
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
return promise.then((result) => {
|
|
537
603
|
console.log(`Deleted ${resolvedResourceType} at ${resolvedPath}`);
|
|
538
604
|
return handleResponse(result, state, callback);
|
|
539
605
|
});
|
|
540
606
|
};
|
|
541
607
|
}
|
|
542
|
-
function findAttributeValue(
|
|
608
|
+
function findAttributeValue(trackedEntity, attributeDisplayName) {
|
|
543
609
|
var _a, _b;
|
|
544
|
-
return (_b = (_a =
|
|
610
|
+
return (_b = (_a = trackedEntity == null ? void 0 : trackedEntity.attributes) == null ? void 0 : _a.find(
|
|
545
611
|
(a) => (a == null ? void 0 : a.displayName.toLowerCase()) == attributeDisplayName.toLowerCase()
|
|
546
612
|
)) == null ? void 0 : _b.value;
|
|
547
613
|
}
|
|
@@ -551,6 +617,37 @@ function attr(attribute, value) {
|
|
|
551
617
|
function dv(dataElement, value) {
|
|
552
618
|
return { dataElement, value };
|
|
553
619
|
}
|
|
620
|
+
function callNewTracker(type = "update", configuration, options, resourceType, data = {}) {
|
|
621
|
+
const { params, requestConfig, ...opts } = options;
|
|
622
|
+
let importStrategy;
|
|
623
|
+
switch (type) {
|
|
624
|
+
case "create":
|
|
625
|
+
importStrategy = "CREATE";
|
|
626
|
+
break;
|
|
627
|
+
case "update":
|
|
628
|
+
importStrategy = "UPDATE";
|
|
629
|
+
break;
|
|
630
|
+
case "delete":
|
|
631
|
+
importStrategy = "DELETE";
|
|
632
|
+
break;
|
|
633
|
+
default:
|
|
634
|
+
importStrategy = "CREATE_AND_UPDATE";
|
|
635
|
+
}
|
|
636
|
+
return request(configuration, {
|
|
637
|
+
method: "post",
|
|
638
|
+
url: generateUrl(
|
|
639
|
+
configuration,
|
|
640
|
+
{
|
|
641
|
+
...opts,
|
|
642
|
+
importStrategy
|
|
643
|
+
},
|
|
644
|
+
"tracker"
|
|
645
|
+
),
|
|
646
|
+
params: { async: false, ...params },
|
|
647
|
+
data: ensureArray(data, resourceType),
|
|
648
|
+
...requestConfig
|
|
649
|
+
});
|
|
650
|
+
}
|
|
554
651
|
|
|
555
652
|
// src/index.js
|
|
556
653
|
var src_default = Adaptor_exports;
|
|
@@ -559,6 +656,7 @@ var src_default = Adaptor_exports;
|
|
|
559
656
|
alterState,
|
|
560
657
|
attr,
|
|
561
658
|
create,
|
|
659
|
+
cursor,
|
|
562
660
|
dataPath,
|
|
563
661
|
dataValue,
|
|
564
662
|
dateFns,
|
|
@@ -579,6 +677,7 @@ var src_default = Adaptor_exports;
|
|
|
579
677
|
merge,
|
|
580
678
|
metadata,
|
|
581
679
|
patch,
|
|
680
|
+
post,
|
|
582
681
|
sourceValue,
|
|
583
682
|
update,
|
|
584
683
|
upsert
|