From d15f5e79190a89a7ad06a97548ed8e719a039787 Mon Sep 17 00:00:00 2001 From: favor-star Date: Mon, 1 Jul 2024 14:54:42 +0200 Subject: [PATCH] Added more hooks to manage notifications for products lifecycle --- src/controllers/productsController.ts | 28 ++++++++++++++++++- src/helpers/send-email.ts | 40 ++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/controllers/productsController.ts b/src/controllers/productsController.ts index f33ebc03..b40d2614 100644 --- a/src/controllers/productsController.ts +++ b/src/controllers/productsController.ts @@ -358,7 +358,33 @@ Product.afterCreate(async product => { } sendEmail('added_product_notification', { email: user.email, name: user.firstName }); }); - +Product.afterUpdate(async product => { + const notification = await Notification.create({ + message: `Product called: ${product.name} was updated successfully`, + isRead: false, + userId: product.sellerId, + }); + const user = await User.findOne({ + where: { id: product.sellerId }, + attributes: ['email', 'firstName', 'lastName'], + }); + if (!user) { + return Promise.reject(new Error("User cannot be found! So the email won't be send successfully")); + } + sendEmail('updated_product_notification', { email: user.email, name: user.firstName }); +}); +Product.afterDestroy(async product => { + const notification = await Notification.create({ + message: 'Product was deleted successfully', + isRead: false, + userId: product.sellerId, + }); + const user = await User.findOne({ where: { id: product.sellerId }, attributes: ['email', 'firstName', 'lastName'] }); + if (!user) { + return Promise.reject(new Error("User can't be found, so the email won't be sent successfully")); + } + sendEmail('deleted_product_notification', { email: user.email, name: user.firstName }); +}); // Review a product (feedback + rating) export const provideReviewToProduct = async (req: Request, res: Response) => { try { diff --git a/src/helpers/send-email.ts b/src/helpers/send-email.ts index eed9e95f..c1358943 100644 --- a/src/helpers/send-email.ts +++ b/src/helpers/send-email.ts @@ -232,7 +232,7 @@ export const sendEmail = async (type: string, data: IData) => { button: { color: '#22BC66', text: 'View on the platform', - link: 'https://e-commerce-mavericks.com/login', + link: process.env.URL_HOST as string, }, }, outro: 'Thank you for working with us. If you need any help, please free to contact us!', @@ -241,6 +241,44 @@ export const sendEmail = async (type: string, data: IData) => { mailOptions.subject = 'Product added successfully'; mailOptions.html = mailGenerator.generate(email); break; + case 'updated_product_notification': + email = { + body: { + name: data.name, + intro: `Your product has been updated successfully!`, + action: { + instructions: 'To view your product on platform, Click here:', + button: { + color: '#22BC66', + text: 'View on the platform', + link: process.env.URL_HOST as string, + }, + }, + outro: 'Thank you for working with us. If you need any help, please free to contact us!', + }, + }; + mailOptions.subject = 'Product updated successfully'; + mailOptions.html = mailGenerator.generate(email); + break; + case 'deleted_product_notification': + email = { + body: { + name: data.name, + intro: `Your product has been deleted successfully!`, + action: { + instructions: 'To view your other products on platform, Click here:', + button: { + color: '#22BC66', + text: 'View on the platform', + link: process.env.URL_HOST as string, + }, + }, + outro: 'Thank you for working with us. If you need any help, please free to contact us!', + }, + }; + mailOptions.subject = 'Product deleted successfully'; + mailOptions.html = mailGenerator.generate(email); + break; case 'password_prompt': email = {