From 8e4b05e556848cc9893495871b48945983318632 Mon Sep 17 00:00:00 2001 From: alexeh Date: Sun, 13 Oct 2024 06:35:39 +0200 Subject: [PATCH] add isolated email templates and template builder --- .../email/templates/email-template.builder.ts | 37 +++++++++++++++++++ .../email-update-confirmation.template.ts | 15 ++++++++ .../templates/reset-password.template.ts | 15 ++++++++ .../email/templates/welcome-email.template.ts | 12 ++++++ 4 files changed, 79 insertions(+) create mode 100644 api/src/modules/notifications/email/templates/email-template.builder.ts create mode 100644 api/src/modules/notifications/email/templates/email-update-confirmation.template.ts create mode 100644 api/src/modules/notifications/email/templates/reset-password.template.ts create mode 100644 api/src/modules/notifications/email/templates/welcome-email.template.ts diff --git a/api/src/modules/notifications/email/templates/email-template.builder.ts b/api/src/modules/notifications/email/templates/email-template.builder.ts new file mode 100644 index 00000000..abda136d --- /dev/null +++ b/api/src/modules/notifications/email/templates/email-template.builder.ts @@ -0,0 +1,37 @@ +import { WELCOME_EMAIL_HTML_CONTENT } from '@api/modules/notifications/email/templates/welcome-email.template'; +import { RESET_PASSWORD_HTML_CONTENT } from '@api/modules/notifications/email/templates/reset-password.template'; +import { EMAIL_UPDATE_CONFIRMATION_HTML_CONTENT } from '@api/modules/notifications/email/templates/email-update-confirmation.template'; + +export enum TEMPLATE_TYPE { + WELCOME = 'welcome', + RESET_PASSWORD = 'reset-password', + EMAIL_UPDATE_CONFIRMATION = 'email-update-confirmation', +} + +export interface TemplateConfig { + url: string; + expiration: number; + type: TEMPLATE_TYPE; +} + +export class EmailTemplateBuilder { + url: string; + expiration: number; + type: TEMPLATE_TYPE; + templateMap = { + WELCOME_EMAIL_HTML_CONTENT: WELCOME_EMAIL_HTML_CONTENT, + RESET_PASSWORD_HTML_CONTENT: RESET_PASSWORD_HTML_CONTENT, + EMAIL_UPDATE_CONFIRMATION_HTML_CONTENT: + EMAIL_UPDATE_CONFIRMATION_HTML_CONTENT, + }; + constructor(contentConfig: TemplateConfig) { + this.url = contentConfig.url; + this.expiration = contentConfig.expiration; + this.type = contentConfig.type; + } + + build() { + const template = this.templateMap[this.type]; + return template(this.url, this.expiration); + } +} diff --git a/api/src/modules/notifications/email/templates/email-update-confirmation.template.ts b/api/src/modules/notifications/email/templates/email-update-confirmation.template.ts new file mode 100644 index 00000000..4a895942 --- /dev/null +++ b/api/src/modules/notifications/email/templates/email-update-confirmation.template.ts @@ -0,0 +1,15 @@ +export const EMAIL_UPDATE_CONFIRMATION_HTML_CONTENT = ( + url, + expiration, +): string => ` +

Dear User,

+
+

We have received a request to change the email address associated with your account. If you initiated this request, please confirm the change by clicking the link below:

+
+

Confirm Email Change

+
+

This link will redirect you to our app to confirm the new email address. For security reasons, this link will expire in ${expiration}.

+

If you did not request this email change, please ignore this message; your account information will remain unchanged.

+
+

Thank you for using our platform. We value your security and account privacy.

+

Best regards.

`; diff --git a/api/src/modules/notifications/email/templates/reset-password.template.ts b/api/src/modules/notifications/email/templates/reset-password.template.ts new file mode 100644 index 00000000..d75d627a --- /dev/null +++ b/api/src/modules/notifications/email/templates/reset-password.template.ts @@ -0,0 +1,15 @@ +export const RESET_PASSWORD_HTML_CONTENT = ( + url: string, + expiration: number, +): string => ` +

Dear User,

+
+

We recently received a request to reset your password for your account. If you made this request, please click on the link below to securely change your password:

+
+

Secure Password Reset Link

+
+

This link will direct you to our app to create a new password. For security reasons, this link will expire after ${expiration}.

+

If you did not request a password reset, please ignore this email; your password will remain the same.

+
+

Thank you for using the platform. We're committed to ensuring your account's security.

+

Best regards.

`; diff --git a/api/src/modules/notifications/email/templates/welcome-email.template.ts b/api/src/modules/notifications/email/templates/welcome-email.template.ts new file mode 100644 index 00000000..df52a50c --- /dev/null +++ b/api/src/modules/notifications/email/templates/welcome-email.template.ts @@ -0,0 +1,12 @@ +export const WELCOME_EMAIL_HTML_CONTENT = (url, expiration): string => ` +

Dear User,

+
+

We recently received a request to reset your password for your account. If you made this request, please click on the link below to securely change your password:

+
+

Secure Password Reset Link

+
+

This link will direct you to our app to create a new password. For security reasons, this link will expire after ${expiration}.

+

If you did not request a password reset, please ignore this email; your password will remain the same.

+
+

Thank you for using the platform. We're committed to ensuring your account's security.

+

Best regards.

`;