@overmap-ai/core 1.0.58-asset-description.5 → 1.0.58-asset-description.7

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.
@@ -5368,6 +5368,16 @@ var __publicField = (obj, key, value) => {
5368
5368
  store.dispatch(setCategories(result));
5369
5369
  }
5370
5370
  }
5371
+ function chunkArray(arr, chunkSize) {
5372
+ const chunks = [];
5373
+ let index2 = 0;
5374
+ const arrLength = arr.length;
5375
+ while (index2 < arrLength) {
5376
+ chunks.push(arr.slice(index2, index2 += chunkSize));
5377
+ }
5378
+ return chunks;
5379
+ }
5380
+ const BULK_ADD_ASSET_BATCH_SIZE = 1e3;
5371
5381
  class AssetService extends BaseApiService {
5372
5382
  // Basic CRUD functions
5373
5383
  add(asset, workspaceId) {
@@ -5444,36 +5454,49 @@ var __publicField = (obj, key, value) => {
5444
5454
  throw err;
5445
5455
  });
5446
5456
  }
5447
- async addBatch(assetsToCreate, workspaceId, assetTypeId) {
5457
+ addBulk(assetsToCreate, workspaceId, assetTypeId) {
5458
+ const { store } = this.client;
5448
5459
  const fullAssets = assetsToCreate.map((asset) => {
5449
5460
  return { ...offline(asset), submitted_at: (/* @__PURE__ */ new Date()).toISOString() };
5450
5461
  });
5451
- const { store } = this.client;
5452
- store.dispatch(addAssetsInBatches(fullAssets));
5453
- const promise = this.client.enqueueRequest({
5454
- description: "Batch create assets",
5455
- method: HttpMethod.POST,
5456
- url: `/assets/types/${assetTypeId}/add-assets/`,
5457
- queryParams: {
5458
- workspace_id: workspaceId.toString()
5459
- },
5460
- payload: {
5461
- assets: fullAssets
5462
- },
5463
- blockers: [assetTypeId],
5464
- blocks: fullAssets.map((c) => c.offline_id)
5465
- });
5466
- void promise.then((result) => {
5467
- for (const assets of Object.values(result)) {
5468
- store.dispatch(updateAsset(assets));
5469
- }
5470
- }).catch((e) => {
5471
- for (const asset of fullAssets) {
5472
- store.dispatch(removeAsset(asset.offline_id));
5473
- }
5474
- throw e;
5462
+ const assetBatches = chunkArray(fullAssets, BULK_ADD_ASSET_BATCH_SIZE).map((assetBatch) => {
5463
+ return {
5464
+ batchId: uuid.v4(),
5465
+ payload: {
5466
+ assets: assetBatch
5467
+ }
5468
+ };
5475
5469
  });
5476
- return promise;
5470
+ store.dispatch(addAssetsInBatches(fullAssets));
5471
+ let prevBatchId = null;
5472
+ const batchPromises = Promise.all(
5473
+ assetBatches.map((assetBatch) => {
5474
+ const { batchId, payload } = assetBatch;
5475
+ const promise = this.client.enqueueRequest({
5476
+ description: "Batch create assets",
5477
+ method: HttpMethod.POST,
5478
+ url: `/assets/types/${assetTypeId}/add-assets/`,
5479
+ queryParams: {
5480
+ workspace_id: workspaceId.toString()
5481
+ },
5482
+ payload,
5483
+ blockers: prevBatchId ? [assetTypeId, prevBatchId] : [assetTypeId],
5484
+ blocks: assetBatch.payload.assets.map((c) => c.offline_id).concat([batchId])
5485
+ });
5486
+ promise.then((result) => {
5487
+ for (const assets of Object.values(result)) {
5488
+ store.dispatch(updateAsset(assets));
5489
+ }
5490
+ }).catch(() => {
5491
+ for (const asset of assetBatch.payload.assets) {
5492
+ store.dispatch(removeAsset(asset.offline_id));
5493
+ }
5494
+ });
5495
+ prevBatchId = assetBatch.batchId;
5496
+ return promise;
5497
+ })
5498
+ );
5499
+ return [fullAssets, batchPromises.then((result) => result)];
5477
5500
  }
5478
5501
  async refreshStore() {
5479
5502
  const { store } = this.client;
@@ -7259,6 +7282,7 @@ var __publicField = (obj, key, value) => {
7259
7282
  }
7260
7283
  return { values: newValues, files };
7261
7284
  };
7285
+ const MAX_BULK_ADD_SUBMISSIONS = 1e3;
7262
7286
  class UserFormSubmissionService extends BaseApiService {
7263
7287
  constructor() {
7264
7288
  super(...arguments);
@@ -7345,103 +7369,129 @@ var __publicField = (obj, key, value) => {
7345
7369
  const offlineSubmissions = [];
7346
7370
  const offlineAttachments = [];
7347
7371
  const submissionOfflineIds = [];
7348
- const submissionsPayload = [];
7349
- const attachmentsPayload = [];
7350
- let files = {};
7372
+ const allFilesRecord = {};
7351
7373
  const { values: fileSeperatedCommonFieldValues, files: commonFiles } = separateFilesFromValues(commonFieldValues);
7352
- files = commonFiles;
7353
7374
  const submittedAt = (/* @__PURE__ */ new Date()).toISOString();
7354
7375
  const createdBy = store.getState().userReducer.currentUser.id;
7355
- for (const assetId in fieldValuesByAsset) {
7356
- const { values: fileSeperatedSubmissionSpecificValues, files: submissionSpecificFiles } = separateFilesFromValues(fieldValuesByAsset[assetId] ?? {});
7357
- files = Object.assign(files, submissionSpecificFiles);
7358
- const submissionValues = { ...fileSeperatedCommonFieldValues, ...fileSeperatedSubmissionSpecificValues };
7359
- const submission = offline({
7360
- form_revision: formRevision,
7361
- values: submissionValues,
7362
- created_by: createdBy,
7363
- submitted_at: submittedAt,
7364
- asset: assetId
7365
- });
7366
- submissionOfflineIds.push(submission.offline_id);
7367
- submissionsPayload.push({
7368
- offline_id: submission.offline_id,
7369
- asset_id: assetId,
7370
- form_data: fileSeperatedSubmissionSpecificValues
7371
- });
7372
- offlineSubmissions.push(submission);
7373
- for (const [fieldIdentifier, fileArray] of Object.entries(files)) {
7374
- for (const file of fileArray) {
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
7376
+ const assetIdBatches = chunkArray(Object.keys(fieldValuesByAsset), MAX_BULK_ADD_SUBMISSIONS);
7377
+ const bulkAddBatches = await Promise.all(
7378
+ assetIdBatches.map(async (assetIdBatch) => {
7379
+ const batchId = uuid.v4();
7380
+ const submissionsPayload = [];
7381
+ const attachmentsPayload = [];
7382
+ let files = { ...commonFiles };
7383
+ for (const assetId of assetIdBatch) {
7384
+ const { values: fileSeperatedSubmissionSpecificValues, files: submissionSpecificFiles } = separateFilesFromValues(fieldValuesByAsset[assetId] ?? {});
7385
+ files = Object.assign(files, submissionSpecificFiles);
7386
+ const submissionValues = {
7387
+ ...fileSeperatedCommonFieldValues,
7388
+ ...fileSeperatedSubmissionSpecificValues
7389
+ };
7390
+ const submission = offline({
7391
+ form_revision: formRevision,
7392
+ values: submissionValues,
7393
+ created_by: createdBy,
7394
+ submitted_at: submittedAt,
7395
+ asset: assetId
7383
7396
  });
7384
- offlineAttachments.push(offlineAttachment);
7385
- attachmentsPayload.push({
7386
- offline_id: offlineAttachment.offline_id,
7387
- submission_id: submission.offline_id,
7388
- sha1,
7389
- name: file.name,
7390
- field_identifier: fieldIdentifier
7397
+ submissionOfflineIds.push(submission.offline_id);
7398
+ submissionsPayload.push({
7399
+ offline_id: submission.offline_id,
7400
+ asset_id: assetId,
7401
+ form_data: fileSeperatedSubmissionSpecificValues
7391
7402
  });
7403
+ offlineSubmissions.push(submission);
7404
+ for (const [fieldIdentifier, fileArray] of Object.entries(files)) {
7405
+ for (const file of fileArray) {
7406
+ const sha1 = await hashFile(file);
7407
+ await this.client.files.addCache(file, sha1);
7408
+ const offlineAttachment = offline({
7409
+ file_name: file.name,
7410
+ file_sha1: sha1,
7411
+ file: URL.createObjectURL(file),
7412
+ submission: submission.offline_id,
7413
+ field_identifier: fieldIdentifier
7414
+ });
7415
+ offlineAttachments.push(offlineAttachment);
7416
+ attachmentsPayload.push({
7417
+ offline_id: offlineAttachment.offline_id,
7418
+ submission_id: submission.offline_id,
7419
+ sha1,
7420
+ name: file.name,
7421
+ field_identifier: fieldIdentifier
7422
+ });
7423
+ }
7424
+ }
7392
7425
  }
7393
- }
7394
- }
7395
- const filesRecord = {};
7396
- for (const file of Object.values(files).flat()) {
7397
- const sha1 = await hashFile(file);
7398
- filesRecord[sha1] = {
7399
- sha1,
7400
- extension: file.name.split(".").pop() || "",
7401
- file_type: file.type,
7402
- size: file.size
7403
- };
7404
- }
7426
+ const filePaylods = [];
7427
+ for (const file of Object.values(files).flat()) {
7428
+ const sha1 = await hashFile(file);
7429
+ const filePayload = {
7430
+ sha1,
7431
+ extension: file.name.split(".").pop() || "",
7432
+ file_type: file.type,
7433
+ size: file.size
7434
+ };
7435
+ allFilesRecord[sha1] = filePayload;
7436
+ filePaylods.push(filePayload);
7437
+ }
7438
+ return {
7439
+ batchId,
7440
+ payload: {
7441
+ form_data: fileSeperatedCommonFieldValues,
7442
+ submitted_at: submittedAt,
7443
+ submissions: submissionsPayload,
7444
+ attachments: attachmentsPayload,
7445
+ files: filePaylods
7446
+ }
7447
+ };
7448
+ })
7449
+ );
7405
7450
  store.dispatch(addFormSubmissions(offlineSubmissions));
7406
7451
  store.dispatch(addFormSubmissionAttachments(offlineAttachments));
7407
- const promise = this.client.enqueueRequest({
7408
- description: "Bulk add form submissions",
7409
- method: HttpMethod.POST,
7410
- url: `/forms/revisions/${formRevision}/bulk-respond/`,
7411
- payload: {
7412
- form_data: fileSeperatedCommonFieldValues,
7413
- submitted_at: submittedAt,
7414
- submissions: submissionsPayload,
7415
- attachments: attachmentsPayload,
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",
7452
+ let prevBatchId = null;
7453
+ const batchPromises = Promise.all(
7454
+ bulkAddBatches.map((batch) => {
7455
+ const { payload, batchId } = batch;
7456
+ const batchAssetIds = payload.submissions.map((x) => x.asset_id);
7457
+ const batchSubmissionOfflineIds = payload.submissions.map((x) => x.offline_id);
7458
+ const batchAttachmentsOfflineIds = payload.attachments.map((x) => x.offline_id);
7459
+ const promise = this.client.enqueueRequest({
7460
+ description: "Bulk add form submissions",
7431
7461
  method: HttpMethod.POST,
7432
- isExternalUrl: true,
7433
- isAuthNeeded: false,
7434
- attachmentHash: sha1,
7435
- blockers: [`s3-${file.sha1}.${file.extension}`],
7436
- blocks: [sha1],
7437
- s3url: presigned_url
7462
+ url: `/forms/revisions/${formRevision}/bulk-respond/`,
7463
+ payload,
7464
+ blockers: prevBatchId ? batchAssetIds.concat([prevBatchId]) : batchAssetIds,
7465
+ blocks: batchSubmissionOfflineIds.concat([batchId])
7438
7466
  });
7439
- }
7440
- }).catch(() => {
7441
- store.dispatch(deleteFormSubmissions(submissionOfflineIds));
7442
- store.dispatch(deleteFormSubmissionAttachments(offlineAttachments.map((x) => x.offline_id)));
7443
- });
7444
- return [offlineSubmissions, promise.then(({ submissions }) => submissions)];
7467
+ promise.then(({ submissions, attachments, presigned_urls }) => {
7468
+ store.dispatch(updateFormSubmissions(submissions));
7469
+ store.dispatch(updateFormSubmissionAttachments(attachments));
7470
+ for (const [sha1, presigned_url] of Object.entries(presigned_urls)) {
7471
+ const file = allFilesRecord[sha1];
7472
+ if (!file)
7473
+ continue;
7474
+ void this.client.enqueueRequest({
7475
+ url: presigned_url.url,
7476
+ description: "Upload file",
7477
+ method: HttpMethod.POST,
7478
+ isExternalUrl: true,
7479
+ isAuthNeeded: false,
7480
+ attachmentHash: sha1,
7481
+ blockers: [`s3-${file.sha1}.${file.extension}`],
7482
+ blocks: [sha1],
7483
+ s3url: presigned_url
7484
+ });
7485
+ }
7486
+ }).catch(() => {
7487
+ store.dispatch(deleteFormSubmissions(batchSubmissionOfflineIds));
7488
+ store.dispatch(deleteFormSubmissionAttachments(batchAttachmentsOfflineIds));
7489
+ });
7490
+ prevBatchId = batchId;
7491
+ return promise;
7492
+ })
7493
+ );
7494
+ return [offlineSubmissions, batchPromises.then((results) => results.map(({ submissions }) => submissions))];
7445
7495
  }
7446
7496
  update(submission) {
7447
7497
  const { store } = this.client;