Skip to content

Commit

Permalink
Dispatch Event Plugin (#2425)
Browse files Browse the repository at this point in the history
* feat: dispatch Event Plugin

* feat: version bump

* feat: lint and build fix

* feat: test fix

* feat: pr comments fixes

* feat: pr comments fix, test fix

* feat: test fix

* feat: test fix

* feat: conflict
  • Loading branch information
tomer-shvadron authored Jun 3, 2024
1 parent 45ba659 commit 242c9c7
Show file tree
Hide file tree
Showing 39 changed files with 690 additions and 238 deletions.
8 changes: 8 additions & 0 deletions apps/backoffice-v2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @ballerine/backoffice-v2

## 0.7.10

### Patch Changes

- Updated dependencies
- @ballerine/workflow-browser-sdk@0.6.11
- @ballerine/workflow-node-sdk@0.6.11

## 0.7.9

### Patch Changes
Expand Down
6 changes: 3 additions & 3 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.9",
"version": "0.7.10",
"description": "Ballerine - Backoffice",
"homepage": "https://github.com/ballerine-io/ballerine",
"repository": {
Expand Down Expand Up @@ -53,8 +53,8 @@
"@ballerine/common": "0.9.6",
"@ballerine/blocks": "0.2.3",
"@ballerine/ui": "^0.5.1",
"@ballerine/workflow-browser-sdk": "0.6.10",
"@ballerine/workflow-node-sdk": "0.6.10",
"@ballerine/workflow-browser-sdk": "0.6.11",
"@ballerine/workflow-node-sdk": "0.6.11",
"@fontsource/inter": "^4.5.15",
"@formkit/auto-animate": "1.0.0-beta.5",
"@hookform/resolvers": "^3.1.0",
Expand Down
7 changes: 7 additions & 0 deletions apps/kyb-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# kyb-app

## 0.3.11

### Patch Changes

- Updated dependencies
- @ballerine/workflow-browser-sdk@0.6.11

## 0.3.10

### Patch Changes
Expand Down
4 changes: 2 additions & 2 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.10",
"version": "0.3.11",
"type": "module",
"scripts": {
"dev": "vite",
Expand All @@ -17,7 +17,7 @@
"@ballerine/common": "^0.9.6",
"@ballerine/blocks": "0.2.3",
"@ballerine/ui": "0.5.2",
"@ballerine/workflow-browser-sdk": "0.6.10",
"@ballerine/workflow-browser-sdk": "0.6.11",
"@lukemorales/query-key-factory": "^1.0.3",
"@radix-ui/react-icons": "^1.3.0",
"@rjsf/core": "^5.9.0",
Expand Down
7 changes: 7 additions & 0 deletions examples/headless-example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @ballerine/headless-example

## 0.3.10

### Patch Changes

- Updated dependencies
- @ballerine/workflow-browser-sdk@0.6.11

## 0.3.9

### Patch Changes
Expand Down
4 changes: 2 additions & 2 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.9",
"version": "0.3.10",
"type": "module",
"scripts": {
"spellcheck": "cspell \"*\"",
Expand Down Expand Up @@ -35,7 +35,7 @@
},
"dependencies": {
"@ballerine/common": "0.9.6",
"@ballerine/workflow-browser-sdk": "0.6.10",
"@ballerine/workflow-browser-sdk": "0.6.11",
"@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/workflow-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ballerine/workflow-core

## 0.6.11

### Patch Changes

- Version bump

## 0.6.10

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion 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 <[email protected]>",
"version": "0.6.10",
"version": "0.6.11",
"description": "workflow-core",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
Expand Down
1 change: 1 addition & 0 deletions packages/workflow-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
createWorkflow,
Error,
Errors,
WorkflowEvents,
HttpError,
JmespathTransformer,
JsonSchemaValidator,
Expand Down
2 changes: 1 addition & 1 deletion packages/workflow-core/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Error, Errors } from './types';
export { Error, Errors, WorkflowEvents } from './types';
export type {
WorkflowEvent,
WorkflowEventWithoutState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import { WorkflowRunner } from '../../workflow-runner';
import { WorkflowRunnerArgs } from '../../types';
import { ISerializableHttpPluginParams } from './types';

function createWorkflowRunner(
const createWorkflowRunner = (
definition: WorkflowRunnerArgs['definition'],
apiPluginsSchemas: ISerializableHttpPluginParams[],
) {
) => {
return new WorkflowRunner({
runtimeId: '',
definition,
extensions: {
apiPlugins: apiPluginsSchemas,
},
workflowContext: { machineContext: { entity: { id: 'some_id' } } },
});
}
};

describe('workflow-runner', () => {
describe('api plugins', () => {
Expand Down Expand Up @@ -46,6 +47,7 @@ describe('workflow-runner', () => {
const apiPluginsSchemas = [
{
name: 'ballerineEnrichment',
displayName: 'Ballerine Enrichment',
url: 'https://simple-kyb-demo.s3.eu-central-1.amazonaws.com/mock-data/business_test_us.json',
method: 'GET' as const,
stateNames: ['checkBusinessScore'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { IDispatchEventPluginParams } from './types';
import { WorkflowRunner } from '../../../lib/workflow-runner';
import { Transformer, Transformers } from '../../../lib/utils';
import { AnyRecord, isErrorWithMessage } from '@ballerine/common';

export type IDispatchEventPluginParamsWithTransfomers = Omit<
IDispatchEventPluginParams,
'transformers'
> & {
transformers: ReturnType<WorkflowRunner['fetchTransformers']>;
};

export class DispatchEventPlugin {
public static pluginKind = 'dispatch-event';
name: string;
eventName: string;
payload?: AnyRecord;
stateNames: string[];
errorAction?: string;
successAction?: string;
transformers?: Transformers;
displayName: string | undefined;

constructor(pluginParams: IDispatchEventPluginParamsWithTransfomers) {
this.name = pluginParams.name;
this.payload = pluginParams.payload;
this.eventName = pluginParams.eventName;
this.stateNames = pluginParams.stateNames;
this.errorAction = pluginParams.errorAction;
this.displayName = pluginParams.displayName;
this.transformers = pluginParams.transformers;
this.successAction = pluginParams.successAction;
}

async getPluginEvent(record: AnyRecord) {
return {
eventName: this.eventName,
event: {
type: this.eventName,
payload: await this.__transformData(this.transformers || [], {
...record,
...this.payload,
}),
state: this.stateNames[0] ?? '',
},
};
}

private async __transformData(transformers: Transformers, record: AnyRecord) {
let mutatedRecord = record;

for (const transformer of transformers) {
mutatedRecord = await this.__transformByTransformer(transformer, mutatedRecord);
}

return mutatedRecord;
}

private async __transformByTransformer(transformer: Transformer, record: AnyRecord) {
try {
return (await transformer.transform(record, { input: 'json', output: 'json' })) as AnyRecord;
} catch (error) {
throw new Error(
`Error transforming data: ${
isErrorWithMessage(error) ? error.message : ''
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
} for transformer mapping: ${JSON.stringify(transformer.mapping)}`,
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { ApiPlugin } from './api-plugin';
export { WebhookPlugin } from './webhook-plugin';
export { DispatchEventPlugin } from './dispatch-event-plugin';
export type {
WebhookPluginParams,
IApiPluginParams,
Expand Down
33 changes: 23 additions & 10 deletions packages/workflow-core/src/lib/plugins/external-plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TJsonSchema, Transformers, Validator } from '../../utils';
import { THelperFormatingLogic } from '../../utils/context-transformers/types';
import { ActionablePlugin } from '../types';
import { ChildWorkflowPluginParams } from '../common-plugin/types';
import { AnyRecord } from '@ballerine/common';

export interface ValidatableTransformer {
transformers: Transformers;
Expand All @@ -10,7 +11,7 @@ export interface ValidatableTransformer {
export interface IApiPluginParams {
name: string;
pluginKind?: string;
stateNames: Array<string>;
stateNames: string[];
url: string;
method: 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'GET';
request: ValidatableTransformer;
Expand All @@ -21,12 +22,24 @@ export interface IApiPluginParams {
persistResponseDestination?: string;
displayName: string | undefined;

invoke?(...args: Array<any>): any;
invoke?(...args: any[]): any;
}

export interface IDispatchEventPluginParams {
name: string;
eventName: string;
payload?: AnyRecord;
stateNames: string[];
displayName?: string;
errorAction?: string;
successAction?: string;
transformers?: SerializableValidatableTransformer['transform'];
}

export interface WebhookPluginParams {
name: string;
pluginKind: string;
stateNames: Array<string>;
stateNames: string[];
url: string;
method: IApiPluginParams['method'];
headers: IApiPluginParams['headers'];
Expand All @@ -36,13 +49,13 @@ export interface WebhookPluginParams {
export interface IterativePluginParams {
name: string;
pluginKind: string;
stateNames: Array<string>;
stateNames: string[];
iterateOn: Omit<IApiPluginParams['request'], 'schemaValidator'>;
actionPlugin: ActionablePlugin;
successAction?: string;
errorAction?: string;

invoke?(...args: Array<any>): any;
invoke?(...args: any[]): any;
}

export interface SerializableValidatableTransformer {
Expand All @@ -58,12 +71,12 @@ export interface ISerializableHttpPluginParams
request: SerializableValidatableTransformer;
response: SerializableValidatableTransformer;

invoke?(...args: Array<any>): any;
invoke?(...args: any[]): any;
}

export interface SerializableWebhookPluginParams extends Omit<WebhookPluginParams, 'request'> {
name: string;
stateNames: Array<string>;
stateNames: string[];
url: string;
method: ISerializableHttpPluginParams['method'];
headers: ISerializableHttpPluginParams['headers'];
Expand All @@ -75,15 +88,15 @@ export interface ISerializableChildPluginParams
pluginKind: string;
transformers: Omit<SerializableValidatableTransformer, 'schema'>['transform'];

invoke?(...args: Array<any>): Promise<any>;
invoke?(...args: any[]): Promise<any>;
}

export interface SerializableIterativePluginParams {
name: string;
stateNames: Array<string>;
iterateOn: Omit<SerializableValidatableTransformer['transform'], 'schema'>;
stateNames: string[];
successAction?: string;
errorAction?: string;
iterateOn: SerializableValidatableTransformer['transform'];

invoke?(...args: any): void;
}
2 changes: 1 addition & 1 deletion packages/workflow-core/src/lib/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export type {
ValidatableTransformer,
SerializableValidatableTransformer,
} from './external-plugin';
export { ApiPlugin, WebhookPlugin } from './external-plugin';
export { ApiPlugin, WebhookPlugin, DispatchEventPlugin } from './external-plugin';
Loading

0 comments on commit 242c9c7

Please sign in to comment.