diff --git a/src/chat/dto/chatroom.dto.js b/src/chat/dto/chatroom.dto.js index 383a96f..64584e6 100644 --- a/src/chat/dto/chatroom.dto.js +++ b/src/chat/dto/chatroom.dto.js @@ -21,6 +21,7 @@ export class ChatroomListResponseDto { this.artist_profile_image = room.artist.profileImage; this.commission_id = room.commission.id; this.commission_title = room.commission.title; + this.commission_thumbnail = room.commission.thumbnail || null; // 이미지가 있으면 이미지 URL, 없으면 텍스트 content const lastMsg = room.chatMessages[0]; this.last_message = lastMsg?.imageUrl || lastMsg?.content || null; diff --git a/src/chat/service/chatroom.service.js b/src/chat/service/chatroom.service.js index b4480bb..ef8e260 100644 --- a/src/chat/service/chatroom.service.js +++ b/src/chat/service/chatroom.service.js @@ -49,16 +49,23 @@ export const ChatroomService = { async getChatroomsByUserId(dto) { const user = await UserRepository.findUserById(dto.userId); - if (!user) { - throw new UserNotFoundError({ userId: dto.userId }); - } + if (!user) throw new UserNotFoundError({ userId: dto.userId }); const chatrooms = await ChatroomRepository.findChatroomsByUser(dto.userId); - console.log(dto.accountId) + // thumbnail 한 번에 조회 + const commissionIds = chatrooms.map(r => r.commission.id); + const images = await CommissionRepository.findImagesByCommissionId(commissionIds); + const thumbnailMap = Object.fromEntries(images.map(img => [img.targetId.toString(), img.imageUrl])); + + // DTO 생성 const result = []; for (const room of chatrooms) { + room.commission.thumbnail = thumbnailMap[room.commission.id.toString()] || null; + + // 방마다 unreadCount 조회 const unreadCount = await ChatRepository.countUnreadMessages(room.id, dto.accountId); + result.push(new ChatroomListResponseDto(room, unreadCount)); } diff --git a/src/commission/service/commission.service.js b/src/commission/service/commission.service.js index 8a64840..88f9337 100644 --- a/src/commission/service/commission.service.js +++ b/src/commission/service/commission.service.js @@ -56,35 +56,30 @@ export const CommissionService = { * S3 이미지 업로드 처리 */ async uploadRequestImage(file) { - try { - // 1. 파일 존재 여부 확인 - if (!file) { - throw new ImageUploadFailedError({ reason: '파일이 업로드되지 않았습니다' }); - } - - // 2. 파일 크기 검증 - if (file.size > 10 * 1024 * 1024) { - throw new FileSizeExceededError({ fileSize: file.size }); - } + // 1. 파일 존재 여부 확인 + if (!file) { + throw new ImageUploadFailedError({ reason: '파일이 업로드되지 않았습니다' }); + } - // 3. 파일 확장자 검증 - const ext = path.extname(file.originalname).toLowerCase().replace('.', ''); - if (!['jpeg', 'jpg', 'png'].includes(ext)) { - throw new UnsupportedImageFormatError({ fileType: file.mimetype }); - } + // 2. 파일 크기 검증 + if (file.size > 10 * 1024 * 1024) { + throw new FileSizeExceededError({ fileSize: file.size }); + } - // 4. S3 업로드 (requests 폴더에 저장) - const imageUrl = await uploadToS3(file.buffer, 'requests', ext); + // 3. 파일 확장자 검증 + const ext = path.extname(file.originalname).toLowerCase().replace('.', ''); + if (!['jpeg', 'jpg', 'png'].includes(ext)) { + throw new UnsupportedImageFormatError({ fileType: file.mimetype }); + } - return { - image_url: imageUrl, - file_size: file.size, - file_type: file.mimetype - }; + // 4. S3 업로드 + const imageUrl = await uploadToS3(file.buffer, 'requests', ext); - } catch (error) { - throw error; - } + return { + image_url: imageUrl, + file_size: file.size, + file_type: file.mimetype + }; }, /** diff --git a/src/common/swagger/chat.json b/src/common/swagger/chat.json index 0ecc87b..9e237e1 100644 --- a/src/common/swagger/chat.json +++ b/src/common/swagger/chat.json @@ -115,6 +115,7 @@ "artist_profile_image": { "type": "string", "example": "https://example.com/artist1.png" }, "commission_id": { "type": "string", "example": "1" }, "commission_title": { "type": "string", "example": "테스트 커미션 글" }, + "commission_thumbnail": { "type": ["string", "null"], "example": "https://example.com/sample-thumbnail.png" }, "last_message": { "type": ["string", "null"], "example": null }, "last_message_time": { "type": ["string", "null"], "format": "date-time", "example": null }, "has_unread": { "type": "integer", "example": 0 }