@izara_project/izara-core-library-asynchronous-flow 1.0.39 → 1.0.40

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
@@ -3,7 +3,7 @@
3
3
  "author": "Sven Mason <thebarbariansven@gmail.com>",
4
4
  "license": "AGPL-3.0-or-later",
5
5
  "homepage": "https://bitbucket.org/izara-core-libraries/izara-core-library-asynchronous-flow#readme",
6
- "version": "1.0.39",
6
+ "version": "1.0.40",
7
7
  "description": "Shared asynchronous flow logic",
8
8
  "type": "module",
9
9
  "main": "index.js",
@@ -28,13 +28,20 @@ import { sqs } from '@izara_project/izara-core-library-external-request';
28
28
 
29
29
  import { identifierUuid } from '@izara_project/izara-shared-core';
30
30
 
31
+ function createAwaitingStepId(partitionKey, prefix = '') {
32
+ return prefix + partitionKey;
33
+ }
34
+ function createPendingStepId(identifierId, prefix = '') {
35
+ return prefix + identifierId;
36
+ }
37
+
31
38
  /**
32
39
  * Create a field name for storing the unique-request id, with optional prefix.
33
40
  * @param {string} prefix
34
41
  * @param {string} [uniqueRequestIdFieldName='UniqueRequestId']
35
42
  * @returns {string}
36
43
  */
