Skip to content

Commit 42ae10b

Browse files
authored
Merge pull request #177 from umc-commit/refactor/176-chatroom-api
[REFACTOR] 채팅방 조회 썸네일 추가
2 parents 45ec15d + 0d7220d commit 42ae10b

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

src/chat/dto/chatroom.dto.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class ChatroomListResponseDto {
2121
this.artist_profile_image = room.artist.profileImage;
2222
this.commission_id = room.commission.id;
2323
this.commission_title = room.commission.title;
24+
this.commission_thumbnail = room.commission.thumbnail || null;
2425
// 이미지가 있으면 이미지 URL, 없으면 텍스트 content
2526
const lastMsg = room.chatMessages[0];
2627
this.last_message = lastMsg?.imageUrl || lastMsg?.content || null;

src/chat/service/chatroom.service.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,23 @@ export const ChatroomService = {
4949

5050
async getChatroomsByUserId(dto) {
5151
const user = await UserRepository.findUserById(dto.userId);
52-
if (!user) {
53-
throw new UserNotFoundError({ userId: dto.userId });
54-
}
52+
if (!user) throw new UserNotFoundError({ userId: dto.userId });
5553

5654
const chatrooms = await ChatroomRepository.findChatroomsByUser(dto.userId);
57-
console.log(dto.accountId)
5855

56+
// thumbnail 한 번에 조회
57+
const commissionIds = chatrooms.map(r => r.commission.id);
58+
const images = await CommissionRepository.findImagesByCommissionId(commissionIds);
59+
const thumbnailMap = Object.fromEntries(images.map(img => [img.targetId.toString(), img.imageUrl]));
60+
61+
// DTO 생성
5962
const result = [];
6063
for (const room of chatrooms) {
64+
room.commission.thumbnail = thumbnailMap[room.commission.id.toString()] || null;
65+
66+
// 방마다 unreadCount 조회
6167
const unreadCount = await ChatRepository.countUnreadMessages(room.id, dto.accountId);
68+
6269
result.push(new ChatroomListResponseDto(room, unreadCount));
6370
}
6471

src/commission/service/commission.service.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,30 @@ export const CommissionService = {
5656
* S3 이미지 업로드 처리
5757
*/
5858
async uploadRequestImage(file) {
59-
try {
60-
// 1. 파일 존재 여부 확인
61-
if (!file) {
62-
throw new ImageUploadFailedError({ reason: '파일이 업로드되지 않았습니다' });
63-
}
64-
65-
// 2. 파일 크기 검증
66-
if (file.size > 10 * 1024 * 1024) {
67-
throw new FileSizeExceededError({ fileSize: file.size });
68-
}
59+
// 1. 파일 존재 여부 확인
60+
if (!file) {
61+
throw new ImageUploadFailedError({ reason: '파일이 업로드되지 않았습니다' });
62+
}
6963

70-
// 3. 파일 확장자 검증
71-
const ext = path.extname(file.originalname).toLowerCase().replace('.', '');
72-
if (!['jpeg', 'jpg', 'png'].includes(ext)) {
73-
throw new UnsupportedImageFormatError({ fileType: file.mimetype });
74-
}
64+
// 2. 파일 크기 검증
65+
if (file.size > 10 * 1024 * 1024) {
66+
throw new FileSizeExceededError({ fileSize: file.size });
67+
}
7568

76-
// 4. S3 업로드 (requests 폴더에 저장)
77-
const imageUrl = await uploadToS3(file.buffer, 'requests', ext);
69+
// 3. 파일 확장자 검증
70+
const ext = path.extname(file.originalname).toLowerCase().replace('.', '');
71+
if (!['jpeg', 'jpg', 'png'].includes(ext)) {
72+
throw new UnsupportedImageFormatError({ fileType: file.mimetype });
73+
}
7874

79-
return {
80-
image_url: imageUrl,
81-
file_size: file.size,
82-
file_type: file.mimetype
83-
};
75+
// 4. S3 업로드
76+
const imageUrl = await uploadToS3(file.buffer, 'requests', ext);
8477

85-
} catch (error) {
86-
throw error;
87-
}
78+
return {
79+
image_url: imageUrl,
80+
file_size: file.size,
81+
file_type: file.mimetype
82+
};
8883
},
8984

9085
/**

src/common/swagger/chat.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
"artist_profile_image": { "type": "string", "example": "https://example.com/artist1.png" },
116116
"commission_id": { "type": "string", "example": "1" },
117117
"commission_title": { "type": "string", "example": "테스트 커미션 글" },
118+
"commission_thumbnail": { "type": ["string", "null"], "example": "https://example.com/sample-thumbnail.png" },
118119
"last_message": { "type": ["string", "null"], "example": null },
119120
"last_message_time": { "type": ["string", "null"], "format": "date-time", "example": null },
120121
"has_unread": { "type": "integer", "example": 0 }

0 commit comments

Comments
 (0)