@openfn/language-commcare 3.1.2 → 3.2.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 CHANGED
@@ -342,6 +342,184 @@
342
342
  ]
343
343
  },
344
344
  "valid": true
345
+ },
346
+ {
347
+ "name": "request",
348
+ "params": [
349
+ "method",
350
+ "path",
351
+ "body",
352
+ "options"
353
+ ],
354
+ "docs": {
355
+ "description": "Make a general HTTP request against the Commcare server.",
356
+ "tags": [
357
+ {
358
+ "title": "example",
359
+ "description": "request(\"GET\", \"/a/asri/api/v0.5/case\");",
360
+ "caption": "Make a GET request to get cases"
361
+ },
362
+ {
363
+ "title": "function",
364
+ "description": null,
365
+ "name": null
366
+ },
367
+ {
368
+ "title": "public",
369
+ "description": null,
370
+ "type": null
371
+ },
372
+ {
373
+ "title": "param",
374
+ "description": "HTTP method to use",
375
+ "type": {
376
+ "type": "NameExpression",
377
+ "name": "string"
378
+ },
379
+ "name": "method"
380
+ },
381
+ {
382
+ "title": "param",
383
+ "description": "Path to resource",
384
+ "type": {
385
+ "type": "NameExpression",
386
+ "name": "string"
387
+ },
388
+ "name": "path"
389
+ },
390
+ {
391
+ "title": "param",
392
+ "description": "Object which will be attached to the body",
393
+ "type": {
394
+ "type": "NameExpression",
395
+ "name": "object"
396
+ },
397
+ "name": "body"
398
+ },
399
+ {
400
+ "title": "param",
401
+ "description": "Optional request params",
402
+ "type": {
403
+ "type": "NameExpression",
404
+ "name": "RequestOptions"
405
+ },
406
+ "name": "options"
407
+ },
408
+ {
409
+ "title": "returns",
410
+ "description": null,
411
+ "type": {
412
+ "type": "NameExpression",
413
+ "name": "Operation"
414
+ }
415
+ },
416
+ {
417
+ "title": "state",
418
+ "description": "{CommcareHttpState}"
419
+ }
420
+ ]
421
+ },
422
+ "valid": true
423
+ },
424
+ {
425
+ "name": "bulk",
426
+ "params": [
427
+ "type",
428
+ "data",
429
+ "params"
430
+ ],
431
+ "docs": {
432
+ "description": "Bulk upload data to CommCare for case-data or lookup-table. Accepts an array of objects, converts them into\nan XLS representation, and uploads.",
433
+ "tags": [
434
+ {
435
+ "title": "public",
436
+ "description": null,
437
+ "type": null
438
+ },
439
+ {
440
+ "title": "function",
441
+ "description": null,
442
+ "name": null
443
+ },
444
+ {
445
+ "title": "example",
446
+ "description": "bulk(\n'case-data',\n [\n {name: 'Mamadou', phone: '000000'},\n ],\n {\n case_type: 'student',\n search_field: 'external_id',\n create_new_cases: 'on',\n }\n)",
447
+ "caption": "Upload a single row of data for case-data"
448
+ },
449
+ {
450
+ "title": "example",
451
+ "description": "bulk(\n 'lookup-table',\n {\n types: [{\n\n 'DELETE(Y/N)':'N',\n table_id: 'fruit',\n 'is_global?':'yes',\n 'field 1': 'type',\n 'field 2': 'name',\n }],\n fruit: [{\n UID: '',\n 'DELETE(Y/N)':'N',\n 'field:type': 'citrus',\n 'field:name': 'Orange',\n }],\n },\n{replace: false}\n)",
452
+ "caption": "Upload a single row of data for a lookup-table"
453
+ },
454
+ {
455
+ "title": "param",
456
+ "description": " The type of data being processed.",
457
+ "type": {
458
+ "type": "UnionType",
459
+ "elements": [
460
+ {
461
+ "type": "StringLiteralType",
462
+ "value": "case-data"
463
+ },
464
+ {
465
+ "type": "StringLiteralType",
466
+ "value": "lookup-table"
467
+ }
468
+ ]
469
+ },
470
+ "name": "type"
471
+ },
472
+ {
473
+ "title": "param",
474
+ "description": "An object or an array of objects to upload.\n- If type is `'case-data'`, this should be an object array of objects.\n- If type is `'lookup-table'`, this should be an object.",
475
+ "type": {
476
+ "type": "UnionType",
477
+ "elements": [
478
+ {
479
+ "type": "NameExpression",
480
+ "name": "Object"
481
+ },
482
+ {
483
+ "type": "TypeApplication",
484
+ "expression": {
485
+ "type": "NameExpression",
486
+ "name": "Array"
487
+ },
488
+ "applications": [
489
+ {
490
+ "type": "NameExpression",
491
+ "name": "Object"
492
+ }
493
+ ]
494
+ }
495
+ ]
496
+ },
497
+ "name": "data"
498
+ },
499
+ {
500
+ "title": "param",
501
+ "description": "Input parameters, see {@link https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143946459/Bulk+Upload+Case+Data CommCare docs} for case-data and {@link https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143946023/Bulk+upload+Lookup+Tables Commcare Docs} for lookup-table.",
502
+ "type": {
503
+ "type": "NameExpression",
504
+ "name": "Object"
505
+ },
506
+ "name": "params"
507
+ },
508
+ {
509
+ "title": "state",
510
+ "description": "data - the response from the CommCare Server"
511
+ },
512
+ {
513
+ "title": "returns",
514
+ "description": null,
515
+ "type": {
516
+ "type": "NameExpression",
517
+ "name": "Operation"
518
+ }
519
+ }
520
+ ]
521
+ },
522
+ "valid": true
345
523
  }
