-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from atlp-rwanda/feat-add-notification-sound
feat-add-notification-sound
- Loading branch information
Showing
2 changed files
with
72 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |