Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat-add-notification-sound #127

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
};
Loading