From 194f11052ed170ff3f81c6dadd28b90ec3d1841a Mon Sep 17 00:00:00 2001 From: JeanIrad Date: Tue, 30 Jul 2024 10:10:09 +0200 Subject: [PATCH] notifying user upon changing role --- src/controllers/userController.ts | 3 +++ ...65312-addingExpectedDeliveryDateToOrder.js | 16 +++++++++++++ src/helpers/send-email.ts | 23 ++++++++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/database/migrations/20240723165312-addingExpectedDeliveryDateToOrder.js diff --git a/src/controllers/userController.ts b/src/controllers/userController.ts index 1e2bf9fe..5b91bd4d 100644 --- a/src/controllers/userController.ts +++ b/src/controllers/userController.ts @@ -202,7 +202,10 @@ export const editUserRole = async (req: Request, res: Response) => { await user.update({ RoleId: roleId }, { transaction }); await transaction.commit(); + const name = user.firstName + ' ' + user.lastName; + const email = user.email; + await sendEmail('role', { name, email, role: role.name }); res.status(200).json({ ok: true, message: 'Role assigned successfully.' }); } catch (error) { await transaction.rollback(); diff --git a/src/database/migrations/20240723165312-addingExpectedDeliveryDateToOrder.js b/src/database/migrations/20240723165312-addingExpectedDeliveryDateToOrder.js new file mode 100644 index 00000000..f3ccf326 --- /dev/null +++ b/src/database/migrations/20240723165312-addingExpectedDeliveryDateToOrder.js @@ -0,0 +1,16 @@ +'use strict'; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.addColumn('orders', 'expectedDeliveryDate', { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Date.now(), + }); + }, + + async down(queryInterface, Sequelize) { + await queryInterface.removeColumn('orders', 'expectedDeliveryDate'); + }, +}; diff --git a/src/helpers/send-email.ts b/src/helpers/send-email.ts index c1358943..c7bfb96b 100644 --- a/src/helpers/send-email.ts +++ b/src/helpers/send-email.ts @@ -12,6 +12,7 @@ interface IData { link?: string; otp?: string; productName?: string; + role?: string; } interface EmailContent { name: string; @@ -53,6 +54,26 @@ export const sendEmail = async (type: string, data: IData) => { let email; switch (type) { + case 'role': + email = { + body: { + name: data.name, + intro: 'Your role has been changed up on your request!', + action: { + instructions: `You have requested to change the role to ${data.role}!`, + button: { + color: '#000c24', + text: 'Revert back the Action', + link: 'https://e-commerce-mavericks-fn.netlify.app', + }, + }, + outro: 'Ignore this email, if you did not request that!', + }, + }; + mailOptions.subject = 'Account Role Changed'; + mailOptions.html = mailGenerator.generate(email); + break; + case 'account_verify': email = { body: { @@ -105,7 +126,7 @@ export const sendEmail = async (type: string, data: IData) => { button: { color: '#22BC66', text: 'Access Account', - link: 'https://e-commerce-mavericks.com/login', + link: 'https://e-commerce-mavericks-fn.netlify.app/login', }, }, outro: 'If you did not request this action, please contact support immediately.',