@webiny/api-form-builder-so-ddb 5.34.8 → 5.35.0-beta.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.
@@ -1,34 +1,21 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.createFormStorageOperations = void 0;
9
-
10
8
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
-
12
9
  var _error = _interopRequireDefault(require("@webiny/error"));
13
-
14
10
  var _query = require("@webiny/db-dynamodb/utils/query");
15
-
16
11
  var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
17
-
18
12
  var _batchWrite = require("@webiny/db-dynamodb/utils/batchWrite");
19
-
20
13
  var _filter = require("@webiny/db-dynamodb/utils/filter");
21
-
22
14
  var _sort = require("@webiny/db-dynamodb/utils/sort");
23
-
24
15
  var _utils = require("@webiny/utils");
25
-
26
16
  var _FormDynamoDbFieldPlugin = require("../../plugins/FormDynamoDbFieldPlugin");
27
-
28
17
  var _cursor = require("@webiny/db-dynamodb/utils/cursor");
29
-
30
18
  var _get = require("@webiny/db-dynamodb/utils/get");
31
-
32
19
  const createFormStorageOperations = params => {
33
20
  const {
34
21
  entity,
@@ -36,7 +23,6 @@ const createFormStorageOperations = params => {
36
23
  plugins
37
24
  } = params;
38
25
  const formDynamoDbFields = plugins.byType(_FormDynamoDbFieldPlugin.FormDynamoDbFieldPlugin.type);
39
-
40
26
  const createFormPartitionKey = params => {
41
27
  const {
42
28
  tenant,
@@ -44,15 +30,12 @@ const createFormStorageOperations = params => {
44
30
  } = params;
45
31
  return `T#${tenant}#L#${locale}#FB#F`;
46
32
  };
47
-
48
33
  const createFormLatestPartitionKey = params => {
49
34
  return `${createFormPartitionKey(params)}#L`;
50
35
  };
51
-
52
36
  const createFormLatestPublishedPartitionKey = params => {
53
37
  return `${createFormPartitionKey(params)}#LP`;
54
38
  };
55
-
56
39
  const createFormGSIPartitionKey = params => {
57
40
  const {
58
41
  tenant,
@@ -64,13 +47,11 @@ const createFormStorageOperations = params => {
64
47
  } = (0, _utils.parseIdentifier)(targetId);
65
48
  return `T#${tenant}#L#${locale}#FB#F#${id}`;
66
49
  };
67
-
68
50
  const createRevisionSortKey = ({
69
51
  id
70
52
  }) => {
71
53
  return `${id}`;
72
54
  };
73
-
74
55
  const createFormLatestSortKey = ({
75
56
  id,
76
57
  formId
@@ -78,7 +59,6 @@ const createFormStorageOperations = params => {
78
59
  const value = (0, _utils.parseIdentifier)(id || formId);
79
60
  return value.id;
80
61
  };
81
-
82
62
  const createLatestPublishedSortKey = ({
83
63
  id,
84
64
  formId
@@ -86,51 +66,42 @@ const createFormStorageOperations = params => {
86
66
  const value = (0, _utils.parseIdentifier)(id || formId);
87
67
  return value.id;
88
68
  };
89
-
90
69
  const createGSISortKey = version => {
91
70
  return `${version}`;
92
71
  };
93
-
94
72
  const createFormType = () => {
95
73
  return "fb.form";
96
74
  };
97
-
98
75
  const createFormLatestType = () => {
99
76
  return "fb.form.latest";
100
77
  };
101
-
102
78
  const createFormLatestPublishedType = () => {
103
79
  return "fb.form.latestPublished";
104
80
  };
105
-
106
81
  const createRevisionKeys = form => {
107
82
  return {
108
83
  PK: createFormPartitionKey(form),
109
84
  SK: createRevisionSortKey(form)
110
85
  };
111
86
  };
112
-
113
87
  const createLatestKeys = form => {
114
88
  return {
115
89
  PK: createFormLatestPartitionKey(form),
116
90
  SK: createFormLatestSortKey(form)
117
91
  };
118
92
  };
119
-
120
93
  const createLatestPublishedKeys = form => {
121
94
  return {
122
95
  PK: createFormLatestPublishedPartitionKey(form),
123
96
  SK: createLatestPublishedSortKey(form)
124
97
  };
125
98
  };
126
-
127
99
  const createGSIKeys = form => {
128
100
  return {
129
101
  GSI1_PK: createFormGSIPartitionKey(form),
130
102
  GSI1_SK: createGSISortKey(form.version)
131
103
  };
132
104
  };
133
-
134
105
  const createForm = async params => {
135
106
  const {
136
107
  form
@@ -143,7 +114,6 @@ const createFormStorageOperations = params => {
143
114
  })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, form), latestKeys), {}, {
144
115
  TYPE: createFormLatestType()
145
116
  }))];
146
-
147
117
  try {
148
118
  await (0, _batchWrite.batchWriteAll)({
149
119
  table,
@@ -156,10 +126,8 @@ const createFormStorageOperations = params => {
156
126
  form
157
127
  });
158
128
  }
159
-
160
129
  return form;
161
130
  };
162
-
163
131
  const createFormFrom = async params => {
164
132
  const {
165
133
  form,
@@ -174,7 +142,6 @@ const createFormStorageOperations = params => {
174
142
  })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, form), latestKeys), {}, {
175
143
  TYPE: createFormLatestType()
176
144
  }))];
