-
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.
- Loading branch information
1 parent
89ac0c5
commit c60805f
Showing
10 changed files
with
273 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Request, Response } from 'express'; | ||
import { getNotificationsService, deleteNotificationService } from '../services'; | ||
|
||
export const getAllNotifications = async (req: Request, res: Response) => { | ||
await getNotificationsService(req, res); | ||
}; | ||
|
||
export const deleteNotification = async (req: Request, res: Response) => { | ||
await deleteNotificationService(req, res); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
ManyToOne, | ||
OneToMany, | ||
CreateDateColumn, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
import { IsNotEmpty } from 'class-validator'; | ||
import { User } from './User'; | ||
import { NotificationItem } from './NotificationItem'; | ||
|
||
@Entity() | ||
export class Notification { | ||
@PrimaryGeneratedColumn('uuid') | ||
@IsNotEmpty() | ||
id!: string; | ||
|
||
@ManyToOne(() => User) | ||
user!: User; | ||
|
||
@OneToMany(() => NotificationItem, notificationItem => notificationItem.notification) | ||
allNotifications!: NotificationItem[]; | ||
|
||
@Column('decimal') | ||
unRead: number = 0; | ||
|
||
@CreateDateColumn() | ||
createdAt!: Date; | ||
|
||
@UpdateDateColumn() | ||
updatedAt!: Date; | ||
|
||
updateUnread (): void { | ||
if (this.allNotifications) { | ||
let unRead: number = 0; | ||
for (let i = 0; i < this.allNotifications.length; i++) { | ||
if(this.allNotifications[i].isRead === false){ | ||
unRead +=1 | ||
} | ||
} | ||
this.unRead = unRead; | ||
} else { | ||
this.unRead = 0; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Column, | ||
ManyToOne, | ||
CreateDateColumn, | ||
} from 'typeorm'; | ||
import { IsNotEmpty, IsIn, IsBoolean } from 'class-validator'; | ||
import { Notification } from './Notification'; | ||
|
||
@Entity() | ||
export class NotificationItem{ | ||
@PrimaryGeneratedColumn('uuid') | ||
@IsNotEmpty() | ||
id!: string; | ||
|
||
@ManyToOne(() => Notification, nofication => nofication.allNotifications, { onDelete: 'CASCADE' }) | ||
@IsNotEmpty() | ||
notification!: Notification; | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
content!: string | ||
|
||
@Column() | ||
@IsNotEmpty() | ||
@IsIn([ | ||
'product', | ||
'cart', | ||
'order', | ||
'user', | ||
'wish list', | ||
'coupon', | ||
]) | ||
type!: string | ||
|
||
@Column({ default: false }) | ||
@IsNotEmpty() | ||
@IsBoolean() | ||
isRead!: boolean | ||
|
||
@Column({ nullable: true }) | ||
link!: string | ||
|
||
@CreateDateColumn() | ||
createdAt!: Date; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { RequestHandler, Router } from 'express'; | ||
import { authMiddleware } from '../middlewares/verifyToken'; | ||
import {getAllNotifications, deleteNotification} from '../controllers/notificarionControllers' | ||
|
||
const router = Router(); | ||
router.get('/', authMiddleware as RequestHandler, getAllNotifications); | ||
router.delete('/:id', authMiddleware as RequestHandler, deleteNotification); | ||
|
||
export default router; |
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Request, Response } from 'express'; | ||
import { Notification } from '../../entities/Notification'; | ||
import { NotificationItem } from '../../entities/NotificationItem'; | ||
import { responseSuccess, responseError } from '../../utils/response.utils'; | ||
import { getRepository } from 'typeorm'; | ||
|
||
export const deleteNotificationService = async(req: Request, res: Response) => { | ||
try { | ||
if (!req.params.id) { | ||
return responseError(res, 400, 'Notification id is required'); | ||
} | ||
const notificationRepo = getRepository(Notification); | ||
const notificationItemRepo = getRepository(NotificationItem); | ||
|
||
const notificationItem = await notificationItemRepo | ||
.findOne({where: {id: req.params.id}, relations: ['notification']}); | ||
|
||
if(!notificationItem){ | ||
return responseError(res, 404, 'Notification not found'); | ||
} | ||
|
||
if(notificationItem.notification.user.id !== req.user?.id){ | ||
return responseError(res, 401, "You are not authorized to perform this"); | ||
} | ||
|
||
await notificationItemRepo.remove(notificationItem as NotificationItem); | ||
|
||
const notification = await notificationRepo | ||
.findOne({where: {user: { id: req.user?.id }}, relations: ['allNotifications', 'user'] }); | ||
|
||
if(notification){ | ||
notification.updateUnread(); | ||
await notificationRepo.save(notification); | ||
|
||
const notificationDeatils = { | ||
id: notification.id, | ||
user: { | ||
id: notification.user.id, | ||
firstName: notification.user.firstName, | ||
lastName: notification.user.lastName | ||
}, | ||
notifications: notification.allNotifications, | ||
unRead: notification.unRead | ||
} | ||
|
||
return responseSuccess(res, 200, 'Notifications deleted successfully', { notificationDeatils }); | ||
} | ||
|
||
} catch (error) { | ||
return responseError(res, 400, (error as Error).message); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Request, Response } from 'express'; | ||
import { Notification } from '../../entities/Notification'; | ||
import { responseSuccess, responseError } from '../../utils/response.utils'; | ||
import { getRepository } from 'typeorm'; | ||
|
||
export const getNotificationsService = async(req: Request, res: Response) => { | ||
try { | ||
const notificationRepo = getRepository(Notification); | ||
|
||
const notification = await notificationRepo | ||
.findOne({where: {user: { id: req.user?.id }}, relations: ['user','allNotifications'], | ||
order: { | ||
createdAt: 'DESC', | ||
}, | ||
}) | ||
|
||
if(!notification){ | ||
return responseSuccess(res, 200, `No notifications`, { notifications: [] }); | ||
} | ||
|
||
const notificationDeatils = { | ||
id: notification.id, | ||
user: { | ||
id: notification.user.id, | ||
firstName: notification.user.firstName, | ||
lastName: notification.user.lastName | ||
}, | ||
notifications: notification.allNotifications, | ||
unRead: notification.unRead | ||
} | ||
|
||
return responseSuccess(res, 200, 'Notifications retrieved successfully', { notificationDeatils }); | ||
} catch (error) { | ||
return responseError(res, 400, (error as Error).message); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Notification } from "../entities/Notification"; | ||
import { NotificationItem } from "../entities/NotificationItem"; | ||
import { getRepository } from 'typeorm'; | ||
import { User } from "../entities/User"; | ||
|
||
interface noticationInfo{ | ||
content: string; | ||
type: 'product'|'cart'|'order'|'user'|'wish list'|'coupon'; | ||
user: User; | ||
link?: string; | ||
} | ||
|
||
export const sendNotification = async(data: noticationInfo) =>{ | ||
try { | ||
const notificationRepo = getRepository(Notification) | ||
const notiificationItemRepo = 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 notiificationItemRepo.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); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} |