Skip to content
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
1 change: 1 addition & 0 deletions src/chat/controller/chatroom.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/chat/dto/chatroom.dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
94 changes: 68 additions & 26 deletions src/chat/repository/chatroom.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/chat/service/chatroom.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down