Skip to content

Commit

Permalink
Merge pull request #492 from boostcampwm2023/BE-ChangeCreateChat-#487
Browse files Browse the repository at this point in the history
[BE/#487] 없는 유저나 게시글에 대해 채팅하려고 하면 오류 반환
  • Loading branch information
namewhat99 authored Dec 11, 2023
2 parents 12ff31a + 83506b4 commit 687dbb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
17 changes: 7 additions & 10 deletions BE/src/chat/chat.controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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')
Expand Down
30 changes: 19 additions & 11 deletions BE/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,8 @@ export class ChatService {
private chatRepository: Repository<ChatEntity>,
@InjectRepository(UserEntity)
private userRepository: Repository<UserEntity>,
@InjectRepository(PostEntity)
private postRepository: Repository<PostEntity>,
private fcmHandler: FcmHandler,
private configService: ConfigService,
) {}
Expand All @@ -45,6 +48,9 @@ export class ChatService {
userId: string,
writerId: string,
): Promise<ChatRoom> {
if (userId === writerId) {
throw new HttpException('자신과는 채팅할 수 없습니다.', 400);
}
const isExist = await this.chatRoomRepository.findOne({
where: { post_id: postId, user: userId, writer: writerId },
});
Expand All @@ -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) {
Expand Down Expand Up @@ -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 };
}
Expand Down

0 comments on commit 687dbb1

Please sign in to comment.