From 83506b47c77b95208e9691196543b4bab2131047 Mon Sep 17 00:00:00 2001 From: koomin1227 Date: Mon, 11 Dec 2023 19:00:29 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20Feat=20:=20=EC=97=86=EB=8A=94=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=80=EB=82=98=20=EA=B2=8C=EC=8B=9C=EA=B8=80?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=B4=20=EC=B1=84=ED=8C=85=ED=95=98?= =?UTF-8?q?=EB=A0=A4=EA=B3=A0=20=ED=95=98=EB=A9=B4=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/chat/chat.controller.ts | 17 +++++++---------- BE/src/chat/chat.service.ts | 30 +++++++++++++++++++----------- 2 files changed, 26 insertions(+), 21 deletions(-) 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 6419957..6b3fffa 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 }; }