Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: notifaction table #60

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading