@overmap-ai/core 1.0.58-asset-description.8 → 1.0.58-asset-description.10

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.
@@ -5399,7 +5399,6 @@ var __publicField = (obj, key, value) => {
5399
5399
  }
5400
5400
  return chunks;
5401
5401
  }
5402
- const BULK_ADD_ASSET_BATCH_SIZE = 1e3;
5403
5402
  class AssetService extends BaseApiService {
5404
5403
  // Basic CRUD functions
5405
5404
  add(asset, workspaceId) {
@@ -5476,45 +5475,53 @@ var __publicField = (obj, key, value) => {
5476
5475
  throw err;
5477
5476
  });
5478
5477
  }
5479
- addBulk(assetsToCreate, workspaceId, assetTypeId) {
5478
+ // TODO: payload does not require asset_type
5479
+ bulkAdd(assetsToCreate, workspaceId, assetTypeId, batchSize) {
5480
5480
  const { store } = this.client;
5481
- const fullAssets = assetsToCreate.map((asset) => {
5482
- return { ...offline(asset), submitted_at: (/* @__PURE__ */ new Date()).toISOString() };
5483
- });
5484
- const assetBatches = chunkArray(fullAssets, BULK_ADD_ASSET_BATCH_SIZE).map((assetBatch) => {
5481
+ const submittedAt = (/* @__PURE__ */ new Date()).toISOString();
5482
+ const assetBatches = chunkArray(assetsToCreate, batchSize).map((assetBatch) => {
5483
+ const assetPayloads = assetBatch.map((assetPayload) => {
5484
+ return offline({
5485
+ ...assetPayload,
5486
+ submitted_at: submittedAt
5487
+ });
5488
+ });
5485
5489
  return {
5486
5490
  batchId: uuid.v4(),
5487
5491
  payload: {
5488
- assets: assetBatch
5492
+ assets: assetPayloads
5489
5493
  }
5490
5494
  };
5491
5495
  });
5492
- store.dispatch(addAssetsInBatches(fullAssets));
5496
+ const batchPromises = [];
5493
5497
  let prevBatchId = null;
5494
- const batchPromises = Promise.all(
5495
- assetBatches.map((assetBatch) => {
5496
- const { batchId, payload } = assetBatch;
5497
- const promise = this.client.enqueueRequest({
5498
- description: "Batch create assets",
5499
- method: HttpMethod.POST,
5500
- url: `/assets/types/${assetTypeId}/add-assets/`,
5501
- queryParams: {
5502
- workspace_id: workspaceId.toString()
5503
- },
5504
- payload,
5505
- blockers: prevBatchId ? [assetTypeId, prevBatchId] : [assetTypeId],
5506
- blocks: assetBatch.payload.assets.map((c) => c.offline_id).concat([batchId])
5507
- });
5508
- promise.then((result) => {
5509
- store.dispatch(updateAssets(Object.values(result)));
5510
- }).catch(() => {
5511
- store.dispatch(removeAssets(assetBatch.payload.assets.map((c) => c.offline_id)));
5512
- });
5513
- prevBatchId = assetBatch.batchId;
5514
- return promise;
5515
- })
5516
- );
5517
- return [fullAssets, batchPromises.then((result) => result)];
5498
+ for (const assetBatch of assetBatches) {
5499
+ const { batchId, payload } = assetBatch;
5500
+ const batchAssetOfflineIds = payload.assets.map((c) => c.offline_id);
5501
+ const blockers = [assetTypeId];
5502
+ if (prevBatchId)
5503
+ blockers.push(prevBatchId);
5504
+ const blocks2 = batchAssetOfflineIds;
5505
+ blocks2.push(batchId);
5506
+ const promise = this.client.enqueueRequest({
5507
+ description: "Batch create assets",
5508
+ method: HttpMethod.POST,
5509
+ url: `/assets/types/${assetTypeId}/add-assets/`,
5510
+ queryParams: {
5511
+ workspace_id: workspaceId.toString()
5512
+ },
5513
+ payload,
5514
+ blockers,
5515
+ blocks: blocks2
5516
+ });
5517
+ prevBatchId = assetBatch.batchId;
5518
+ batchPromises.push(promise);
5519
+ }
5520
+ void Promise.all(batchPromises).then((result) => {
5521
+ const allCreatedAssets = result.flat();
5522
+ store.dispatch(addAssetsInBatches(allCreatedAssets));
5523
+ });
5524
+ return batchPromises;
5518
5525
  }
5519
5526
  async refreshStore() {
5520
5527
  const { store } = this.client;
@@ -7300,7 +7307,6 @@ var __publicField = (obj, key, value) => {
7300
7307
  }
7301
7308
  return { values: newValues, files };
7302
7309
  };
7303
- const MAX_BULK_ADD_SUBMISSIONS = 1e3;
7304
7310
  class UserFormSubmissionService extends BaseApiService {
7305
7311
  constructor() {
7306
7312
  super(...arguments);
@@ -7381,63 +7387,38 @@ var __publicField = (obj, key, value) => {
7381
7387
  }
7382
7388
  // Note currently the bulkAdd method is specific to form submissions for assets
7383
7389
  // TODO: adapt the support bulk adding to any model type
7384
- async bulkAdd(args) {
7385
- const { formRevision, commonFieldValues, fieldValuesByAsset } = args;
7390
+ async bulkAdd(args, batchSize) {
7386
7391
  const { store } = this.client;
7387
- const offlineSubmissions = [];
7388
- const offlineAttachments = [];
7389
- const submissionOfflineIds = [];
7392
+ const { formRevision, commonFieldValues, fieldValuesByAsset } = args;
7390
7393
  const allFilesRecord = {};
7391
7394
  const { values: fileSeperatedCommonFieldValues, files: commonFiles } = separateFilesFromValues(commonFieldValues);
7392
7395
  const submittedAt = (/* @__PURE__ */ new Date()).toISOString();
7393
- const createdBy = store.getState().userReducer.currentUser.id;
7394
- const assetIdBatches = chunkArray(Object.keys(fieldValuesByAsset), MAX_BULK_ADD_SUBMISSIONS);
7396
+ const assetIdBatches = chunkArray(Object.keys(fieldValuesByAsset), batchSize);
7395
7397
  const bulkAddBatches = await Promise.all(
7396
7398
  assetIdBatches.map(async (assetIdBatch) => {
7397
7399
  const batchId = uuid.v4();
7398
- const submissionsPayload = [];
7399
- const attachmentsPayload = [];
7400
- let files = { ...commonFiles };
7400
+ const submissionPayloads = [];
7401
+ const attachmentPayloads = [];
7402
+ const files = { ...commonFiles };
7401
7403
  for (const assetId of assetIdBatch) {
7402
7404
  const { values: fileSeperatedSubmissionSpecificValues, files: submissionSpecificFiles } = separateFilesFromValues(fieldValuesByAsset[assetId] ?? {});
7403
- files = Object.assign(files, submissionSpecificFiles);
7404
- const submissionValues = {
7405
- ...fileSeperatedCommonFieldValues,
7406
- ...fileSeperatedSubmissionSpecificValues
7407
- };
7408
- const submission = offline({
7409
- form_revision: formRevision,
7410
- values: submissionValues,
7411
- created_by: createdBy,
7412
- submitted_at: submittedAt,
7413
- asset: assetId
7414
- });
7415
- submissionOfflineIds.push(submission.offline_id);
7416
- submissionsPayload.push({
7417
- offline_id: submission.offline_id,
7405
+ Object.assign(files, submissionSpecificFiles);
7406
+ const submissionPayload = offline({
7418
7407
  asset_id: assetId,
7419
7408
  form_data: fileSeperatedSubmissionSpecificValues
7420
7409
  });
7421
- offlineSubmissions.push(submission);
7410
+ submissionPayloads.push(submissionPayload);
7422
7411
  for (const [fieldIdentifier, fileArray] of Object.entries(files)) {
7423
7412
  for (const file of fileArray) {
7424
7413
  const sha1 = await hashFile(file);
7425
7414
  await this.client.files.addCache(file, sha1);
7426
- const offlineAttachment = offline({
7427
- file_name: file.name,
7428
- file_sha1: sha1,
7429
- file: URL.createObjectURL(file),
7430
- submission: submission.offline_id,
7431
- field_identifier: fieldIdentifier
7432
- });
7433
- offlineAttachments.push(offlineAttachment);
7434
- attachmentsPayload.push({
7435
- offline_id: offlineAttachment.offline_id,
7436
- submission_id: submission.offline_id,
7415
+ const attachmentPayload = offline({
7416
+ submission_id: submissionPayload.offline_id,
7437
7417
  sha1,
7438
7418
  name: file.name,
7439
7419
  field_identifier: fieldIdentifier
7440
7420
  });
7421
+ attachmentPayloads.push(attachmentPayload);
7441
7422
  }
7442
7423
  }
7443
7424
  }
@@ -7458,58 +7439,66 @@ var __publicField = (obj, key, value) => {
7458
7439
  payload: {
7459
7440
  form_data: fileSeperatedCommonFieldValues,
7460
7441
  submitted_at: submittedAt,
7461
- submissions: submissionsPayload,
7462
- attachments: attachmentsPayload,
7442
+ submissions: submissionPayloads,
7443
+ attachments: attachmentPayloads,
7463
7444
  files: filePaylods
7464
7445
  }
7465
7446
  };
7466
7447
  })
7467
7448
  );
7468
- store.dispatch(addFormSubmissions(offlineSubmissions));
7469
- store.dispatch(addFormSubmissionAttachments(offlineAttachments));
7449
+ const batchPromises = [];
7470
7450
  let prevBatchId = null;
7471
- const batchPromises = Promise.all(
7472
- bulkAddBatches.map((batch) => {
7473
- const { payload, batchId } = batch;
7474
- const batchAssetIds = payload.submissions.map((x) => x.asset_id);
7475
- const batchSubmissionOfflineIds = payload.submissions.map((x) => x.offline_id);
7476
- const batchAttachmentsOfflineIds = payload.attachments.map((x) => x.offline_id);
7477
- const promise = this.client.enqueueRequest({
7478
- description: "Bulk add form submissions",
7479
- method: HttpMethod.POST,
7480
- url: `/forms/revisions/${formRevision}/bulk-respond/`,
7481
- payload,
7482
- blockers: prevBatchId ? batchAssetIds.concat([prevBatchId]) : batchAssetIds,
7483
- blocks: batchSubmissionOfflineIds.concat([batchId])
7484
- });
7485
- promise.then(({ submissions, attachments, presigned_urls }) => {
7486
- store.dispatch(updateFormSubmissions(submissions));
7487
- store.dispatch(updateFormSubmissionAttachments(attachments));
7488
- for (const [sha1, presigned_url] of Object.entries(presigned_urls)) {
7489
- const file = allFilesRecord[sha1];
7490
- if (!file)
7491
- continue;
7492
- void this.client.enqueueRequest({
7493
- url: presigned_url.url,
7494
- description: "Upload file",
7495
- method: HttpMethod.POST,
7496
- isExternalUrl: true,
7497
- isAuthNeeded: false,
7498
- attachmentHash: sha1,
7499
- blockers: [`s3-${file.sha1}.${file.extension}`],
7500
- blocks: [sha1],
7501
- s3url: presigned_url
7502
- });
7503
- }
7504
- }).catch(() => {
7505
- store.dispatch(deleteFormSubmissions(batchSubmissionOfflineIds));
7506
- store.dispatch(deleteFormSubmissionAttachments(batchAttachmentsOfflineIds));
7507
- });
7508
- prevBatchId = batchId;
7509
- return promise;
7510
- })
7511
- );
7512
- return [offlineSubmissions, batchPromises.then((results) => results.map(({ submissions }) => submissions))];
7451
+ for (const batch of bulkAddBatches) {
7452
+ const { payload, batchId } = batch;
7453
+ const batchAssetIds = payload.submissions.map((x) => x.asset_id);
7454
+ const batchSubmissionOfflineIds = payload.submissions.map((x) => x.offline_id);
7455
+ const batchAttachmentsOfflineIds = payload.attachments.map((x) => x.offline_id);
7456
+ const blockers = batchAssetIds;
7457
+ if (prevBatchId)
7458
+ blockers.push(prevBatchId);
7459
+ const blocks2 = [...batchSubmissionOfflineIds, ...batchAttachmentsOfflineIds, batchId];
7460
+ const promise = this.client.enqueueRequest({
7461
+ description: "Bulk add form submissions",
7462
+ method: HttpMethod.POST,
7463
+ url: `/forms/revisions/${formRevision}/bulk-respond/`,
7464
+ payload,
7465
+ blockers,
7466
+ blocks: blocks2
7467
+ });
7468
+ void promise.then(({ presigned_urls }) => {
7469
+ for (const [sha1, presignedUrl] of Object.entries(presigned_urls)) {
7470
+ const file = allFilesRecord[sha1];
7471
+ if (!file)
7472
+ continue;
7473
+ void this.client.enqueueRequest({
7474
+ url: presignedUrl.url,
7475
+ description: "Upload file",
7476
+ method: HttpMethod.POST,
7477
+ isExternalUrl: true,
7478
+ isAuthNeeded: false,
7479
+ attachmentHash: sha1,
7480
+ blockers: [`s3-${file.sha1}.${file.extension}`],
7481
+ blocks: [sha1],
7482
+ s3url: presignedUrl
7483
+ });
7484
+ }
7485
+ });
7486
+ prevBatchId = batchId;
7487
+ batchPromises.push(promise);
7488
+ }
7489
+ void Promise.all(batchPromises).then((results) => {
7490
+ const createdSubmissions = [];
7491
+ const createdAttachments = [];
7492
+ for (const result of results) {
7493
+ for (const createdSubmission of result.submissions)
7494
+ createdSubmissions.push(createdSubmission);
7495
+ for (const createdAttachment of result.attachments)
7496
+ createdAttachments.push(createdAttachment);
7497
+ }
7498
+ store.dispatch(addFormSubmissions(createdSubmissions));
7499
+ store.dispatch(addFormSubmissionAttachments(createdAttachments));
7500
+ });
7501
+ return batchPromises;
7513
7502
  }
7514
7503
  update(submission) {
7515
7504
  const { store } = this.client;
@@ -13123,10 +13112,7 @@ var __publicField = (obj, key, value) => {
13123
13112
  helpText = showInputOnly ? null : helpText;
13124
13113
  label = showInputOnly ? "" : label;
13125
13114
  const color = blocks.useSeverityColor(severity);
13126
- const value = React.useMemo(
13127
- () => Array.isArray(fieldProps.value) ? fieldProps.value : [],
13128
- [fieldProps.value]
13129
- );
13115
+ const value = React.useMemo(() => Array.isArray(fieldProps.value) ? fieldProps.value : [], [fieldProps.value]);
13130
13116
  const { onChange, onBlur } = fieldProps;
13131
13117
  const droppableId = `${inputId}-droppable`;
13132
13118
  const { disabled } = rest;
@@ -14942,39 +14928,37 @@ var __publicField = (obj, key, value) => {
14942
14928
  meta: { readonly }
14943
14929
  };
14944
14930
  }
14945
- function decodeFormValues(schema, values) {
14946
- let allFields = [];
14931
+ function flattenFields(schema) {
14932
+ const allFields = [];
14947
14933
  for (const field of schema.fields) {
14948
14934
  if (field instanceof FieldSection) {
14949
- allFields = allFields.concat(field.fields);
14935
+ for (const subField of field.fields) {
14936
+ allFields.push(subField);
14937
+ }
14950
14938
  } else {
14951
14939
  if (!(field instanceof BaseField)) {
14952
- throw new Error("Invalid field type");
14940
+ throw new Error(`Invalid field type: ${field.type}`);
14953
14941
  }
14954
14942
  allFields.push(field);
14955
14943
  }
14956
14944
  }
14945
+ return allFields;
14946
+ }
14947
+ function decodeFormValues(schema, values) {
14948
+ const allFields = flattenFields(schema);
14957
14949
  const result = {};
14958
14950
  for (const field of allFields) {
14959
- if (!(field.identifier in values))
14960
- ;
14961
- const value = values[field.identifier];
14962
- result[field.identifier] = field.decodeJsonToValue(value);
14951
+ const value = values[field.identifier] ?? null;
14952
+ if (value !== null) {
14953
+ result[field.identifier] = field.decodeJsonToValue(value);
14954
+ } else {
14955
+ result[field.identifier] = value;
14956
+ }
14963
14957
  }
14964
14958
  return result;
14965
14959
  }
14966
14960
  function encodeFormValues(schema, values) {
14967
- let allFields = [];
14968
- for (const field of schema.fields) {
14969
- if (field instanceof FieldSection) {
14970
- allFields = allFields.concat(field.fields);
14971
- } else {
14972
- if (!(field instanceof BaseField)) {
14973
- throw new Error("Invalid field type");
14974
- }
14975
- allFields.push(field);
14976
- }
14977
- }
14961
+ const allFields = flattenFields(schema);
14978
14962
  const result = {};
14979
14963
  for (const field of allFields) {
14980
14964
  const value = values[field.identifier];
@@ -16777,6 +16761,7 @@ var __publicField = (obj, key, value) => {
16777
16761
  emptyStringField,
16778
16762
  emptyTextField,
16779
16763
  encodeFormValues,
16764
+ flattenFields,
16780
16765
  formRevisionToSchema,
16781
16766
  initialFormValues,
16782
16767
  isConditionMet,
@@ -17018,6 +17003,7 @@ var __publicField = (obj, key, value) => {
17018
17003
  exports2.fileReducer = fileReducer;
17019
17004
  exports2.fileSlice = fileSlice;
17020
17005
  exports2.fileToBlob = fileToBlob;
17006
+ exports2.flattenFields = flattenFields;
17021
17007
  exports2.flipCoordinates = flipCoordinates;
17022
17008
  exports2.formReducer = formReducer;
17023
17009
  exports2.formRevisionReducer = formRevisionReducer;