Skip to content

Commit

Permalink
Add send email step
Browse files Browse the repository at this point in the history
  • Loading branch information
martmull committed Sep 9, 2024
1 parent 6339a4e commit e484c60
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 33 deletions.
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>
{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
@@ -0,0 +1,10 @@
export type BaseWorkflowSettings = {
errorHandlingOptions: {
retryOnFailure: {
value: boolean;
};
continueOnFailure: {
value: boolean;
};
};
};
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;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
WorkflowCodeSettings,
WorkflowSystemActionSettings,
} 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',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +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 { WorkflowStep } from 'src/modules/workflow/common/types/workflow-step.type';
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 {
constructor(
private readonly environmentService: EnvironmentService,
private readonly emailService: EmailService,
) {}

async execute({
step,
payload,
}: {
step: WorkflowStep;
payload?: object;
}): Promise<WorkflowStepResult> {
return {
data: {
step,
payload,
},
step: WorkflowSystemStep;
payload: {
email: string;
[key: string]: string;
};
}): Promise<WorkflowStepResult> {
let mainText = step.settings.template;

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

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,
});

return { data: null };
}
}

0 comments on commit e484c60

Please sign in to comment.