@overmap-ai/core 1.0.58-asset-description.5 → 1.0.58-asset-description.6
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.
|
@@ -7239,6 +7239,15 @@ var __publicField = (obj, key, value) => {
|
|
|
7239
7239
|
store.dispatch(setFormRevisionAttachments(Object.values(result.attachments)));
|
|
7240
7240
|
}
|
|
7241
7241
|
}
|
|
7242
|
+
function chunkArray(arr, chunkSize) {
|
|
7243
|
+
const chunks = [];
|
|
7244
|
+
let index2 = 0;
|
|
7245
|
+
const arrLength = arr.length;
|
|
7246
|
+
while (index2 < arrLength) {
|
|
7247
|
+
chunks.push(arr.slice(index2, index2 += chunkSize));
|
|
7248
|
+
}
|
|
7249
|
+
return chunks;
|
|
7250
|
+
}
|
|
7242
7251
|
const isArrayOfFiles = (value) => {
|
|
7243
7252
|
return Array.isArray(value) && value[0] instanceof File;
|
|
7244
7253
|
};
|
|
@@ -7259,6 +7268,7 @@ var __publicField = (obj, key, value) => {
|
|
|
7259
7268
|
}
|
|
7260
7269
|
return { values: newValues, files };
|
|
7261
7270
|
};
|
|
7271
|
+
const MAX_BULK_ADD_SUBMISSIONS = 1e3;
|
|
7262
7272
|
class UserFormSubmissionService extends BaseApiService {
|
|
7263
7273
|
constructor() {
|
|
7264
7274
|
super(...arguments);
|
|
@@ -7345,103 +7355,129 @@ var __publicField = (obj, key, value) => {
|
|
|
7345
7355
|
const offlineSubmissions = [];
|
|
7346
7356
|
const offlineAttachments = [];
|
|
7347
7357
|
const submissionOfflineIds = [];
|
|
7348
|
-
const
|
|
7349
|
-
const attachmentsPayload = [];
|
|
7350
|
-
let files = {};
|
|
7358
|
+
const allFilesRecord = {};
|
|
7351
7359
|
const { values: fileSeperatedCommonFieldValues, files: commonFiles } = separateFilesFromValues(commonFieldValues);
|
|
7352
|
-
files = commonFiles;
|
|
7353
7360
|
const submittedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
7354
7361
|
const createdBy = store.getState().userReducer.currentUser.id;
|
|
7355
|
-
|
|
7356
|
-
|
|
7357
|
-
|
|
7358
|
-
|
|
7359
|
-
|
|
7360
|
-
|
|
7361
|
-
|
|
7362
|
-
|
|
7363
|
-
|
|
7364
|
-
|
|
7365
|
-
|
|
7366
|
-
|
|
7367
|
-
|
|
7368
|
-
|
|
7369
|
-
|
|
7370
|
-
|
|
7371
|
-
|
|
7372
|
-
|
|
7373
|
-
|
|
7374
|
-
|
|
7375
|
-
const sha1 = await hashFile(file);
|
|
7376
|
-
await this.client.files.addCache(file, sha1);
|
|
7377
|
-
const offlineAttachment = offline({
|
|
7378
|
-
file_name: file.name,
|
|
7379
|
-
file_sha1: sha1,
|
|
7380
|
-
file: URL.createObjectURL(file),
|
|
7381
|
-
submission: submission.offline_id,
|
|
7382
|
-
field_identifier: fieldIdentifier
|
|
7362
|
+
const assetIdBatches = chunkArray(Object.keys(fieldValuesByAsset), MAX_BULK_ADD_SUBMISSIONS);
|
|
7363
|
+
const bulkAddBatches = await Promise.all(
|
|
7364
|
+
assetIdBatches.map(async (assetIdBatch) => {
|
|
7365
|
+
const batchId = uuid.v4();
|
|
7366
|
+
const submissionsPayload = [];
|
|
7367
|
+
const attachmentsPayload = [];
|
|
7368
|
+
let files = { ...commonFiles };
|
|
7369
|
+
for (const assetId of assetIdBatch) {
|
|
7370
|
+
const { values: fileSeperatedSubmissionSpecificValues, files: submissionSpecificFiles } = separateFilesFromValues(fieldValuesByAsset[assetId] ?? {});
|
|
7371
|
+
files = Object.assign(files, submissionSpecificFiles);
|
|
7372
|
+
const submissionValues = {
|
|
7373
|
+
...fileSeperatedCommonFieldValues,
|
|
7374
|
+
...fileSeperatedSubmissionSpecificValues
|
|
7375
|
+
};
|
|
7376
|
+
const submission = offline({
|
|
7377
|
+
form_revision: formRevision,
|
|
7378
|
+
values: submissionValues,
|
|
7379
|
+
created_by: createdBy,
|
|
7380
|
+
submitted_at: submittedAt,
|
|
7381
|
+
asset: assetId
|
|
7383
7382
|
});
|
|
7384
|
-
|
|
7385
|
-
|
|
7386
|
-
offline_id:
|
|
7387
|
-
|
|
7388
|
-
|
|
7389
|
-
name: file.name,
|
|
7390
|
-
field_identifier: fieldIdentifier
|
|
7383
|
+
submissionOfflineIds.push(submission.offline_id);
|
|
7384
|
+
submissionsPayload.push({
|
|
7385
|
+
offline_id: submission.offline_id,
|
|
7386
|
+
asset_id: assetId,
|
|
7387
|
+
form_data: fileSeperatedSubmissionSpecificValues
|
|
7391
7388
|
});
|
|
7389
|
+
offlineSubmissions.push(submission);
|
|
7390
|
+
for (const [fieldIdentifier, fileArray] of Object.entries(files)) {
|
|
7391
|
+
for (const file of fileArray) {
|
|
7392
|
+
const sha1 = await hashFile(file);
|
|
7393
|
+
await this.client.files.addCache(file, sha1);
|
|
7394
|
+
const offlineAttachment = offline({
|
|
7395
|
+
file_name: file.name,
|
|
7396
|
+
file_sha1: sha1,
|
|
7397
|
+
file: URL.createObjectURL(file),
|
|
7398
|
+
submission: submission.offline_id,
|
|
7399
|
+
field_identifier: fieldIdentifier
|
|
7400
|
+
});
|
|
7401
|
+
offlineAttachments.push(offlineAttachment);
|
|
7402
|
+
attachmentsPayload.push({
|
|
7403
|
+
offline_id: offlineAttachment.offline_id,
|
|
7404
|
+
submission_id: submission.offline_id,
|
|
7405
|
+
sha1,
|
|
7406
|
+
name: file.name,
|
|
7407
|
+
field_identifier: fieldIdentifier
|
|
7408
|
+
});
|
|
7409
|
+
}
|
|
7410
|
+
}
|
|
7392
7411
|
}
|
|
7393
|
-
|
|
7394
|
-
|
|
7395
|
-
|
|
7396
|
-
|
|
7397
|
-
|
|
7398
|
-
|
|
7399
|
-
|
|
7400
|
-
|
|
7401
|
-
|
|
7402
|
-
|
|
7403
|
-
|
|
7404
|
-
|
|
7412
|
+
const filePaylods = [];
|
|
7413
|
+
for (const file of Object.values(files).flat()) {
|
|
7414
|
+
const sha1 = await hashFile(file);
|
|
7415
|
+
const filePayload = {
|
|
7416
|
+
sha1,
|
|
7417
|
+
extension: file.name.split(".").pop() || "",
|
|
7418
|
+
file_type: file.type,
|
|
7419
|
+
size: file.size
|
|
7420
|
+
};
|
|
7421
|
+
allFilesRecord[sha1] = filePayload;
|
|
7422
|
+
filePaylods.push(filePayload);
|
|
7423
|
+
}
|
|
7424
|
+
return {
|
|
7425
|
+
batchId,
|
|
7426
|
+
payload: {
|
|
7427
|
+
form_data: fileSeperatedCommonFieldValues,
|
|
7428
|
+
submitted_at: submittedAt,
|
|
7429
|
+
submissions: submissionsPayload,
|
|
7430
|
+
attachments: attachmentsPayload,
|
|
7431
|
+
files: filePaylods
|
|
7432
|
+
}
|
|
7433
|
+
};
|
|
7434
|
+
})
|
|
7435
|
+
);
|
|
7405
7436
|
store.dispatch(addFormSubmissions(offlineSubmissions));
|
|
7406
7437
|
store.dispatch(addFormSubmissionAttachments(offlineAttachments));
|
|
7407
|
-
|
|
7408
|
-
|
|
7409
|
-
|
|
7410
|
-
|
|
7411
|
-
|
|
7412
|
-
|
|
7413
|
-
|
|
7414
|
-
|
|
7415
|
-
|
|
7416
|
-
files: Object.values(filesRecord)
|
|
7417
|
-
},
|
|
7418
|
-
blockers: Object.keys(fieldValuesByAsset),
|
|
7419
|
-
blocks: submissionOfflineIds
|
|
7420
|
-
});
|
|
7421
|
-
promise.then(({ submissions, attachments, presigned_urls }) => {
|
|
7422
|
-
store.dispatch(updateFormSubmissions(submissions));
|
|
7423
|
-
store.dispatch(updateFormSubmissionAttachments(attachments));
|
|
7424
|
-
for (const [sha1, presigned_url] of Object.entries(presigned_urls)) {
|
|
7425
|
-
const file = filesRecord[sha1];
|
|
7426
|
-
if (!file)
|
|
7427
|
-
continue;
|
|
7428
|
-
void this.client.enqueueRequest({
|
|
7429
|
-
url: presigned_url.url,
|
|
7430
|
-
description: "Upload file",
|
|
7438
|
+
let prevBatchId = null;
|
|
7439
|
+
const batchPromises = Promise.all(
|
|
7440
|
+
bulkAddBatches.map((batch) => {
|
|
7441
|
+
const { payload, batchId } = batch;
|
|
7442
|
+
const batchAssetIds = payload.submissions.map((x) => x.asset_id);
|
|
7443
|
+
const batchSubmissionOfflineIds = payload.submissions.map((x) => x.offline_id);
|
|
7444
|
+
const batchAttachmentsOfflineIds = payload.attachments.map((x) => x.offline_id);
|
|
7445
|
+
const promise = this.client.enqueueRequest({
|
|
7446
|
+
description: "Bulk add form submissions",
|
|
7431
7447
|
method: HttpMethod.POST,
|
|
7432
|
-
|
|
7433
|
-
|
|
7434
|
-
|
|
7435
|
-
|
|
7436
|
-
blocks: [sha1],
|
|
7437
|
-
s3url: presigned_url
|
|
7448
|
+
url: `/forms/revisions/${formRevision}/bulk-respond/`,
|
|
7449
|
+
payload,
|
|
7450
|
+
blockers: prevBatchId ? batchAssetIds.concat([prevBatchId]) : batchAssetIds,
|
|
7451
|
+
blocks: batchSubmissionOfflineIds.concat([batchId])
|
|
7438
7452
|
});
|
|
7439
|
-
|
|
7440
|
-
|
|
7441
|
-
|
|
7442
|
-
|
|
7443
|
-
|
|
7444
|
-
|
|
7453
|
+
promise.then(({ submissions, attachments, presigned_urls }) => {
|
|
7454
|
+
store.dispatch(updateFormSubmissions(submissions));
|
|
7455
|
+
store.dispatch(updateFormSubmissionAttachments(attachments));
|
|
7456
|
+
for (const [sha1, presigned_url] of Object.entries(presigned_urls)) {
|
|
7457
|
+
const file = allFilesRecord[sha1];
|
|
7458
|
+
if (!file)
|
|
7459
|
+
continue;
|
|
7460
|
+
void this.client.enqueueRequest({
|
|
7461
|
+
url: presigned_url.url,
|
|
7462
|
+
description: "Upload file",
|
|
7463
|
+
method: HttpMethod.POST,
|
|
7464
|
+
isExternalUrl: true,
|
|
7465
|
+
isAuthNeeded: false,
|
|
7466
|
+
attachmentHash: sha1,
|
|
7467
|
+
blockers: [`s3-${file.sha1}.${file.extension}`],
|
|
7468
|
+
blocks: [sha1],
|
|
7469
|
+
s3url: presigned_url
|
|
7470
|
+
});
|
|
7471
|
+
}
|
|
7472
|
+
}).catch(() => {
|
|
7473
|
+
store.dispatch(deleteFormSubmissions(batchSubmissionOfflineIds));
|
|
7474
|
+
store.dispatch(deleteFormSubmissionAttachments(batchAttachmentsOfflineIds));
|
|
7475
|
+
});
|
|
7476
|
+
prevBatchId = batchId;
|
|
7477
|
+
return promise;
|
|
7478
|
+
})
|
|
7479
|
+
);
|
|
7480
|
+
return [offlineSubmissions, batchPromises.then((results) => results.map(({ submissions }) => submissions))];
|
|
7445
7481
|
}
|
|
7446
7482
|
update(submission) {
|
|
7447
7483
|
const { store } = this.client;
|