Skip to content

Commit

Permalink
feat: reworked getOrderedSteps & fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chesterkmr committed Nov 27, 2024
1 parent 020f5ca commit 581b982
Show file tree
Hide file tree
Showing 22 changed files with 132 additions and 47 deletions.
9 changes: 9 additions & 0 deletions apps/backoffice-v2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @ballerine/backoffice-v2

## 0.7.73

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54
- @ballerine/workflow-browser-sdk@0.6.69
- @ballerine/workflow-node-sdk@0.6.69

## 0.7.72

### Patch Changes
Expand Down
8 changes: 4 additions & 4 deletions apps/backoffice-v2/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ballerine/backoffice-v2",
"version": "0.7.72",
"version": "0.7.73",
"description": "Ballerine - Backoffice",
"homepage": "https://github.com/ballerine-io/ballerine",
"type": "module",
Expand Down Expand Up @@ -52,11 +52,11 @@
},
"dependencies": {
"@ballerine/blocks": "0.2.27",
"@ballerine/common": "0.9.53",
"@ballerine/common": "0.9.54",
"@ballerine/react-pdf-toolkit": "^1.2.47",
"@ballerine/ui": "^0.5.47",
"@ballerine/workflow-browser-sdk": "0.6.68",
"@ballerine/workflow-node-sdk": "0.6.68",
"@ballerine/workflow-browser-sdk": "0.6.69",
"@ballerine/workflow-node-sdk": "0.6.69",
"@botpress/webchat": "^2.1.10",
"@botpress/webchat-generator": "^0.2.9",
"@fontsource/inter": "^4.5.15",
Expand Down
8 changes: 8 additions & 0 deletions apps/kyb-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# kyb-app

## 0.3.84

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54
- @ballerine/workflow-browser-sdk@0.6.69

## 0.3.83

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions apps/kyb-app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/kyb-app",
"private": true,
"version": "0.3.83",
"version": "0.3.84",
"type": "module",
"scripts": {
"dev": "vite",
Expand All @@ -16,9 +16,9 @@
},
"dependencies": {
"@ballerine/blocks": "0.2.27",
"@ballerine/common": "^0.9.53",
"@ballerine/common": "^0.9.54",
"@ballerine/ui": "0.5.47",
"@ballerine/workflow-browser-sdk": "0.6.68",
"@ballerine/workflow-browser-sdk": "0.6.69",
"@lukemorales/query-key-factory": "^1.0.3",
"@radix-ui/react-icons": "^1.3.0",
"@rjsf/core": "^5.9.0",
Expand Down
8 changes: 8 additions & 0 deletions examples/headless-example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @ballerine/headless-example

## 0.3.68

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54
- @ballerine/workflow-browser-sdk@0.6.69

## 0.3.67

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions examples/headless-example/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/headless-example",
"private": true,
"version": "0.3.67",
"version": "0.3.68",
"type": "module",
"scripts": {
"spellcheck": "cspell \"*\"",
Expand Down Expand Up @@ -34,8 +34,8 @@
"vite": "^4.5.3"
},
"dependencies": {
"@ballerine/common": "0.9.53",
"@ballerine/workflow-browser-sdk": "0.6.68",
"@ballerine/common": "0.9.54",
"@ballerine/workflow-browser-sdk": "0.6.69",
"@felte/reporter-svelte": "^1.1.5",
"@felte/validator-zod": "^1.0.13",
"@fontsource/inter": "^4.5.15",
Expand Down
6 changes: 6 additions & 0 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ballerine/common

## 0.9.54

### Patch Changes

- Reworked getOrderedSteps & fixed tests

## 0.9.53

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": false,
"name": "@ballerine/common",
"author": "Ballerine <dev@ballerine.com>",
"version": "0.9.53",
"version": "0.9.54",
"description": "common",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
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;

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);
});
});
7 changes: 7 additions & 0 deletions packages/workflow-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @ballerine/workflow-core

## 0.6.69

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54

