diff --git a/BE/src/chat/chat.controller.ts b/BE/src/chat/chat.controller.ts index dc5631e..dff3460 100644 --- a/BE/src/chat/chat.controller.ts +++ b/BE/src/chat/chat.controller.ts @@ -1,11 +1,11 @@ import { + Body, Controller, Get, - Post, - Body, + HttpException, Param, + Post, UseGuards, - HttpException, } from '@nestjs/common'; import { ChatService } from './chat.service'; import { AuthGuard } from '../utils/auth.guard'; @@ -36,17 +36,14 @@ export class ChatController { @Post('room') @UseGuards(AuthGuard) async roomCreate(@Body() body: CreateRoomDto, @UserHash() userId: string) { - const room = await this.chatService.createRoom( + const isUserPostExist = await this.chatService.isUserPostExist( body.post_id, - userId, body.writer, ); - - if (room === null) { - throw new HttpException('해당 post 는 없습니다', 404); - } else { - return room; + if (!isUserPostExist) { + throw new HttpException('해당 게시글 또는 유저가 없습니다', 404); } + return await this.chatService.createRoom(body.post_id, userId, body.writer); } @Get('unread') diff --git a/BE/src/chat/chat.service.ts b/BE/src/chat/chat.service.ts index 6359cad..fb1548a 100644 --- a/BE/src/chat/chat.service.ts +++ b/BE/src/chat/chat.service.ts @@ -9,6 +9,7 @@ import { FcmHandler, PushMessage } from '../utils/fcmHandler'; import * as jwt from 'jsonwebtoken'; import { ConfigService } from '@nestjs/config'; import { JwtPayload } from 'jsonwebtoken'; +import { PostEntity } from '../entities/post.entity'; export interface ChatRoom { room_id: number; @@ -26,6 +27,8 @@ export class ChatService { private chatRepository: Repository, @InjectRepository(UserEntity) private userRepository: Repository, + @InjectRepository(PostEntity) + private postRepository: Repository, private fcmHandler: FcmHandler, private configService: ConfigService, ) {} @@ -45,6 +48,9 @@ export class ChatService { userId: string, writerId: string, ): Promise { + if (userId === writerId) { + throw new HttpException('자신과는 채팅할 수 없습니다.', 400); + } const isExist = await this.chatRoomRepository.findOne({ where: { post_id: postId, user: userId, writer: writerId }, }); @@ -56,16 +62,18 @@ export class ChatService { chatRoom.writer = writerId; chatRoom.user = userId; - try { - const roomId = (await this.chatRoomRepository.save(chatRoom)).id; - return { room_id: roomId }; - } catch (e) { - if (e.errno === 1452) { - return null; - } else { - throw new HttpException('서버 오류', 500); - } - } + const roomId = (await this.chatRoomRepository.save(chatRoom)).id; + return { room_id: roomId }; + } + + async isUserPostExist(postId: number, writerId: string) { + const isUserExist = await this.userRepository.exist({ + where: { user_hash: writerId }, + }); + const isPostExist = await this.postRepository.exist({ + where: { id: postId }, + }); + return isPostExist && isUserExist; } async findRoomList(userId: string) { @@ -197,7 +205,7 @@ export class ChatService { ]) .getRawMany(); - for (let room of rooms) { + for (const room of rooms) { if (room.sender !== userId && room.is_read === 0) { return { all_read: false }; }