Skip to content

Commit 2d8af9d

Browse files
authored
Merge pull request #439 from PromptPlace/develop
Develop
2 parents a4a11a8 + 67a6579 commit 2d8af9d

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/chat/repositories/chat.repository.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class ChatRepository {
7676

7777
// 채팅방을 나갔으면 그 이후의 메세지만 조회
7878
if (leftAt) {
79-
whereConditions.created_at = { gt: leftAt };
79+
whereConditions.sent_at = { gt: leftAt };
8080
}
8181

8282
const [messages, totalCount] = await Promise.all([
@@ -105,6 +105,7 @@ export class ChatRepository {
105105
return {
106106
messages: actualMessages,
107107
totalCount,
108+
hasMore: hasNextPage,
108109
};
109110
}
110111

@@ -258,14 +259,16 @@ export class ChatRepository {
258259
}
259260

260261
// == 메세지 저장
261-
async saveMessage(
262-
roomId: number,
263-
senderId: number,
264-
content: string,
265-
files: { url: string; contentType: AttachmentType; name: string; size: number }[]
266-
) {
267-
268-
return prisma.chatMessage.create({
262+
async saveMessage(
263+
roomId: number,
264+
senderId: number,
265+
content: string,
266+
files: { url: string; contentType: AttachmentType; name: string; size: number }[]
267+
) {
268+
// 트랜잭션으로 메세지 생성과 채팅방 업데이트를 묶어서 처리
269+
return prisma.$transaction(async (tx) => {
270+
// 1. 메세지 생성
271+
const savedMessage = await tx.chatMessage.create({
269272
data: {
270273
room_id: roomId,
271274
sender_id: senderId,
@@ -290,8 +293,14 @@ export class ChatRepository {
290293
attachments: true,
291294
},
292295
});
293-
}
294296

295-
296-
}
297+
// 2. 채팅방의 마지막 메세지 ID 업데이트
298+
await tx.chatRoom.update({
299+
where: { room_id: roomId },
300+
data: { last_message_id: savedMessage.message_id },
301+
});
297302

303+
return savedMessage;
304+
});
305+
}
306+
}

src/chat/services/chat.service.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ export class ChatService {
6363
this.chatRepo.findMessagesByRoomId(roomId, cursor, limit, myId),
6464
updateReadStatus
6565
]);
66-
67-
// 페이지네이션
68-
const hasMore = messageInfo.messages.length > limit;
69-
const messages = hasMore ? messageInfo.messages.slice(0, limit) : messageInfo.messages;
66+
67+
const hasMore = messageInfo.hasMore;
68+
const messages = messageInfo.messages;
7069

7170
return ChatRoomDetailResponseDto.from({
7271
roomDetail,
@@ -88,7 +87,6 @@ export class ChatService {
8887
filter,
8988
search
9089
});
91-
9290

9391
// 페이지네이션
9492
const hasMore = roomList.rooms.length > limit;

0 commit comments

Comments
 (0)