346
524
  ],
347
525
  "exports": [],
package/dist/index.cjs CHANGED
@@ -27,6 +27,7 @@ var src_exports = {};
27
27
  __export(src_exports, {
28
28
  alterState: () => import_language_common3.alterState,
29
29
  arrayToString: () => import_language_common3.arrayToString,
30
+ bulk: () => bulk,
30
31
  combine: () => import_language_common3.combine,
31
32
  cursor: () => import_language_common3.cursor,
32
33
  dataPath: () => import_language_common3.dataPath,
@@ -45,6 +46,7 @@ __export(src_exports, {
45
46
  lastReferenceValue: () => import_language_common3.lastReferenceValue,
46
47
  merge: () => import_language_common3.merge,
47
48
  post: () => post,
49
+ request: () => request2,
48
50
  sourceValue: () => import_language_common3.sourceValue,
49
51
  submit: () => submit,
50
52
  submitXls: () => submitXls
@@ -56,6 +58,7 @@ var Adaptor_exports = {};
56
58
  __export(Adaptor_exports, {
57
59
  alterState: () => import_language_common3.alterState,
58
60
  arrayToString: () => import_language_common3.arrayToString,
61
+ bulk: () => bulk,
59
62
  combine: () => import_language_common3.combine,
60
63
  cursor: () => import_language_common3.cursor,
61
64
  dataPath: () => import_language_common3.dataPath,
@@ -73,6 +76,7 @@ __export(Adaptor_exports, {
73
76
  lastReferenceValue: () => import_language_common3.lastReferenceValue,
74
77
  merge: () => import_language_common3.merge,
75
78
  post: () => post,
79
+ request: () => request2,
76
80
  sourceValue: () => import_language_common3.sourceValue,
77
81
  submit: () => submit,
78
82
  submitXls: () => submitXls
@@ -294,6 +298,67 @@ function fetchReportData(reportId, params, postUrl) {
294
298
  return state;
295
299
  };
296
300
  }
301
+ function request2(method, path, body, options = {}) {
302
+ return async (state) => {
303
+ const [resolvedMethod, resolvedPath, resolvedBody, resolvedOptions] = (0, import_util2.expandReferences)(state, method, path, body, options);
304
+ const response = await request(state.configuration, resolvedPath, {
305
+ method: resolvedMethod,
306
+ data: resolvedBody,
307
+ params: resolvedOptions,
308
+ contentType: "application/json"
309
+ });
310
+ return prepareNextState(state, response);
311
+ };
312
+ }
313
+ function bulk(type, data, params) {
314
+ return async (state) => {
315
+ const { domain } = state.configuration;
316
+ const [resolvedData, resolvedParams] = (0, import_util2.expandReferences)(
317
+ state,
318
+ data,
319
+ params
320
+ );
321
+ let path, file;
322
+ const workbook = import_xlsx.default.utils.book_new();
323
+ if (type.toLowerCase() === "lookup-table") {
324
+ path = `/a/${domain}/fixtures/fixapi/`;
325
+ file = "file-to-upload";
326
+ Object.keys(resolvedData).forEach((sectionName) => {
327
+ const sectionData = resolvedData[sectionName];
328
+ const newSheet = import_xlsx.default.utils.json_to_sheet(sectionData);
329
+ import_xlsx.default.utils.book_append_sheet(workbook, newSheet, sectionName);
330
+ });
331
+ } else if (type.toLowerCase() === "case-data") {
332
+ path = `/a/${domain}/importer/excel/bulk_upload_api/`;
333
+ file = "file";
334
+ const worksheet = import_xlsx.default.utils.json_to_sheet(resolvedData);
335
+ const ws_name = "SheetJS";
336
+ import_xlsx.default.utils.book_append_sheet(workbook, worksheet, ws_name);
337
+ } else {
338
+ const e = new Error("Unrecognized type");
339
+ e.description = `The type key was not recognized: ${type}`;
340
+ e.fix = "Set type to case-data or lookup-table";
341
+ throw e;
342
+ }
343
+ const buffer = import_xlsx.default.write(workbook, { type: "buffer", bookType: "xlsx" });
344
+ const form = new FormData();
345
+ form.append(file, new import_node_buffer.Blob([buffer]), "data.xlsx");
346
+ for (const key in resolvedParams) {
347
+ form.append(key, resolvedParams[key]);
348
+ }
349
+ const response = await request(state.configuration, path, {
350
+ method: "POST",
351
+ data: form
352
+ });
353
+ return prepareNextState(state, {
354
+ ...response,
355
+ body: {
356
+ message: response.body.message.replace(/\n/g, ""),
357
+ code: response.body.code
358
+ }
359
+ });
360
+ };
361
+ }
297
362
 
298
363
  // src/index.js
299
364
  var src_default = Adaptor_exports;
@@ -301,6 +366,7 @@ var src_default = Adaptor_exports;
301
366
  0 && (module.exports = {
302
367
  alterState,
303
368
  arrayToString,
369
+ bulk,
304
370
  combine,
305
371
  cursor,
306
372
  dataPath,
@@ -318,6 +384,7 @@ var src_default = Adaptor_exports;
318
384
  lastReferenceValue,
319
385
  merge,
320
386
  post,
387
+ request,
321
388
  sourceValue,
322
389
  submit,
323
390
  submitXls
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ var Adaptor_exports = {};
9
9
  __export(Adaptor_exports, {
10
10
  alterState: () => alterState,
11
11
  arrayToString: () => arrayToString,
12
+ bulk: () => bulk,
12
13
  combine: () => combine,
13
14
  cursor: () => cursor,
14
15
  dataPath: () => dataPath,
@@ -26,6 +27,7 @@ __export(Adaptor_exports, {
26
27
  lastReferenceValue: () => lastReferenceValue,
27
28
  merge: () => merge,
28
29
  post: () => post,
30
+ request: () => request2,
29
31
  sourceValue: () => sourceValue,
30
32
  submit: () => submit,
31
33
  submitXls: () => submitXls
@@ -268,12 +270,74 @@ function fetchReportData(reportId, params, postUrl) {
268
270
  return state;
269
271
  };
270
272
  }
273
+ function request2(method, path, body, options = {}) {
274
+ return async (state) => {
275
+ const [resolvedMethod, resolvedPath, resolvedBody, resolvedOptions] = expandReferences(state, method, path, body, options);
276
+ const response = await request(state.configuration, resolvedPath, {
277
+ method: resolvedMethod,
278
+ data: resolvedBody,
279
+ params: resolvedOptions,
280
+ contentType: "application/json"
281
+ });
282
+ return prepareNextState(state, response);
283
+ };
284
+ }
285
+ function bulk(type, data, params) {
286
+ return async (state) => {
287
+ const { domain } = state.configuration;
288
+ const [resolvedData, resolvedParams] = expandReferences(
289
+ state,
290
+ data,
291
+ params
292
+ );
293
+ let path, file;
294
+ const workbook = xlsx.utils.book_new();
295
+ if (type.toLowerCase() === "lookup-table") {
296
+ path = `/a/${domain}/fixtures/fixapi/`;
297
+ file = "file-to-upload";
298
+ Object.keys(resolvedData).forEach((sectionName) => {
299
+ const sectionData = resolvedData[sectionName];
300
+ const newSheet = xlsx.utils.json_to_sheet(sectionData);
301
+ xlsx.utils.book_append_sheet(workbook, newSheet, sectionName);
302
+ });
303
+ } else if (type.toLowerCase() === "case-data") {
304
+ path = `/a/${domain}/importer/excel/bulk_upload_api/`;
305
+ file = "file";
306
+ const worksheet = xlsx.utils.json_to_sheet(resolvedData);
307
+ const ws_name = "SheetJS";
308
+ xlsx.utils.book_append_sheet(workbook, worksheet, ws_name);
309
+ } else {
310
+ const e = new Error("Unrecognized type");
311
+ e.description = `The type key was not recognized: ${type}`;
312
+ e.fix = "Set type to case-data or lookup-table";
313
+ throw e;
314
+ }
315
+ const buffer = xlsx.write(workbook, { type: "buffer", bookType: "xlsx" });
316
+ const form = new FormData();
317
+ form.append(file, new Blob([buffer]), "data.xlsx");
318
+ for (const key in resolvedParams) {
319
+ form.append(key, resolvedParams[key]);
320
+ }
321
+ const response = await request(state.configuration, path, {
322
+ method: "POST",
323
+ data: form
324
+ });
325
+ return prepareNextState(state, {
326
+ ...response,
327
+ body: {
328
+ message: response.body.message.replace(/\n/g, ""),
329
+ code: response.body.code
330
+ }
331
+ });
332
+ };
333
+ }
271
334
 
272
335
  // src/index.js
273
336
  var src_default = Adaptor_exports;
274
337
  export {
275
338
  alterState,
276
339
  arrayToString,
340
+ bulk,
277
341
  combine,
278
342
  cursor,
279
343
  dataPath,
@@ -292,6 +356,7 @@ export {
292
356
  lastReferenceValue,
293
357
  merge,
294
358
  post,
359
+ request2 as request,
295
360
  sourceValue,
296
361
  submit,
297
362
  submitXls
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-commcare",
3
- "version": "3.1.2",
3
+ "version": "3.2.1",
4
4
  "description": "Commcare Language Pack for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -114,6 +114,67 @@ export function submit(data: any): Operation;
114
114
  * @returns {Operation}
115
115
  */
116
116
  export function fetchReportData(reportId: string, params: any, postUrl: string): Operation;
117
+ /**
118
+ * Make a general HTTP request against the Commcare server.
119
+ * @example <caption>Make a GET request to get cases</caption>
120
+ * request("GET", "/a/asri/api/v0.5/case");
121
+ * @function
122
+ * @public
123
+ * @param {string} method - HTTP method to use
124
+ * @param {string} path - Path to resource
125
+ * @param {object} body - Object which will be attached to the body
126
+ * @param {RequestOptions} options - Optional request params
127
+ * @returns {Operation}
128
+ * @state {CommcareHttpState}
129
+ */
130
+ export function request(method: string, path: string, body: object, options?: RequestOptions): Operation;
131
+ /**
132
+ * Bulk upload data to CommCare for case-data or lookup-table. Accepts an array of objects, converts them into
133
+ * an XLS representation, and uploads.
134
+ * @public
135
+ * @function
136
+ * @example <caption>Upload a single row of data for case-data</caption>
137
+ * bulk(
138
+ * 'case-data',
139
+ * [
140
+ * {name: 'Mamadou', phone: '000000'},
141
+ * ],
142
+ * {
143
+ * case_type: 'student',
144
+ * search_field: 'external_id',
145
+ * create_new_cases: 'on',
146
+ * }
147
+ * )
148
+ * @example <caption>Upload a single row of data for a lookup-table</caption>
149
+ * bulk(
150
+ * 'lookup-table',
151
+ * {
152
+ * types: [{
153
+ *
154
+ * 'DELETE(Y/N)':'N',
155
+ * table_id: 'fruit',
156
+ * 'is_global?':'yes',
157
+ * 'field 1': 'type',
158
+ * 'field 2': 'name',
159
+ * }],
160
+ * fruit: [{
161
+ * UID: '',
162
+ * 'DELETE(Y/N)':'N',
163
+ * 'field:type': 'citrus',
164
+ * 'field:name': 'Orange',
165
+ * }],
166
+ * },
167
+ * {replace: false}
168
+ * )
169
+ * @param {('case-data'|'lookup-table')} type - The type of data being processed.
170
+ * @param {(Object|Object[])} data - An object or an array of objects to upload.
171
+ * - If type is `'case-data'`, this should be an object array of objects.
172
+ * - If type is `'lookup-table'`, this should be an object.
173
+ * @param {Object} params - Input parameters, see {@link https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143946459/Bulk+Upload+Case+Data CommCare docs} for case-data and {@link https://dimagi.atlassian.net/wiki/spaces/commcarepublic/pages/2143946023/Bulk+upload+Lookup+Tables Commcare Docs} for lookup-table.
174
+ * @state data - the response from the CommCare Server
175
+ * @returns {Operation}
176
+ */
177
+ export function bulk(type: ('case-data' | 'lookup-table'), data: (any | any[]), params: any): Operation;
117
178
  /**
118
179
  * State object
119
180
  */