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 3 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
9 changes: 7 additions & 2 deletions packages/twenty-emails/src/components/BaseEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import { Logo } from 'src/components/Logo';

type BaseEmailProps = PropsWithChildren<{
width?: number;
withLogo?: boolean;
}>;

export const BaseEmail = ({ children, width }: BaseEmailProps) => {
export const BaseEmail = ({
children,
width,
withLogo = true,
}: BaseEmailProps) => {
return (
<Html lang="en">
<BaseHead />
<Container width={width || 290}>
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 making default width a named constant for better maintainability

<Logo />
{withLogo && <Logo />}
{children}
</Container>
</Html>
Expand Down
2 changes: 1 addition & 1 deletion packages/twenty-emails/src/components/BaseHead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { emailTheme } from 'src/common-style';
export const BaseHead = () => {
return (
<Head>
<title>Twenty email</title>
<title>An email from Twenty</title>
<Font
fontFamily={emailTheme.font.family}
fallbackFontFamily="sans-serif"
Expand Down
28 changes: 28 additions & 0 deletions packages/twenty-emails/src/emails/workflow-action.email.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { BaseEmail } from 'src/components/BaseEmail';
import { Title } from 'src/components/Title';
import { MainText } from 'src/components/MainText';
import { CallToAction } from 'src/components/CallToAction';

type WorkflowActionEmailProps = {
mainText?: string;
title?: string;
callToAction?: {
value: string;
href: string;
};
};
export const WorkflowActionEmail = ({
mainText,
title,
callToAction,
}: WorkflowActionEmailProps) => {
return (
<BaseEmail withLogo={false}>
{title && <Title value={title} />}
{mainText && <MainText>{mainText}</MainText>}
{callToAction && (
<CallToAction value={callToAction.value} href={callToAction.href} />
)}
</BaseEmail>
);
};
1 change: 1 addition & 0 deletions packages/twenty-emails/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './emails/delete-inactive-workspaces.email';
export * from './emails/password-reset-link.email';
export * from './emails/password-update-notify.email';
export * from './emails/send-invite-link.email';
export * from './emails/workflow-action.email';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type BaseWorkflowSettings = {
export type BaseWorkflowSettings = {
errorHandlingOptions: {
retryOnFailure: {
value: boolean;
Expand All @@ -8,7 +8,3 @@ type BaseWorkflowSettings = {
};
};
};

export type WorkflowCodeSettings = BaseWorkflowSettings & {
serverlessFunctionId: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BaseWorkflowSettings } from 'src/modules/workflow/common/types/settings/workflow-base-settings.type';

export type WorkflowCodeSettings = BaseWorkflowSettings & {
serverlessFunctionId: string;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BaseWorkflowSettings } from 'src/modules/workflow/common/types/settings/workflow-base-settings.type';
import { WorkflowSystemActionType } from 'src/modules/workflow/common/types/workflow-system-action.type';

type BaseSystemActionSettings = BaseWorkflowSettings & {
systemActionType: WorkflowSystemActionType;
};

export type WorkflowSystemSendEmailActionSettings = BaseSystemActionSettings & {
subject: string;
template?: string;
title?: string;
callToAction?: {
value: string;
href: string;
};
};

export type WorkflowSystemActionSettings =
WorkflowSystemSendEmailActionSettings;
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,9 @@
import { WorkflowCodeSettings } from 'src/modules/workflow/common/types/workflow-settings.type';
import { WorkflowCodeSettings } from 'src/modules/workflow/common/types/settings/workflow-code-settings.type';
import { WorkflowSystemActionSettings } from 'src/modules/workflow/common/types/settings/workflow-system-action-settings.type';

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

type BaseWorkflowStep = {
Expand All @@ -15,4 +17,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,63 @@
import { Injectable } from '@nestjs/common';

import { WorkflowActionEmail } from 'twenty-emails';
import { render } from '@react-email/components';

import { WorkflowSystemAction } from 'src/modules/workflow/workflow-system-action/workflow-system-action.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 { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
import { EmailService } from 'src/engine/integrations/email/email.service';
import { isDefined } from 'src/utils/is-defined';

@Injectable()
export class SendEmailAction implements WorkflowSystemAction {
martmull marked this conversation as resolved.
Show resolved Hide resolved
constructor(
private readonly environmentService: EnvironmentService,
private readonly emailService: EmailService,
) {}

async execute({
step,
payload,
}: {
step: WorkflowSystemStep;
payload: {
email: string;
[key: string]: string;
};
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 more specific type for payload instead of [key: string]: string. This could lead to runtime errors if non-string values are passed.

}): Promise<WorkflowStepResult> {
let mainText = step.settings.template;

if (isDefined(payload)) {
Object.keys(payload).forEach(
(key: string) =>
(mainText = mainText?.replace(`{{${key}}}`, payload[key])),
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: This template replacement logic doesn't handle cases where a placeholder in the template doesn't have a corresponding value in the payload. Consider adding a fallback or error handling for missing values.


const email = WorkflowActionEmail({
mainText: mainText,
title: step.settings.title,
callToAction: step.settings.callToAction,
});
const html = render(email, {
pretty: true,
});
const text = render(email, {
plainText: true,
});

await this.emailService.send({
from: `${this.environmentService.get(
'EMAIL_FROM_NAME',
)} <${this.environmentService.get('EMAIL_FROM_ADDRESS')}>`,
to: payload.email,
subject: step.settings.subject,
text,
html,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Add error handling around the email sending process. If the email fails to send, the current implementation will throw an unhandled exception.


return { data: null };
}
}
Loading