From b3ec3331c78f3ded8003ebc0940e61bd122b0be1 Mon Sep 17 00:00:00 2001 From: Illia Rudniev Date: Mon, 2 Dec 2024 17:11:33 +0200 Subject: [PATCH] feat: reworked getOrderedSteps & fixed tests (#2868) --- apps/backoffice-v2/CHANGELOG.md | 4 +++ apps/kyb-app/CHANGELOG.md | 3 ++ examples/headless-example/CHANGELOG.md | 3 ++ packages/common/CHANGELOG.md | 1 + .../collection-flow/get-ordered-steps.ts | 35 ++++++++++++------- .../set-collection-flow-status.unit.test.ts | 24 ++++++++----- packages/workflow-core/CHANGELOG.md | 1 + sdks/workflow-browser-sdk/CHANGELOG.md | 2 ++ sdks/workflow-node-sdk/CHANGELOG.md | 1 + services/workflows-service/CHANGELOG.md | 2 ++ 10 files changed, 55 insertions(+), 21 deletions(-) diff --git a/apps/backoffice-v2/CHANGELOG.md b/apps/backoffice-v2/CHANGELOG.md index 0c9355dae0..a8ea4f8f56 100644 --- a/apps/backoffice-v2/CHANGELOG.md +++ b/apps/backoffice-v2/CHANGELOG.md @@ -42,6 +42,10 @@ ### Patch Changes +- Updated dependencies + - @ballerine/common@0.9.54 + - @ballerine/workflow-browser-sdk@0.6.69 + - @ballerine/workflow-node-sdk@0.6.69 - @ballerine/workflow-browser-sdk@0.6.69 - @ballerine/workflow-node-sdk@0.6.69 diff --git a/apps/kyb-app/CHANGELOG.md b/apps/kyb-app/CHANGELOG.md index ed10586e0b..16d272c29d 100644 --- a/apps/kyb-app/CHANGELOG.md +++ b/apps/kyb-app/CHANGELOG.md @@ -27,10 +27,13 @@ ## 0.3.86 +## 0.3.84 + ### Patch Changes - Updated dependencies - @ballerine/common@0.9.54 + - @ballerine/workflow-browser-sdk@0.6.69 - @ballerine/workflow-browser-sdk@0.6.71 ## 0.3.85 diff --git a/examples/headless-example/CHANGELOG.md b/examples/headless-example/CHANGELOG.md index 741ea45344..2e39ac0a3c 100644 --- a/examples/headless-example/CHANGELOG.md +++ b/examples/headless-example/CHANGELOG.md @@ -18,10 +18,13 @@ ## 0.3.70 +## 0.3.68 + ### Patch Changes - Updated dependencies - @ballerine/common@0.9.54 + - @ballerine/workflow-browser-sdk@0.6.69 - @ballerine/workflow-browser-sdk@0.6.71 ## 0.3.69 diff --git a/packages/common/CHANGELOG.md b/packages/common/CHANGELOG.md index e8cdb62ad5..b2947e3f2c 100644 --- a/packages/common/CHANGELOG.md +++ b/packages/common/CHANGELOG.md @@ -10,6 +10,7 @@ ### Patch Changes +- Reworked getOrderedSteps & fixed tests - bump ## 0.9.53 diff --git a/packages/common/src/utils/collection-flow/get-ordered-steps.ts b/packages/common/src/utils/collection-flow/get-ordered-steps.ts index b33c552973..35e0c87fbd 100644 --- a/packages/common/src/utils/collection-flow/get-ordered-steps.ts +++ b/packages/common/src/utils/collection-flow/get-ordered-steps.ts @@ -1,5 +1,3 @@ -import { createMachine, interpret } from 'xstate'; - export interface IGetOrderedStepsParams { // The event to send to the machine to move to the next step eventName?: string; @@ -14,25 +12,36 @@ export const getOrderedSteps = ( ) => { const { eventName = 'NEXT', finalStates = [] } = params; + if (!definition?.states || !definition?.initial) { + throw new Error('Invalid state machine definition'); + } + const steps: string[] = [definition.initial]; + let currentState = definition.initial; + + while (currentState) { + const stateConfig = definition.states[currentState]; - const machine = createMachine({ - initial: definition.initial, - context: {}, - states: definition.states, - }); + if (!stateConfig) { + throw new Error(`Invalid state: ${currentState}`); + } + + // Check if state has transition for the event + const transition = stateConfig?.on?.[eventName]; - const service = interpret(machine).start(); + if (!transition) { + break; + } - while (service.getSnapshot().can({ type: eventName })) { - service.send({ type: eventName }); - const stateValue = service.getSnapshot().value as string; + // Get next state from transition + const nextState = typeof transition === 'string' ? transition : transition.target; - if (finalStates.includes(stateValue)) { + if (!nextState || stateConfig.type === 'final' || finalStates.includes(nextState)) { break; } - steps.push(stateValue); + steps.push(nextState); + currentState = nextState; } return steps; diff --git a/packages/common/src/utils/collection-flow/set-collection-flow-status.unit.test.ts b/packages/common/src/utils/collection-flow/set-collection-flow-status.unit.test.ts index 1bfcec972c..449faa25a5 100644 --- a/packages/common/src/utils/collection-flow/set-collection-flow-status.unit.test.ts +++ b/packages/common/src/utils/collection-flow/set-collection-flow-status.unit.test.ts @@ -1,5 +1,5 @@ import { DefaultContextSchema } from '@/schemas'; -import { describe, expect, it } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import { setCollectionFlowStatus } from './set-collection-flow-status'; describe('setCollectionFlowStatus', () => { @@ -19,17 +19,25 @@ describe('setCollectionFlowStatus', () => { expect(updatedContext).toBe(context); }); - it('will throw an error if the status is invalid', () => { - const context = {}; + it('will log a warning if the status is invalid', () => { + const context = { + collectionFlow: { state: {} }, + }; + const consoleSpy = vi.spyOn(console, 'warn'); - expect(() => - setCollectionFlowStatus(context as DefaultContextSchema, 'invalid' as any), - ).toThrow(); + const result = setCollectionFlowStatus(context as DefaultContextSchema, 'invalid' as any); + + expect(consoleSpy).toHaveBeenCalledWith('Invalid status: invalid'); + expect(result).toBe(context); }); - it('will throw an error if the collection flow state is not present', () => { + it('will log a warning if the collection flow state is not present', () => { const context = {}; + const consoleSpy = vi.spyOn(console, 'warn'); + + const result = setCollectionFlowStatus(context as DefaultContextSchema, 'completed'); - expect(() => setCollectionFlowStatus(context as DefaultContextSchema, 'completed')).toThrow(); + expect(consoleSpy).toHaveBeenCalledWith('Collection flow state is not present.'); + expect(result).toBe(context); }); }); diff --git a/packages/workflow-core/CHANGELOG.md b/packages/workflow-core/CHANGELOG.md index a05a7dba81..22c4f1103f 100644 --- a/packages/workflow-core/CHANGELOG.md +++ b/packages/workflow-core/CHANGELOG.md @@ -15,6 +15,7 @@ - version bump ## 0.6.71 +## 0.6.69 ### Patch Changes diff --git a/sdks/workflow-browser-sdk/CHANGELOG.md b/sdks/workflow-browser-sdk/CHANGELOG.md index 515f17755b..f6f04f6271 100644 --- a/sdks/workflow-browser-sdk/CHANGELOG.md +++ b/sdks/workflow-browser-sdk/CHANGELOG.md @@ -19,6 +19,8 @@ ## 0.6.71 +## 0.6.69 + ### Patch Changes - Updated dependencies diff --git a/sdks/workflow-node-sdk/CHANGELOG.md b/sdks/workflow-node-sdk/CHANGELOG.md index f830100c54..4fbe827c20 100644 --- a/sdks/workflow-node-sdk/CHANGELOG.md +++ b/sdks/workflow-node-sdk/CHANGELOG.md @@ -32,6 +32,7 @@ ### Patch Changes +- @ballerine/workflow-core@0.6.69 - Updated dependencies - @ballerine/workflow-core@0.6.69 diff --git a/services/workflows-service/CHANGELOG.md b/services/workflows-service/CHANGELOG.md index 563b10540d..6d3dd78dd6 100644 --- a/services/workflows-service/CHANGELOG.md +++ b/services/workflows-service/CHANGELOG.md @@ -42,6 +42,8 @@ ### Patch Changes +- Updated dependencies + - @ballerine/common@0.9.54 - version bump - Updated dependencies - @ballerine/workflow-core@0.6.69