@openfn/language-commcare 3.1.2 → 3.2.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/ast.json CHANGED
@@ -342,6 +342,157 @@
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 [\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)",
452
+ "caption": "Upload a single row of data for a lookup-table"
453
+ },
454
+ {
455
+ "title": "param",
456
+ "description": "case-data or lookup-table",
457
+ "type": {
458
+ "type": "NameExpression",
459
+ "name": "string"
460
+ },
461
+ "name": "type"
462
+ },
463
+ {
464
+ "title": "param",
465
+ "description": "Array of objects to upload",
466
+ "type": {
467
+ "type": "NameExpression",
468
+ "name": "array"
469
+ },
470
+ "name": "data"
471
+ },
472
+ {
473
+ "title": "param",
474
+ "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.",
475
+ "type": {
476
+ "type": "NameExpression",
477
+ "name": "Object"
478
+ },
479
+ "name": "params"
480
+ },
481
+ {
482
+ "title": "state",
483
+ "description": "data - the response from the CommCare Server"
484
+ },
485
+ {
486
+ "title": "returns",
487
+ "description": null,
488
+ "type": {
489
+ "type": "NameExpression",
490
+ "name": "Operation"
491
+ }
492
+ }
493
+ ]
494
+ },
495
+ "valid": true
345
496
  }
346
497
  ],
347
498
  "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,63 @@ 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 [json] = (0, import_util2.expandReferences)(state, data);
317
+ let path, file;
318
+ const workbook = import_xlsx.default.utils.book_new();
319
+ if (type.toLowerCase() === "lookup-table") {
320
+ path = `/a/${domain}/fixtures/fixapi/`;
321
+ file = "file-to-upload";
322
+ Object.keys(json).forEach((sectionName) => {
323
+ const sectionData = data[sectionName];
324
+ const newSheet = import_xlsx.default.utils.json_to_sheet(sectionData);
325
+ import_xlsx.default.utils.book_append_sheet(workbook, newSheet, sectionName);
326
+ });
327
+ } else if (type.toLowerCase() === "case-data") {
328
+ path = `/a/${domain}/importer/excel/bulk_upload_api/`;
329
+ file = "file";
330
+ const worksheet = import_xlsx.default.utils.json_to_sheet(json);
331
+ const ws_name = "SheetJS";
332
+ import_xlsx.default.utils.book_append_sheet(workbook, worksheet, ws_name);
333
+ } else {
334
+ const e = new Error("Unrecognized type");
335
+ e.description = `The type key was not recognized: ${type}`;
336
+ e.fix = "Set type to case-data or lookup-table";
337
+ throw e;
338
+ }
339
+ const buffer = import_xlsx.default.write(workbook, { type: "buffer", bookType: "xlsx" });
340
+ const form = new FormData();
341
+ form.append(file, new import_node_buffer.Blob([buffer]), "data.xlsx");
342
+ for (const key in params) {
343
+ form.append(key, params[key]);
344
+ }
345
+ const response = await request(state.configuration, path, {
346
+ method: "POST",
347
+ data: form
348
+ });
349
+ return prepareNextState(state, {
350
+ ...response,
351
+ body: {
352
+ message: response.body.message.replace(/\n/g, ""),
353
+ code: response.body.code
354
+ }
355
+ });
356
+ };
357
+ }
297
358
 
298
359
  // src/index.js
299
360
  var src_default = Adaptor_exports;
@@ -301,6 +362,7 @@ var src_default = Adaptor_exports;
301
362
  0 && (module.exports = {
302
363
  alterState,
303
364
  arrayToString,
365
+ bulk,
304
366
  combine,
305
367
  cursor,
306
368
  dataPath,
@@ -318,6 +380,7 @@ var src_default = Adaptor_exports;
318
380
  lastReferenceValue,
319
381
  merge,
320
382
  post,
383
+ request,
321
384
  sourceValue,
322
385
  submit,
323
386
  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,70 @@ 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 [json] = expandReferences(state, data);
289
+ let path, file;
290
+ const workbook = xlsx.utils.book_new();
291
+ if (type.toLowerCase() === "lookup-table") {
292
+ path = `/a/${domain}/fixtures/fixapi/`;
293
+ file = "file-to-upload";
294
+ Object.keys(json).forEach((sectionName) => {
295
+ const sectionData = data[sectionName];
296
+ const newSheet = xlsx.utils.json_to_sheet(sectionData);
297
+ xlsx.utils.book_append_sheet(workbook, newSheet, sectionName);
298
+ });
299
+ } else if (type.toLowerCase() === "case-data") {
300
+ path = `/a/${domain}/importer/excel/bulk_upload_api/`;
301
+ file = "file";
302
+ const worksheet = xlsx.utils.json_to_sheet(json);
303
+ const ws_name = "SheetJS";
304
+ xlsx.utils.book_append_sheet(workbook, worksheet, ws_name);
305
+ } else {
306
+ const e = new Error("Unrecognized type");
307
+ e.description = `The type key was not recognized: ${type}`;
308
+ e.fix = "Set type to case-data or lookup-table";
309
+ throw e;
310
+ }
311
+ const buffer = xlsx.write(workbook, { type: "buffer", bookType: "xlsx" });
312
+ const form = new FormData();
313
+ form.append(file, new Blob([buffer]), "data.xlsx");
314
+ for (const key in params) {
315
+ form.append(key, params[key]);
316
+ }
317
+ const response = await request(state.configuration, path, {
318
+ method: "POST",
319
+ data: form
320
+ });
321
+ return prepareNextState(state, {
322
+ ...response,
323
+ body: {
324
+ message: response.body.message.replace(/\n/g, ""),
325
+ code: response.body.code
326
+ }
327
+ });
328
+ };
329
+ }
271
330
 
272
331
  // src/index.js
273
332
  var src_default = Adaptor_exports;
274
333
  export {
275
334
  alterState,
276
335
  arrayToString,
336
+ bulk,
277
337
  combine,
278
338
  cursor,
279
339
  dataPath,
@@ -292,6 +352,7 @@ export {
292
352
  lastReferenceValue,
293
353
  merge,
294
354
  post,
355
+ request2 as request,
295
356
  sourceValue,
296
357
  submit,
297
358
  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.0",
4
4
  "description": "Commcare Language Pack for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -114,6 +114,63 @@ 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
+ * [
139
+ * {name: 'Mamadou', phone: '000000'},
140
+ * ],
141
+ * {
142
+ * case_type: 'student',
143
+ * search_field: 'external_id',
144
+ * create_new_cases: 'on',
145
+ * }
146
+ * )
147
+ * @example <caption>Upload a single row of data for a lookup-table</caption>
148
+ * bulk(
149
+ * 'lookup-table'
150
+ * {
151
+ * types: [{
152
+ *
153
+ * 'DELETE(Y/N)':'N',
154
+ * table_id: 'fruit',
155
+ * 'is_global?':'yes',
156
+ * 'field 1': 'type',
157
+ * 'field 2': 'name',
158
+ * }],
159
+ * fruit: [{
160
+ * UID: '',
161
+ * 'DELETE(Y/N)':'N',
162
+ * 'field:type': 'citrus',
163
+ * 'field:name': 'Orange',
164
+ * }],
165
+ * }
166
+ * )
167
+ * @param {string} type - case-data or lookup-table
168
+ * @param {array} data - Array of objects to upload
169
+ * @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.
170
+ * @state data - the response from the CommCare Server
171
+ * @returns {Operation}
172
+ */
173
+ export function bulk(type: string, data: any[], params: any): Operation;
117
174
  /**
118
175
  * State object
119
176
  */