Skip to content

Commit

Permalink
ADD: notification on add friend (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoubessabiry committed Oct 20, 2023
2 parents 9d69fbe + 48ffa01 commit 9b92410
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions backend/code/prisma/dbml/schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ Table room_members {
}
}

Table notifications {
createdAt DateTime [default: `now()`, unique, not null]
id String [pk]
recipientId String [not null]
content NotifType [not null]
is_read Boolean [not null, default: false]
readAt DateTime
}

Enum NotifType {
addFriend
}

Enum RoomType {
public
private
Expand Down
17 changes: 17 additions & 0 deletions backend/code/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ model RoomMember {
@@map("room_members")
}

model Notification {
createdAt DateTime @default(now())
id String @id @default(cuid())
recipientId String
content NotifType
is_read Boolean @default(false)
readAt DateTime?
@@map("notifications")
@@unique([createdAt])
}

enum NotifType {
addFriend
}

enum RoomType {
public
private
Expand Down
9 changes: 9 additions & 0 deletions backend/code/src/friends/friends.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions backend/code/src/gateways/gateways.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Gateways implements OnGatewayConnection {
client.join(`Romm:${room.room.id}`);
});
});
client.join(`notif:${userId}`);
}

@WebSocketServer() private server: Server;
Expand All @@ -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);
}
}

0 comments on commit 9b92410

Please sign in to comment.