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

6658 workflows add a first twenty piece email sender #6965

Merged
merged 24 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add system action executor
  • Loading branch information
martmull committed Sep 9, 2024
commit 6339a4ebf91f0865ae99f0d7cc2c6b99f9cb5a6b
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { WorkflowSystemActionType } from 'src/modules/workflow/common/types/workflow-system-action.type';

type BaseWorkflowSettings = {
errorHandlingOptions: {
retryOnFailure: {
Expand All @@ -12,3 +14,7 @@ type BaseWorkflowSettings = {
export type WorkflowCodeSettings = BaseWorkflowSettings & {
serverlessFunctionId: string;
};

export type WorkflowSystemActionSettings = BaseWorkflowSettings & {
systemActionType: WorkflowSystemActionType;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type WorkflowError = {
stackTrace: string;
};

export type WorkflowResult = {
export type WorkflowStepResult = {
data: object | null;
error?: WorkflowError;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { WorkflowCodeSettings } from 'src/modules/workflow/common/types/workflow-settings.type';
import {
WorkflowCodeSettings,
WorkflowSystemActionSettings,
} from 'src/modules/workflow/common/types/workflow-settings.type';

export enum WorkflowStepType {
CODE_ACTION = 'CODE_ACTION',
SYSTEM_ACTION = 'SYSTEM_ACTION',
}

type BaseWorkflowStep = {
Expand All @@ -15,4 +19,9 @@ export type WorkflowCodeStep = BaseWorkflowStep & {
settings: WorkflowCodeSettings;
};

export type WorkflowStep = WorkflowCodeStep;
export type WorkflowSystemStep = BaseWorkflowStep & {
type: WorkflowStepType.SYSTEM_ACTION;
settings: WorkflowSystemActionSettings;
};

export type WorkflowStep = WorkflowCodeStep | WorkflowSystemStep;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum WorkflowSystemActionType {
SEND_EMAIL = 'SEND_EMAIL',
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ import {
} from 'src/modules/workflow/workflow-step-executor/workflow-step-executor.exception';
import { WorkflowStepExecutor } from 'src/modules/workflow/workflow-step-executor/workflow-step-executor.interface';
import { CodeActionExecutor } from 'src/modules/workflow/workflow-step-executor/workflow-step-executors/code-action-executor';
import { SystemActionExecutor } from 'src/modules/workflow/workflow-step-executor/workflow-step-executors/system-action-executor';

@Injectable()
export class WorkflowStepExecutorFactory {
constructor(private readonly codeActionExecutor: CodeActionExecutor) {}
constructor(
private readonly codeActionExecutor: CodeActionExecutor,
private readonly systemActionExecutor: SystemActionExecutor,
) {}

get(stepType: WorkflowStepType): WorkflowStepExecutor {
switch (stepType) {
case WorkflowStepType.CODE_ACTION:
return this.codeActionExecutor;
case WorkflowStepType.SYSTEM_ACTION:
return this.systemActionExecutor;
default:
throw new WorkflowStepExecutorException(
`Workflow step executor not found for step type '${stepType}'`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WorkflowResult } from 'src/modules/workflow/common/types/workflow-result.type';
import { WorkflowStepResult } from 'src/modules/workflow/common/types/workflow-step-result.type';
import { WorkflowStep } from 'src/modules/workflow/common/types/workflow-step.type';

export interface WorkflowStepExecutor {
Expand All @@ -8,5 +8,5 @@ export interface WorkflowStepExecutor {
}: {
step: WorkflowStep;
payload?: object;
}): Promise<WorkflowResult>;
}): Promise<WorkflowStepResult>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { ServerlessFunctionModule } from 'src/engine/metadata-modules/serverless
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { WorkflowStepExecutorFactory } from 'src/modules/workflow/workflow-step-executor/workflow-step-executor.factory';
import { CodeActionExecutor } from 'src/modules/workflow/workflow-step-executor/workflow-step-executors/code-action-executor';
import { SystemActionExecutor } from 'src/modules/workflow/workflow-step-executor/workflow-step-executors/system-action-executor';
import { WorkflowSystemActionModule } from 'src/modules/workflow/workflow-system-action/workflow-system-action.module';

@Module({
imports: [ServerlessFunctionModule],
imports: [ServerlessFunctionModule, WorkflowSystemActionModule],
providers: [
WorkflowStepExecutorFactory,
CodeActionExecutor,
SystemActionExecutor,
ScopedWorkspaceContextFactory,
],
exports: [WorkflowStepExecutorFactory],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';

import { ServerlessFunctionService } from 'src/engine/metadata-modules/serverless-function/serverless-function.service';
import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory';
import { WorkflowResult } from 'src/modules/workflow/common/types/workflow-result.type';
import { WorkflowStepResult } from 'src/modules/workflow/common/types/workflow-step-result.type';
import { WorkflowCodeStep } from 'src/modules/workflow/common/types/workflow-step.type';
import {
WorkflowStepExecutorException,
Expand All @@ -23,7 +23,7 @@ export class CodeActionExecutor implements WorkflowStepExecutor {
}: {
step: WorkflowCodeStep;
payload?: object;
}): Promise<WorkflowResult> {
}): Promise<WorkflowStepResult> {
const { workspaceId } = this.scopedWorkspaceContextFactory.create();

if (!workspaceId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Injectable } from '@nestjs/common';

import { WorkflowStepExecutor } from 'src/modules/workflow/workflow-step-executor/workflow-step-executor.interface';
import { WorkflowSystemStep } from 'src/modules/workflow/common/types/workflow-step.type';
import { WorkflowStepResult } from 'src/modules/workflow/common/types/workflow-step-result.type';
import { WorkflowSystemActionFactory } from 'src/modules/workflow/workflow-system-action/workflow-system-action.factory';

@Injectable()
export class SystemActionExecutor implements WorkflowStepExecutor {
constructor(
private readonly workflowSystemActionFactory: WorkflowSystemActionFactory,
) {}

async execute({
step,
payload,
}: {
step: WorkflowSystemStep;
payload?: object;
}): Promise<WorkflowStepResult> {
const workflowSystemAction = this.workflowSystemActionFactory.get(
step.settings.systemActionType,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Add error handling in case the systemActionType is invalid or not found


return await workflowSystemAction.execute({ step, payload });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider wrapping this in a try/catch block to handle potential errors during execution

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CustomException } from 'src/utils/custom-exception';

export class WorkflowSystemActionException extends CustomException {
code: WorkflowSystemActionExceptionCode;
constructor(message: string, code: WorkflowSystemActionExceptionCode) {
super(message, code);
}
}

export enum WorkflowSystemActionExceptionCode {
INVALID_SYSTEM_ACTION_TYPE = 'INVALID_SYSTEM_ACTION_TYPE',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@nestjs/common';

import { WorkflowSystemAction } from 'src/modules/workflow/workflow-system-action/workflow-system-action.interface';
import {
WorkflowSystemActionException,
WorkflowSystemActionExceptionCode,
} from 'src/modules/workflow/workflow-system-action/workflow-system-action.exception';
import { SendEmailAction } from 'src/modules/workflow/workflow-system-action/workflow-system-actions/send-email-action';
import { WorkflowSystemActionType } from 'src/modules/workflow/common/types/workflow-system-action.type';

@Injectable()
export class WorkflowSystemActionFactory {
constructor(private readonly sendEmailAction: SendEmailAction) {}

get(
workflowSystemActionType: WorkflowSystemActionType,
): WorkflowSystemAction {
switch (workflowSystemActionType) {
case WorkflowSystemActionType.SEND_EMAIL:
return this.sendEmailAction;
default:
throw new WorkflowSystemActionException(
`Workflow system action not found for system action type '${workflowSystemActionType}'`,
WorkflowSystemActionExceptionCode.INVALID_SYSTEM_ACTION_TYPE,
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a Map instead of a switch statement for better maintainability as more action types are added

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { WorkflowSystemStep } from 'src/modules/workflow/common/types/workflow-step.type';
import { WorkflowStepResult } from 'src/modules/workflow/common/types/workflow-step-result.type';

export interface WorkflowSystemAction {
execute({
step,
payload,
}: {
step: WorkflowSystemStep;
payload?: object;
}): Promise<WorkflowStepResult>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';

import { WorkflowSystemActionFactory } from 'src/modules/workflow/workflow-system-action/workflow-system-action.factory';
import { SendEmailAction } from 'src/modules/workflow/workflow-system-action/workflow-system-actions/send-email-action';

@Module({
providers: [WorkflowSystemActionFactory, SendEmailAction],
exports: [WorkflowSystemActionFactory],
})
export class WorkflowSystemActionModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Injectable } from '@nestjs/common';

import { WorkflowSystemAction } from 'src/modules/workflow/workflow-system-action/workflow-system-action.interface';
import { WorkflowStep } from 'src/modules/workflow/common/types/workflow-step.type';
import { WorkflowStepResult } from 'src/modules/workflow/common/types/workflow-step-result.type';

@Injectable()
export class SendEmailAction implements WorkflowSystemAction {
martmull marked this conversation as resolved.
Show resolved Hide resolved
async execute({
step,
payload,
}: {
step: WorkflowStep;
payload?: object;
}): Promise<WorkflowStepResult> {
return {
data: {
step,
payload,
},
};
}
}