@openfn/language-commcare 3.2.0 → 3.2.2

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
@@ -443,29 +443,56 @@
443
443
  },
444
444
  {
445
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)",
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
447
  "caption": "Upload a single row of data for case-data"
448
448
  },
449
449
  {
450
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)",
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
452
  "caption": "Upload a single row of data for a lookup-table"
453
453
  },
454
454
  {
455
455
  "title": "param",
456
- "description": "case-data or lookup-table",
456
+ "description": " The type of data being processed.",
457
457
  "type": {
458
- "type": "NameExpression",
459
- "name": "string"
458
+ "type": "UnionType",
459
+ "elements": [
460
+ {
461
+ "type": "StringLiteralType",
462
+ "value": "case-data"
463
+ },
464
+ {
465
+ "type": "StringLiteralType",
466
+ "value": "lookup-table"
467
+ }
468
+ ]
460
469
  },
461
470
  "name": "type"
462
471
  },
463
472
  {
464
473
  "title": "param",
465
- "description": "Array of objects to upload",
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.",
466
475
  "type": {
467
- "type": "NameExpression",
468
- "name": "array"
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
+ ]
469
496
  },
470
497
  "name": "data"
471
498
  },
package/dist/index.cjs CHANGED
@@ -313,21 +313,25 @@ function request2(method, path, body, options = {}) {
313
313
  function bulk(type, data, params) {
314
314
  return async (state) => {
315
315
  const { domain } = state.configuration;
316
- const [json] = (0, import_util2.expandReferences)(state, data);
316
+ const [resolvedData, resolvedParams] = (0, import_util2.expandReferences)(
317
+ state,
318
+ data,
319
+ params
320
+ );
317
321
  let path, file;
318
322
  const workbook = import_xlsx.default.utils.book_new();
319
323
  if (type.toLowerCase() === "lookup-table") {
320
324
  path = `/a/${domain}/fixtures/fixapi/`;
321
325
  file = "file-to-upload";
322
- Object.keys(json).forEach((sectionName) => {
323
- const sectionData = data[sectionName];
326
+ Object.keys(resolvedData).forEach((sectionName) => {
327
+ const sectionData = resolvedData[sectionName];
324
328
  const newSheet = import_xlsx.default.utils.json_to_sheet(sectionData);
325
329
  import_xlsx.default.utils.book_append_sheet(workbook, newSheet, sectionName);
326
330
  });
327
331
  } else if (type.toLowerCase() === "case-data") {
328
332
  path = `/a/${domain}/importer/excel/bulk_upload_api/`;
329
333
  file = "file";
330
- const worksheet = import_xlsx.default.utils.json_to_sheet(json);
334
+ const worksheet = import_xlsx.default.utils.json_to_sheet(resolvedData);
331
335
  const ws_name = "SheetJS";
332
336
  import_xlsx.default.utils.book_append_sheet(workbook, worksheet, ws_name);
333
337
  } else {
@@ -339,8 +343,8 @@ function bulk(type, data, params) {
339
343
  const buffer = import_xlsx.default.write(workbook, { type: "buffer", bookType: "xlsx" });
340
344
  const form = new FormData();
341
345
  form.append(file, new import_node_buffer.Blob([buffer]), "data.xlsx");
342
- for (const key in params) {
343
- form.append(key, params[key]);
346
+ for (const key in resolvedParams) {
347
+ form.append(key, resolvedParams[key]);
344
348
  }
345
349
  const response = await request(state.configuration, path, {
346
350
  method: "POST",
package/dist/index.js CHANGED
@@ -285,21 +285,25 @@ function request2(method, path, body, options = {}) {
285
285
  function bulk(type, data, params) {
286
286
  return async (state) => {
287
287
  const { domain } = state.configuration;
288
- const [json] = expandReferences(state, data);
288
+ const [resolvedData, resolvedParams] = expandReferences(
289
+ state,
290
+ data,
291
+ params
292
+ );
289
293
  let path, file;
290
294
  const workbook = xlsx.utils.book_new();
291
295
  if (type.toLowerCase() === "lookup-table") {
292
296
  path = `/a/${domain}/fixtures/fixapi/`;
293
297
  file = "file-to-upload";
294
- Object.keys(json).forEach((sectionName) => {
295
- const sectionData = data[sectionName];
298
+ Object.keys(resolvedData).forEach((sectionName) => {
299
+ const sectionData = resolvedData[sectionName];
296
300
  const newSheet = xlsx.utils.json_to_sheet(sectionData);
297
301
  xlsx.utils.book_append_sheet(workbook, newSheet, sectionName);
298
302
  });
299
303
  } else if (type.toLowerCase() === "case-data") {
300
304
  path = `/a/${domain}/importer/excel/bulk_upload_api/`;
301
305
  file = "file";
302
- const worksheet = xlsx.utils.json_to_sheet(json);
306
+ const worksheet = xlsx.utils.json_to_sheet(resolvedData);
303
307
  const ws_name = "SheetJS";
304
308
  xlsx.utils.book_append_sheet(workbook, worksheet, ws_name);
305
309
  } else {
@@ -311,8 +315,8 @@ function bulk(type, data, params) {
311
315
  const buffer = xlsx.write(workbook, { type: "buffer", bookType: "xlsx" });
312
316
  const form = new FormData();
313
317
  form.append(file, new Blob([buffer]), "data.xlsx");
314
- for (const key in params) {
315
- form.append(key, params[key]);
318
+ for (const key in resolvedParams) {
319
+ form.append(key, resolvedParams[key]);
316
320
  }
317
321
  const response = await request(state.configuration, path, {
318
322
  method: "POST",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-commcare",
3
- "version": "3.2.0",
3
+ "version": "3.2.2",
4
4
  "description": "Commcare Language Pack for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -21,7 +21,7 @@
21
21
  "js2xmlparser": "^1.0.0",
22
22
  "lodash-fp": "^0.10.4",
23
23
  "xlsx": "https://cdn.sheetjs.com/xlsx-0.19.3/xlsx-0.19.3.tgz",
24
- "@openfn/language-common": "2.0.2"
24
+ "@openfn/language-common": "2.0.3"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@openfn/simple-ast": "^0.4.1",
@@ -29,7 +29,6 @@
29
29
  "chai": "^4.3.7",
30
30
  "deep-eql": "^4.1.3",
31
31
  "esno": "^0.16.3",
32
- "mocha": "^9.2.2",
33
32
  "rimraf": "^3.0.2"
34
33
  },
35
34
  "type": "module",
@@ -135,6 +135,7 @@ export function request(method: string, path: string, body: object, options?: Re
135
135
  * @function
136
136
  * @example <caption>Upload a single row of data for case-data</caption>
137
137
  * bulk(
138
+ * 'case-data',
138
139
  * [
139
140
  * {name: 'Mamadou', phone: '000000'},
140
141
  * ],
@@ -146,7 +147,7 @@ export function request(method: string, path: string, body: object, options?: Re
146
147
  * )
147
148
  * @example <caption>Upload a single row of data for a lookup-table</caption>
148
149
  * bulk(
149
- * 'lookup-table'
150
+ * 'lookup-table',
150
151
  * {
151
152
  * types: [{
152
153
  *
@@ -162,15 +163,18 @@ export function request(method: string, path: string, body: object, options?: Re
162
163
  * 'field:type': 'citrus',
163
164
  * 'field:name': 'Orange',
164
165
  * }],
165
- * }
166
+ * },
167
+ * {replace: false}
166
168
  * )
167
- * @param {string} type - case-data or lookup-table
168
- * @param {array} data - Array of objects to upload
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.
169
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.
170
174
  * @state data - the response from the CommCare Server
171
175
  * @returns {Operation}
172
176
  */
173
- export function bulk(type: string, data: any[], params: any): Operation;
177
+ export function bulk(type: ('case-data' | 'lookup-table'), data: (any | any[]), params: any): Operation;
174
178
  /**
175
179
  * State object
176
180
  */