From 58d4bea38240eeafe46977f252b569cdb987004d Mon Sep 17 00:00:00 2001 From: Ayoub Essabiry Date: Thu, 19 Oct 2023 18:53:51 +0100 Subject: [PATCH 1/2] ADD: notifaction table --- backend/code/prisma/dbml/schema.dbml | 9 +++++++++ backend/code/prisma/schema.prisma | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/backend/code/prisma/dbml/schema.dbml b/backend/code/prisma/dbml/schema.dbml index 92079d3..77d256e 100644 --- a/backend/code/prisma/dbml/schema.dbml +++ b/backend/code/prisma/dbml/schema.dbml @@ -106,6 +106,15 @@ Table room_members { } } +Table notifications { + createdAt DateTime [default: `now()`, not null] + id String [pk] + recipientId String [not null] + content String [not null] + is_read Boolean [not null, default: false] + readAt DateTime +} + Enum RoomType { public private diff --git a/backend/code/prisma/schema.prisma b/backend/code/prisma/schema.prisma index dbbadf8..d9e541f 100644 --- a/backend/code/prisma/schema.prisma +++ b/backend/code/prisma/schema.prisma @@ -136,6 +136,18 @@ model RoomMember { @@map("room_members") } +model Notification { + createdAt DateTime @default(now()) + + id String @id + recipientId String + content String + is_read Boolean @default(false) + readAt DateTime? + + @@map("notifications") +} + enum RoomType { public private From 48ffa0109c4acadfc1ea70d2c057a6f6fdc5391e Mon Sep 17 00:00:00 2001 From: ayoubessabiry Date: Fri, 20 Oct 2023 14:42:49 +0100 Subject: [PATCH 2/2] ADD: notification on add friend --- backend/code/prisma/dbml/schema.dbml | 8 ++++++-- backend/code/prisma/schema.prisma | 9 +++++++-- backend/code/src/friends/friends.service.ts | 9 +++++++++ backend/code/src/gateways/gateways.gateway.ts | 6 ++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/backend/code/prisma/dbml/schema.dbml b/backend/code/prisma/dbml/schema.dbml index 77d256e..76ee46e 100644 --- a/backend/code/prisma/dbml/schema.dbml +++ b/backend/code/prisma/dbml/schema.dbml @@ -107,14 +107,18 @@ Table room_members { } Table notifications { - createdAt DateTime [default: `now()`, not null] + createdAt DateTime [default: `now()`, unique, not null] id String [pk] recipientId String [not null] - content String [not null] + content NotifType [not null] is_read Boolean [not null, default: false] readAt DateTime } +Enum NotifType { + addFriend +} + Enum RoomType { public private diff --git a/backend/code/prisma/schema.prisma b/backend/code/prisma/schema.prisma index d9e541f..d116cd5 100644 --- a/backend/code/prisma/schema.prisma +++ b/backend/code/prisma/schema.prisma @@ -139,13 +139,18 @@ model RoomMember { model Notification { createdAt DateTime @default(now()) - id String @id + id String @id @default(cuid()) recipientId String - content String + content NotifType is_read Boolean @default(false) readAt DateTime? @@map("notifications") + @@unique([createdAt]) +} + +enum NotifType { + addFriend } enum RoomType { diff --git a/backend/code/src/friends/friends.service.ts b/backend/code/src/friends/friends.service.ts index 549aeef..384be90 100644 --- a/backend/code/src/friends/friends.service.ts +++ b/backend/code/src/friends/friends.service.ts @@ -2,12 +2,14 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { PrismaService } from 'src/prisma/prisma.service'; import { UsersService } from 'src/users/users.service'; import { FriendResponseDto } from './dto/frined-response.dto'; +import { EventEmitter2 } from '@nestjs/event-emitter'; @Injectable() export class FriendsService { constructor( private readonly prisma: PrismaService, private readonly usersService: UsersService, + private evenEmitter: EventEmitter2, ) {} async addFriend(userId: string, friendId: string) { @@ -34,6 +36,13 @@ export class FriendsService { }, update: {}, }); + const notifData = await this.prisma.notification.create({ + data: { + recipientId: friendId, + content: 'addFriend', + }, + }); + this.evenEmitter.emit('addFriendNotif', notifData); return new FriendResponseDto(frinedship); } diff --git a/backend/code/src/gateways/gateways.gateway.ts b/backend/code/src/gateways/gateways.gateway.ts index 70740ee..16154f4 100644 --- a/backend/code/src/gateways/gateways.gateway.ts +++ b/backend/code/src/gateways/gateways.gateway.ts @@ -36,6 +36,7 @@ export class Gateways implements OnGatewayConnection { client.join(`Romm:${room.room.id}`); }); }); + client.join(`notif:${userId}`); } @WebSocketServer() private server: Server; @@ -45,4 +46,9 @@ export class Gateways implements OnGatewayConnection { const chanellname: string = `Romm:${message.roomId}`; this.server.to(chanellname).emit('message', message); } + @OnEvent('addFriendNotif') + sendFriendReq(notif: any) { + const channellname: string = `notif:${notif.recipientId}`; + this.server.to(channellname).emit('message', notif); + } }