diff --git a/src/chat/controller/chatroom.controller.js b/src/chat/controller/chatroom.controller.js index e9dba1f..8774ce8 100644 --- a/src/chat/controller/chatroom.controller.js +++ b/src/chat/controller/chatroom.controller.js @@ -31,6 +31,7 @@ export const getChatroom = async (req, res, next) => { const dto = new GetChatroomDto({ userId, accountId: BigInt(req.user.accountId), + role: req.user.role, }); const chatrooms = await ChatroomService.getChatroomsByUserId(dto); diff --git a/src/chat/dto/chatroom.dto.js b/src/chat/dto/chatroom.dto.js index 64584e6..c3d7174 100644 --- a/src/chat/dto/chatroom.dto.js +++ b/src/chat/dto/chatroom.dto.js @@ -7,9 +7,10 @@ export class CreateChatroomDto { } export class GetChatroomDto { - constructor({ userId, accountId }) { + constructor({ userId, accountId, role}) { this.userId = BigInt(userId); this.accountId = BigInt(accountId); + this.role = role; } } diff --git a/src/chat/repository/chatroom.repository.js b/src/chat/repository/chatroom.repository.js index 124ba9c..3b07342 100644 --- a/src/chat/repository/chatroom.repository.js +++ b/src/chat/repository/chatroom.repository.js @@ -22,35 +22,77 @@ export const ChatroomRepository = { }); }, - async findChatroomsByUser(userId) { + async findChatroomsByUser(account) { // 1. 채팅방 기본 정보 + 마지막 메시지(내용, 생성시간, id) 조회 - const chatrooms = await prisma.chatroom.findMany({ - where: { userId }, - include: { - artist: { - select: { - id: true, - nickname: true, - profileImage: true, - } + let chatrooms; + + if (account.role === "client") { + // ✅ 클라이언트가 보는 채팅방 + chatrooms = await prisma.chatroom.findMany({ + where: { + userId: BigInt(account.userId), + hiddenUser: false, }, - commission: { - select: { - id: true, - title: true, - } + include: { + artist: { + select: { + id: true, + nickname: true, + profileImage: true, + }, + }, + commission: { + select: { + id: true, + title: true, + }, + }, + chatMessages: { + orderBy: { createdAt: "desc" }, + take: 1, + select: { + id: true, + content: true, + createdAt: true, + }, + }, }, - chatMessages: { - orderBy: { createdAt: "desc" }, - take: 1, - select: { - id: true, - content: true, - createdAt: true, - } - } - } - }); + }); + } else if (account.role === "artist") { + // ✅ 아티스트가 보는 채팅방 + chatrooms = await prisma.chatroom.findMany({ + where: { + artistId: BigInt(account.artistId), + hiddenArtist: false, + }, + include: { + user: { + select: { + id: true, + nickname: true, + profileImage: true, + }, + }, + commission: { + select: { + id: true, + title: true, + }, + }, + chatMessages: { + orderBy: { createdAt: "desc" }, + take: 1, + select: { + id: true, + content: true, + createdAt: true, + }, + }, + }, + }); + } else { + return []; + } // 2. 마지막 메시지 ID 목록 수집 const messageIds = chatrooms diff --git a/src/chat/service/chatroom.service.js b/src/chat/service/chatroom.service.js index 74ff332..961444b 100644 --- a/src/chat/service/chatroom.service.js +++ b/src/chat/service/chatroom.service.js @@ -51,7 +51,7 @@ export const ChatroomService = { const user = await UserRepository.findUserById(dto.userId); if (!user) throw new UserNotFoundError({ userId: dto.userId }); - const chatrooms = await ChatroomRepository.findChatroomsByUser(dto.userId); + const chatrooms = await ChatroomRepository.findChatroomsByUser(dto); // 1. thumbnail 한 번에 조회 (BigInt 변환) const commissionIds = chatrooms.map(r => BigInt(r.commission.id));