diff --git a/nest-cli.json b/nest-cli.json index ee012c5..60e4148 100644 --- a/nest-cli.json +++ b/nest-cli.json @@ -10,8 +10,7 @@ "include": "config", "exclude": "**/*.md" }, - { "include": "common/modules/**/templates/*", "watchAssets": true } -, + { "include": "common/modules/**/templates/**/*", "watchAssets": true }, "i18n", "files" ] diff --git a/package-lock.json b/package-lock.json index 1745511..2a54f0c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,6 +36,7 @@ "firebase-admin": "^13.0.2", "google-libphonenumber": "^3.2.39", "handlebars": "^4.7.8", + "handlebars-layouts": "^3.1.4", "jwk-to-pem": "^2.0.7", "lodash": "^4.17.21", "moment": "^2.30.1", @@ -6983,6 +6984,15 @@ "uglify-js": "^3.1.4" } }, + "node_modules/handlebars-layouts": { + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/handlebars-layouts/-/handlebars-layouts-3.1.4.tgz", + "integrity": "sha512-2llBmvnj8ueOfxNHdRzJOcgalzZjYVd9+WAl93kPYmlX4WGx7FTHTzNxhK+i9YKY2OSjzfehgpLiIwP/OJr6tw==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/handlebars/node_modules/source-map": { "version": "0.6.1", "license": "BSD-3-Clause", diff --git a/package.json b/package.json index 96293e4..a991cb2 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "firebase-admin": "^13.0.2", "google-libphonenumber": "^3.2.39", "handlebars": "^4.7.8", + "handlebars-layouts": "^3.1.4", "jwk-to-pem": "^2.0.7", "lodash": "^4.17.21", "moment": "^2.30.1", diff --git a/src/common/modules/notification/listeners/notification-created.listener.ts b/src/common/modules/notification/listeners/notification-created.listener.ts index 190c128..03d9a7c 100644 --- a/src/common/modules/notification/listeners/notification-created.listener.ts +++ b/src/common/modules/notification/listeners/notification-created.listener.ts @@ -72,12 +72,17 @@ export class NotificationCreatedListener { private async sendEmail({ to, subject, data, template }: SendEmailRequestDto) { this.logger.log(`Sending email to ${to}`); - await this.mailerService.sendMail({ - to, - subject, - template, - context: { ...data }, - }); - this.logger.log(`Email sent to ${to}`); + try { + await this.mailerService.sendMail({ + to, + subject, + template, + context: { ...data, currentYear: new Date().getFullYear() }, + }); + this.logger.log(`Email sent to ${to}`); + } catch (error) { + this.logger.error(`Failed to send email to ${to} error: ${JSON.stringify(error)}`); + throw error; + } } } diff --git a/src/common/modules/notification/templates/otp.hbs b/src/common/modules/notification/templates/otp.hbs index e849df4..0c491b3 100644 --- a/src/common/modules/notification/templates/otp.hbs +++ b/src/common/modules/notification/templates/otp.hbs @@ -1,21 +1,22 @@ - -
-

Your OTP Code

-

To verify your account, please use the following One-Time Password (OTP):

+{{>header}} +
+
- - +{{>footer}} \ No newline at end of file diff --git a/src/common/modules/notification/templates/partials/footer.hbs b/src/common/modules/notification/templates/partials/footer.hbs new file mode 100644 index 0000000..56a3290 --- /dev/null +++ b/src/common/modules/notification/templates/partials/footer.hbs @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/src/common/modules/notification/templates/partials/header.hbs b/src/common/modules/notification/templates/partials/header.hbs new file mode 100644 index 0000000..476e1eb --- /dev/null +++ b/src/common/modules/notification/templates/partials/header.hbs @@ -0,0 +1,95 @@ + + + + Zod OTP Email + + + + + + + + + \ No newline at end of file diff --git a/src/core/module-options/mailer-options.ts b/src/core/module-options/mailer-options.ts index efd35c1..44683b5 100644 --- a/src/core/module-options/mailer-options.ts +++ b/src/core/module-options/mailer-options.ts @@ -1,7 +1,6 @@ import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter'; import { ConfigService } from '@nestjs/config'; import path from 'path'; - export function buildMailerOptions(config: ConfigService) { return { transport: { @@ -20,5 +19,13 @@ export function buildMailerOptions(config: ConfigService) { strict: true, }, }, + options: { + partials: { + dir: path.join(__dirname, '../../common/modules/notification/templates', 'partials'), + options: { + strict: true, + }, + }, + }, }; }