Skip to content

Commit

Permalink
Added more hooks to manage notifications for products lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Favor-star committed Jul 1, 2024
1 parent 741d543 commit d15f5e7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/controllers/productsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 39 additions & 1 deletion src/helpers/send-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!',
Expand All @@ -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 = {
Expand Down

0 comments on commit d15f5e7

Please sign in to comment.