From 719045273488a7e7438541df5f066f1575fac8fb Mon Sep 17 00:00:00 2001 From: Solange Duhimbaze Ihirwe <159579750+solangeihirwe03@users.noreply.github.com> Date: Wed, 26 Jun 2024 21:02:56 +0200 Subject: [PATCH] [deliver #187584937] buyer track order status --- package-lock.json | 2 +- src/helpers/notifications.ts | 22 ++++++++++++++++++++-- src/services/sendEmail.ts | 19 ++++++++++++++++++- src/types/index.d.ts | 4 ++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 394a78d5..d7ea0ce6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11382,4 +11382,4 @@ } } } -} \ No newline at end of file +} diff --git a/src/helpers/notifications.ts b/src/helpers/notifications.ts index 10746123..f9b6259c 100644 --- a/src/helpers/notifications.ts +++ b/src/helpers/notifications.ts @@ -1,4 +1,4 @@ -import { sendEmailNotification } from "../services/sendEmail"; +import { sendEmailNotification, sendEmailOrderStatus } from "../services/sendEmail"; import userRepositories from "../modules/user/repository/userRepositories"; import { EventEmitter } from "events"; import cron from "node-cron"; @@ -6,8 +6,10 @@ import productRepository from "../modules/product/repositories/productRepositori import Products from "../databases/models/products"; import Shops from "../databases/models/shops"; import Users from "../databases/models/users"; -import { IProductsWithShop } from "../types/index"; +import { IProductsWithShop, IOrderWithCart } from "../types/index"; import { io } from "../index"; +import Orders from "../databases/models/orders"; +import Carts from "../databases/models/carts"; export const eventEmitter = new EventEmitter(); @@ -24,6 +26,13 @@ const saveAndEmitNotification = async (userId: string, message: string, event: s await sendEmailNotification(userId, message); }; +const fetchOrderWithCarts = async (orderId: string): Promise => { + return (await Orders.findOne({ + where: { id: orderId }, + include: { model: Carts, as: "carts" } + })) as IOrderWithCart; +}; + eventEmitter.on("productAdded", async (product) => { const productWithShop = await fetchProductWithShop(product.id); const userId = productWithShop.shops.userId; @@ -74,6 +83,15 @@ eventEmitter.on("passwordExpiry", async ({ userId, message }) => { await saveAndEmitNotification(userId, message, "passwordExpiry"); }); +eventEmitter.on('orderStatusUpdated', async (order) => { + const orderStatus = await fetchOrderWithCarts(order.id) + const userId = orderStatus.carts.userId + const message = `The order that was created on ${order.orderDate} status has been updated to ${order.status}.`; + await userRepositories.addNotification(userId, message); + await sendEmailOrderStatus(userId, message); + io.to(userId).emit('orderStatusUpdated', message) +}); + cron.schedule("0 0 * * *", async () => { const users = await Users.findAll(); for (const user of users) { diff --git a/src/services/sendEmail.ts b/src/services/sendEmail.ts index 9a44bdad..a7005fee 100644 --- a/src/services/sendEmail.ts +++ b/src/services/sendEmail.ts @@ -45,4 +45,21 @@ const sendEmailNotification = async (userId: string, message: string) => { } }; -export { sendEmail, transporter, sendEmailNotification }; \ No newline at end of file +const sendEmailOrderStatus = async (userId: string, message: string) => { + try { + const user = await authRepository.findUserByAttributes("id", userId); + const mailOptions: SendMailOptions = { + from: process.env.MAIL_ID, + to: user.email, + subject: "Order status", + text: message + }; + + await transporter.sendMail(mailOptions); + + } catch (error) { + throw new Error(error); + } +}; + +export { sendEmail, transporter, sendEmailNotification, sendEmailOrderStatus }; \ No newline at end of file diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 0aeb0acf..0a582a93 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -125,4 +125,8 @@ export interface SellerRequestAttribute { requestStatus: string; createdAt?: Date; updatedAt?: Date; +} + +export interface IOrderWithCart extends OrderAttributes { + carts?: CartAttributes; } \ No newline at end of file