From 429ecd9607b486e643dccc3c27f08310e91f392b Mon Sep 17 00:00:00 2001 From: Wajahat Islam Gul Date: Tue, 23 Sep 2025 07:41:34 +0500 Subject: [PATCH] fix: email template not found error --- .../forgot-password-user-mail.ejs | 2 +- apps/auth-service/src/utils/sendMail/index.ts | 28 ++++++++++++------ apps/auth-service/webpack.config.js | 9 +++++- .../src/utils/send-email/index.ts | 29 +++++++++++++------ apps/order-service/webpack.config.js | 9 +++++- 5 files changed, 56 insertions(+), 21 deletions(-) diff --git a/apps/auth-service/src/utils/email-templates/forgot-password-user-mail.ejs b/apps/auth-service/src/utils/email-templates/forgot-password-user-mail.ejs index aaaecd4..25c2e12 100644 --- a/apps/auth-service/src/utils/email-templates/forgot-password-user-mail.ejs +++ b/apps/auth-service/src/utils/email-templates/forgot-password-user-mail.ejs @@ -161,7 +161,7 @@

How to reset your password:

diff --git a/apps/auth-service/src/utils/sendMail/index.ts b/apps/auth-service/src/utils/sendMail/index.ts index dc7262b..7fda5f1 100644 --- a/apps/auth-service/src/utils/sendMail/index.ts +++ b/apps/auth-service/src/utils/sendMail/index.ts @@ -2,6 +2,7 @@ import dotenv from 'dotenv'; import ejs from 'ejs'; import nodemail from 'nodemailer'; import path from 'path'; +import { existsSync } from 'node:fs'; dotenv.config(); @@ -17,15 +18,24 @@ const transporter = nodemail.createTransport({ //Render an EJS email template const renderEmailTemplate = async (templateName: string, data: Record): Promise => { - const templatePath = path.join( - process.cwd(), - 'apps', - 'auth-service', - 'src', - 'utils', - 'email-templates', - `${templateName}.ejs`, - ); + const templateCandidates = [ + path.join(__dirname, '..', 'email-templates', `${templateName}.ejs`), + path.join( + process.cwd(), + 'apps', + 'auth-service', + 'src', + 'utils', + 'email-templates', + `${templateName}.ejs`, + ), + ]; + + const templatePath = templateCandidates.find((candidate) => existsSync(candidate)); + + if (!templatePath) { + throw new Error(`Email template ${templateName} not found`); + } return ejs.renderFile(templatePath, data); }; diff --git a/apps/auth-service/webpack.config.js b/apps/auth-service/webpack.config.js index 7a2f3d5..9d80b29 100644 --- a/apps/auth-service/webpack.config.js +++ b/apps/auth-service/webpack.config.js @@ -17,7 +17,14 @@ module.exports = { compiler: 'tsc', main: './src/main.ts', tsConfig: './tsconfig.app.json', - assets: ['apps/auth-service/src/assets'], // Changed from './src/assets' to 'apps/auth-service/src/assets' + assets: [ + 'apps/auth-service/src/assets', + { + input: 'apps/auth-service/src/utils/email-templates', + glob: '**/*', + output: 'email-templates', + }, + ], // ensure email templates ship with the build output optimization: false, outputHashing: 'none', generatePackageJson: true, diff --git a/apps/order-service/src/utils/send-email/index.ts b/apps/order-service/src/utils/send-email/index.ts index dc7262b..ffea84a 100644 --- a/apps/order-service/src/utils/send-email/index.ts +++ b/apps/order-service/src/utils/send-email/index.ts @@ -2,6 +2,7 @@ import dotenv from 'dotenv'; import ejs from 'ejs'; import nodemail from 'nodemailer'; import path from 'path'; +import { existsSync } from 'node:fs'; dotenv.config(); @@ -17,15 +18,25 @@ const transporter = nodemail.createTransport({ //Render an EJS email template const renderEmailTemplate = async (templateName: string, data: Record): Promise => { - const templatePath = path.join( - process.cwd(), - 'apps', - 'auth-service', - 'src', - 'utils', - 'email-templates', - `${templateName}.ejs`, - ); + const templateCandidates = [ + path.join(__dirname, 'email-templates', `${templateName}.ejs`), + path.join(__dirname, '..', 'email-templates', `${templateName}.ejs`), + path.join( + process.cwd(), + 'apps', + 'order-service', + 'src', + 'utils', + 'email-templates', + `${templateName}.ejs`, + ), + ]; + + const templatePath = templateCandidates.find((candidate) => existsSync(candidate)); + + if (!templatePath) { + throw new Error(`Email template ${templateName} not found`); + } return ejs.renderFile(templatePath, data); }; diff --git a/apps/order-service/webpack.config.js b/apps/order-service/webpack.config.js index dc0730e..624f21f 100644 --- a/apps/order-service/webpack.config.js +++ b/apps/order-service/webpack.config.js @@ -17,7 +17,14 @@ module.exports = { compiler: 'tsc', main: './src/main.ts', tsConfig: './tsconfig.app.json', - assets: ['./src/assets'], + assets: [ + './src/assets', + { + input: 'apps/order-service/src/utils/email-templates', + glob: '**/*', + output: 'email-templates', + }, + ], optimization: false, outputHashing: 'none', generatePackageJson: true,