@izara_project/izara-core-library-asynchronous-flow 1.0.5 → 1.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@izara_project/izara-core-library-asynchronous-flow",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Shared asynchronous flow logic",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -28,46 +28,46 @@ const sqsSharedLib = require('@izara_project/izara-core-library-sqs');
28
28
  // External service interactions
29
29
  const { sqs } = require('@izara_project/izara-core-library-external-request');
30
30
 
31
- module.exports.createFieldNameUniqueRequestId = (
31
+ function createFieldNameUniqueRequestId(
32
32
  prefix,
33
33
  uniqueRequestIdFieldName = 'UniqueRequestId'
34
- ) => {
34
+ ) {
35
35
  return prefix + uniqueRequestIdFieldName;
36
36
  }
37
37
 
38
- module.exports.createConcatenatedAwaitingStepId = (
38
+ function createConcatenatedAwaitingStepId(
39
39
  partitionKey,
40
40
  sortId,
41
41
  prefix = ""
42
- ) => {
42
+ ) {
43
43
  return prefix + partitionKey + "_" + sortId
44
44
  }
45
45
 
46
- module.exports.createConcatenatedPendingStepId = (
46
+ function createConcatenatedPendingStepId(
47
47
  parentId,
48
48
  childId
49
- ) => {
49
+ ) {
50
50
  return parentId + "_" + childId
51
51
  }
52
52
 
53
- module.exports.createAwaitingStepId = (
53
+ function createAwaitingStepId(
54
54
  partitionKey,
55
55
  prefix = ""
56
- ) => {
56
+ ) {
57
57
  return prefix + partitionKey
58
58
  }
59
59
 
60
- module.exports.createPendingStepId = (
60
+ function createPendingStepId(
61
61
  identifierId,
62
62
  prefix = ""
63
- ) => {
63
+ ) {
64
64
  return prefix + identifierId
65
65
  }
66
66
 
67
- module.exports.explodePendingStepId = (
67
+ function explodePendingStepId(
68
68
  pendingStepId,
69
69
  prefix = ""
70
- ) => {
70
+ ) {
71
71
  if (prefix === "") {
72
72
  return pendingStepId;
73
73
  } else {
@@ -84,10 +84,10 @@ module.exports.explodePendingStepId = (
84
84
  }
85
85
 
86
86
  // copy from izara-core-library-trigger-cache
87
- module.exports.createFieldNameCacheComplete = (prefix = 'cache') => { // timeStamp
87
+ function createFieldNameCacheComplete(prefix = 'cache') {
88
88
  return prefix + 'CacheComplete';
89
89
  }
90
- module.exports.createFieldNameStatus = (prefix = 'cache') => {
90
+ function createFieldNameStatus(prefix = 'cache') {
91
91
  return prefix + 'Status';
92
92
  }
93
93
 
@@ -98,13 +98,13 @@ module.exports.createFieldNameStatus = (prefix = 'cache') => {
98
98
  // logic can prepend any prefix to awaitingStepId if want to be share one table and differentiate different external step ids
99
99
 
100
100
  //not yet used ?
101
- module.exports.createAwaitingMultipleStepsWithAdditionalAttributes = async (
101
+ async function createAwaitingMultipleStepsWithAdditionalAttributes(
102
102
  _izContext,
103
103
  records, // array of object including properties: awaitingStepId / (optional) additionalAttributes
104
104
  pendingStepId,
105
105
  tableName = "AwaitingMultipleSteps",
106
106
  byPendingTableName = "AwaitingMultipleStepByPending"
107
- ) => {
107
+ ) {
108
108
  _izContext.logger.debug("Lib:createAwaitingMultipleStepsWithAdditionalAttributes", {
109
109
  records: records,
110
110
  pendingStepId: pendingStepId,
@@ -146,13 +146,13 @@ module.exports.createAwaitingMultipleStepsWithAdditionalAttributes = async (
146
146
 
147
147
  }
148
148
 
149
- module.exports.createAwaitingMultipleSteps = async (
149
+ async function createAwaitingMultipleSteps(
150
150
  _izContext,
151
151
  records, // array of awaitingStepIds
152
152
  pendingStepId,
153
153
  tableName = "AwaitingMultipleSteps",
154
154
  byPendingTableName = "AwaitingMultipleStepByPending"
155
- ) => {
155
+ ) {
156
156
  _izContext.logger.debug("Lib:createAwaitingMultipleSteps", {
157
157
  records: records,
158
158
  pendingStepId: pendingStepId,
@@ -201,13 +201,13 @@ module.exports.createAwaitingMultipleSteps = async (
201
201
  // One awaitingStepId can have multiple pendingStepIds that are awaiting it
202
202
  // logic can prepend any prefix to awaitingStepId if want to be share one table and differentiate different external step ids
203
203
 
204
- module.exports.createAwaitingStep = async (
204
+ async function createAwaitingStep(
205
205
  _izContext,
206
206
  awaitingStepId,
207
207
  pendingStepId,
208
208
  tableName = "AwaitingStep",
209
209
  additionalAttributes = {} // save in top level
210
- ) => {
210
+ ) {
211
211
 
212
212
  let attributesWhenCreate = {
213
213
  awaitingStepId: awaitingStepId,
@@ -230,11 +230,11 @@ module.exports.createAwaitingStep = async (
230
230
 
231
231
 
232
232
 
233
- module.exports.findPendingStepIdsAwaitingStep = async (
233
+ async function findPendingStepIdsAwaitingStep(
234
234
  _izContext,
235
235
  awaitingStepId,
236
236
  tableName = "AwaitingStep"
237
- ) => {
237
+ ) {
238
238
 
239
239
  let pendingStepIds = [];
240
240
 
@@ -253,11 +253,11 @@ module.exports.findPendingStepIdsAwaitingStep = async (
253
253
  return pendingStepIds
254
254
  }
255
255
 
256
- module.exports.findPendingStepsAwaitingStep = async (
256
+ async function findPendingStepsAwaitingStep(
257
257
  _izContext,
258
258
  awaitingStepId,
259
259
  tableName = "AwaitingStep"
260
- ) => {
260
+ ) {
261
261
 
262
262
  let listPendingSteps = await dynamodbSharedLib.query(
263
263
  _izContext,
@@ -272,14 +272,14 @@ module.exports.findPendingStepsAwaitingStep = async (
272
272
 
273
273
 
274
274
 
275
- module.exports.checkAllAwaitingStepsFinished = async (
275
+ async function checkAllAwaitingStepsFinished(
276
276
  _izContext,
277
277
  pendingStepId,
278
278
  currentAwaitingStepId = null,
279
279
  tableName = 'AwaitingMultipleSteps',
280
280
  byPendingTableName = "AwaitingMultipleStepByPending"
281
281
  // indexName = ''
282
- ) => {
282
+ ) {
283
283
  _izContext.logger.debug("Lib:checkAllAwaitingStepsFinished", {
284
284
  pendingStepId: pendingStepId,
285
285
  currentAwaitingStepId: currentAwaitingStepId,
@@ -384,12 +384,12 @@ module.exports.checkAllAwaitingStepsFinished = async (
384
384
  // return true;
385
385
  }
386
386
 
387
- module.exports.clearAllAwaitingSteps = async (
387
+ async function clearAllAwaitingSteps(
388
388
  _izContext,
389
389
  pendingStepId,
390
390
  tableName = 'AwaitingMultipleSteps',
391
391
  byPendingTableName = "AwaitingMultipleStepByPending"
392
- ) => {
392
+ ) {
393
393
  _izContext.logger.debug("Lib:clearAllAwaitingSteps", {
394
394
  pendingStepId: pendingStepId,
395
395
  tableName: tableName,
@@ -442,12 +442,12 @@ module.exports.clearAllAwaitingSteps = async (
442
442
  }
443
443
 
444
444
 
445
- module.exports.removeAwaitingStep = async (
445
+ async function removeAwaitingStep(
446
446
  _izContext,
447
447
  awaitingStepId,
448
448
  pendingStepId,
449
449
  tableName = "AwaitingStep"
450
- ) => {
450
+ ) {
451
451
 
452
452
  await dynamodbSharedLib.deleteItem(
453
453
  _izContext,
@@ -459,13 +459,13 @@ module.exports.removeAwaitingStep = async (
459
459
  );
460
460
  }
461
461
 
462
- module.exports.removeAwaitingMultipleStep = async (
462
+ async function removeAwaitingMultipleStep(
463
463
  _izContext,
464
464
  awaitingStepId,
465
465
  pendingStepId,
466
466
  tableName = "AwaitingMultipleSteps",
467
467
  byPendingTableName = "AwaitingMultipleStepByPending"
468
- ) => {
468
+ ) {
469
469
  _izContext.logger.debug("Lib:removeAwaitingMultipleStep", {
470
470
  awaitingStepId: awaitingStepId,
471
471
  pendingStepId: pendingStepId,
@@ -502,14 +502,14 @@ module.exports.removeAwaitingMultipleStep = async (
502
502
  }
503
503
 
504
504
  // only works with AwaitingStep, does not work with AwaitingMultipleSteps, need to make new function that checks conditional pass? If pass delete from byPending table too
505
- module.exports.removeAwaitingStepWithCheckUniqueRequestId = async (
505
+ async function removeAwaitingStepWithCheckUniqueRequestId(
506
506
  _izContext,
507
507
  awaitingStepId,
508
508
  pendingStepId,
509
509
  checkUniqueRequestId,
510
510
  prefix = "",
511
511
  tableName = "AwaitingStep"
512
- ) => {
512
+ ) {
513
513
 
514
514
  // if logicalElements add queryElements.logicalElements
515
515
  // set condition uniqueRequestId match with exists
@@ -561,14 +561,14 @@ module.exports.removeAwaitingStepWithCheckUniqueRequestId = async (
561
561
 
562
562
  * @returns [string] [returnStatus, existingRecord]
563
563
  */
564
- module.exports.checkUniqueRequestProcessing = async (
564
+ async function checkUniqueRequestProcessing(
565
565
  _izContext,
566
566
  tableName,
567
567
  primaryKey,
568
568
  prefix = '',
569
569
  overwriteUniqueRequestId = null,
570
570
  uniqueRequestIdFieldName = "UniqueRequestId" // if set will check/add this fieldname with _izContext.uniqueRequestId value
571
- ) => {
571
+ ) {
572
572
  try {
573
573
 
574
574
  _izContext.logger.debug('current uniqueRequestId:', _izContext.uniqueRequestId)
@@ -666,13 +666,13 @@ module.exports.checkUniqueRequestProcessing = async (
666
666
  };
667
667
 
668
668
  // ----- Helper funtion, common use case in triggerFlow ---------
669
- module.exports.checkTimeCacheComplete = async (
669
+ async function checkTimeCacheComplete(
670
670
  _izContext,
671
671
  fullMainTableName,
672
672
  keyValues,
673
673
  timeCacheComplete,
674
674
  prefix
675
- ) => {
675
+ ) {
676
676
  let [returnValue, newTimeCacheComplete, uniqueRequestId] = await this.checkAndGetTimeCacheComplete(
677
677
  _izContext,
678
678
  fullMainTableName,
@@ -682,13 +682,13 @@ module.exports.checkTimeCacheComplete = async (
682
682
  );
683
683
  return [returnValue, uniqueRequestId];
684
684
  }
685
- module.exports.checkAndGetTimeCacheComplete = async (
685
+ async function checkAndGetTimeCacheComplete(
686
686
  _izContext,
687
687
  fullMainTableName,
688
688
  keyValues,
689
689
  timeCacheComplete,
690
690
  prefix
691
- ) => {
691
+ ) {
692
692
  _izContext.logger.debug('event checkTimeCacheComplete', {
693
693
  fullMainTableName: fullMainTableName,
694
694
  keyValues: keyValues,
@@ -734,13 +734,13 @@ module.exports.checkAndGetTimeCacheComplete = async (
734
734
 
735
735
 
736
736
  // ----- shared both stored cache and triggered cache, check uniqueRequestId changed ---------
737
- module.exports.checkCacheUniqueRequestId = async (
737
+ async function checkCacheUniqueRequestId(
738
738
  _izContext,
739
739
  fullMainTableName,
740
740
  keyValues,
741
741
  checkUniqueRequestId,
742
742
  uniqueRequestIdCompleteFieldName
743
- ) => {
743
+ ) {
744
744
 
745
745
  let cacheObject = await dynamodbSharedLib.getItem(
746
746
  _izContext,
@@ -813,38 +813,41 @@ module.exports.checkCacheUniqueRequestId = async (
813
813
 
814
814
  //====================================== end Multiple Lambda Invocations (Logic pagination of handling results)
815
815
 
816
- module.exports.validateStartKeyParam = (
816
+ function validateStartKeyParam(
817
817
  _izContext,
818
818
  startKey,
819
819
  partitionKeyFieldName,
820
- sortKeyFieldName = null
821
- ) => {
820
+ sortKeyFieldName
821
+ ) {
822
+ const stringNotEmptyRegex = /^[A-Za-z0-9_-]+$/;
823
+
824
+ if (
825
+ !partitionKeyFieldName || !sortKeyFieldName
826
+ || !stringNotEmptyRegex.test(partitionKeyFieldName)
827
+ || !stringNotEmptyRegex.test(sortKeyFieldName)
828
+ ) {
829
+ throw new NoRetryError("validateStartKeyParam: Invalid partitionKeyFieldName or sortKeyFieldName");
830
+ }
822
831
 
823
832
  if (startKey && Object.keys(startKey).length != 0) {
824
- if (
825
- startKey.constructor !== Object ||
826
- !startKey[partitionKeyFieldName]
827
- ) {
828
- startKey = false; // we can type check for false to see if invalid
829
- }
830
- if (sortKeyFieldName !== null && sortKeyFieldName !== undefined && !startKey[sortKeyFieldName]) {
831
- startKey = false;
833
+ if (!startKey[partitionKeyFieldName] || !startKey[sortKeyFieldName]) {
834
+ throw new NoRetryError("validateStartKeyParam: Invalid startKey, missing partitionKeyFieldName or sortKeyFieldName");
832
835
  }
833
836
  } else {
834
- startKey = null; // if empty set to null so lib functions process correctly (not want empty object)
837
+ startKey = null; // if empty set to null so lib functions process correctly
835
838
  }
836
839
 
837
840
  return startKey;
838
841
  }
839
842
 
840
843
 
841
- module.exports.validateMultipleInvocations = async (
844
+ async function validateMultipleInvocations(
842
845
  _izContext,
843
846
  messageProperty,
844
847
  passOnStartKey = {},
845
848
  numberInvocation,
846
849
  queueName
847
- ) => {
850
+ ) {
848
851
  _izContext.logger.debug("Lib:validateMultipleInvocations", {
849
852
  messageProperty: messageProperty,
850
853
  passOnStartKey: passOnStartKey,
@@ -877,3 +880,41 @@ module.exports.validateMultipleInvocations = async (
877
880
  throw (err)
878
881
  }
879
882
  }
883
+
884
+ module.exports = {
885
+ // Helper functions
886
+ createFieldNameUniqueRequestId,
887
+ createConcatenatedAwaitingStepId,
888
+ createConcatenatedPendingStepId,
889
+ createAwaitingStepId,
890
+ createPendingStepId,
891
+ explodePendingStepId,
892
+ createFieldNameCacheComplete,
893
+ createFieldNameStatus,
894
+
895
+ // AwaitingMultipleSteps functions
896
+ createAwaitingMultipleStepsWithAdditionalAttributes,
897
+ createAwaitingMultipleSteps,
898
+ checkAllAwaitingStepsFinished,
899
+ clearAllAwaitingSteps,
900
+ removeAwaitingMultipleStep,
901
+
902
+ // AwaitingStep functions
903
+ createAwaitingStep,
904
+ findPendingStepIdsAwaitingStep,
905
+ findPendingStepsAwaitingStep,
906
+ removeAwaitingStep,
907
+ removeAwaitingStepWithCheckUniqueRequestId,
908
+
909
+ // Unique Request Processing functions
910
+ checkUniqueRequestProcessing,
911
+
912
+ // Cache functions
913
+ checkTimeCacheComplete,
914
+ checkAndGetTimeCacheComplete,
915
+ checkCacheUniqueRequestId,
916
+
917
+ // Validation functions
918
+ validateStartKeyParam,
919
+ validateMultipleInvocations
920
+ };