Skip to content

Commit

Permalink
Merge(hyunsoo/chat): 약속 관련기능, 에러처리기능 구현 #92
Browse files Browse the repository at this point in the history
Feature/Refactor(hyunsoo/chat): 약속내용 전송기능구현// 현재, 이전 채팅 내역에서 약속, 메세지, 이미지일때 구분하도록 로직 수정//웹 소켓 서버에서 에러가 날때 @error로 이벤트 처리 #92
  • Loading branch information
Kimsoo0119 authored Mar 18, 2023
2 parents 56069e3 + fcb75a1 commit d91f391
Show file tree
Hide file tree
Showing 15 changed files with 378 additions and 226 deletions.
48 changes: 39 additions & 9 deletions main-project/src/chats/chats-gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import { Socket } from 'socket.io';
import { Boards } from 'src/boards/entity/board.entity';
import { BoardsRepository } from 'src/boards/repository/board.repository';
import { UserType } from 'src/common/configs/user-type.config';
import { Meetings } from 'src/meetings/entity/meeting.entity';
import { MeetingRepository } from 'src/meetings/repository/meeting.repository';
import { EntityManager } from 'typeorm';
import { CreateChatDto } from './dto/create-chat.dto';
import { MessagePayloadDto } from './dto/message-payload.dto';
import { SendMeetingDto } from './dto/send-meeting.dto';
import { ChatList } from './entity/chat-list.entity';
import {
ChatMessage,
ChatRoom,
ChatRoomBeforeCreate,
ChatRoomOfBoard,
Expand All @@ -32,7 +36,7 @@ export class ChatsGatewayService {
private readonly chatListRepository: ChatListRepository,
private readonly chatUsersRepository: ChatUsersRepository,
private readonly chatLogRepository: ChatLogRepository,
private readonly boardRepository: BoardsRepository,
private readonly meetingRepository: MeetingRepository,
) {}

async initSocket(socket, userNo: number): Promise<any> {
Expand Down Expand Up @@ -71,8 +75,7 @@ export class ChatsGatewayService {
const { chatRoomNo, message }: MessagePayloadDto = messagePayload;

await this.checkChatRoom(chatRoomNo, userNo);

await this.saveMessage(messagePayload);
await this.saveMessage({ chatRoomNo, userNo, message });

socket.broadcast.to(`${chatRoomNo}`).emit('message', {
message,
Expand All @@ -91,10 +94,10 @@ export class ChatsGatewayService {

await this.checkChatRoom(chatRoomNo, userNo);

const chatLogNo: number = await this.saveMessageByEntityManager(
manager,
messagePayload,
);
const chatLogNo: number = await this.saveMessageByEntityManager(manager, {
chatRoomNo,
userNo,
});

await this.saveFileUrls(manager, messagePayload, chatLogNo);

Expand All @@ -107,7 +110,7 @@ export class ChatsGatewayService {

private async saveMessageByEntityManager(
manager: EntityManager,
messagePayload: MessagePayloadDto,
messagePayload: ChatMessage,
): Promise<number> {
const insertId: number = await manager
.getCustomRepository(ChatLogRepository)
Expand Down Expand Up @@ -139,7 +142,7 @@ export class ChatsGatewayService {
}
}

private async saveMessage(messagePayload: MessagePayloadDto): Promise<void> {
private async saveMessage(messagePayload: ChatMessage): Promise<void> {
const insertId: number = await this.chatLogRepository.saveMessage(
messagePayload,
);
Expand Down Expand Up @@ -167,6 +170,7 @@ export class ChatsGatewayService {
throw new BadRequestException('채팅방에 유저의 정보가 없습니다.');
}
}

async leaveChatRoom(
userNo: number,
socket: Socket,
Expand All @@ -180,4 +184,30 @@ export class ChatsGatewayService {
.to(`${chatRoomNo}`)
.emit('message', { message: `${userNo}가 나갔습니다.` });
}

async sendMeetingMessage(
socket: Socket,
userNo: number,
messagePayload: SendMeetingDto,
) {
const { location, time, chatRoomNo, meetingNo } = messagePayload;

await this.checkMeetingExistence(meetingNo, chatRoomNo);
await this.saveMessage({ chatRoomNo, userNo, meetingNo });

socket.broadcast
.to(`${chatRoomNo}`)
.emit('message', { meeting: { location, time } });
}

private async checkMeetingExistence(meetingNo, chatRoomNo): Promise<void> {
const meeting: Meetings =
await this.meetingRepository.getMeetingByChatRoomAndMeetingNumber(
meetingNo,
chatRoomNo,
);
if (!meeting) {
throw new NotFoundException(`해당하는 약속 정보를 찾을 수 없습니다.`);
}
}
}
99 changes: 50 additions & 49 deletions main-project/src/chats/chats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { APIResponse } from 'src/common/interface/interface';
import { TransactionInterceptor } from 'src/common/interceptor/transaction-interceptor';
import { TransactionDecorator } from 'src/common/decorator/transaction-manager.decorator';
import { EntityManager } from 'typeorm';
import { Delete, UseGuards } from '@nestjs/common/decorators';
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';
Expand All @@ -37,23 +37,18 @@ export class ChatsController {
private readonly chatControllerService: ChatsControllerService,
private readonly awsService: AwsService,
) {}
@Post('/:boardNo/:guestTeamNo')
@ApiCreateChatRoom()

@Get('/:chatRoomNo/chat-log')
@ApiGetCurrentChatLog()
@UseGuards(JwtAuthGuard)
@UseInterceptors(TransactionInterceptor)
async createChatRoom(
async getCurrentChatLog(
@GetUser() userNo: number,
@TransactionDecorator() manager: EntityManager,
@Param('boardNo', ParseIntPipe) boardNo: number,
@Param('guestTeamNo', ParseIntPipe) guestTeamNo: number,
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
): Promise<APIResponse> {
await this.chatControllerService.createChatRoom(
userNo,
manager,
boardNo,
guestTeamNo,
);
return { msg: '여름 신청 수락' };
const currentChatLog: ChatLog[] =
await this.chatControllerService.getCurrentChatLog(userNo, chatRoomNo);

return { response: { currentChatLog } };
}

@Get('/:chatRoomNo/chat-log/:currentChatLogNo')
Expand All @@ -74,39 +69,23 @@ export class ChatsController {
return { response: { previousChatLog } };
}

@Get('/:chatRoomNo/chat-log')
@ApiGetCurrentChatLog()
@UseGuards(JwtAuthGuard)
async getCurrentChatLog(
@GetUser() userNo: number,
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
): Promise<APIResponse> {
const currentChatLog: ChatLog[] =
await this.chatControllerService.getCurrentChatLog(userNo, chatRoomNo);

return { response: { currentChatLog } };
}

@Post('/:chatRoomNo/invitation/:userNo')
@Post('/:chatRoomNo/invitation')
@ApiRejecteInvitation()
@UseGuards(JwtAuthGuard)
@UseInterceptors(TransactionInterceptor)
@ApiInviteUser()
async inviteUser(
@GetUser() targetUserNo: number,
async rejecteInvitation(
@GetUser() userNo: number,
@TransactionDecorator() manager: EntityManager,
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
@Param('userNo', ParseIntPipe) userNo: number,
@Body() invitation: AcceptInvitationDto,
): Promise<APIResponse> {
await this.chatControllerService.inviteUser(
userNo,
await this.chatControllerService.rejecteInvitation(
manager,
targetUserNo,
userNo,
chatRoomNo,
invitation,
);

return {
msg: '채팅방 초대 성공',
};
return { msg: '채팅방 초대 거절 성공' };
}

@Patch('/:chatRoomNo/invitation')
Expand All @@ -131,23 +110,26 @@ export class ChatsController {
};
}

@Post('/:chatRoomNo/invitation')
@ApiRejecteInvitation()
@Post('/:chatRoomNo/invitation/:userNo')
@UseGuards(JwtAuthGuard)
@UseInterceptors(TransactionInterceptor)
async rejecteInvitation(
@GetUser() userNo: number,
@ApiInviteUser()
async inviteUser(
@GetUser() targetUserNo: number,
@TransactionDecorator() manager: EntityManager,
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
@Body() invitation: AcceptInvitationDto,
@Param('userNo', ParseIntPipe) userNo: number,
): Promise<APIResponse> {
await this.chatControllerService.rejecteInvitation(
manager,
await this.chatControllerService.inviteUser(
userNo,
manager,
targetUserNo,
chatRoomNo,
invitation,
);
return { msg: '채팅방 초대 거절 성공' };

return {
msg: '채팅방 초대 성공',
};
}

@Post('/:chatRoomNo/files')
Expand All @@ -167,4 +149,23 @@ export class ChatsController {
response: { uploadedFileUrlList },
};
}

@Post('/:boardNo/:guestTeamNo')
@ApiCreateChatRoom()
@UseGuards(JwtAuthGuard)
@UseInterceptors(TransactionInterceptor)
async createChatRoom(
@GetUser() userNo: number,
@TransactionDecorator() manager: EntityManager,
@Param('boardNo', ParseIntPipe) boardNo: number,
@Param('guestTeamNo', ParseIntPipe) guestTeamNo: number,
): Promise<APIResponse> {
await this.chatControllerService.createChatRoom(
userNo,
manager,
boardNo,
guestTeamNo,
);
return { msg: '여름 신청 수락' };
}
}
Loading

0 comments on commit d91f391

Please sign in to comment.