Skip to content

Commit

Permalink
Merge pull request #127 from atlp-rwanda/feat-add-notification-sound
Browse files Browse the repository at this point in the history
feat-add-notification-sound
  • Loading branch information
faid-terence authored Jul 24, 2024
2 parents 4cefa3a + cad3134 commit a465b74
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 69 deletions.
30 changes: 15 additions & 15 deletions src/services/orderServices/updateOrderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,41 @@ 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}`,
});
});
}

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}`,
});
});

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
111 changes: 57 additions & 54 deletions src/utils/sendNotification.ts
Original file line number Diff line number Diff line change
@@ -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);
}
};

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);
}
};

0 comments on commit a465b74

Please sign in to comment.