diff --git a/packages/twenty-front/src/modules/workflow/components/WorkflowEditActionFormSendEmail.tsx b/packages/twenty-front/src/modules/workflow/components/WorkflowEditActionFormSendEmail.tsx index be2a495202aca..f7d3255c12a96 100644 --- a/packages/twenty-front/src/modules/workflow/components/WorkflowEditActionFormSendEmail.tsx +++ b/packages/twenty-front/src/modules/workflow/components/WorkflowEditActionFormSendEmail.tsx @@ -1,21 +1,21 @@ +import { GMAIL_SEND_SCOPE } from '@/accounts/constants/GmailSendScope'; +import { ConnectedAccount } from '@/accounts/types/ConnectedAccount'; +import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState'; +import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords'; +import { useTriggerGoogleApisOAuth } from '@/settings/accounts/hooks/useTriggerGoogleApisOAuth'; +import { Select, SelectOption } from '@/ui/input/components/Select'; import { TextArea } from '@/ui/input/components/TextArea'; import { TextInput } from '@/ui/input/components/TextInput'; import { WorkflowEditActionFormBase } from '@/workflow/components/WorkflowEditActionFormBase'; +import { workflowIdState } from '@/workflow/states/workflowIdState'; import { WorkflowSendEmailStep } from '@/workflow/types/Workflow'; import { useTheme } from '@emotion/react'; import styled from '@emotion/styled'; -import React, { useEffect } from 'react'; +import { useEffect } from 'react'; import { Controller, useForm } from 'react-hook-form'; +import { useRecoilValue } from 'recoil'; import { IconMail, IconPlus, isDefined } from 'twenty-ui'; import { useDebouncedCallback } from 'use-debounce'; -import { Select, SelectOption } from '@/ui/input/components/Select'; -import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords'; -import { ConnectedAccount } from '@/accounts/types/ConnectedAccount'; -import { useRecoilValue } from 'recoil'; -import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState'; -import { useTriggerGoogleApisOAuth } from '@/settings/accounts/hooks/useTriggerGoogleApisOAuth'; -import { workflowIdState } from '@/workflow/states/workflowIdState'; -import { GMAIL_SEND_SCOPE } from '@/accounts/constants/GmailSendScope'; const StyledTriggerSettings = styled.div` padding: ${({ theme }) => theme.spacing(6)}; @@ -37,6 +37,7 @@ type WorkflowEditActionFormSendEmailProps = type SendEmailFormData = { connectedAccountId: string; + email: string; subject: string; body: string; }; @@ -53,6 +54,7 @@ export const WorkflowEditActionFormSendEmail = ( const form = useForm({ defaultValues: { connectedAccountId: '', + email: '', subject: '', body: '', }, @@ -83,10 +85,11 @@ export const WorkflowEditActionFormSendEmail = ( useEffect(() => { form.setValue( 'connectedAccountId', - props.action.settings.connectedAccountId ?? '', + props.action.settings.input.connectedAccountId ?? '', ); - form.setValue('subject', props.action.settings.subject ?? ''); - form.setValue('body', props.action.settings.body ?? ''); + form.setValue('email', props.action.settings.input.email ?? ''); + form.setValue('subject', props.action.settings.input.subject ?? ''); + form.setValue('body', props.action.settings.input.body ?? ''); }, [props.action.settings, form]); const saveAction = useDebouncedCallback( @@ -99,9 +102,12 @@ export const WorkflowEditActionFormSendEmail = ( ...props.action, settings: { ...props.action.settings, - connectedAccountId: formData.connectedAccountId, - subject: formData.subject, - body: formData.body, + input: { + connectedAccountId: formData.connectedAccountId, + email: formData.email, + subject: formData.subject, + body: formData.body, + }, }, }); @@ -134,12 +140,12 @@ export const WorkflowEditActionFormSendEmail = ( }; if ( - isDefined(props.action.settings.connectedAccountId) && - props.action.settings.connectedAccountId !== '' + isDefined(props.action.settings.input.connectedAccountId) && + props.action.settings.input.connectedAccountId !== '' ) { filter.or.push({ id: { - eq: props.action.settings.connectedAccountId, + eq: props.action.settings.input.connectedAccountId, }, }); } @@ -198,6 +204,21 @@ export const WorkflowEditActionFormSendEmail = ( /> )} /> + ( + { + field.onChange(email); + handleSave(); + }} + /> + )} + /> { @@ -66,7 +66,9 @@ export const WorkflowEditActionFormServerlessFunction = ( ...props.action, settings: { ...props.action.settings, - serverlessFunctionId: updatedFunction, + input: { + serverlessFunctionId: updatedFunction, + }, }, }); }} diff --git a/packages/twenty-front/src/modules/workflow/types/Workflow.ts b/packages/twenty-front/src/modules/workflow/types/Workflow.ts index 0ed8422846b9c..65b2e9a25a154 100644 --- a/packages/twenty-front/src/modules/workflow/types/Workflow.ts +++ b/packages/twenty-front/src/modules/workflow/types/Workflow.ts @@ -10,13 +10,18 @@ type BaseWorkflowStepSettings = { }; export type WorkflowCodeStepSettings = BaseWorkflowStepSettings & { - serverlessFunctionId: string; + input: { + serverlessFunctionId: string; + }; }; export type WorkflowSendEmailStepSettings = BaseWorkflowStepSettings & { - connectedAccountId: string; - subject?: string; - body?: string; + input: { + connectedAccountId: string; + email: string; + subject?: string; + body?: string; + }; }; type BaseWorkflowStep = { diff --git a/packages/twenty-front/src/modules/workflow/utils/__tests__/addCreateStepNodes.test.ts b/packages/twenty-front/src/modules/workflow/utils/__tests__/addCreateStepNodes.test.ts index d0f46dde35764..c2c9d760fe153 100644 --- a/packages/twenty-front/src/modules/workflow/utils/__tests__/addCreateStepNodes.test.ts +++ b/packages/twenty-front/src/modules/workflow/utils/__tests__/addCreateStepNodes.test.ts @@ -21,7 +21,9 @@ describe('addCreateStepNodes', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, }, { @@ -34,7 +36,9 @@ describe('addCreateStepNodes', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, }, ]; diff --git a/packages/twenty-front/src/modules/workflow/utils/__tests__/generateWorkflowDiagram.test.ts b/packages/twenty-front/src/modules/workflow/utils/__tests__/generateWorkflowDiagram.test.ts index ebf4f3c210203..663e1ef5661ce 100644 --- a/packages/twenty-front/src/modules/workflow/utils/__tests__/generateWorkflowDiagram.test.ts +++ b/packages/twenty-front/src/modules/workflow/utils/__tests__/generateWorkflowDiagram.test.ts @@ -42,7 +42,9 @@ describe('generateWorkflowDiagram', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, }, { @@ -55,7 +57,9 @@ describe('generateWorkflowDiagram', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, }, ]; @@ -96,7 +100,9 @@ describe('generateWorkflowDiagram', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, }, { @@ -109,7 +115,9 @@ describe('generateWorkflowDiagram', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, }, ]; diff --git a/packages/twenty-front/src/modules/workflow/utils/__tests__/getWorkflowVersionDiagram.test.ts b/packages/twenty-front/src/modules/workflow/utils/__tests__/getWorkflowVersionDiagram.test.ts index 907524a725e79..9c10c2af4f9ff 100644 --- a/packages/twenty-front/src/modules/workflow/utils/__tests__/getWorkflowVersionDiagram.test.ts +++ b/packages/twenty-front/src/modules/workflow/utils/__tests__/getWorkflowVersionDiagram.test.ts @@ -80,7 +80,9 @@ describe('getWorkflowVersionDiagram', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, diff --git a/packages/twenty-front/src/modules/workflow/utils/__tests__/insertStep.test.ts b/packages/twenty-front/src/modules/workflow/utils/__tests__/insertStep.test.ts index ba93aa6c341e8..b264d5edafbc1 100644 --- a/packages/twenty-front/src/modules/workflow/utils/__tests__/insertStep.test.ts +++ b/packages/twenty-front/src/modules/workflow/utils/__tests__/insertStep.test.ts @@ -25,7 +25,9 @@ describe('insertStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -63,7 +65,9 @@ describe('insertStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -95,7 +99,9 @@ describe('insertStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -108,7 +114,9 @@ describe('insertStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -129,7 +137,9 @@ describe('insertStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -165,7 +175,9 @@ describe('insertStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -178,7 +190,9 @@ describe('insertStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -199,7 +213,9 @@ describe('insertStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, diff --git a/packages/twenty-front/src/modules/workflow/utils/__tests__/removeStep.test.ts b/packages/twenty-front/src/modules/workflow/utils/__tests__/removeStep.test.ts index 01385411bf028..349d2f74200bd 100644 --- a/packages/twenty-front/src/modules/workflow/utils/__tests__/removeStep.test.ts +++ b/packages/twenty-front/src/modules/workflow/utils/__tests__/removeStep.test.ts @@ -10,7 +10,9 @@ it('returns a deep copy of the provided steps array instead of mutating it', () retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'first', + input: { + serverlessFunctionId: 'first', + }, }, type: 'CODE', valid: true, @@ -47,7 +49,9 @@ it('removes a step in a non-empty steps array', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -67,7 +71,9 @@ it('removes a step in a non-empty steps array', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -81,7 +87,9 @@ it('removes a step in a non-empty steps array', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, diff --git a/packages/twenty-front/src/modules/workflow/utils/__tests__/replaceStep.test.ts b/packages/twenty-front/src/modules/workflow/utils/__tests__/replaceStep.test.ts index 93286c59013e3..41e4f8cbae8bb 100644 --- a/packages/twenty-front/src/modules/workflow/utils/__tests__/replaceStep.test.ts +++ b/packages/twenty-front/src/modules/workflow/utils/__tests__/replaceStep.test.ts @@ -11,7 +11,9 @@ describe('replaceStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'first', + input: { + serverlessFunctionId: 'first', + }, }, type: 'CODE', valid: true, @@ -39,7 +41,9 @@ describe('replaceStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'second', + input: { + serverlessFunctionId: 'second', + }, }, }, stepId: stepToBeReplaced.id, @@ -57,7 +61,9 @@ describe('replaceStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -77,7 +83,9 @@ describe('replaceStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, @@ -91,7 +99,9 @@ describe('replaceStep', () => { retryOnFailure: { value: true }, continueOnFailure: { value: false }, }, - serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + input: { + serverlessFunctionId: 'a5434be2-c10b-465c-acec-46492782a997', + }, }, type: 'CODE', valid: true, diff --git a/packages/twenty-front/src/modules/workflow/utils/getStepDefaultDefinition.ts b/packages/twenty-front/src/modules/workflow/utils/getStepDefaultDefinition.ts index 48e8f9bd448fe..fd63158d25483 100644 --- a/packages/twenty-front/src/modules/workflow/utils/getStepDefaultDefinition.ts +++ b/packages/twenty-front/src/modules/workflow/utils/getStepDefaultDefinition.ts @@ -14,7 +14,9 @@ export const getStepDefaultDefinition = ( type: 'CODE', valid: false, settings: { - serverlessFunctionId: '', + input: { + serverlessFunctionId: '', + }, errorHandlingOptions: { continueOnFailure: { value: false, @@ -33,9 +35,12 @@ export const getStepDefaultDefinition = ( type: 'SEND_EMAIL', valid: false, settings: { - connectedAccountId: '', - subject: '', - body: '', + input: { + connectedAccountId: '', + email: '', + subject: '', + body: '', + }, errorHandlingOptions: { continueOnFailure: { value: false, diff --git a/packages/twenty-server/src/modules/mail-sender/workflow-actions/send-email.workflow-action.ts b/packages/twenty-server/src/modules/mail-sender/workflow-actions/send-email.workflow-action.ts index 026b4537d898a..ccecfaf0c71b4 100644 --- a/packages/twenty-server/src/modules/mail-sender/workflow-actions/send-email.workflow-action.ts +++ b/packages/twenty-server/src/modules/mail-sender/workflow-actions/send-email.workflow-action.ts @@ -1,26 +1,26 @@ import { Injectable, Logger } from '@nestjs/common'; -import { z } from 'zod'; +import DOMPurify from 'dompurify'; import Handlebars from 'handlebars'; import { JSDOM } from 'jsdom'; -import DOMPurify from 'dompurify'; +import { z } from 'zod'; -import { WorkflowActionResult } from 'src/modules/workflow/workflow-executor/types/workflow-action-result.type'; -import { WorkflowSendEmailStep } from 'src/modules/workflow/workflow-executor/types/workflow-action.type'; -import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service'; import { EmailService } from 'src/engine/core-modules/email/email.service'; -import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity'; -import { - WorkflowStepExecutorException, - WorkflowStepExecutorExceptionCode, -} from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception'; +import { EnvironmentService } from 'src/engine/core-modules/environment/environment.service'; import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory'; +import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; +import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity'; import { MailSenderException, MailSenderExceptionCode, } from 'src/modules/mail-sender/exceptions/mail-sender.exception'; import { GmailClientProvider } from 'src/modules/messaging/message-import-manager/drivers/gmail/providers/gmail-client.provider'; -import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; +import { + WorkflowStepExecutorException, + WorkflowStepExecutorExceptionCode, +} from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception'; +import { WorkflowActionResult } from 'src/modules/workflow/workflow-executor/types/workflow-action-result.type'; +import { WorkflowSendEmailStep } from 'src/modules/workflow/workflow-executor/types/workflow-action.type'; import { isDefined } from 'src/utils/is-defined'; @Injectable() @@ -50,12 +50,12 @@ export class SendEmailWorkflowAction { 'connectedAccount', ); const connectedAccount = await connectedAccountRepository.findOneBy({ - id: step.settings.connectedAccountId, + id: step.settings.input.connectedAccountId, }); if (!isDefined(connectedAccount)) { throw new MailSenderException( - `Connected Account '${step.settings.connectedAccountId}' not found`, + `Connected Account '${step.settings.input.connectedAccountId}' not found`, MailSenderExceptionCode.CONNECTED_ACCOUNT_NOT_FOUND, ); } @@ -73,37 +73,35 @@ export class SendEmailWorkflowAction { async execute({ step, - payload, + context, }: { step: WorkflowSendEmailStep; - payload: { - email: string; - [key: string]: string; - }; + context: Record; }): Promise { const emailProvider = await this.getEmailClient(step); + const { email, body, subject } = step.settings.input; try { const emailSchema = z.string().trim().email('Invalid email'); - const result = emailSchema.safeParse(payload.email); + const result = emailSchema.safeParse(email); if (!result.success) { - this.logger.warn(`Email '${payload.email}' invalid`); + this.logger.warn(`Email '${email}' invalid`); return { result: { success: false } }; } - const body = Handlebars.compile(step.settings.body)(payload); - const subject = Handlebars.compile(step.settings.subject)(payload); + const inferedBody = Handlebars.compile(body)(context); + const inferedSubject = Handlebars.compile(subject)(context); const window = new JSDOM('').window; const purify = DOMPurify(window); - const safeBody = purify.sanitize(body || ''); - const safeSubject = purify.sanitize(subject || ''); + const safeBody = purify.sanitize(inferedBody || ''); + const safeSubject = purify.sanitize(inferedSubject || ''); const message = [ - `To: ${payload.email}`, + `To: ${email}`, `Subject: ${safeSubject || ''}`, 'MIME-Version: 1.0', 'Content-Type: text/plain; charset="UTF-8"', diff --git a/packages/twenty-server/src/modules/serverless/workflow-actions/code.workflow-action.ts b/packages/twenty-server/src/modules/serverless/workflow-actions/code.workflow-action.ts index 1e42dcf81eb05..982e666f83728 100644 --- a/packages/twenty-server/src/modules/serverless/workflow-actions/code.workflow-action.ts +++ b/packages/twenty-server/src/modules/serverless/workflow-actions/code.workflow-action.ts @@ -2,12 +2,12 @@ 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 { WorkflowActionResult } from 'src/modules/workflow/workflow-executor/types/workflow-action-result.type'; -import { WorkflowCodeStep } from 'src/modules/workflow/workflow-executor/types/workflow-action.type'; import { WorkflowStepExecutorException, WorkflowStepExecutorExceptionCode, } from 'src/modules/workflow/workflow-executor/exceptions/workflow-step-executor.exception'; +import { WorkflowActionResult } from 'src/modules/workflow/workflow-executor/types/workflow-action-result.type'; +import { WorkflowCodeStep } from 'src/modules/workflow/workflow-executor/types/workflow-action.type'; @Injectable() export class CodeWorkflowAction { @@ -18,10 +18,10 @@ export class CodeWorkflowAction { async execute({ step, - payload, + context, }: { step: WorkflowCodeStep; - payload?: object; + context?: Record; }): Promise { const { workspaceId } = this.scopedWorkspaceContextFactory.create(); @@ -34,9 +34,9 @@ export class CodeWorkflowAction { const result = await this.serverlessFunctionService.executeOneServerlessFunction( - step.settings.serverlessFunctionId, + step.settings.input.serverlessFunctionId, workspaceId, - payload || {}, + context || {}, ); if (result.error) { diff --git a/packages/twenty-server/src/modules/workflow/common/standard-objects/workflow-run.workspace-entity.ts b/packages/twenty-server/src/modules/workflow/common/standard-objects/workflow-run.workspace-entity.ts index aa14605f74a06..545e88a470769 100644 --- a/packages/twenty-server/src/modules/workflow/common/standard-objects/workflow-run.workspace-entity.ts +++ b/packages/twenty-server/src/modules/workflow/common/standard-objects/workflow-run.workspace-entity.ts @@ -32,17 +32,21 @@ export enum WorkflowRunStatus { FAILED = 'FAILED', } -export type WorkflowRunOutput = { - steps: { - id: string; - name: string; - type: string; +type StepRunOutput = { + id: string; + name: string; + type: string; + outputs: { attemptCount: number; result: object | undefined; error: string | undefined; }[]; }; +export type WorkflowRunOutput = { + steps: Record; +}; + @WorkspaceEntity({ standardId: STANDARD_OBJECT_IDS.workflowRun, namePlural: 'workflowRuns', diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/interfaces/workflow-action.interface.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/interfaces/workflow-action.interface.ts index 47b481949a769..ca4f0741af4cc 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-executor/interfaces/workflow-action.interface.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-executor/interfaces/workflow-action.interface.ts @@ -4,9 +4,9 @@ import { WorkflowStep } from 'src/modules/workflow/workflow-executor/types/workf export interface WorkflowAction { execute({ step, - payload, + context, }: { step: WorkflowStep; - payload?: object; + context?: Record; }): Promise; } diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-step-settings.type.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-step-settings.type.ts index bb8f8351faab8..6fcda7e4e1311 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-step-settings.type.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-executor/types/workflow-step-settings.type.ts @@ -10,11 +10,16 @@ type BaseWorkflowStepSettings = { }; export type WorkflowCodeStepSettings = BaseWorkflowStepSettings & { - serverlessFunctionId: string; + input: { + serverlessFunctionId: string; + }; }; export type WorkflowSendEmailStepSettings = BaseWorkflowStepSettings & { - connectedAccountId: string; - subject?: string; - body?: string; + input: { + connectedAccountId: string; + email: string; + subject?: string; + body?: string; + }; }; diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts index c50684f876c67..b1d9fb6b03bb5 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workspace-services/workflow-executor.workspace-service.ts @@ -21,14 +21,14 @@ export class WorkflowExecutorWorkspaceService { async execute({ currentStepIndex, steps, - payload, + context, output, attemptCount = 1, }: { currentStepIndex: number; steps: WorkflowStep[]; output: WorkflowExecutorOutput; - payload?: object; + context?: Record; attemptCount?: number; }): Promise { if (currentStepIndex >= steps.length) { @@ -41,57 +41,54 @@ export class WorkflowExecutorWorkspaceService { const result = await workflowAction.execute({ step, - payload, + context, }); - const baseStepOutput = { + const stepOutput = output.steps[step.name]; + + const error = + result.error?.errorMessage ?? + (result.result ? undefined : 'Execution result error, no data or error'); + + const updatedStepOutput = { id: step.id, name: step.name, type: step.type, - attemptCount, + outputs: [ + ...(stepOutput?.outputs ?? []), + { + attemptCount, + result: result.result, + error, + }, + ], }; const updatedOutput = { ...output, - steps: [ + steps: { ...output.steps, - { - ...baseStepOutput, - result: result.result, - error: result.error?.errorMessage, - }, - ], + [step.name]: updatedStepOutput, + }, }; if (result.result) { return await this.execute({ currentStepIndex: currentStepIndex + 1, steps, - payload: result.result, + context: { + ...context, + [step.name]: result.result, + }, output: updatedOutput, }); } - if (!result.error) { - return { - ...output, - steps: [ - ...output.steps, - { - ...baseStepOutput, - result: undefined, - error: 'Execution result error, no data or error', - }, - ], - status: WorkflowRunStatus.FAILED, - }; - } - if (step.settings.errorHandlingOptions.continueOnFailure.value) { return await this.execute({ currentStepIndex: currentStepIndex + 1, steps, - payload, + context, output: updatedOutput, }); } @@ -103,7 +100,7 @@ export class WorkflowExecutorWorkspaceService { return await this.execute({ currentStepIndex, steps, - payload, + context, output: updatedOutput, attemptCount: attemptCount + 1, }); diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/run-workflow.job.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/run-workflow.job.ts index 5a79462a355c9..644595df6f3b6 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/run-workflow.job.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-runner/jobs/run-workflow.job.ts @@ -40,9 +40,11 @@ export class RunWorkflowJob { await this.workflowExecutorWorkspaceService.execute({ currentStepIndex: 0, steps: workflowVersion.steps || [], - payload, + context: { + trigger: payload, + }, output: { - steps: [], + steps: {}, status: WorkflowRunStatus.RUNNING, }, });