## 0.6.68

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/workflow-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/workflow-core",
"author": "Ballerine <dev@ballerine.com>",
"version": "0.6.68",
"version": "0.6.69",
"description": "workflow-core",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
Expand Down Expand Up @@ -31,7 +31,7 @@
"node": ">=12"
},
"dependencies": {
"@ballerine/common": "0.9.53",
"@ballerine/common": "0.9.54",
"ajv": "^8.12.0",
"country-state-city": "^3.1.4",
"i18n-iso-countries": "^7.6.0",
Expand Down
7 changes: 7 additions & 0 deletions sdks/web-ui-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# web-ui-sdk

## 1.5.55

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54

## 1.5.54

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions sdks/web-ui-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"types": "dist/index.d.ts",
"name": "@ballerine/web-ui-sdk",
"private": false,
"version": "1.5.54",
"version": "1.5.55",
"type": "module",
"files": [
"dist"
Expand Down Expand Up @@ -96,7 +96,7 @@
"vitest": "^0.24.5"
},
"dependencies": {
"@ballerine/common": "0.9.53",
"@ballerine/common": "0.9.54",
"@zerodevx/svelte-toast": "^0.8.0",
"compressorjs": "^1.1.1",
"deepmerge": "^4.3.0",
Expand Down
8 changes: 8 additions & 0 deletions sdks/workflow-browser-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @ballerine/workflow-browser-sdk

## 0.6.69

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54
- @ballerine/workflow-core@0.6.69

## 0.6.68

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions sdks/workflow-browser-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/workflow-browser-sdk",
"author": "Ballerine <dev@ballerine.com>",
"version": "0.6.68",
"version": "0.6.69",
"description": "workflow-browser-sdk",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
Expand Down Expand Up @@ -33,8 +33,8 @@
"node": ">=12"
},
"dependencies": {
"@ballerine/common": "0.9.53",
"@ballerine/workflow-core": "0.6.68",
"@ballerine/common": "0.9.54",
"@ballerine/workflow-core": "0.6.69",
"xstate": "^4.37.0"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions sdks/workflow-node-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ballerine/workflow-node-sdk

## 0.6.69

### Patch Changes

- @ballerine/workflow-core@0.6.69

## 0.6.68

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions sdks/workflow-node-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/workflow-node-sdk",
"author": "Ballerine <dev@ballerine.com>",
"version": "0.6.68",
"version": "0.6.69",
"description": "workflow-node-sdk",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
Expand All @@ -28,7 +28,7 @@
"node": ">=12"
},
"dependencies": {
"@ballerine/workflow-core": "0.6.68",
"@ballerine/workflow-core": "0.6.69",
"json-logic-js": "^2.0.2",
"xstate": "^4.36.0"
},
Expand Down
9 changes: 9 additions & 0 deletions services/workflows-service/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @ballerine/workflows-service

## 0.7.73

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54
- @ballerine/workflow-core@0.6.69
- @ballerine/workflow-node-sdk@0.6.69

## 0.7.72

### Patch Changes
Expand Down
8 changes: 4 additions & 4 deletions services/workflows-service/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ballerine/workflows-service",
"private": false,
"version": "0.7.72",
"version": "0.7.73",
"description": "workflow-service",
"scripts": {
"spellcheck": "cspell \"*\"",
Expand Down Expand Up @@ -48,9 +48,9 @@
"@aws-sdk/client-secrets-manager": "^3.620.1",
"@aws-sdk/lib-storage": "3.347.1",
"@aws-sdk/s3-request-presigner": "3.347.1",
"@ballerine/common": "0.9.53",
"@ballerine/workflow-core": "0.6.68",
"@ballerine/workflow-node-sdk": "0.6.68",
"@ballerine/common": "0.9.54",
"@ballerine/workflow-core": "0.6.69",
"@ballerine/workflow-node-sdk": "0.6.69",
"@faker-js/faker": "^7.6.0",
"@nestjs/axios": "^2.0.0",
"@nestjs/common": "^9.3.12",
Expand Down
2 changes: 1 addition & 1 deletion services/workflows-service/prisma/data-migrations
2 changes: 1 addition & 1 deletion websites/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dependencies": {
"@astrojs/starlight": "0.11.1",
"@astrojs/tailwind": "^4.0.0",
"@ballerine/common": "^0.9.53",
"@ballerine/common": "^0.9.54",
"astro": "3.3.3",
"sharp": "^0.32.4",
"shiki": "^0.14.3"
Expand Down

0 comments on commit 581b982

Please sign in to comment.