diff --git a/src/services/orderServices/updateOrderService.ts b/src/services/orderServices/updateOrderService.ts index 132870c..c747c85 100644 --- a/src/services/orderServices/updateOrderService.ts +++ b/src/services/orderServices/updateOrderService.ts @@ -65,20 +65,20 @@ export const updateOrderService = async (req: Request, res: Response) => { // Save updated order status await orderRepository.save(order); - + if (orderStatus === 'received') { const admins = await getRepository(User).find({ where: { - role: 'ADMIN' - } + role: 'ADMIN', + }, }); - - admins.forEach( async (admin) => { + + admins.forEach(async admin => { await sendNotification({ content: `The Buyer named "${order.buyer.firstName} ${order.buyer.lastName}", has confirmed that they have successfully received their order.`, type: 'order', user: admin, - link: `/admin/dashboard/products/${order.id}` + link: `/admin/dashboard/orders/${order.id}`, }); }); } @@ -86,20 +86,20 @@ export const updateOrderService = async (req: Request, res: Response) => { const vendorOrders = await getRepository(VendorOrders).find({ where: { order: { - id: order.id - } + id: order.id, + }, }, relations: { - vendor: true - } + vendor: true, + }, }); - vendorOrders.forEach(async (vendorOrder) => { + vendorOrders.forEach(async vendorOrder => { await sendNotification({ content: `The Buyer named "${order.buyer.firstName} ${order.buyer.lastName}", has marked their order as "${orderStatus}". Please ensure that you update the order status on your side as well.`, type: 'order', user: vendorOrder.vendor, - link: `/vendor/dashboard/orders/${vendorOrder.id}` + link: `/vendor/dashboard/orders/${vendorOrder.id}`, }); }); @@ -133,7 +133,7 @@ export const updateOrderService = async (req: Request, res: Response) => { } }; -async function processRefund (order: Order, entityManager: EntityManager) { +async function processRefund(order: Order, entityManager: EntityManager) { const buyer = order.buyer; // Refund buyer @@ -161,6 +161,6 @@ async function processRefund (order: Order, entityManager: EntityManager) { order.quantity = 0; } -function isOrderFinalStatus (status: string): boolean { +function isOrderFinalStatus(status: string): boolean { return ['cancelled', 'delivered', 'returned', 'completed'].includes(status); -} \ No newline at end of file +} diff --git a/src/utils/sendNotification.ts b/src/utils/sendNotification.ts index de46d32..e7de176 100644 --- a/src/utils/sendNotification.ts +++ b/src/utils/sendNotification.ts @@ -1,58 +1,61 @@ -import { Notification } from "../entities/Notification"; -import { NotificationItem } from "../entities/NotificationItem"; +import { Notification } from '../entities/Notification'; +import { NotificationItem } from '../entities/NotificationItem'; import { getRepository } from 'typeorm'; -import { User } from "../entities/User"; -import { getIO } from "./socket"; -import { getNotifications } from "./getNotifications"; - -interface noticationInfo{ - content: string; - type: 'product'|'cart'|'order'|'user'|'wish list'|'coupon'; - user: User; - link?: string; +import { User } from '../entities/User'; +import { getIO } from './socket'; +import { getNotifications } from './getNotifications'; + +interface noticationInfo { + content: string; + type: 'product' | 'cart' | 'order' | 'user' | 'wishlist' | 'coupon'; + user: User; + link?: string; } -export const sendNotification = async (data: noticationInfo) =>{ - try { - const notificationRepo = getRepository(Notification) - const notificationItemRepo = getRepository(NotificationItem); - - let notification = await notificationRepo - .findOne({ - where: { - user: {id: data.user.id}}, - relations: ['allNotifications', 'user'] - }); - - if(!notification){ - notification = new Notification(); - notification.user = data.user; - await notificationRepo.save(notification); - } - - const notificationItem = new NotificationItem(); - notificationItem.notification = notification; - notificationItem.content = data.content; - notificationItem.type = data.type; - if(data.link){ - notificationItem.link = data.link - } - await notificationItemRepo.save(notificationItem); - - //Update numbers - notification = await notificationRepo - .findOne({where: {id: notification.id, user: {id: data.user.id}}, relations: ['allNotifications', 'user'] }); - - if(notification){ - notification.updateUnread(); - await notificationRepo.save(notification); - } - - getIO().emit('notification', { - action: `${data.user.email} notification`, - notifications: await getNotifications(data.user.id) - }); - } catch (error) { - console.log(error); +export const sendNotification = async (data: noticationInfo) => { + try { + const notificationRepo = getRepository(Notification); + const notificationItemRepo = getRepository(NotificationItem); + + let notification = await notificationRepo.findOne({ + where: { + user: { id: data.user.id }, + }, + relations: ['allNotifications', 'user'], + }); + + if (!notification) { + notification = new Notification(); + notification.user = data.user; + await notificationRepo.save(notification); } -}; \ No newline at end of file + + const notificationItem = new NotificationItem(); + notificationItem.notification = notification; + notificationItem.content = data.content; + notificationItem.type = data.type; + if (data.link) { + notificationItem.link = data.link; + } + await notificationItemRepo.save(notificationItem); + + //Update numbers + notification = await notificationRepo.findOne({ + where: { id: notification.id, user: { id: data.user.id } }, + relations: ['allNotifications', 'user'], + }); + + if (notification) { + notification.updateUnread(); + await notificationRepo.save(notification); + } + + getIO().emit('notification', { + action: `${data.user.email} notification`, + sound: true, + notifications: await getNotifications(data.user.id), + }); + } catch (error) { + console.log(error); + } +};