From 130cc5616df787ede4732c9635a358a2581bcb4f Mon Sep 17 00:00:00 2001 From: kyeah <39901387+dmobtxx19@users.noreply.github.com> Date: Sun, 19 Nov 2023 01:42:37 +0900 Subject: [PATCH] fix: getChatRoom with deleted user --- src/chat/chat.service.ts | 24 +++++++++++------------- src/dtos/chat.dto.ts | 6 ++++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/chat/chat.service.ts b/src/chat/chat.service.ts index e8389da..955836b 100644 --- a/src/chat/chat.service.ts +++ b/src/chat/chat.service.ts @@ -147,19 +147,17 @@ export class ChatService { const otherUserInfo = await this.socketUserModel.findOne({ userId: otherUser.userId, }); - if (otherUserInfo) { - const latestChat = await this.chattingModel - .find({ roomId: room.id }) - .sort({ createdAt: -1 }) - .limit(1); - - return RoomInfoDto.ToDto( - room.id, - user.hasRead, - otherUserInfo, - latestChat[0], - ); - } + const latestChat = await this.chattingModel + .find({ roomId: room.id }) + .sort({ createdAt: -1 }) + .limit(1); + + return RoomInfoDto.ToDto( + room.id, + user.hasRead, + otherUserInfo, + latestChat[0], + ); } async getChats(roomId: string, userId: number) { diff --git a/src/dtos/chat.dto.ts b/src/dtos/chat.dto.ts index 33546dc..8f29ed6 100644 --- a/src/dtos/chat.dto.ts +++ b/src/dtos/chat.dto.ts @@ -87,8 +87,10 @@ export class RoomInfoDto { return { roomId: roomId, hasRead: hasRead, - nickname: otherUserSchema.userNickname, - sex: otherUserSchema.userSex, + nickname: otherUserSchema + ? otherUserSchema.userNickname + : '탈퇴한 사용자', + sex: otherUserSchema ? otherUserSchema.userSex : null, latest_chat: chattingSchema ? chattingSchema.chat : null, latest_time: chattingSchema ? chattingSchema.createdAt : null, };