@openfn/language-dhis2 5.0.7 → 6.0.0

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/dist/index.js CHANGED
@@ -12,6 +12,11 @@ import axios from "axios";
12
12
 
13
13
  // src/Utils.js
14
14
  import { composeNextState } from "@openfn/language-common";
15
+ function shouldUseNewTracker(resourceType) {
16
+ return /^(enrollments|relationships|events|trackedEntities)$/.test(
17
+ resourceType
18
+ );
19
+ }
15
20
  var CONTENT_TYPES = {
16
21
  xml: "application/xml",
17
22
  json: "application/json",
@@ -40,9 +45,8 @@ function handleResponse(result, state, callback) {
40
45
  function prettyJson(data) {
41
46
  return JSON.stringify(data, null, 2);
42
47
  }
43
- var isArray = (variable) => !!variable && variable.constructor === Array;
44
- function nestArray(data, key) {
45
- return isArray(data) ? { [key]: data } : data;
48
+ function ensureArray(data, key) {
49
+ return Array.isArray(data) ? { [key]: data } : { [key]: [data] };
46
50
  }
47
51
  function generateUrl(configuration, options, resourceType, path = null) {
48
52
  let { hostUrl, apiVersion } = configuration;
@@ -153,6 +157,7 @@ __export(Adaptor_exports, {
153
157
  alterState: () => alterState,
154
158
  attr: () => attr,
155
159
  create: () => create,
160
+ cursor: () => cursor,
156
161
  dataPath: () => dataPath,
157
162
  dataValue: () => dataValue,
158
163
  dateFns: () => dateFns,
@@ -172,16 +177,15 @@ __export(Adaptor_exports, {
172
177
  lastReferenceValue: () => lastReferenceValue,
173
178
  merge: () => merge,
174
179
  patch: () => patch,
180
+ post: () => post,
175
181
  sourceValue: () => sourceValue,
176
182
  update: () => update,
177
183
  upsert: () => upsert
178
184
  });
179
185
  import axios3 from "axios";
186
+ import { execute as commonExecute } from "@openfn/language-common";
187
+ import { expandReferences } from "@openfn/language-common/util";
180
188
  import _ from "lodash";
181
- import {
182
- execute as commonExecute,
183
- expandReferences
184
- } from "@openfn/language-common";
185
189
 
186
190
  // src/Client.js
187
191
  import axios2 from "axios";
@@ -210,6 +214,7 @@ import {
210
214
  dataPath,
211
215
  dataValue,
212
216
  dateFns,
217
+ cursor,
213
218
  each,
214
219
  field,
215
220
  fields,
@@ -230,10 +235,11 @@ function execute(...operations) {
230
235
  return (state) => {
231
236
  var _a;
232
237
  const version = (_a = state.configuration) == null ? void 0 : _a.apiVersion;
233
- if (+version >= 42)
238
+ if (+version < 36) {
234
239
  console.warn(
235
- `WARNING: This adaptor is incompatible with DHIS2 API version 42+. See here: https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/tracker.html.`
240
+ `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`
236
241
  );
242
+ }
237
243
  return commonExecute(
238
244
  configMigrationHelper,
239
245
  ...operations
@@ -293,21 +299,31 @@ axios3.interceptors.response.use(
293
299
  });
294
300
  }
295
301
  );
296
- function create(resourceType, data, options = {}, callback = false) {
302
+ function create(resourceType, data, options = {}, callback = (s) => s) {
297
303
  return (state) => {
298
304
  console.log(`Preparing create operation...`);
299
- const resolvedResourceType = expandReferences(resourceType)(state);
300
- const resolvedData = expandReferences(data)(state);
301
- const resolvedOptions = expandReferences(options)(state);
305
+ const [resolvedResourceType, resolvedData, resolvedOptions] = expandReferences(state, resourceType, data, options);
302
306
  const { params, requestConfig } = resolvedOptions;
303
307
  const { configuration } = state;
304
- return request(configuration, {
305
- method: "post",
306
- url: generateUrl(configuration, resolvedOptions, resolvedResourceType),
307
- params,
308
- data: nestArray(resolvedData, resolvedResourceType),
309
- ...requestConfig
310
- }).then((result) => {
308
+ let promise;
309
+ if (shouldUseNewTracker(resolvedResourceType)) {
310
+ promise = callNewTracker(
311
+ "create",
312
+ configuration,
313
+ resolvedOptions,
314
+ resolvedResourceType,
315
+ resolvedData
316
+ );
317
+ } else {
318
+ promise = request(configuration, {
319
+ method: "post",
320
+ url: generateUrl(configuration, resolvedOptions, resolvedResourceType),
321
+ params,
322
+ data: resolvedData,
323
+ ...requestConfig
324
+ });
325
+ }
326
+ return promise.then((result) => {
311
327
  const details = `with response ${JSON.stringify(result.data, null, 2)}`;
312
328
  console.log(`Created ${resolvedResourceType} ${details}`);
313
329
  const { location } = result.headers;
@@ -317,27 +333,36 @@ function create(resourceType, data, options = {}, callback = false) {
317
333
  });
318
334
  };
319
335
  }
320
- function update(resourceType, path, data, options = {}, callback = false) {
336
+ function update(resourceType, path, data, options = {}, callback = (s) => s) {
321
337
  return (state) => {
322
338
  console.log(`Preparing update operation...`);
323
- const resolvedResourceType = expandReferences(resourceType)(state);
324
- const resolvedPath = expandReferences(path)(state);
325
- const resolvedData = expandReferences(data)(state);
326
- const resolvedOptions = expandReferences(options)(state);
327
- const { params, requestConfig } = resolvedOptions;
339
+ const [resolvedResourceType, resolvedPath, resolvedData, resolvedOptions] = expandReferences(state, resourceType, path, data, options);
340
+ const { requestConfig } = resolvedOptions;
328
341
  const { configuration } = state;
329
- return request(configuration, {
330
- method: "put",
331
- url: generateUrl(
342
+ let promise;
343
+ if (shouldUseNewTracker(resolvedResourceType)) {
344
+ promise = callNewTracker(
345
+ "update",
332
346
  configuration,
333
347
  resolvedOptions,
334
348
  resolvedResourceType,
335
- path
336
- ),
337
- params,
338
- data: resolvedData,
339
- ...requestConfig
340
- }).then((result) => {
349
+ resolvedData
350
+ );
351
+ } else {
352
+ promise = request(configuration, {
353
+ method: "put",
354
+ url: generateUrl(
355
+ configuration,
356
+ resolvedOptions,
357
+ resolvedResourceType,
358
+ resolvedPath
359
+ ),
360
+ options,
361
+ data: resolvedData,
362
+ ...requestConfig
363
+ });
364
+ }
365
+ return promise.then((result) => {
341
366
  console.log(`Updated ${resolvedResourceType} at ${resolvedPath}`);
342
367
  return handleResponse(result, state, callback);
343
368
  });
@@ -346,9 +371,7 @@ function update(resourceType, path, data, options = {}, callback = false) {
346
371
  function get(resourceType, query, options = {}, callback = false) {
347
372
  return (state) => {
348
373
  console.log("Preparing get operation...");
349
- const resolvedResourceType = expandReferences(resourceType)(state);
350
- const resolvedQuery = expandReferences(query)(state);
351
- const resolvedOptions = expandReferences(options)(state);
374
+ const [resolvedResourceType, resolvedQuery, resolvedOptions] = expandReferences(state, resourceType, query, options);
352
375
  const { params, requestConfig } = resolvedOptions;
353
376
  const { configuration } = state;
354
377
  return request(configuration, {
@@ -363,26 +386,59 @@ function get(resourceType, query, options = {}, callback = false) {
363
386
  });
364
387
  };
365
388
  }
366
- function upsert(resourceType, query, data, options = {}, callback = false) {
389
+ function post(resourceType, data, query, options = {}, callback = (s) => s) {
367
390
  return (state) => {
368
- console.log(`Preparing upsert via 'get' then 'create' OR 'update'...`);
369
- return get(
370
- resourceType,
371
- query,
372
- options
373
- )(state).then((resp) => {
374
- const resources = resp.data[resourceType];
375
- if (resources.length > 1) {
376
- throw new RangeError(
377
- `Cannot upsert on Non-unique attribute. The operation found more than one records for your request.`
378
- );
379
- } else if (resources.length <= 0) {
380
- return create(resourceType, data, options)(state);
381
- } else {
382
- const path = resources[0][selectId(resourceType)];
383
- return update(resourceType, path, data, options)(state);
384
- }
391
+ console.log("Preparing post operation...");
392
+ const [resolvedResourceType, resolvedQuery, resolvedOptions, resolvedData] = expandReferences(state, resourceType, query, options, data);
393
+ const { params, requestConfig } = resolvedOptions;
394
+ const { configuration } = state;
395
+ return request(configuration, {
396
+ method: "post",
397
+ url: generateUrl(configuration, resolvedOptions, resolvedResourceType),
398
+ params: { ...resolvedQuery, ...params },
399
+ responseType: "json",
400
+ data: resolvedData,
401
+ ...requestConfig
385
402
  }).then((result) => {
403
+ console.log(`Created ${resolvedResourceType}`);
404
+ return handleResponse(result, state, callback);
405
+ });
406
+ };
407
+ }
408
+ function upsert(resourceType, query, data, options = {}, callback = (s) => s) {
409
+ return (state) => {
410
+ const [resolvedResourceType, resolvedOptions, resolvedData] = expandReferences(state, resourceType, options, data);
411
+ let promise;
412
+ if (shouldUseNewTracker(resolvedResourceType)) {
413
+ const { configuration } = state;
414
+ promise = callNewTracker(
415
+ "create_and_update",
416
+ configuration,
417
+ resolvedOptions,
418
+ resolvedResourceType,
419
+ resolvedData
420
+ );
421
+ } else {
422
+ console.log(`Preparing upsert via 'get' then 'create' OR 'update'...`);
423
+ promise = get(
424
+ resourceType,
425
+ query,
426
+ options
427
+ )(state).then((resp) => {
428
+ const resources = resp.data[resourceType];
429
+ if (resources.length > 1) {
430
+ throw new RangeError(
431
+ `Cannot upsert on Non-unique attribute. The operation found more than one records for your request.`
432
+ );
433
+ } else if (resources.length <= 0) {
434
+ return create(resourceType, data, options)(state);
435
+ } else {
436
+ const path = resources[0][selectId(resourceType)];
437
+ return update(resourceType, path, data, options)(state);
438
+ }
439
+ });
440
+ }
441
+ return promise.then((result) => {
386
442
  console.log(`Performed a "composed upsert" on ${resourceType}`);
387
443
  return handleResponse(result, state, callback);
388
444
  });
@@ -454,13 +510,10 @@ ${prettyJson(
454
510
  });
455
511
  };
456
512
  }
457
- function patch(resourceType, path, data, options = {}, callback = false) {
513
+ function patch(resourceType, path, data, options = {}, callback = (s) => s) {
458
514
  return (state) => {
459
515
  console.log("Preparing patch operation...");
460
- const resolvedResourceType = expandReferences(resourceType)(state);
461
- const resolvedPath = expandReferences(path)(state);
462
- const resolvedData = expandReferences(data)(state);
463
- const resolvedOptions = expandReferences(options)(state);
516
+ const [resolvedResourceType, resolvedPath, resolvedData, resolvedOptions] = expandReferences(state, resourceType, path, data, options);
464
517
  const { params, requestConfig } = resolvedOptions;
465
518
  const { configuration } = state;
466
519
  return request(configuration, {
@@ -483,32 +536,41 @@ function patch(resourceType, path, data, options = {}, callback = false) {
483
536
  function destroy(resourceType, path, data = null, options = {}, callback = false) {
484
537
  return (state) => {
485
538
  console.log("Preparing destroy operation...");
486
- const resolvedResourceType = expandReferences(resourceType)(state);
487
- const resolvedPath = expandReferences(path)(state);
488
- const resolvedData = expandReferences(data)(state);
489
- const resolvedOptions = expandReferences(options)(state);
539
+ const [resolvedResourceType, resolvedPath, resolvedData, resolvedOptions] = expandReferences(state, resourceType, path, data, options);
490
540
  const { params, requestConfig } = resolvedOptions;
491
541
  const { configuration } = state;
492
- return request({
493
- method: "delete",
494
- url: generateUrl(
542
+ let promise;
543
+ if (shouldUseNewTracker(resolvedResourceType)) {
544
+ promise = callNewTracker(
545
+ "delete",
495
546
  configuration,
496
547
  resolvedOptions,
497
548
  resolvedResourceType,
498
- resolvedPath
499
- ),
500
- params,
501
- resolvedData,
502
- ...requestConfig
503
- }).then((result) => {
549
+ resolvedData
550
+ );
551
+ } else {
552
+ promise = request({
553
+ method: "delete",
554
+ url: generateUrl(
555
+ configuration,
556
+ resolvedOptions,
557
+ resolvedResourceType,
558
+ resolvedPath
559
+ ),
560
+ params,
561
+ resolvedData,
562
+ ...requestConfig
563
+ });
564
+ }
565
+ return promise.then((result) => {
504
566
  console.log(`Deleted ${resolvedResourceType} at ${resolvedPath}`);
505
567
  return handleResponse(result, state, callback);
506
568
  });
507
569
  };
508
570
  }
509
- function findAttributeValue(trackedEntityInstance, attributeDisplayName) {
571
+ function findAttributeValue(trackedEntity, attributeDisplayName) {
510
572
  var _a, _b;
511
- return (_b = (_a = trackedEntityInstance == null ? void 0 : trackedEntityInstance.attributes) == null ? void 0 : _a.find(
573
+ return (_b = (_a = trackedEntity == null ? void 0 : trackedEntity.attributes) == null ? void 0 : _a.find(
512
574
  (a) => (a == null ? void 0 : a.displayName.toLowerCase()) == attributeDisplayName.toLowerCase()
513
575
  )) == null ? void 0 : _b.value;
514
576
  }
@@ -518,6 +580,37 @@ function attr(attribute, value) {
518
580
  function dv(dataElement, value) {
519
581
  return { dataElement, value };
520
582
  }
583
+ function callNewTracker(type = "update", configuration, options, resourceType, data = {}) {
584
+ const { params, requestConfig, ...opts } = options;
585
+ let importStrategy;
586
+ switch (type) {
587
+ case "create":
588
+ importStrategy = "CREATE";
589
+ break;
590
+ case "update":
591
+ importStrategy = "UPDATE";
592
+ break;
593
+ case "delete":
594
+ importStrategy = "DELETE";
595
+ break;
596
+ default:
597
+ importStrategy = "CREATE_AND_UPDATE";
598
+ }
599
+ return request(configuration, {
600
+ method: "post",
601
+ url: generateUrl(
602
+ configuration,
603
+ {
604
+ ...opts,
605
+ importStrategy
606
+ },
607
+ "tracker"
608
+ ),
609
+ params: { async: false, ...params },
610
+ data: ensureArray(data, resourceType),
611
+ ...requestConfig
612
+ });
613
+ }
521
614
 
522
615
  // src/index.js
523
616
  var src_default = Adaptor_exports;
@@ -525,6 +618,7 @@ export {
525
618
  alterState,
526
619
  attr,
527
620
  create,
621
+ cursor,
528
622
  dataPath,
529
623
  dataValue,
530
624
  dateFns,
@@ -546,6 +640,7 @@ export {
546
640
  merge,
547
641
  metadata_default as metadata,
548
642
  patch,
643
+ post,
549
644
  sourceValue,
550
645
  update,
551
646
  upsert
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-dhis2",
3
- "version": "5.0.7",
3
+ "version": "6.0.0",
4
4
  "description": "DHIS2 Language Pack for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -45,10 +45,10 @@
45
45
  "scripts": {
46
46
  "build": "pnpm clean && build-adaptor dhis2",
47
47
  "clean": "rimraf dist types docs",
48
- "integration-test": "mocha --experimental-specifier-resolution=node --no-warnings test/integration.cjs",
49
48
  "pack": "pnpm pack --pack-destination ../../dist",
50
- "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings --exclude test/integration.cjs --recursive --timeout 10000",
51
- "test": "mocha --experimental-specifier-resolution=node --no-warnings --exclude test/integration.cjs --recursive --timeout 10000",
49
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings --exclude test/integration.js --recursive --timeout 10000",
50
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings --exclude test/integration.js --recursive --timeout 10000",
51
+ "test:integration": "mocha --experimental-specifier-resolution=node --no-warnings test/integration.js",
52
52
  "lint": "eslint src"
53
53
  }
54
54
  }