Skip to content

Commit

Permalink
Merge pull request #71 from modern-agile-team/feature/notice
Browse files Browse the repository at this point in the history
Add(hobiJeong/notice) : 읽지 않은 채팅 개수 불러오기(날짜기준)
  • Loading branch information
hobiJeong authored Oct 24, 2023
2 parents 44c18e3 + edd02e1 commit 292fe21
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/chat/controllers/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
Delete,
Get,
Param,
ParseIntPipe,
Post,
Query,
Sse,
UploadedFile,
UseInterceptors,
Expand All @@ -25,6 +27,7 @@ import { ApiGetOneChatRoom } from '../swagger-decorators/get-one-chat-room.decor
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';

@ApiTags('CHAT')
@Controller('chat-room')
Expand Down Expand Up @@ -92,4 +95,13 @@ export class ChatController {
file,
);
}

@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);
}
}
3 changes: 2 additions & 1 deletion src/chat/dto/post-chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ export class PostChatDto {
description: 'FormData. 이미지 파일',
})
@IsOptional()
imageUrl: FormData;
@IsString()
imageUrl: string;
}
3 changes: 1 addition & 2 deletions src/chat/dto/received-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsNotEmpty, IsNumber } from 'class-validator';
import { IsNumber } from 'class-validator';

export class ReceivedUserDto {
@ApiProperty({
Expand All @@ -9,6 +9,5 @@ export class ReceivedUserDto {
})
@Type(() => Number)
@IsNumber()
@IsNotEmpty()
receiverId: number;
}
6 changes: 6 additions & 0 deletions src/chat/repositories/chat.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@ 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) } }],
});
}
}
8 changes: 8 additions & 0 deletions src/chat/services/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,12 @@ export class ChatService {

return chat;
}

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);
}
}
46 changes: 46 additions & 0 deletions src/chat/swagger-decorators/get-chat-unread-counts.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { applyDecorators } from '@nestjs/common';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';

export function ApiGetChatUnreadCounts() {
return applyDecorators(
ApiOperation({
summary: '지정한 시간 이후로 읽지 않은 채팅 개수 받아오기',
description: 'getTime()',
}),
ApiResponse({
status: 200,
description: '개수 받아오기 성공',
content: {
JSON: {
example: 161,
},
},
}),
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,
},
},
},
}),
);
}

0 comments on commit 292fe21

Please sign in to comment.