@openfn/language-commcare 3.2.0 → 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 +35 -8
- package/dist/index.cjs +10 -6
- package/dist/index.js +10 -6
- package/package.json +1 -1
- package/types/Adaptor.d.ts +9 -5
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'
|
|
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": "
|
|
456
|
+
"description": " The type of data being processed.",
|
|
457
457
|
"type": {
|
|
458
|
-
"type": "
|
|
459
|
-
"
|
|
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": "
|
|
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": "
|
|
468
|
-
"
|
|
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 [
|
|
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(
|
|
323
|
-
const sectionData =
|
|
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(
|
|
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
|
|
343
|
-
form.append(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 [
|
|
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(
|
|
295
|
-
const sectionData =
|
|
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(
|
|
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
|
|
315
|
-
form.append(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
package/types/Adaptor.d.ts
CHANGED
|
@@ -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 {
|
|
168
|
-
* @param {
|
|
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:
|
|
177
|
+
export function bulk(type: ('case-data' | 'lookup-table'), data: (any | any[]), params: any): Operation;
|
|
174
178
|
/**
|
|
175
179
|
* State object
|
|
176
180
|
*/
|