Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reworked getOrderedSteps & fixed tests #2868

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/backoffice-v2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

### Patch Changes

- Updated dependencies
- @ballerine/[email protected]
- @ballerine/[email protected]
- @ballerine/[email protected]
- @ballerine/[email protected]
- @ballerine/[email protected]

Expand Down
3 changes: 3 additions & 0 deletions apps/kyb-app/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

## 0.3.86

## 0.3.84

### Patch Changes

- Updated dependencies
- @ballerine/[email protected]
- @ballerine/[email protected]
- @ballerine/[email protected]

## 0.3.85
Expand Down
3 changes: 3 additions & 0 deletions examples/headless-example/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

## 0.3.70

## 0.3.68

### Patch Changes

- Updated dependencies
- @ballerine/[email protected]
- @ballerine/[email protected]
- @ballerine/[email protected]

## 0.3.69
Expand Down
1 change: 1 addition & 0 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Patch Changes

- Reworked getOrderedSteps & fixed tests
- bump

## 0.9.53
Expand Down
35 changes: 22 additions & 13 deletions packages/common/src/utils/collection-flow/get-ordered-steps.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
chesterkmr marked this conversation as resolved.
Show resolved Hide resolved

if (finalStates.includes(stateValue)) {
if (!nextState || stateConfig.type === 'final' || finalStates.includes(nextState)) {
break;
}

steps.push(stateValue);
steps.push(nextState);
currentState = nextState;
}

return steps;
Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});
1 change: 1 addition & 0 deletions packages/workflow-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# @ballerine/workflow-core

## 0.6.71
## 0.6.69

### Patch Changes

Expand Down
2 changes: 2 additions & 0 deletions sdks/workflow-browser-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.6.71

## 0.6.69

### Patch Changes

- Updated dependencies
Expand Down
1 change: 1 addition & 0 deletions sdks/workflow-node-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

### Patch Changes

- @ballerine/[email protected]
- Updated dependencies
- @ballerine/[email protected]

Expand Down
2 changes: 2 additions & 0 deletions services/workflows-service/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

### Patch Changes

- Updated dependencies
- @ballerine/[email protected]
- version bump
- Updated dependencies
- @ballerine/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion services/workflows-service/prisma/data-migrations