Skip to content

Commit

Permalink
Feat-Notification-Management
Browse files Browse the repository at this point in the history
  • Loading branch information
Calebgisa72 committed May 31, 2024
1 parent 89ac0c5 commit 150e1a5
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/controllers/notificarionControllers.ts
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);
};
62 changes: 62 additions & 0 deletions src/entities/Notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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')
total: number = 0;

@Column('decimal')
unRead: number = 0;

@CreateDateColumn()
createdAt!: Date;

@UpdateDateColumn()
updatedAt!: Date;

updateTotal (): void {
if (this.allNotifications) {
let total: number = 0;
for (let i = 0; i < this.allNotifications.length; i++) {
total += 1;
}
this.total = total;
} else {
this.total = 0;
}
}
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;
}
}
}
47 changes: 47 additions & 0 deletions src/entities/NotificationItem.ts
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;
}
9 changes: 9 additions & 0 deletions src/routes/NoficationRoutes.ts
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;
2 changes: 2 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import productRoutes from './ProductRoutes';
import wishListRoutes from './wishListRoute';
import couponRoute from './couponRoutes';
import cartRoutes from './CartRoutes';
import notificationRoute from './NoficationRoutes'

const router = Router();

Expand All @@ -17,5 +18,6 @@ router.use('/product', productRoutes);
router.use('/wish-list', wishListRoutes);
router.use('/cart', cartRoutes);
router.use('/coupons', couponRoute);
router.use('/notification', notificationRoute);

export default router;
4 changes: 4 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ export * from './vendorOrderServices/updateVendorOrder';
// vendor order management
export * from './adminOrderServices/readOrder';
export * from './adminOrderServices/updateOrder';

// Nofication management
export * from './notificationServices/getNotifications'
export * from './notificationServices/deleteNotification'
9 changes: 9 additions & 0 deletions src/services/notificationServices/deleteNotification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Request, Response } from 'express';
import { User } from '../../entities/User';
import { Notification } from '../../entities/Notification';
import { NotificationItem } from '../../entities/NotificationItem';
import { getRepository } from 'typeorm';

export const deleteNotificationService = async(req: Request, res: Response) => {

}
8 changes: 8 additions & 0 deletions src/services/notificationServices/getNotifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Request, Response } from 'express';
import { User } from '../../entities/User';
import { Notification } from '../../entities/Notification';
import { getRepository } from 'typeorm';

export const getNotificationsService = async(req: Request, res: Response) => {

}
13 changes: 13 additions & 0 deletions src/utils/sendNotification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Notification } from "../entities/Notification";
import { NotificationItem } from "../entities/NotificationItem";
import { getRepository } from 'typeorm';

interface noticationInfo{
content: string;
type: string;
link?: string;
}

export const sendNotification = (data: noticationInfo) =>{

}

0 comments on commit 150e1a5

Please sign in to comment.