177
-
178
145
  try {
179
146
  await (0, _batchWrite.batchWriteAll)({
180
147
  table,
@@ -189,10 +156,8 @@ const createFormStorageOperations = params => {
189
156
  latest
190
157
  });
191
158
  }
192
-
193
159
  return form;
194
160
  };
195
-
196
161
  const updateForm = async params => {
197
162
  const {
198
163
  form,
@@ -218,13 +183,11 @@ const createFormStorageOperations = params => {
218
183
  const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, form), revisionKeys), gsiKeys), {}, {
219
184
  TYPE: createFormType()
220
185
  }))];
221
-
222
186
  if (isLatestForm) {
223
187
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, form), latestKeys), {}, {
224
188
  TYPE: createFormLatestType()
225
189
  })));
226
190
  }
227
-
228
191
  try {
229
192
  await (0, _batchWrite.batchWriteAll)({
230
193
  table,
@@ -239,10 +202,8 @@ const createFormStorageOperations = params => {
239
202
  latestForm
240
203
  });
241
204
  }
242
-
243
205
  return form;
244
206
  };
245
-
246
207
  const getForm = async params => {
247
208
  const {
248
209
  where
@@ -254,14 +215,11 @@ const createFormStorageOperations = params => {
254
215
  published,
255
216
  version
256
217
  } = where;
257
-
258
218
  if (latest && published) {
259
219
  throw new _error.default("Cannot have both latest and published params.");
260
220
  }
261
-
262
221
  let partitionKey;
263
222
  let sortKey;
264
-
265
223
  if (latest) {
266
224
  partitionKey = createFormLatestPartitionKey(where);
267
225
  sortKey = createFormLatestSortKey(where);
@@ -284,12 +242,10 @@ const createFormStorageOperations = params => {
284
242
  where
285
243
  });
286
244
  }
287
-
288
245
  const keys = {
289
246
  PK: partitionKey,
290
247
  SK: sortKey
291
248
  };
292
-
293
249
  try {
294
250
  const item = await (0, _get.get)({
295
251
  entity,
@@ -302,7 +258,6 @@ const createFormStorageOperations = params => {
302
258
  });
303
259
  }
304
260
  };
305
-
306
261
  const listForms = async params => {
307
262
  const {
308
263
  sort,
@@ -318,7 +273,6 @@ const createFormStorageOperations = params => {
318
273
  }
319
274
  };
320
275
  let results;
321
-
322
276
  try {
323
277
  results = await (0, _query.queryAll)(queryAllParams);
324
278
  } catch (ex) {
@@ -327,13 +281,11 @@ const createFormStorageOperations = params => {
327
281
  partitionKey: queryAllParams.partitionKey
328
282
  });
329
283
  }
330
-
331
284
  const totalCount = results.length;
332
285
  const where = (0, _objectSpread2.default)({}, initialWhere);
333
286
  /**
334
287
  * We need to remove conditions so we do not filter by them again.
335
288
  */
336
-
337
289
  delete where.tenant;
338
290
  delete where.locale;
339
291
  const filteredItems = (0, _filter.filterItems)({
@@ -355,7 +307,6 @@ const createFormStorageOperations = params => {
355
307
  * Although we do not need a cursor here, we will use it as such to keep it standardized.
356
308
  * Number is simply encoded.
357
309
  */
358
-
359
310
  const cursor = items.length > 0 ? (0, _cursor.encodeCursor)(start + limit) : null;
360
311
  const meta = {
361
312
  hasMoreItems,
@@ -367,7 +318,6 @@ const createFormStorageOperations = params => {
367
318
  meta
368
319
  };
369
320
  };
370
-
371
321
  const listFormRevisions = async params => {
372
322
  const {
373
323
  where: initialWhere,
@@ -392,7 +342,6 @@ const createFormStorageOperations = params => {
392
342
  }
393
343
  };
394
344
  let items = [];
395
-
396
345
  try {
397
346
  items = await (0, _query.queryAll)(queryAllParams);
398
347
  } catch (ex) {
@@ -401,12 +350,10 @@ const createFormStorageOperations = params => {
401
350
  options: queryAllParams.options
402
351
  });
403
352
  }
404
-
405
353
  const where = (0, _objectSpread2.default)({}, initialWhere);
406
354
  /**
407
355
  * We need to remove conditions so we do not filter by them again.
408
356
  */
409
-
410
357
  delete where.id;
411
358
  delete where.formId;
412
359
  delete where.tenant;
@@ -423,7 +370,6 @@ const createFormStorageOperations = params => {
423
370
  fields: formDynamoDbFields
424
371
  });
425
372
  };
426
-
427
373
  const deleteForm = async params => {
428
374
  const {
429
375
  form
@@ -432,7 +378,6 @@ const createFormStorageOperations = params => {
432
378
  /**
433
379
  * This will find all form records.
434
380
  */
435
-
436
381
  const queryAllParams = {
437
382
  entity,
438
383
  partitionKey: createFormPartitionKey(form),
@@ -440,7 +385,6 @@ const createFormStorageOperations = params => {
440
385
  beginsWith: form.formId
441
386
  }
442
387
  };
443
-
444
388
  try {
445
389
  items = await (0, _query.queryAll)(queryAllParams);
446
390
  } catch (ex) {
@@ -449,25 +393,20 @@ const createFormStorageOperations = params => {
449
393
  options: queryAllParams.options
450
394
  });
451
395
  }
452
-
453
396
  let hasLatestPublishedRecord = false;
454
397
  const deleteItems = items.map(item => {
455
398
  if (!hasLatestPublishedRecord && item.published) {
456
399
  hasLatestPublishedRecord = true;
457
400
  }
458
-
459
401
  return entity.deleteBatch({
460
402
  PK: item.PK,
461
403
  SK: item.SK
462
404
  });
463
405
  });
464
-
465
406
  if (hasLatestPublishedRecord) {
466
407
  deleteItems.push(entity.deleteBatch(createLatestPublishedKeys(items[0])));
467
408
  }
468
-
469
409
  deleteItems.push(entity.deleteBatch(createLatestKeys(items[0])));
470
-
471
410
  try {
472
411
  await (0, _batchWrite.batchWriteAll)({
473
412
  table,
@@ -476,7 +415,6 @@ const createFormStorageOperations = params => {
476
415
  } catch (ex) {
477
416
  throw new _error.default(ex.message || "Could not delete form and it's submissions.", ex.code || "DELETE_FORM_AND_SUBMISSIONS_ERROR");
478
417
  }
479
-
480
418
  return form;
481
419
  };
482
420
  /**
@@ -485,8 +423,6 @@ const createFormStorageOperations = params => {
485
423
  * - get previously published revision and update the record if it exists or delete if it does not
486
424
  * - update latest record if current one is the latest
487
425
  */
488
-
489
-
490
426
  const deleteFormRevision = async params => {
491
427
  const {
492
428
  form,
@@ -500,7 +436,6 @@ const createFormStorageOperations = params => {
500
436
  const isLatest = latestForm ? latestForm.id === form.id : false;
501
437
  const isLatestPublished = latestPublishedForm ? latestPublishedForm.id === form.id : false;
502
438
  const items = [entity.deleteBatch(revisionKeys)];
503
-
504
439
  if (isLatest || isLatestPublished) {
505
440
  /**
506
441
  * Sort out the latest published record.
@@ -509,7 +444,6 @@ const createFormStorageOperations = params => {
509
444
  const previouslyPublishedForm = revisions.filter(f => !!f.publishedOn && f.version !== form.version).sort((a, b) => {
510
445
  return new Date(b.publishedOn).getTime() - new Date(a.publishedOn).getTime();
511
446
  }).shift();
512
-
513
447
  if (previouslyPublishedForm) {
514
448
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, previouslyPublishedForm), createLatestPublishedKeys(previouslyPublishedForm)), {}, {
515
449
  GSI1_PK: null,
@@ -523,8 +457,6 @@ const createFormStorageOperations = params => {
523
457
  /**
524
458
  * Sort out the latest record.
525
459
  */
526
-
527
-
528
460
  if (isLatest) {
529
461
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, previous), latestKeys), {}, {
530
462
  GSI1_PK: null,
@@ -536,8 +468,6 @@ const createFormStorageOperations = params => {
536
468
  /**
537
469
  * Now save the batch data.
538
470
  */
539
-
540
-
541
471
  try {
542
472
  await (0, _batchWrite.batchWriteAll)({
543
473
  table,
@@ -553,6 +483,7 @@ const createFormStorageOperations = params => {
553
483
  });
554
484
  }
555
485
  };
486
+
556
487
  /**
557
488
  * We need to save form in:
558
489
  * - regular form record
@@ -560,8 +491,6 @@ const createFormStorageOperations = params => {
560
491
  * - latest form record - if form is latest one
561
492
  * - elasticsearch latest form record
562
493
  */
563
-
564
-
565
494
  const publishForm = async params => {
566
495
  const {
567
496
  form,
@@ -591,7 +520,6 @@ const createFormStorageOperations = params => {
591
520
  /**
592
521
  * Update revision and latest published records
593
522
  */
594
-
595
523
  const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, form), revisionKeys), gsiKeys), {}, {
596
524
  TYPE: createFormType()
597
525
  })), entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, form), latestPublishedKeys), {}, {
@@ -600,13 +528,11 @@ const createFormStorageOperations = params => {
600
528
  /**
601
529
  * Update the latest form as well
602
530
  */
603
-
604
531
  if (isLatestForm) {
605
532
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, form), latestKeys), {}, {
606
533
  TYPE: createFormLatestType()
607
534
  })));
608
535
  }
609
-
610
536
  try {
611
537
  await (0, _batchWrite.batchWriteAll)({
612
538
  table,
@@ -622,17 +548,15 @@ const createFormStorageOperations = params => {
622
548
  latestPublishedKeys
623
549
  });
624
550
  }
625
-
626
551
  return form;
627
552
  };
553
+
628
554
  /**
629
555
  * We need to:
630
556
  * - update form revision record
631
557
  * - if latest published (LP) is current form, find the previously published record and update LP if there is some previously published, delete otherwise
632
558
  * - if is latest update the Elasticsearch record
633
559
  */
634
-
635
-
636
560
  const unpublishForm = async params => {
637
561
  const {
638
562
  form,
@@ -668,7 +592,6 @@ const createFormStorageOperations = params => {
668
592
  const items = [entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, form), revisionKeys), gsiKeys), {}, {
669
593
  TYPE: createFormType()
670
594
  }))];
671
-
672
595
  if (isLatest) {
673
596
  entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, form), latestKeys), {}, {
674
597
  TYPE: createFormLatestType()
@@ -678,8 +601,6 @@ const createFormStorageOperations = params => {
678
601
  * In case previously published revision exists, replace current one with that one.
679
602
  * And if it does not, delete the record.
680
603
  */
681
-
682
-
683
604
  if (isLatestPublished) {
684
605
  const revisions = await listFormRevisions({
685
606
  where: {
@@ -692,7 +613,6 @@ const createFormStorageOperations = params => {
692
613
  sort: ["savedOn_DESC"]
693
614
  });
694
615
  const previouslyPublishedRevision = revisions.shift();
695
-
696
616
  if (previouslyPublishedRevision) {
697
617
  items.push(entity.putBatch((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, previouslyPublishedRevision), latestPublishedKeys), {}, {
698
618
  TYPE: createFormLatestPublishedType()
@@ -701,7 +621,6 @@ const createFormStorageOperations = params => {
701
621
  items.push(entity.deleteBatch(latestPublishedKeys));
702
622
  }
703
623
  }
704
-
705
624
  try {
706
625
  await (0, _batchWrite.batchWriteAll)({
707
626
  table,
@@ -719,7 +638,6 @@ const createFormStorageOperations = params => {
719
638
  });
720
639
  }
721
640
  };
722
-
723
641
  return {
724
642
  createForm,
725
643
  createFormFrom,
@@ -734,5 +652,4 @@ const createFormStorageOperations = params => {
734
652
  createFormPartitionKey
735
653
  };
736
654
  };
737
-
738
655
  exports.createFormStorageOperations = createFormStorageOperations;