@izara_project/izara-core-library-asynchronous-flow 1.0.25 → 1.0.28
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 +1 -1
- package/src/awaitingMultipleSteps.js +101 -105
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.
|
|
6
|
+
"version": "1.0.28",
|
|
7
7
|
"description": "Shared asynchronous flow logic",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "index.js",
|
|
@@ -300,17 +300,7 @@ export async function findAwaitingMultipleStepByPending(
|
|
|
300
300
|
return listAwaitingSteps.Items;
|
|
301
301
|
}
|
|
302
302
|
|
|
303
|
-
|
|
304
|
-
* Same as checkAllAwaitingStepsFinished but writes errors/additionalAttributes to the *_ByPending row.
|
|
305
|
-
* @async
|
|
306
|
-
* @param {IzContext} _izContext
|
|
307
|
-
* @param {string} pendingStepId
|
|
308
|
-
* @param {string|null} [currentAwaitingStepId=null]
|
|
309
|
-
* @param {any[]} [errorsFound=[]]
|
|
310
|
-
* @param {Attrs} [additionalAttributes={}]
|
|
311
|
-
* @returns {Promise<boolean>}
|
|
312
|
-
*/
|
|
313
|
-
export async function checkAllAwaitingStepsFinished(
|
|
303
|
+
export async function checkAllAwaitingStepsFinishedShared(
|
|
314
304
|
_izContext,
|
|
315
305
|
pendingStepId,
|
|
316
306
|
currentAwaitingStepId = null,
|
|
@@ -338,35 +328,114 @@ export async function checkAllAwaitingStepsFinished(
|
|
|
338
328
|
}
|
|
339
329
|
);
|
|
340
330
|
}
|
|
341
|
-
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Same as checkAllAwaitingStepsFinished but writes errors/additionalAttributes to the *_ByPending row.
|
|
336
|
+
* @async
|
|
337
|
+
* @param {IzContext} _izContext
|
|
338
|
+
* @param {string} pendingStepId
|
|
339
|
+
* @param {string|null} [currentAwaitingStepId=null]
|
|
340
|
+
* @param {any[]} [errorsFound=[]]
|
|
341
|
+
* @param {Attrs} [additionalAttributes={}]
|
|
342
|
+
* @returns {Promise<boolean>}
|
|
343
|
+
*/
|
|
344
|
+
export async function checkAllAwaitingStepsFinished(
|
|
345
|
+
_izContext,
|
|
346
|
+
pendingStepId,
|
|
347
|
+
currentAwaitingStepId = null,
|
|
348
|
+
errorsFound = [],
|
|
349
|
+
additionalAttributes = {}
|
|
350
|
+
) {
|
|
351
|
+
await checkAllAwaitingStepsFinishedShared(
|
|
352
|
+
_izContext,
|
|
353
|
+
pendingStepId,
|
|
354
|
+
currentAwaitingStepId,
|
|
355
|
+
errorsFound,
|
|
356
|
+
additionalAttributes
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
const items = await dynamodbSharedLib.query(
|
|
342
360
|
_izContext,
|
|
343
361
|
dynamodbSharedLib.tableName(_izContext, 'AwaitingMultipleStepByPending'),
|
|
344
|
-
{
|
|
345
|
-
|
|
346
|
-
},
|
|
347
|
-
{
|
|
348
|
-
limit: 50 // arbitrary limit so not pull too many results, need >2 because some others might be marked as "complete" = true
|
|
349
|
-
}
|
|
362
|
+
{ pendingStepId },
|
|
363
|
+
{ limit: 50 }
|
|
350
364
|
);
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
365
|
+
|
|
366
|
+
for (const item of items.Items) {
|
|
367
|
+
if (item.awaitingStepId === currentAwaitingStepId) continue;
|
|
368
|
+
if (!item.complete) return false;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Checks if all awaiting steps for a pending step are complete after marking the current one as complete.
|
|
377
|
+
* If all steps are finished, it returns `true` and an object containing the `additionalAttributes` from each of the completed steps, indexed by their `awaitingStepId`.
|
|
378
|
+
* If any other step is not yet complete, it returns `[false, null]`.
|
|
379
|
+
* @async
|
|
380
|
+
* @param {IzContext} _izContext
|
|
381
|
+
* @param {string} pendingStepId
|
|
382
|
+
* @param {string|null} [currentAwaitingStepId=null] - The step that has just finished.
|
|
383
|
+
* @param {any[]} [errorsFound=[]]
|
|
384
|
+
* @param {Attrs} [additionalAttributes={}]
|
|
385
|
+
* @returns {Promise<[boolean, Record<string, any>|null]>} A tuple indicating completion status and the collected attributes, or null if not all steps are complete.
|
|
386
|
+
*/
|
|
387
|
+
export async function checkAllAwaitingStepsFinishedWithReturnParams(
|
|
388
|
+
_izContext,
|
|
389
|
+
pendingStepId,
|
|
390
|
+
currentAwaitingStepId = null,
|
|
391
|
+
errorsFound = [],
|
|
392
|
+
additionalAttributes = {}
|
|
393
|
+
) {
|
|
394
|
+
_izContext.logger.debug('Lib:checkAllAwaitingStepsFinishedWithReturnParams', {
|
|
395
|
+
pendingStepId,
|
|
396
|
+
currentAwaitingStepId,
|
|
397
|
+
errorsFound,
|
|
398
|
+
additionalAttributes
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
await checkAllAwaitingStepsFinishedShared(
|
|
402
|
+
_izContext,
|
|
403
|
+
pendingStepId,
|
|
404
|
+
currentAwaitingStepId,
|
|
405
|
+
errorsFound,
|
|
406
|
+
additionalAttributes
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
const awaitingStepItems = await dynamodbSharedLib.query(
|
|
410
|
+
_izContext,
|
|
411
|
+
dynamodbSharedLib.tableName(_izContext, 'AwaitingMultipleStepByPending'),
|
|
412
|
+
{ pendingStepId },
|
|
413
|
+
{ limit: 50 }
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
_izContext.logger.debug('awaitingStepItems:::', awaitingStepItems);
|
|
417
|
+
|
|
418
|
+
const collectedAttributes = {};
|
|
419
|
+
const collectedErrors = [];
|
|
420
|
+
|
|
421
|
+
for (const item of awaitingStepItems.Items) {
|
|
422
|
+
if (item.awaitingStepId !== currentAwaitingStepId && !item.complete) {
|
|
423
|
+
return [false, null, collectedErrors];
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (item.errorsFound?.length > 0) {
|
|
427
|
+
collectedErrors.push(...item.errorsFound);
|
|
360
428
|
}
|
|
361
|
-
|
|
362
|
-
if (
|
|
363
|
-
|
|
429
|
+
|
|
430
|
+
if (item.additionalAttributes) {
|
|
431
|
+
collectedAttributes[item.awaitingStepId] = item.additionalAttributes;
|
|
364
432
|
}
|
|
365
433
|
}
|
|
366
434
|
|
|
367
|
-
return true
|
|
435
|
+
return [true, collectedAttributes, collectedErrors];
|
|
368
436
|
}
|
|
369
437
|
|
|
438
|
+
|
|
370
439
|
/**
|
|
371
440
|
* Remove all awaiting records for a given pendingStepId from both tables.
|
|
372
441
|
* @async
|
|
@@ -487,77 +556,4 @@ export async function removeAwaitingMultipleStep(
|
|
|
487
556
|
);
|
|
488
557
|
}
|
|
489
558
|
await Promise.all(promiseArray);
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
/**
|
|
493
|
-
* Checks if all awaiting steps for a pending step are complete after marking the current one as complete.
|
|
494
|
-
* If all steps are finished, it returns `true` and an object containing the `additionalAttributes` from each of the completed steps, keyed by their `awaitingStepId`.
|
|
495
|
-
* If any other step is not yet complete, it returns `[false, null]`.
|
|
496
|
-
* @async
|
|
497
|
-
* @param {IzContext} _izContext
|
|
498
|
-
* @param {string} pendingStepId
|
|
499
|
-
* @param {string|null} [currentAwaitingStepId=null] - The step that has just finished.
|
|
500
|
-
* @param {any[]} [errorsFound=[]]
|
|
501
|
-
* @param {Attrs} [additionalAttributes={}]
|
|
502
|
-
* @returns {Promise<[boolean, Record<string, any>|null]>} A tuple indicating completion status and the collected attributes, or null if not all steps are complete.
|
|
503
|
-
*/
|
|
504
|
-
export async function syncAndCheckStepsCompletion(
|
|
505
|
-
_izContext,
|
|
506
|
-
pendingStepId,
|
|
507
|
-
currentAwaitingStepId = null,
|
|
508
|
-
errorsFound = [],
|
|
509
|
-
additionalAttributes = {}
|
|
510
|
-
) {
|
|
511
|
-
_izContext.logger.debug(
|
|
512
|
-
'Lib:checkAllAwaitingMultipleStepsFinishedWithErrorAndReturnAdditionalAttributes',
|
|
513
|
-
{
|
|
514
|
-
pendingStepId,
|
|
515
|
-
currentAwaitingStepId,
|
|
516
|
-
errorsFound,
|
|
517
|
-
additionalAttributes
|
|
518
|
-
}
|
|
519
|
-
);
|
|
520
|
-
|
|
521
|
-
if (currentAwaitingStepId) {
|
|
522
|
-
await dynamodbSharedLib.updateItem(
|
|
523
|
-
_izContext,
|
|
524
|
-
dynamodbSharedLib.tableName(_izContext, 'AwaitingMultipleStepByPending'),
|
|
525
|
-
{ pendingStepId, awaitingStepId: currentAwaitingStepId },
|
|
526
|
-
{
|
|
527
|
-
complete: true,
|
|
528
|
-
errorsFound,
|
|
529
|
-
additionalAttributes
|
|
530
|
-
}
|
|
531
|
-
);
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
const awaitingStepItems = await dynamodbSharedLib.query(
|
|
535
|
-
_izContext,
|
|
536
|
-
dynamodbSharedLib.tableName(_izContext, 'AwaitingMultipleStepByPending'),
|
|
537
|
-
{ pendingStepId },
|
|
538
|
-
{
|
|
539
|
-
limit: 50
|
|
540
|
-
}
|
|
541
|
-
);
|
|
542
|
-
|
|
543
|
-
_izContext.logger.debug('awaitingStepItems:::', awaitingStepItems);
|
|
544
|
-
|
|
545
|
-
const collectedAttributes = {};
|
|
546
|
-
const collectedErrors = [];
|
|
547
|
-
|
|
548
|
-
for (const item of awaitingStepItems.Items) {
|
|
549
|
-
if (item.awaitingStepId !== currentAwaitingStepId && !item.complete) {
|
|
550
|
-
return [false, null, []];
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
if (item.errorsFound?.length > 0) {
|
|
554
|
-
collectedErrors.push(...item.errorsFound);
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
if (item.additionalAttributes) {
|
|
558
|
-
collectedAttributes[item.awaitingStepId] = item.additionalAttributes;
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
return [true, collectedAttributes, collectedErrors];
|
|
563
|
-
}
|
|
559
|
+
}
|