Skip to content

Commit

Permalink
Merge(hyunsoo/chat): 채팅 컨트롤러 스웨거 커스텀 한 스웨거로 변경 #92
Browse files Browse the repository at this point in the history
Refactor(hyunsoo/chat): 채팅 컨트롤러 스웨거 커스텀 한 스웨거로 변경 #92
  • Loading branch information
Kimsoo0119 authored Feb 14, 2023
2 parents 48a68ac + a0c0cda commit d1267b3
Show file tree
Hide file tree
Showing 16 changed files with 474 additions and 50 deletions.
19 changes: 6 additions & 13 deletions main-project/src/chats/chats-controller.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ export class ChatsControllerService {
chatRoomNo: number,
currentChatLogNo: number,
): Promise<ChatLog[]> {
await this.checkChatRoomExists(chatRoomNo);

await this.checkUserInChatRoom({
userNo,
chatRoomNo,
Expand All @@ -59,8 +57,6 @@ export class ChatsControllerService {
userNo: number,
chatRoomNo: number,
): Promise<ChatLog[]> {
await this.checkChatRoomExists(chatRoomNo);

await this.checkUserInChatRoom({
userNo,
chatRoomNo,
Expand Down Expand Up @@ -118,8 +114,6 @@ export class ChatsControllerService {
targetUserNo: number,
chatRoomNo: number,
): Promise<void> {
await this.checkChatRoomExists(chatRoomNo);

const inviter: ChatUser = await this.checkUserInChatRoom({
userNo: targetUserNo,
chatRoomNo,
Expand All @@ -130,6 +124,7 @@ export class ChatsControllerService {
chatRoomNo,
isNeededUser: false,
});
console.log(inviter);

await this.saveNotice(manager, {
userNo,
Expand Down Expand Up @@ -186,11 +181,9 @@ export class ChatsControllerService {
async acceptInvitation(
userNo: number,
chatRoomNo: number,
invitationInfo: AcceptInvitationDto,
{ senderNo, receiverNo, type }: AcceptInvitationDto,
): Promise<void> {
const { inviterNo, targetUserNo, type }: AcceptInvitationDto =
invitationInfo;
if (userNo !== targetUserNo) {
if (userNo !== receiverNo) {
throw new BadRequestException(`초대받은 유저만 수락할 수 있습니다.`);
}
if (type !== NoticeType.INVITE_HOST && type !== NoticeType.INVITE_GUEST) {
Expand All @@ -200,18 +193,18 @@ export class ChatsControllerService {
type === NoticeType.INVITE_HOST ? UserType.HOST : UserType.GUEST;

await this.checkUserInChatRoom({
userNo: inviterNo,
userNo: senderNo,
chatRoomNo,
isNeededUser: true,
});

await this.checkUserInChatRoom({
userNo: targetUserNo,
userNo,
chatRoomNo,
isNeededUser: false,
});

await this.joinChatRoom({ userNo: targetUserNo, chatRoomNo, userType });
await this.joinChatRoom({ userNo, chatRoomNo, userType });
}

private async joinChatRoom(chatUserInfo: ChatUser): Promise<void> {
Expand Down
35 changes: 12 additions & 23 deletions main-project/src/chats/chats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import { EntityManager } from 'typeorm';
import { UseGuards } from '@nestjs/common/decorators';
import { JwtAuthGuard } from 'src/common/guards/jwt-auth.guard';
import { GetUser } from 'src/common/decorator/get-user.decorator';
import { ApiGetPreviousChatLog } from './swagger/get-previous-chat-log.decorator';
import { ApiGetCurrentChatLog } from './swagger/get-current-chat-log.decorator';
import { ApiInviteUser } from './swagger/invite-user.decorator';
import { ApiAcceptInvitation } from './swagger/accept-invitation.decorator';
import { APiUploadFile } from './swagger/upload-file.decorator';

@Controller('chats')
@ApiTags('채팅 APi')
Expand All @@ -32,10 +37,7 @@ export class ChatsController {
) {}

@Get('/:chatRoomNo/chat-log/:currentChatLogNo')
@ApiOperation({
summary: '이전 채팅 내역 API',
description: '이전 채팅 내역 조회',
})
@ApiGetPreviousChatLog()
@UseGuards(JwtAuthGuard)
async getPreviousChatLog(
@GetUser() userNo: number,
Expand All @@ -53,10 +55,7 @@ export class ChatsController {
}

@Get('/:chatRoomNo/chat-log')
@ApiOperation({
summary: '현재 채팅 내역 API',
description: '채팅방에 들어갔을때 가장 최신 채팅 내역 조회',
})
@ApiGetCurrentChatLog()
@UseGuards(JwtAuthGuard)
async getCurrentChatLog(
@GetUser() userNo: number,
Expand All @@ -69,12 +68,9 @@ export class ChatsController {
}

@Post('/:chatRoomNo/invitation/:userNo')
@ApiOperation({
summary: '채팅방 초대 API',
description: '알람을 통해 채팅방 초대',
})
@UseGuards(JwtAuthGuard)
@UseInterceptors(TransactionInterceptor)
@ApiInviteUser()
async inviteUser(
@GetUser() targetUserNo: number,
@TransactionDecorator() manager: EntityManager,
Expand All @@ -94,20 +90,17 @@ export class ChatsController {
}

@Patch('/:chatRoomNo/invitation')
@ApiOperation({
summary: '채팅방 초대 수락 API',
description: '유저 번호, 타입, 채팅방 번호를 통해 초대 수락',
})
@ApiAcceptInvitation()
@UseGuards(JwtAuthGuard)
async acceptInvitation(
@GetUser() userNo: number,
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
@Body() invitationInfo: AcceptInvitationDto,
@Body() invitation: AcceptInvitationDto,
): Promise<APIResponse> {
await this.chatControllerService.acceptInvitation(
userNo,
chatRoomNo,
invitationInfo,
invitation,
);

return {
Expand All @@ -116,11 +109,7 @@ export class ChatsController {
}

@Post('/:chatRoomNo/files')
@ApiOperation({
summary: '파일 전송 API',
description:
'files에 담긴 최대 10개의 파일을 전달받아 s3업로드 후 url배열 반환',
})
@APiUploadFile()
@UseInterceptors(FilesInterceptor('files', 10)) // 10은 최대파일개수
async uploadFile(
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
Expand Down
4 changes: 2 additions & 2 deletions main-project/src/chats/dto/accept-invitation.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { IsNotEmpty, IsNumber } from 'class-validator';
export class AcceptInvitationDto {
@IsNumber()
@IsNotEmpty()
inviterNo: number;
senderNo: number;

@IsNumber()
@IsNotEmpty()
targetUserNo: number;
receiverNo: number;

@IsNumber()
@IsNotEmpty()
Expand Down
83 changes: 83 additions & 0 deletions main-project/src/chats/swagger/accept-invitation.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { applyDecorators } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiBody,
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
} from '@nestjs/swagger';
import { number } from 'joi';
import { SwaggerApiResponse } from 'src/common/swagger/api-response.swagger';

export function ApiAcceptInvitation() {
return applyDecorators(
ApiOperation({
summary: '채팅방 초대 수락 ',
description: '유저 번호, 타입, 채팅방 번호를 통해 초대 수락',
}),
ApiBearerAuth(),
ApiBody({
schema: {
type: 'object',
properties: {
senderNo: {
type: 'number',
minLength: 1,
example: 9,
nullable: false,
description: '초대한 유저의 userNo',
},
receiverNo: {
type: 'number',
minLength: 1,
example: 9,
nullable: false,
description: '초대받은 유저의 userNo',
},
type: {
type: 'number',
maxLength: 1,
nullable: false,
description: '알람 typeNo',
},
},
},
}),
ApiOkResponse(
SwaggerApiResponse.success('채팅방 초대 수락', '채팅방 초대 성공'),
),
ApiNotFoundResponse(
SwaggerApiResponse.exception([
{
name: 'chatRoomNotFound',
example: { msg: '존재하지 않는 채팅방입니다.' },
},
{
name: 'userNotFoundInChatRoom',
example: { msg: '채팅방에서 ( )님의 정보를 찾을 수 없습니다.' },
},
]),
),
ApiBadRequestResponse(
SwaggerApiResponse.exception([
{
name: 'existingChatRoomUser',
example: { msg: '채팅방에 이미 ( )님이 존재합니다.' },
},
{
name: 'alreadyInvitedUser',
example: { msg: '이미 초대를 보낸 상태입니다.' },
},
{
name: 'incorrectUserNumber',
example: { msg: '초대받은 유저만 수락할 수 있습니다.' },
},
{
name: 'invalidNoticeType',
example: { msg: '잘못된 Notice 타입입니다.' },
},
]),
),
);
}
Loading

0 comments on commit d1267b3

Please sign in to comment.