37
- export function createFieldNameUniqueRequestId(
44
+ function createFieldNameUniqueRequestId(
38
45
  prefix,
39
46
  uniqueRequestIdFieldName = 'UniqueRequestId'
40
47
  ) {
@@ -48,11 +55,7 @@ export function createFieldNameUniqueRequestId(
48
55
  * @param {string} [prefix=""]
49
56
  * @returns {string}
50
57
  */
51
- export function createConcatenatedAwaitingStepId(
52
- partitionKey,
53
- sortId,
54
- prefix = ''
55
- ) {
58
+ function createConcatenatedAwaitingStepId(partitionKey, sortId, prefix = '') {
56
59
  return prefix + partitionKey + '_' + sortId;
57
60
  }
58
61
 
@@ -62,7 +65,7 @@ export function createConcatenatedAwaitingStepId(
62
65
  * @param {string} childId
63
66
  * @returns {string}
64
67
  */
65
- export function createConcatenatedPendingStepId(parentId, childId) {
68
+ function createConcatenatedPendingStepId(parentId, childId) {
66
69
  return parentId + '_' + childId;
67
70
  }
68
71
 
@@ -72,7 +75,7 @@ export function createConcatenatedPendingStepId(parentId, childId) {
72
75
  * @param {string} [prefix=''] - The prefix to prepend to the parent Ids.
73
76
  * @returns {string} The parent Flow Id created.
74
77
  */
75
- export function createParentFlowId(prefix = '') {
78
+ function createParentFlowId(prefix = '') {
76
79
  return (prefix ? `${prefix}_` : '') + identifierUuid();
77
80
  }
78
81
 
@@ -84,7 +87,7 @@ export function createParentFlowId(prefix = '') {
84
87
  * @returns {string}
85
88
  * @throws {NoRetryError} If the stripped value equals the prefix (invalid)
86
89
  */
87
- export function explodePendingStepId(pendingStepId, prefix = '') {
90
+ function explodePendingStepId(pendingStepId, prefix = '') {
88
91
  if (!pendingStepId) throw new NoRetryError('pendingStepId is required');
89
92
  if (!prefix) return pendingStepId;
90
93
 
@@ -129,7 +132,7 @@ export function explodePendingStepId(pendingStepId, prefix = '') {
129
132
  * @returns {Promise<[UniqueRequestStatus, Attrs]>}
130
133
  * @throws {NoRetryError} If unable to set uniqueRequestId after 2 tries
131
134
  */
132
- export async function checkUniqueRequestProcessing(
135
+ async function checkUniqueRequestProcessing(
133
136
  _izContext,
134
137
  tableName,
135
138
  primaryKey,
@@ -232,7 +235,7 @@ export async function checkUniqueRequestProcessing(
232
235
  * @param {string} prefix - Field prefix used in cache fields
233
236
  * @returns {Promise<[boolean, string|undefined]>}
234
237
  */
235
- export async function checkTimeCacheComplete(
238
+ async function checkTimeCacheComplete(
236
239
  _izContext,
237
240
  fullMainTableName,
238
241
  keyValues,
@@ -277,7 +280,7 @@ export async function checkTimeCacheComplete(
277
280
  * @returns {Promise<[boolean, any, string|undefined]>}
278
281
  * @throws {NoRetryError} If record cannot be fetched
279
282
  */
280
- export async function checkAndGetTimeCacheComplete(
283
+ async function checkAndGetTimeCacheComplete(
281
284
  _izContext,
282
285
  fullMainTableName,
283
286
  keyValues,
@@ -351,7 +354,7 @@ export async function checkAndGetTimeCacheComplete(
351
354
  * @param {string} uniqueRequestIdCompleteFieldName
352
355
  * @returns {Promise<boolean>}
353
356
  */
354
- export async function checkCacheUniqueRequestId(
357
+ async function checkCacheUniqueRequestId(
355
358
  _izContext,
356
359
  fullMainTableName,
357
360
  keyValues,
@@ -381,7 +384,7 @@ export async function checkCacheUniqueRequestId(
381
384
  * @returns {Attrs|null}
382
385
  * @throws {NoRetryError} On invalid field names or malformed startKey
383
386
  */
384
- export function validateStartKeyParam(
387
+ function validateStartKeyParam(
385
388
  _izContext,
386
389
  startKey,
387
390
  partitionKeyFieldName,
@@ -431,7 +434,7 @@ export function validateStartKeyParam(
431
434
  * @returns {Promise<void>}
432
435
  * @throws {NoRetryError} If `numberInvocation` exceeds internal safety limit
433
436
  */
434
- export async function validateMultipleInvocations(
437
+ async function validateMultipleInvocations(
435
438
  _izContext,
436
439
  messageProperty,
437
440
  passOnStartKey = {},
@@ -477,3 +480,19 @@ export async function validateMultipleInvocations(
477
480
  throw err;
478
481
  }
479
482
  }
483
+
484
+ export {
485
+ createAwaitingStepId,
486
+ createPendingStepId,
487
+ createFieldNameUniqueRequestId,
488
+ createConcatenatedAwaitingStepId,
489
+ createConcatenatedPendingStepId,
490
+ createParentFlowId,
491
+ explodePendingStepId,
492
+ checkUniqueRequestProcessing,
493
+ checkTimeCacheComplete,
494
+ checkAndGetTimeCacheComplete,
495
+ checkCacheUniqueRequestId,
496
+ validateStartKeyParam,
497
+ validateMultipleInvocations
498
+ };
@@ -92,6 +92,22 @@ async function _queryAwaitingStepsByPendingStepId(_izContext, pendingStepId) {
92
92
  // One awaitingStepId can have multiple pendingStepIds awaiting it.
93
93
  // One pendingStepId can have multiple awaitingStepIds awaiting it.
94
94
 
95
+ /**
96
+ * Create awaiting multiple step records to block a flow until multiple steps complete.
97
+ * @async
98
+ * @param {IzContext} _izContext - The Izara context.
99
+ * @param {string} pendingStepId - The ID of the pending step that is waiting.
100
+ * @param {Array<object>} [records=[]] - Array of step items containing awaitingStepId and optional attributes.
101
+ * @param {object} [options={}] - Optional configurations.
102
+ * @param {string} [options.prefix=''] - Prefix to prepend to pendingStepId.
103
+ * @param {object} [options.additionalAttributes={}] - Additional attributes to associate with each pending step record.
104
+ * @param {object} [options.publishConfig=null] - SQS/SNS publish configuration if starting external tasks.
105
+ * @param {Array<object>} options.publishConfig.messages - Messages to publish.
106
+ * @param {object} options.publishConfig.flowType - Flow schema/service metadata.
107
+ * @param {string} options.publishConfig.flowType.flowTag - Tag identifying the flow.
108
+ * @param {string} options.publishConfig.flowType.serviceTag - Tag identifying the service.
109
+ * @returns {Promise<string|undefined>} The awaitingStepId of the first mapped record.
110
+ */
95
111
  export async function createAwaitingMultipleSteps(
96
112
  _izContext,
97
113
  pendingStepId,
@@ -158,10 +174,19 @@ export async function createAwaitingMultipleSteps(
158
174
 
159
175
  await _putAwaitingMultipleStepRecords(
160
176
  _izContext,
161
- finalRecords.map(({ awaitingStepId, additionalAttributes: itemAttrs }) => ({
162
- awaitingStepId,
163
- additionalAttributes: itemAttrs
164
- })),
177
+ finalRecords.map(({ awaitingStepId, additionalAttributes: itemAttrs }) => {
178
+ const hasItemAttrs = itemAttrs && Object.keys(itemAttrs).length > 0;
179
+ const hasOptAttrs =
180
+ additionalAttributes && Object.keys(additionalAttributes).length > 0;
181
+
182
+ return {
183
+ awaitingStepId,
184
+ additionalAttributes:
185
+ hasItemAttrs || hasOptAttrs
186
+ ? { ...additionalAttributes, ...itemAttrs }
187
+ : undefined
188
+ };
189
+ }),
165
190
  finalPendingStepId,
166
191
  {
167
192
  complete: false,
@@ -190,6 +215,15 @@ export async function createAwaitingMultipleSteps(
190
215
  return finalRecords[0]?.awaitingStepId;
191
216
  }
192
217
 
218
+ /**
219
+ * Update an awaiting multiple step record in the Steps table.
220
+ * @async
221
+ * @param {IzContext} _izContext - The Izara context.
222
+ * @param {string} awaitingStepId - The ID of the awaiting step.
223
+ * @param {string} pendingStepId - The ID of the pending step.
224
+ * @param {object} data - Attributes to update.
225
+ * @returns {Promise<object>} The updated data with awaitingStepId merged.
226
+ */
193
227
  export async function updateAwaitingMultipleStep(
194
228
  _izContext,
195
229
  awaitingStepId,
@@ -216,6 +250,13 @@ export async function updateAwaitingMultipleStep(
216
250
  return { ...data, awaitingStepId };
217
251
  }
218
252
 
253
+ /**
254
+ * Query and return the first pending step item that matches the given awaitingStepId.
255
+ * @async
256
+ * @param {IzContext} _izContext - The Izara context.
257
+ * @param {string} awaitingStepId - The ID of the awaiting step.
258
+ * @returns {Promise<object|undefined>} The found item or undefined.
259
+ */
219
260
  export async function findPendingStepAwaitingMultipleSteps(
220
261
  _izContext,
221
262
  awaitingStepId
@@ -232,6 +273,13 @@ export async function findPendingStepAwaitingMultipleSteps(
232
273
  return items[0];
233
274
  }
234
275
 
276
+ /**
277
+ * Query and return all pending step items matching the given awaitingStepId.
278
+ * @async
279
+ * @param {IzContext} _izContext - The Izara context.
280
+ * @param {string} awaitingStepId - The ID of the awaiting step.
281
+ * @returns {Promise<Array<object>>} List of matching items.
282
+ */
235
283
  export async function findPendingStepsAwaitingMultipleSteps(
236
284
  _izContext,
237
285
  awaitingStepId
@@ -239,6 +287,13 @@ export async function findPendingStepsAwaitingMultipleSteps(
239
287
  return _queryPendingStepsByAwaitingStepId(_izContext, awaitingStepId);
240
288
  }
241
289
 
290
+ /**
291
+ * Retrieve the pendingStepId of the first step matching the given awaitingStepId.
292
+ * @async
293
+ * @param {IzContext} _izContext - The Izara context.
294
+ * @param {string} awaitingStepId - The ID of the awaiting step.
295
+ * @returns {Promise<string|undefined>} The pendingStepId of the first item found.
296
+ */
242
297
  export async function findPendingStepIdAwaitingMultipleSteps(
243
298
  _izContext,
244
299
  awaitingStepId
@@ -250,6 +305,13 @@ export async function findPendingStepIdAwaitingMultipleSteps(
250
305
  return items[0]?.pendingStepId;
251
306
  }
252
307
 
308
+ /**
309
+ * Query and return all awaiting steps matching the given pendingStepId from byPending index table.
310
+ * @async
311
+ * @param {IzContext} _izContext - The Izara context.
312
+ * @param {string} pendingStepId - The ID of the pending step.
313
+ * @returns {Promise<Array<object>>} List of matching items.
314
+ */
253
315
  export async function findAwaitingMultipleStepByPending(
254
316
  _izContext,
255
317
  pendingStepId
@@ -257,13 +319,26 @@ export async function findAwaitingMultipleStepByPending(
257
319
  return _queryAwaitingStepsByPendingStepId(_izContext, pendingStepId);
258
320
  }
259
321
 
322
+ /**
323
+ * Check if all awaiting steps for a pendingStepId are completed. If a current awaitingStepId
324
+ * is provided, it updates its status to complete and returns values/errors.
325
+ * @async
326
+ * @param {IzContext} _izContext - The Izara context.
327
+ * @param {string} pendingStepId - The ID of the pending step.
328
+ * @param {string|null} [currentAwaitingStepId=null] - The ID of the current awaiting step that completed.
329
+ * @param {Array<any>} [errorsFound=[]] - Errors collected from the current step.
330
+ * @param {object} [returnValues={}] - Values returned from the current step.
331
+ * @param {object} [settings={ checkIsAllError: false }] - Execution settings.
332
+ * @param {boolean} [settings.checkIsAllError=false] - Whether to verify if all steps failed.
333
+ * @returns {Promise<object>} Status object containing isComplete, collectedAttributes, collectedErrors, returnValues, additionalAttributes, etc.
334
+ */
260
335
  export async function checkAllAwaitingStepsFinishedWithReturnParams(
261
336
  _izContext,
262
337
  pendingStepId,
263
338
  currentAwaitingStepId = null,
264
339
  errorsFound = [],
265
340
  returnValues = {},
266
- settings = { checkIsAllError: true }
341
+ settings = { checkIsAllError: false }
267
342
  ) {
268
343
  _izContext.logger.debug(
269
344
  '[Lib:checkAllAwaitingStepsFinishedWithReturnParams] Input parameters',
@@ -346,6 +421,13 @@ export async function checkAllAwaitingStepsFinishedWithReturnParams(
346
421
  };
347
422
  }
348
423
 
424
+ /**
425
+ * Clean up/delete all awaiting multiple steps records associated with the pendingStepId from both tables.
426
+ * @async
427
+ * @param {IzContext} _izContext - The Izara context.
428
+ * @param {string} pendingStepId - The ID of the pending step.
429
+ * @returns {Promise<boolean>} True if operations completed successfully.
430
+ */
349
431
  export async function clearAllAwaitingSteps(_izContext, pendingStepId) {
350
432
  _izContext.logger.debug('[Lib:clearAllAwaitingSteps] Input parameters', {
351
433
  pendingStepId
@@ -382,6 +464,15 @@ export async function clearAllAwaitingSteps(_izContext, pendingStepId) {
382
464
  return true;
383
465
  }
384
466
 
467
+ /**
468
+ * Delete a specific awaiting step from Steps table (and optionally from byPending table if no errors found).
469
+ * @async
470
+ * @param {IzContext} _izContext - The Izara context.
471
+ * @param {string} awaitingStepId - The ID of the awaiting step.
472
+ * @param {string} pendingStepId - The ID of the pending step.
473
+ * @param {Array<any>} [errorsFound=[]] - List of errors. If empty, the record is removed from byPending too.
474
+ * @returns {Promise<void>}
475
+ */
385
476
  export async function removeAwaitingMultipleStep(
386
477
  _izContext,
387
478
  awaitingStepId,
@@ -47,6 +47,16 @@ async function _findSinglePendingStep(_izContext, awaitingStepId) {
47
47
  // AwaitingStep flow awaiting one external flow, will continue its flow once one external flow completes.
48
48
  // One awaitingStepId can have multiple pendingStepIds awaiting it.
49
49
 
50
+ /**
51
+ * Create a single awaiting step record to block a flow until a specific external flow step completes.
52
+ * @async
53
+ * @param {IzContext} _izContext - The Izara context.
54
+ * @param {string} awaitingStepId - The ID of the awaiting step.
55
+ * @param {string} pendingStepId - The ID of the pending step that is waiting.
56
+ * @param {object} [additionalAttributes={}] - Additional attributes to save with the record.
57
+ * @param {object} [callingFlowConfig={}] - Calling flow configuration.
58
+ * @returns {Promise<void>}
59
+ */
50
60
  export async function createAwaitingStep(
51
61
  _izContext,
52
62
  awaitingStepId,
@@ -80,10 +90,27 @@ export async function createAwaitingStep(
80
90
  );
81
91
  }
82
92
 
93
+ /**
94
+ * Query and return the single pending step item that matches the given awaitingStepId.
95
+ * @async
96
+ * @param {IzContext} _izContext - The Izara context.
97
+ * @param {string} awaitingStepId - The ID of the awaiting step.
98
+ * @returns {Promise<object>} The found pending step record.
99
+ * @throws {NoRetryError} If awaitingStepId is missing or if not exactly one record is found.
100
+ */
83
101
  export async function findPendingStep(_izContext, awaitingStepId) {
84
102
  return _findSinglePendingStep(_izContext, awaitingStepId);
85
103
  }
86
104
 
105
+ /**
106
+ * Delete a specific awaiting step record from AwaitingStep table.
107
+ * @async
108
+ * @param {IzContext} _izContext - The Izara context.
109
+ * @param {string} awaitingStepId - The ID of the awaiting step.
110
+ * @param {string} pendingStepId - The ID of the pending step.
111
+ * @returns {Promise<void>}
112
+ * @throws {NoRetryError} If awaitingStepId or pendingStepId is missing.
113
+ */
87
114
  export async function removeAwaitingStep(
88
115
  _izContext,
89
116
  awaitingStepId,
@@ -105,6 +132,17 @@ export async function removeAwaitingStep(
105
132
  );
106
133
  }
107
134
 
135
+ /**
136
+ * Delete an awaiting step record from AwaitingStep table, conditional on uniqueRequestId.
137
+ * @async
138
+ * @param {IzContext} _izContext - The Izara context.
139
+ * @param {string} awaitingStepId - The ID of the awaiting step.
140
+ * @param {string} pendingStepId - The ID of the pending step.
141
+ * @param {string} checkUniqueRequestId - The uniqueRequestId to check.
142
+ * @param {string} [prefix=''] - Prefix for uniqueRequestId field name.
143
+ * @returns {Promise<void>}
144
+ * @throws {NoRetryError} If required arguments are missing.
145
+ */
108
146
  export async function removeAwaitingStepWithCheckUniqueRequestId(
109
147
  _izContext,
110
148
  awaitingStepId,