@overmap-ai/core 1.0.51-bulk-form-submission.0 → 1.0.51-bulk-form-submission.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.
@@ -3481,33 +3481,19 @@ const selectAppearance = (state) => state.settingReducer.appearance;
3481
3481
  const settingReducer = settingSlice.reducer;
3482
3482
  const selectIsFetchingInitialData = (state) => state.settingReducer.isFetchingInitialData;
3483
3483
  const selectIsLoading = (state) => state.settingReducer.isLoading;
3484
- const LATEST_FORM_REVISION_CACHE = {};
3485
- function considerCachingFormRevision(formRevision, formId2, preferPending = false) {
3486
- var _a2;
3487
- if (!formRevision) {
3488
- if (!formId2) {
3489
- throw new Error("If form revision is null, formId is required.");
3490
- }
3491
- const currentLatestFormRevision = getLatestFormRevisionFromCache(formId2);
3492
- if (currentLatestFormRevision)
3493
- return;
3494
- LATEST_FORM_REVISION_CACHE[formId2] = null;
3495
- return;
3496
- }
3497
- if (formRevision.revision === "Pending") {
3498
- if (preferPending) {
3499
- LATEST_FORM_REVISION_CACHE[formRevision.form] = formRevision;
3500
- }
3501
- return;
3502
- }
3503
- const cachedFormRevision = (_a2 = LATEST_FORM_REVISION_CACHE[formRevision.form]) == null ? void 0 : _a2.revision;
3504
- if (formRevision.revision > (typeof cachedFormRevision === "number" ? cachedFormRevision : -1)) {
3505
- LATEST_FORM_REVISION_CACHE[formRevision.form] = formRevision;
3484
+ const formRevisionSortFn = (formRevisionA, formRevisionB) => {
3485
+ const revisionA = formRevisionA.revision;
3486
+ const revisionB = formRevisionB.revision;
3487
+ if (revisionA === "Pending" && revisionB === "Pending") {
3488
+ return formRevisionA.submitted_at < formRevisionB.submitted_at ? -1 : 1;
3489
+ } else if (revisionA === "Pending") {
3490
+ return 1;
3491
+ } else if (revisionB === "Pending") {
3492
+ return -1;
3493
+ } else {
3494
+ return revisionA < revisionB ? -1 : 1;
3506
3495
  }
3507
- }
3508
- function getLatestFormRevisionFromCache(formId2) {
3509
- return LATEST_FORM_REVISION_CACHE[formId2];
3510
- }
3496
+ };
3511
3497
  const initialState$5 = {
3512
3498
  formRevisions: {},
3513
3499
  attachments: {}
@@ -3520,13 +3506,11 @@ const formRevisionsSlice = createSlice({
3520
3506
  // revision related actions
3521
3507
  setFormRevision: (state, action) => {
3522
3508
  state.formRevisions[action.payload.offline_id] = action.payload;
3523
- considerCachingFormRevision(action.payload);
3524
3509
  },
3525
3510
  setFormRevisions: (state, action) => {
3526
3511
  state.formRevisions = {};
3527
3512
  for (const revision of action.payload) {
3528
3513
  state.formRevisions[revision.offline_id] = revision;
3529
- considerCachingFormRevision(revision);
3530
3514
  }
3531
3515
  },
3532
3516
  addFormRevision: (state, action) => {
@@ -3534,9 +3518,7 @@ const formRevisionsSlice = createSlice({
3534
3518
  throw new Error(`Revision with offline_id ${action.payload.offline_id} already exists`);
3535
3519
  }
3536
3520
  state.formRevisions[action.payload.offline_id] = action.payload;
3537
- considerCachingFormRevision(action.payload);
3538
3521
  },
3539
- // TODO: @Audiopolis / Magnus - do we want to standardize using PayloadAction?
3540
3522
  addFormRevisions: (state, action) => {
3541
3523
  for (const userFormRevision of action.payload) {
3542
3524
  if (state.formRevisions[userFormRevision.offline_id] !== void 0) {
@@ -3545,7 +3527,6 @@ const formRevisionsSlice = createSlice({
3545
3527
  }
3546
3528
  for (const userFormRevision of action.payload) {
3547
3529
  state.formRevisions[userFormRevision.offline_id] = userFormRevision;
3548
- considerCachingFormRevision(userFormRevision);
3549
3530
  }
3550
3531
  },
3551
3532
  // UserFormRevisions do not get updated
@@ -3554,7 +3535,6 @@ const formRevisionsSlice = createSlice({
3554
3535
  throw new Error(`Revision with offline_id ${action.payload} does not exist`);
3555
3536
  }
3556
3537
  delete state.formRevisions[action.payload];
3557
- delete LATEST_FORM_REVISION_CACHE[action.payload];
3558
3538
  },
3559
3539
  deleteFormRevisions: (state, action) => {
3560
3540
  for (const offlineId of action.payload) {
@@ -3564,7 +3544,6 @@ const formRevisionsSlice = createSlice({
3564
3544
  }
3565
3545
  for (const offlineId of action.payload) {
3566
3546
  delete state.formRevisions[offlineId];
3567
- delete LATEST_FORM_REVISION_CACHE[offlineId];
3568
3547
  }
3569
3548
  },
3570
3549
  // attachment related actions
@@ -3642,11 +3621,8 @@ const _selectLatestFormRevision = (formRevisions, formId2) => {
3642
3621
  return ret;
3643
3622
  };
3644
3623
  const selectLatestFormRevisionOfForm = restructureCreateSelectorWithArgs(
3645
- createSelector([selectFormRevisionMapping, (_state, formId2) => formId2], (revisions, formId2) => {
3646
- if (!formId2) {
3647
- throw new Error("formId is required");
3648
- }
3649
- return _selectLatestFormRevision(revisions, formId2);
3624
+ createSelector([selectFormRevisions, (_state, formId2) => formId2], (revisions, formId2) => {
3625
+ return revisions.filter((revision) => revision.form === formId2).sort(formRevisionSortFn).pop();
3650
3626
  })
3651
3627
  );
3652
3628
  const selectFormRevisionsOfForm = restructureCreateSelectorWithArgs(
@@ -3665,12 +3641,19 @@ const selectLatestFormRevisionsOfComponentTypes = restructureCreateSelectorWithA
3665
3641
  ],
3666
3642
  (userForms, revisions, componentTypeIds) => {
3667
3643
  const componentTypeIdsSet = new Set(componentTypeIds);
3644
+ const formsOfComponentTypes = {};
3668
3645
  const ret = {};
3669
3646
  for (const form of Object.values(userForms)) {
3670
3647
  if (form.component_type && componentTypeIdsSet.has(form.component_type)) {
3671
- ret[form.component_type] = _selectLatestFormRevision(revisions, form.offline_id);
3648
+ formsOfComponentTypes[form.component_type] = form;
3672
3649
  }
3673
3650
  }
3651
+ for (const revision of Object.values(revisions)) {
3652
+ const form = formsOfComponentTypes[revision.form];
3653
+ if (!form || !form.component_type || !ret[form.component_type] || formRevisionSortFn(ret[form.component_type], revision) < 0)
3654
+ continue;
3655
+ ret[form.component_type] = revision;
3656
+ }
3674
3657
  return ret;
3675
3658
  }
3676
3659
  )
@@ -3716,9 +3699,9 @@ const formSlice = createSlice({
3716
3699
  state.forms[action.payload.offline_id] = action.payload;
3717
3700
  },
3718
3701
  addForms: (state, action) => {
3719
- action.payload.forEach((userForm) => {
3702
+ for (const userForm of action.payload) {
3720
3703
  state.forms[userForm.offline_id] = userForm;
3721
- });
3704
+ }
3722
3705
  },
3723
3706
  favoriteForm: (state, action) => {
3724
3707
  const { formId: formId2 } = action.payload;
@@ -3820,7 +3803,7 @@ const formSubmissionSlice = createSlice({
3820
3803
  }
3821
3804
  },
3822
3805
  addFormSubmission: (state, action) => {
3823
- if (state.formSubmissions[action.payload.offline_id] !== void 0) {
3806
+ if (action.payload.offline_id in state.formSubmissions) {
3824
3807
  throw new Error(`Submission with offline_id ${action.payload.offline_id} already exists`);
3825
3808
  }
3826
3809
  state.formSubmissions[action.payload.offline_id] = action.payload;
@@ -7003,13 +6986,14 @@ class UserFormService extends BaseApiService {
7003
6986
  };
7004
6987
  const currentUser = state.userReducer.currentUser;
7005
6988
  const activeWorkspaceId = state.workspaceReducer.activeWorkspaceId;
6989
+ const submittedAt = (/* @__PURE__ */ new Date()).toISOString();
7006
6990
  const offlineFormPayload = offline({});
7007
- const offlineRevisionPayload = offline(initialRevision);
6991
+ const offlineRevisionPayload = offline({ ...initialRevision, submitted_at: submittedAt });
7008
6992
  const retForm = {
7009
6993
  ...offlineFormPayload,
7010
6994
  index_workspace: activeWorkspaceId,
7011
6995
  favorite: true,
7012
- submitted_at: (/* @__PURE__ */ new Date()).toISOString(),
6996
+ submitted_at: submittedAt,
7013
6997
  created_by: currentUser.id,
7014
6998
  ...componentTypeId && { component_type: componentTypeId },
7015
6999
  ...ownerAttrs
@@ -7019,7 +7003,8 @@ class UserFormService extends BaseApiService {
7019
7003
  ...payloadWithoutImage,
7020
7004
  created_by: currentUser.id,
7021
7005
  form: retForm.offline_id,
7022
- revision: 0
7006
+ revision: 0,
7007
+ submitted_at: submittedAt
7023
7008
  };
7024
7009
  const { store } = this.client;
7025
7010
  store.dispatch(addForm(retForm));
@@ -7082,7 +7067,8 @@ class UserFormService extends BaseApiService {
7082
7067
  ...payloadWithoutImage,
7083
7068
  created_by: currentUserId,
7084
7069
  revision: "Pending",
7085
- form: formId2
7070
+ form: formId2,
7071
+ submitted_at: (/* @__PURE__ */ new Date()).toISOString()
7086
7072
  };
7087
7073
  store.dispatch(addFormRevision(fullRevision));
7088
7074
  const promise = this.enqueueRequest({
@@ -7289,7 +7275,7 @@ class UserFormSubmissionService extends BaseApiService {
7289
7275
  // Note currently the bulkAdd method is specific to form submissions for components
7290
7276
  // TODO: adapt the support bulk adding to any model type
7291
7277
  async bulkAdd(args) {
7292
- const { form_revision, values: argsValues, componentOfflineIds } = args;
7278
+ const { formRevision, values: argsValues, componentOfflineIds } = args;
7293
7279
  const { store } = this.client;
7294
7280
  const offlineSubmissions = [];
7295
7281
  const offlineAttachments = [];
@@ -7301,7 +7287,7 @@ class UserFormSubmissionService extends BaseApiService {
7301
7287
  const createdBy = store.getState().userReducer.currentUser.id;
7302
7288
  for (const component_id of componentOfflineIds) {
7303
7289
  const submission = offline({
7304
- form_revision,
7290
+ form_revision: formRevision,
7305
7291
  values,
7306
7292
  created_by: createdBy,
7307
7293
  submitted_at: submittedAt,
@@ -7347,7 +7333,7 @@ class UserFormSubmissionService extends BaseApiService {
7347
7333
  const promise = this.enqueueRequest({
7348
7334
  description: "Bulk add form submissions",
7349
7335
  method: HttpMethod.POST,
7350
- url: `/forms/revisions/${form_revision}/bulk-respond/`,
7336
+ url: `/forms/revisions/${formRevision}/bulk-respond/`,
7351
7337
  payload: {
7352
7338
  form_data: values,
7353
7339
  submitted_at: submittedAt,
@@ -7414,8 +7400,10 @@ class UserFormSubmissionService extends BaseApiService {
7414
7400
  const { store } = this.client;
7415
7401
  const state = store.getState();
7416
7402
  const submission = state.formSubmissionReducer.formSubmissions[submissionId];
7403
+ const submissionAttachments = selectAttachmentsOfFormSubmission(submissionId)(state);
7417
7404
  store.dispatch(deleteFormSubmission(submissionId));
7418
7405
  store.dispatch(addActiveProjectFormSubmissionsCount(-1));
7406
+ store.dispatch(deleteFormSubmissionAttachments(submissionAttachments.map((x) => x.offline_id)));
7419
7407
  try {
7420
7408
  return await this.enqueueRequest({
7421
7409
  description: "Delete user form submissions",
@@ -7427,6 +7415,7 @@ class UserFormSubmissionService extends BaseApiService {
7427
7415
  } catch (e) {
7428
7416
  store.dispatch(addActiveProjectFormSubmissionsCount(1));
7429
7417
  store.dispatch(addFormSubmission(submission));
7418
+ store.dispatch(addFormSubmissionAttachments(submissionAttachments));
7430
7419
  throw e;
7431
7420
  }
7432
7421
  }