Skip to content

Commit

Permalink
Merge pull request #447 from boostcampwm2023/BE-unreadChat-#442
Browse files Browse the repository at this point in the history
[BE/#442] /chat/unread API 구현
  • Loading branch information
koomin1227 authored Dec 10, 2023
2 parents f572d63 + 6d9159f commit 4f44135
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
6 changes: 6 additions & 0 deletions BE/src/chat/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export class ChatController {
}
}

@Get('unread')
@UseGuards(AuthGuard)
async unreadChat(@UserHash() userId: string) {
return await this.chatService.unreadChat(userId);
}

@Get()
async testPush(@Body() body) {
await this.fcmHandler.sendPush(body.user, {
Expand Down
65 changes: 60 additions & 5 deletions BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class ChatService {
'post.thumbnail as post_thumbnail',
'chat_info.create_date as last_chat_date',
'chat_info.message as last_chat',
'chat_info.is_read as allread',
'chat_info.is_read as all_read',
'chat_info.sender as sender',
])
.where('chat_room.writer = :userId', { userId: userId })
Expand All @@ -134,13 +134,13 @@ export class ChatService {
: cur.user_profile_img;

if (cur.sender === userId) {
cur.allread = true;
cur.all_read = true;
} else {
if (cur.allread === 0) {
if (cur.all_read === 0) {
chatListInfo.all_read = false;
cur.allread = false;
cur.all_read = false;
} else {
cur.allread = true;
cur.all_read = true;
}
}
delete cur.sender;
Expand All @@ -151,6 +151,61 @@ export class ChatService {
return chatListInfo;
}

async unreadChat(userId: string) {
const subquery = this.chatRepository
.createQueryBuilder('chat')
.select('chat.id', 'id')
.addSelect('chat.chat_room', 'chat_room')
.addSelect('chat.message', 'message')
.addSelect('chat.create_date', 'create_date')
.addSelect('chat.is_read', 'is_read')
.addSelect('chat.sender', 'sender')
.where(
'chat.id IN (SELECT MAX(chat.id) FROM chat GROUP BY chat.chat_room)',
);

const rooms = await this.chatRoomRepository
.createQueryBuilder('chat_room')
.innerJoin(
'(' + subquery.getQuery() + ')',
'chat_info',
'chat_room.id = chat_info.chat_room',
)
.leftJoin(
'chat_room.writerUser',
'writer',
'chat_room.writerUser = writer.user_hash',
)
.leftJoin(
'chat_room.userUser',
'user',
'chat_room.userUser = user.user_hash',
)
.select([
'chat_room.id as room_id',
'chat_room.writer as writer',
'writer.nickname as writer_nickname',
'writer.profile_img as writer_profile_img',
'chat_room.user as user',
'user.nickname as user_nickname',
'user.profile_img as user_profile_img',
'chat_room.post_id as post_id',
'chat_info.create_date as last_chat_date',
'chat_info.message as last_chat',
'chat_info.is_read as is_read',
'chat_info.sender as sender',
])
.getRawMany();

for (let room of rooms) {
if (room.sender !== userId && room.is_read === 0) {
return { all_read: false };
}
}

return { all_read: true };
}

async findRoomById(roomId: number, userId: string) {
await this.chatRepository
.createQueryBuilder('chat')
Expand Down

0 comments on commit 4f44135

Please sign in to comment.