Skip to content

Commit

Permalink
[deliver #187584937] buyer track order status
Browse files Browse the repository at this point in the history
  • Loading branch information
solangeihirwe03 committed Jul 16, 2024
1 parent 0fddfff commit cb1e7b0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions src/helpers/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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";
import productRepository from "../modules/product/repositories/productRepositories";
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();

Expand All @@ -24,6 +26,13 @@ const saveAndEmitNotification = async (userId: string, message: string, event: s
await sendEmailNotification(userId, message);
};

const fetchOrderWithCarts = async (orderId: string): Promise<IOrderWithCart> => {
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;
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 18 additions & 1 deletion src/services/sendEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,21 @@ const sendEmailNotification = async (userId: string, message: string) => {
}
};

export { sendEmail, transporter, sendEmailNotification };
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 };
4 changes: 4 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,8 @@ export interface SellerRequestAttribute {
requestStatus: string;
createdAt?: Date;
updatedAt?: Date;
}

export interface IOrderWithCart extends OrderAttributes {
carts?: CartAttributes;
}

0 comments on commit cb1e7b0

Please sign in to comment.