diff --git a/src/chat/chat.module.ts b/src/chat/chat.module.ts index 9261a4c..0d2d2b2 100644 --- a/src/chat/chat.module.ts +++ b/src/chat/chat.module.ts @@ -5,10 +5,6 @@ import { MongooseModule } from '@nestjs/mongoose'; import { ChatRoom, ChatRoomSchema } from './schemas/chat-room.schemas'; import { Chat, ChatSchema } from './schemas/chat.schemas'; import { ChatImage, ChatImageSchema } from './schemas/chat-image.schemas'; -import { - ChatNotification, - ChatNotificationSchema, -} from './schemas/chat-notifiation.schemas'; import { S3Module } from 'src/common/s3/s3.module'; import { ChatRepository } from './repositories/chat.repository'; import { NotificationService } from './services/notification.service'; @@ -21,7 +17,6 @@ import { AuthModule } from 'src/auth/auth.module'; { name: ChatRoom.name, schema: ChatRoomSchema }, { name: Chat.name, schema: ChatSchema }, { name: ChatImage.name, schema: ChatImageSchema }, - { name: ChatNotification.name, schema: ChatNotificationSchema }, ]), S3Module, AuthModule, diff --git a/src/chat/controllers/chat.controller.ts b/src/chat/controllers/chat.controller.ts index 98cc9a7..0b3d2b6 100644 --- a/src/chat/controllers/chat.controller.ts +++ b/src/chat/controllers/chat.controller.ts @@ -3,44 +3,46 @@ import { Controller, Delete, Get, - Headers, Param, - ParseIntPipe, Post, - Query, Sse, UploadedFile, + UseGuards, UseInterceptors, UsePipes, ValidationPipe, } from '@nestjs/common'; import { ChatService } from '../services/chat.service'; -import { ApiOperation, ApiTags } from '@nestjs/swagger'; +import { ApiTags } from '@nestjs/swagger'; import { ReceivedUserDto } from '../dto/received-user.dto'; import mongoose from 'mongoose'; import { FileInterceptor } from '@nestjs/platform-express'; -import { Users } from 'src/common/decorators/user.decorator'; -import { User } from 'src/users/entities/user.entity'; import { ParseObjectIdPipe } from '../parse-object-id.pipe'; import { ApiCreateChatRoom } from '../swagger-decorators/create-chat-room.decorator'; import { ApiGetChatRooms } from '../swagger-decorators/get-chat-rooms.decorator'; import { ApiGetOneChatRoom } from '../swagger-decorators/get-one-chat-room.decorator'; import { ApiDeleteChatRoom } from '../swagger-decorators/delete-chat-room.decorator'; import { ApiGetChats } from '../swagger-decorators/get-chats.decorator'; -import { ApiGetChatNotification } from '../swagger-decorators/get-chat-notification.decorator'; -import { ApiGetChatUnreadCounts } from '../swagger-decorators/get-chat-unread-counts.decorator'; +import { ApiGetChatNotificationSse } from '../swagger-decorators/get-chat-notification-Sse.decorator'; +// import { ApiGetChatUnreadCounts } from '../swagger-decorators/get-chat-unread-counts.decorator'; import { TokenService } from 'src/auth/services/token.service'; +import { GetUserId } from 'src/common/decorators/get-userId.decorator'; +import { JwtAccessTokenGuard } from 'src/config/guards/jwt-access-token.guard'; +import { GetNotificationsResponseDto } from '../dto/get-notifications-response.dto'; +import { ApiGetChatNotifications } from '../swagger-decorators/get-chat-notifications.decorator'; +import { ApiCreateChatImage } from '../swagger-decorators/create-chat-image.decorators'; @ApiTags('CHAT') -@Controller('chat-room') +@UseGuards(JwtAccessTokenGuard) @UsePipes(ValidationPipe) +@Controller('chat-room') export class ChatController { constructor( private chatService: ChatService, private tokenService: TokenService, ) {} - @ApiGetChatNotification() + @ApiGetChatNotificationSse() @Sse('listener') notificationListener() { return this.chatService.notificationListener(); @@ -48,52 +50,55 @@ export class ChatController { @ApiGetChatRooms() @Get() - async getChatRooms(@Users() user: User) { - return this.chatService.getChatRooms(user.id); + async getChatRooms(@GetUserId() userId: number) { + return this.chatService.getChatRooms(userId); } @ApiGetOneChatRoom() @Get(':roomId') async getOneChatRoom( - @Users() user: User, + @GetUserId() userId: number, @Param('roomId', ParseObjectIdPipe) roomId: mongoose.Types.ObjectId, ) { - return this.chatService.getOneChatRoom(user.id, roomId); + return this.chatService.getOneChatRoom(userId, roomId); } @ApiCreateChatRoom() @Post() - async createChatRoom(@Users() user: User, @Body() body: ReceivedUserDto) { - return this.chatService.createChatRoom(user.id, body.receiverId); + async createChatRoom( + @GetUserId() userId: number, + @Body() body: ReceivedUserDto, + ) { + return this.chatService.createChatRoom(userId, body.receiverId); } @ApiDeleteChatRoom() @Delete(':roomId') async deleteChatRoom( - @Users() user: User, + @GetUserId() userId: number, @Param('roomId', ParseObjectIdPipe) roomId: mongoose.Types.ObjectId, ) { - return this.chatService.deleteChatRoom(user.id, roomId); + return this.chatService.deleteChatRoom(userId, roomId); } @ApiGetChats() @Get(':roomId/chat') async getChats( + @GetUserId() userId: number, @Param('roomId', ParseObjectIdPipe) roomId: mongoose.Types.ObjectId, ) { - return this.chatService.getChats(roomId); + return this.chatService.getChats(userId, roomId); } - @ApiOperation({ summary: '특정 채팅방 채팅 이미지 생성' }) + @ApiCreateChatImage() @Post(':roomId/chat/image') @UseInterceptors(FileInterceptor('file')) async createChatImage( @Param('roomId', ParseObjectIdPipe) roomId: mongoose.Types.ObjectId, - @Headers('access_token') accessToken: string, + @GetUserId() senderId: number, @Body() body: ReceivedUserDto, @UploadedFile() file: Express.Multer.File, ) { - const senderId = await this.tokenService.decodeToken(accessToken); return this.chatService.createChatImage( roomId, senderId, @@ -102,12 +107,20 @@ export class ChatController { ); } - @ApiGetChatUnreadCounts() - @Get(':roomId/chat/unreads') - async getUnreadCounts( - @Param('roomId', ParseObjectIdPipe) roomId: mongoose.Types.ObjectId, - @Query('after', ParseIntPipe) after: number, - ) { - return this.chatService.getUnreadCounts(roomId, after); + @ApiGetChatNotifications() + @Get('chat/notice') + async getChatNotifications( + @GetUserId() userId: number, + ): Promise { + return this.chatService.getChatNotifications(userId); } + + // @ApiGetChatUnreadCounts() + // @Get(':roomId/chat/unreads') + // async getUnreadCounts( + // @Param('roomId', ParseObjectIdPipe) roomId: mongoose.Types.ObjectId, + // @Query('after', ParseIntPipe) after: number, + // ) { + // return this.chatService.getUnreadCounts(roomId, after); + // } } diff --git a/src/chat/dto/get-notifications-response.dto.ts b/src/chat/dto/get-notifications-response.dto.ts new file mode 100644 index 0000000..ff7a0aa --- /dev/null +++ b/src/chat/dto/get-notifications-response.dto.ts @@ -0,0 +1,19 @@ +import mongoose from 'mongoose'; + +export class GetNotificationsResponseDto { + _id: mongoose.Types.ObjectId; + + chatroom_id: mongoose.Types.ObjectId; + + sender: number; + + receiver: number; + + content: string; + + createdAt: Date; + + isSeen: false; + + count: number; +} diff --git a/src/chat/repositories/chat.repository.ts b/src/chat/repositories/chat.repository.ts index 5aff0f1..52ff985 100644 --- a/src/chat/repositories/chat.repository.ts +++ b/src/chat/repositories/chat.repository.ts @@ -53,7 +53,22 @@ export class ChatRepository { } async getChats(roomId: mongoose.Types.ObjectId) { - return this.chatModel.find({ chatroom_id: roomId }); + return this.chatModel.find({ + chatroom_id: roomId, + }); + } + + async updateChatIsSeen(receiverId: number, roomId: mongoose.Types.ObjectId) { + await this.chatModel.updateMany( + { + $and: [ + { receiver: receiverId }, + { chatroom_id: roomId }, + { isSeen: false }, + ], + }, + { $set: { isSeen: true } }, + ); } async createChat( @@ -91,9 +106,19 @@ export class ChatRepository { return returnedChat; } - async getUnreadCounts(roomId: mongoose.Types.ObjectId, after: number) { - return this.chatModel.count({ - $and: [{ chatroom_id: roomId }, { createdAt: { $gt: new Date(after) } }], - }); + async getChatNotifications(userId: number) { + const notifications = await this.chatModel + .find({ + $and: [{ receiver: userId }, { isSeen: false }], + }) + .sort({ createdAt: -1 }); + + return notifications; } + + // async getUnreadCounts(roomId: mongoose.Types.ObjectId, after: number) { + // return this.chatModel.count({ + // $and: [{ chatroom_id: roomId }, { createdAt: { $gt: new Date(after) } }], + // }); + // } } diff --git a/src/chat/schemas/chat-notifiation.schemas.ts b/src/chat/schemas/chat-notifiation.schemas.ts deleted file mode 100644 index c70181b..0000000 --- a/src/chat/schemas/chat-notifiation.schemas.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Prop, Schema, SchemaFactory, SchemaOptions } from '@nestjs/mongoose'; -import * as mongoose from 'mongoose'; -import { Chat } from './chat.schemas'; -import { IsBoolean, IsMongoId, IsNumber } from 'class-validator'; - -const options: SchemaOptions = { - collection: 'ChatNotification', - timestamps: true, -}; - -@Schema(options) -export class ChatNotification { - @IsMongoId() - @Prop({ type: mongoose.Types.ObjectId, ref: Chat.name }) - chat_id: mongoose.Types.ObjectId; - - @IsNumber() - @Prop({ required: true }) - sender: number; - - @IsNumber() - @Prop({ required: true }) - receiver: number; - - @IsBoolean() - @Prop({ required: true, default: false }) - isSeen: boolean; -} - -export const ChatNotificationSchema = - SchemaFactory.createForClass(ChatNotification); diff --git a/src/chat/schemas/chat.schemas.ts b/src/chat/schemas/chat.schemas.ts index fd3c263..5fea2ef 100644 --- a/src/chat/schemas/chat.schemas.ts +++ b/src/chat/schemas/chat.schemas.ts @@ -1,7 +1,7 @@ import { Prop, Schema, SchemaFactory, SchemaOptions } from '@nestjs/mongoose'; import * as mongoose from 'mongoose'; import { ChatRoom } from './chat-room.schemas'; -import { IsMongoId, IsNumber, IsString } from 'class-validator'; +import { IsBoolean, IsMongoId, IsNumber, IsString } from 'class-validator'; const options: SchemaOptions = { collection: 'Chat', @@ -25,6 +25,10 @@ export class Chat { @IsString() @Prop({ required: true }) content: string; + + @IsBoolean() + @Prop({ required: true, default: false }) + isSeen: boolean; } export const ChatSchema = SchemaFactory.createForClass(Chat); diff --git a/src/chat/services/chat.service.ts b/src/chat/services/chat.service.ts index a1b17d2..c538c2e 100644 --- a/src/chat/services/chat.service.ts +++ b/src/chat/services/chat.service.ts @@ -11,10 +11,11 @@ import { InjectModel } from '@nestjs/mongoose'; import { ChatRoom } from '../schemas/chat-room.schemas'; import * as mongoose from 'mongoose'; import { S3Service } from 'src/common/s3/s3.service'; -import { NotificationService } from './notification.service'; -import { ChatNotification } from '../schemas/chat-notifiation.schemas'; import { Subject, catchError, map } from 'rxjs'; import { Chat } from '../schemas/chat.schemas'; +import { User } from 'src/users/entities/user.entity'; +import { EntityManager } from 'typeorm'; +import { GetNotificationsResponseDto } from '../dto/get-notifications-response.dto'; @Injectable() export class ChatService { @@ -22,14 +23,12 @@ export class ChatService { private readonly subject = new Subject(); constructor( private readonly s3Service: S3Service, - private readonly notificationService: NotificationService, private readonly chatRepository: ChatRepository, + private readonly entityManager: EntityManager, @InjectModel(ChatRoom.name) private readonly chatRoomModel: mongoose.Model, @InjectModel(Chat.name) private readonly chatModel: mongoose.Model, - @InjectModel(ChatNotification.name) - private readonly chatNotificationModel: mongoose.Model, ) {} notificationListener() { @@ -46,13 +45,7 @@ export class ChatService { ); } async getChatRooms(myId: number) { - const chatRoom = await this.chatRepository.getChatRooms(myId); - - if (!chatRoom.length) { - throw new NotFoundException('해당 유저가 속한 채팅방이 없습니다.'); - } - - return chatRoom; + return this.chatRepository.getChatRooms(myId); } async getOneChatRoom(myId: number, roomId: mongoose.Types.ObjectId) { @@ -113,13 +106,13 @@ export class ChatService { return this.chatRepository.deleteChatRoom(roomId); } - async getChats(roomId: mongoose.Types.ObjectId) { + async getChats(userId: number, roomId: mongoose.Types.ObjectId) { + await this.getOneChatRoom(userId, roomId); const returnedChat = await this.chatRepository.getChats(roomId); - if (!returnedChat.length) { - throw new NotFoundException( - '해당 채팅룸이 없거나 채팅이 존재하지 않습니다.', - ); + if (returnedChat.length) { + this.chatRepository.updateChatIsSeen(userId, roomId); + return returnedChat; } return returnedChat; @@ -156,14 +149,7 @@ export class ChatService { receiver: returnedChat.receiver, }; - const notification = await new this.chatNotificationModel({ - chat_id: returnedChat.id, - sender: returnedChat.sender, - receiver: returnedChat.receiver, - }).save(); - - // send notification - if (notification) this.subject.next(notification); + if (returnedChat) this.subject.next(returnedChat); return chat; } @@ -218,11 +204,44 @@ export class ChatService { return isChatAndUsers; } - async getUnreadCounts(roomId: mongoose.Types.ObjectId, after: number) { - const returnedRoom = await this.chatRoomModel.findOne({ _id: roomId }); - if (!returnedRoom) { - throw new NotFoundException('해당 채팅 룸을 찾지 못했습니다.'); + async getChatNotifications( + userId: number, + ): Promise { + const isUser = await this.entityManager.findOne(User, { + where: { id: userId }, + }); + + if (!isUser) { + throw new NotFoundException('해당 유저를 찾지 못했습니다.'); } - return this.chatRepository.getUnreadCounts(roomId, after); + + const returnedNotifications = + await this.chatRepository.getChatNotifications(userId); + + const groupedNotifications = {}; + + returnedNotifications.forEach((notification) => { + const chatroomId = notification.chatroom_id.toString(); + if (!groupedNotifications[chatroomId]) { + const newNotification = { + ...notification.toObject(), + count: 1, + content: notification.content.substring(0, 10), + }; + groupedNotifications[chatroomId] = newNotification; + } else { + groupedNotifications[chatroomId]['count'] += 1; + } + }); + + return Object.values(groupedNotifications); } + + // async getUnreadCounts(roomId: mongoose.Types.ObjectId, after: number) { + // const returnedRoom = await this.chatRoomModel.findOne({ _id: roomId }); + // if (!returnedRoom) { + // throw new NotFoundException('해당 채팅 룸을 찾지 못했습니다.'); + // } + // return this.chatRepository.getUnreadCounts(roomId, after); + // } } diff --git a/src/chat/services/notification.service.ts b/src/chat/services/notification.service.ts index 82679a9..085b3b8 100644 --- a/src/chat/services/notification.service.ts +++ b/src/chat/services/notification.service.ts @@ -1,8 +1,8 @@ import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Subject, map } from 'rxjs'; -import { ChatNotification } from '../schemas/chat-notifiation.schemas'; import mongoose from 'mongoose'; +import { Chat } from '../schemas/chat.schemas'; @Injectable() export class NotificationService { @@ -10,8 +10,8 @@ export class NotificationService { private readonly subject = new Subject(); constructor( - @InjectModel(ChatNotification.name) - private readonly chatNotificationModel: mongoose.Model, + @InjectModel(Chat.name) + private readonly chatnModel: mongoose.Model, ) {} notificationListener() { @@ -29,7 +29,7 @@ export class NotificationService { async createNotification(createNotificationsDto: number) { try { - const notification = await new this.chatNotificationModel({ + const notification = await new this.chatnModel({ // description: createNotificationsDto.description, // title: createNotificationsDto.title, isSeen: false, diff --git a/src/chat/swagger-decorators/create-chat-image.decorators.ts b/src/chat/swagger-decorators/create-chat-image.decorators.ts new file mode 100644 index 0000000..c63f700 --- /dev/null +++ b/src/chat/swagger-decorators/create-chat-image.decorators.ts @@ -0,0 +1,80 @@ +import { applyDecorators } from '@nestjs/common'; +import { ApiHeaders, ApiOperation, ApiResponse } from '@nestjs/swagger'; + +export function ApiCreateChatImage() { + return applyDecorators( + ApiOperation({ + summary: + '특정 채팅방 이미지 생성. 채팅 이미지 전송 시, 여기서 생성한 이미지 url을 통해 socket으로 채팅 전송 요청', + description: + 'Headers - access_token, Param - roomId, Formdata - ReceivedUserDto, key: file, value: image.png', + }), + ApiResponse({ + status: 201, + description: '채팅 이미지 url 생성 성공', + content: { + JSON: { + example: { + chatroom_id: '653383a4468680bc4e9f8492', + sender: 69, + receiver: 70, + content: + 'https://ma6-main.s3.ap-northeast-2.amazonaws.com/ChatImages/69_1699363871042.jpeg', + isSeen: false, + _id: '654a3c1f1066329877d1d919', + createdAt: '2023-11-07T13:31:11.263Z', + updatedAt: '2023-11-07T13:31:11.263Z', + __v: 0, + }, + }, + }, + }), + ApiResponse({ + status: 404, + description: '채팅룸 조회 실패.', + content: { + JSON: { + example: { + message: '해당 유저가 속한 채팅방이 없습니다.', + error: 'Not Found', + statusCode: 404, + }, + }, + }, + }), + ApiResponse({ + status: 404, + description: '채팅 이미지 생성 실패.', + content: { + JSON: { + example: { + message: '채팅을 전송할 유저가 채팅방에 없습니다', + error: 'Not Found', + statusCode: 404, + }, + }, + }, + }), + ApiResponse({ + status: 400, + description: 'ObjectId Validation 실패', + content: { + JSON: { + example: { + message: '올바른 ObjectId 형식이 아닙니다.', + error: 'Bad Request', + statusCode: 400, + }, + }, + }, + }), + ApiHeaders([ + { + name: 'access_token', + description: '액세스 토큰', + required: true, + example: '여기에 액세스 토큰', + }, + ]), + ); +} diff --git a/src/chat/swagger-decorators/create-chat-room.decorator.ts b/src/chat/swagger-decorators/create-chat-room.decorator.ts index 444fbb0..cb86ce0 100644 --- a/src/chat/swagger-decorators/create-chat-room.decorator.ts +++ b/src/chat/swagger-decorators/create-chat-room.decorator.ts @@ -1,11 +1,11 @@ import { applyDecorators } from '@nestjs/common'; -import { ApiOperation, ApiResponse } from '@nestjs/swagger'; +import { ApiHeaders, ApiOperation, ApiResponse } from '@nestjs/swagger'; export function ApiCreateChatRoom() { return applyDecorators( ApiOperation({ summary: '채팅룸 생성', - description: 'Header - user-token, Body - received-user-dto', + description: 'Header - access-token, Body - received-user-dto', }), ApiResponse({ status: 201, @@ -50,5 +50,13 @@ export function ApiCreateChatRoom() { }, }, }), + ApiHeaders([ + { + name: 'access_token', + description: '액세스 토큰', + required: true, + example: '여기에 액세스 토큰', + }, + ]), ); } diff --git a/src/chat/swagger-decorators/delete-chat-room.decorator.ts b/src/chat/swagger-decorators/delete-chat-room.decorator.ts index e0f5af3..25347a4 100644 --- a/src/chat/swagger-decorators/delete-chat-room.decorator.ts +++ b/src/chat/swagger-decorators/delete-chat-room.decorator.ts @@ -1,11 +1,11 @@ import { applyDecorators } from '@nestjs/common'; -import { ApiOperation, ApiResponse } from '@nestjs/swagger'; +import { ApiHeaders, ApiOperation, ApiResponse } from '@nestjs/swagger'; export function ApiDeleteChatRoom() { return applyDecorators( ApiOperation({ summary: '채팅룸 삭제', - description: 'Header - user-token, Param - room-id', + description: 'Header - access_token, Param - roomId', }), ApiResponse({ status: 200, @@ -58,5 +58,13 @@ export function ApiDeleteChatRoom() { }, }, }), + ApiHeaders([ + { + name: 'access_token', + description: '액세스 토큰', + required: true, + example: '여기에 액세스 토큰', + }, + ]), ); } diff --git a/src/chat/swagger-decorators/get-chat-notification.decorator.ts b/src/chat/swagger-decorators/get-chat-notification-Sse.decorator.ts similarity index 94% rename from src/chat/swagger-decorators/get-chat-notification.decorator.ts rename to src/chat/swagger-decorators/get-chat-notification-Sse.decorator.ts index 5060b27..53d8cfd 100644 --- a/src/chat/swagger-decorators/get-chat-notification.decorator.ts +++ b/src/chat/swagger-decorators/get-chat-notification-Sse.decorator.ts @@ -1,7 +1,7 @@ import { applyDecorators } from '@nestjs/common'; import { ApiOperation, ApiResponse } from '@nestjs/swagger'; -export function ApiGetChatNotification() { +export function ApiGetChatNotificationSse() { return applyDecorators( ApiOperation({ summary: '채팅 실시간 SSE 알람(미사용 예정)', diff --git a/src/chat/swagger-decorators/get-chat-notifications.decorator.ts b/src/chat/swagger-decorators/get-chat-notifications.decorator.ts new file mode 100644 index 0000000..591461d --- /dev/null +++ b/src/chat/swagger-decorators/get-chat-notifications.decorator.ts @@ -0,0 +1,81 @@ +import { applyDecorators } from '@nestjs/common'; +import { ApiHeaders, ApiOperation, ApiResponse } from '@nestjs/swagger'; + +export function ApiGetChatNotifications() { + return applyDecorators( + ApiOperation({ + summary: `읽지 않은 채팅 알람 미리보기 및 개수(각 채팅룸의 가장 최신의 읽지 않은 메세지 1개와 그 외 읽지 않은 메세지들의 개수(count)) + 단순 채팅 조회와 다른 점은 isSeen이 false인 값만 가져오며, count라는 property가 추가 되었고, content의 경우 앞의 10글자만 잘라서 return + `, + description: 'Headers - access_token', + }), + ApiResponse({ + status: 200, + description: '채팅 알람 및 개수 받아오기 성공', + content: { + JSON: { + example: [ + { + _id: '654a397f55108627dc2f01b6', + chatroom_id: '653383a4468680bc4e9f8492', + sender: 70, + receiver: 69, + content: '허으으으호호호우우우', + isSeen: false, + createdAt: '2023-11-07T13:19:59.847Z', + updatedAt: '2023-11-07T13:19:59.847Z', + __v: 0, + count: 2, + }, + { + _id: '6541b0596f03ac696504c889', + chatroom_id: '653383a4468680bc4e9f8491', + sender: 70, + receiver: 69, + content: 'https://ma', + createdAt: '2023-11-01T01:56:41.232Z', + updatedAt: '2023-11-01T01:56:41.232Z', + __v: 0, + isSeen: false, + count: 1, + }, + ], + }, + }, + }), + ApiResponse({ + status: 404, + description: '채팅룸 조회 실패.', + content: { + JSON: { + example: { + message: '해당 유저가 속한 채팅방이 없습니다.', + error: 'Not Found', + statusCode: 404, + }, + }, + }, + }), + ApiResponse({ + status: 400, + description: 'ObjectId Validation 실패', + content: { + JSON: { + example: { + message: '올바른 ObjectId 형식이 아닙니다.', + error: 'Bad Request', + statusCode: 400, + }, + }, + }, + }), + ApiHeaders([ + { + name: 'access_token', + description: '액세스 토큰', + required: true, + example: '여기에 액세스 토큰', + }, + ]), + ); +} diff --git a/src/chat/swagger-decorators/get-chat-rooms.decorator.ts b/src/chat/swagger-decorators/get-chat-rooms.decorator.ts index 91f5f40..38f3b20 100644 --- a/src/chat/swagger-decorators/get-chat-rooms.decorator.ts +++ b/src/chat/swagger-decorators/get-chat-rooms.decorator.ts @@ -1,5 +1,5 @@ import { applyDecorators } from '@nestjs/common'; -import { ApiOperation, ApiResponse } from '@nestjs/swagger'; +import { ApiHeaders, ApiOperation, ApiResponse } from '@nestjs/swagger'; export function ApiGetChatRooms() { return applyDecorators( @@ -35,18 +35,13 @@ export function ApiGetChatRooms() { }, }, }), - ApiResponse({ - status: 404, - description: '채팅룸 조회 실패.', - content: { - JSON: { - example: { - message: '해당 유저가 속한 채팅방이 없습니다.', - error: 'Not Found', - statusCode: 404, - }, - }, + ApiHeaders([ + { + name: 'access_token', + description: '액세스 토큰', + required: true, + example: '여기에 액세스 토큰', }, - }), + ]), ); } diff --git a/src/chat/swagger-decorators/get-chats.decorator.ts b/src/chat/swagger-decorators/get-chats.decorator.ts index 9a2978a..5ba3414 100644 --- a/src/chat/swagger-decorators/get-chats.decorator.ts +++ b/src/chat/swagger-decorators/get-chats.decorator.ts @@ -1,46 +1,39 @@ import { applyDecorators } from '@nestjs/common'; -import { ApiOperation, ApiResponse } from '@nestjs/swagger'; +import { ApiHeaders, ApiOperation, ApiResponse } from '@nestjs/swagger'; export function ApiGetChats() { return applyDecorators( ApiOperation({ summary: '해당 채팅룸 채팅 전체 조회', - description: 'Param - room-id', + description: 'Param - room-id, Headers - access_token', }), ApiResponse({ status: 200, - description: '성공적으로 채팅방 채팅 조회', + description: + '성공적으로 채팅방 채팅 조회 및 읽지 않았던 채팅들 isSeen: true로 변경', content: { JSON: { example: [ { - _id: '65338bc82d17fd935414495b', - chatroom_id: '653383a4468680bc4e9f8491', - sender: 12345642, - receiver: 123456427, - content: 'testtset', - createdAt: '2023-10-21T08:28:56.916Z', - updatedAt: '2023-10-21T08:28:56.916Z', + _id: '654a1b3a88ad1cdd55124710', + chatroom_id: '653383a4468680bc4e9f8492', + sender: 70, + receiver: 69, + content: '으아아아이 테스트테스트으아으아으앙', + isSeen: true, + createdAt: '2023-11-07T11:10:50.817Z', + updatedAt: '2023-11-07T11:11:13.830Z', __v: 0, }, { - _id: '65338bec2d17fd9354144961', - chatroom_id: '653383a4468680bc4e9f8491', - sender: 12345642, - receiver: 123456427, - content: 'testtset12', - createdAt: '2023-10-21T08:29:32.578Z', - updatedAt: '2023-10-21T08:29:32.578Z', - __v: 0, - }, - { - _id: '65338bf12d17fd9354144967', - chatroom_id: '653383a4468680bc4e9f8491', - sender: 12345642, - receiver: 123456427, - content: 'testtset123', - createdAt: '2023-10-21T08:29:37.481Z', - updatedAt: '2023-10-21T08:29:37.481Z', + _id: '654a1d106770906a7a5d6274', + chatroom_id: '653383a4468680bc4e9f8492', + sender: 70, + receiver: 69, + content: '으아아아삼 테스트테스트으아으아으앙', + isSeen: true, + createdAt: '2023-11-07T11:18:40.425Z', + updatedAt: '2023-11-07T11:18:52.075Z', __v: 0, }, ], @@ -62,16 +55,24 @@ export function ApiGetChats() { }), ApiResponse({ status: 404, - description: '채팅 조회 실패.', + description: '채팅 조회 실패', content: { JSON: { example: { - message: '해당 채팅룸이 없거나 채팅이 존재하지 않습니다.', + message: '해당 유저가 속한 채팅방이 없습니다.', error: 'Not Found', statusCode: 404, }, }, }, }), + ApiHeaders([ + { + name: 'access_token', + description: '액세스 토큰', + required: true, + example: '여기에 액세스 토큰', + }, + ]), ); } diff --git a/src/chat/swagger-decorators/get-one-chat-room.decorator.ts b/src/chat/swagger-decorators/get-one-chat-room.decorator.ts index b409b9c..0eb19b2 100644 --- a/src/chat/swagger-decorators/get-one-chat-room.decorator.ts +++ b/src/chat/swagger-decorators/get-one-chat-room.decorator.ts @@ -1,11 +1,11 @@ import { applyDecorators } from '@nestjs/common'; -import { ApiOperation, ApiResponse } from '@nestjs/swagger'; +import { ApiHeaders, ApiOperation, ApiResponse } from '@nestjs/swagger'; export function ApiGetOneChatRoom() { return applyDecorators( ApiOperation({ summary: '채팅룸 단일 조회', - description: 'Header - user-token, Param - board-id', + description: 'Header - access_token, Param - roomId', }), ApiResponse({ status: 200, @@ -50,5 +50,13 @@ export function ApiGetOneChatRoom() { }, }, }), + ApiHeaders([ + { + name: 'access_token', + description: '액세스 토큰', + required: true, + example: '여기에 액세스 토큰', + }, + ]), ); }