Skip to content

Commit 6683416

Browse files
authored
Merge pull request #92 from umc-commit/refactor/89-chat-api-refactor
[REFACTOR] 채팅방 API JWT 인증 적용
2 parents 42fd1b3 + 3f7eaba commit 6683416

File tree

11 files changed

+268
-55
lines changed

11 files changed

+268
-55
lines changed

.github/workflows/cd.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ jobs:
4242
ssh ec2 "echo '$ENV_FILE' > /opt/app/.env"
4343
env:
4444
ENV_FILE: ${{ secrets.ENV_FILE }}
45+
46+
- name: Restore Firebase service account key
47+
run: |
48+
ssh ec2 "mkdir -p /opt/app/config && echo $FIREBASE_SERVICE_ACCOUNT_BASE64 | base64 -d > /opt/app/config/service-account-key.json"
49+
env:
50+
FIREBASE_SERVICE_ACCOUNT_BASE64: ${{ secrets.FIREBASE_SERVICE_ACCOUNT_BASE64 }}
4551

4652
- name: Install dependencies
4753
run: ssh ec2 "cd /opt/app && npm install"

src/chat/chat.routes.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import express from "express";
22
import { createChatroom } from "./controller/chatroom.controller.js";
3-
import { showChatroom } from "./controller/chatroom.controller.js";
3+
import { getChatroom } from "./controller/chatroom.controller.js";
44
import { deleteChatrooms } from "./controller/chatroom.controller.js";
5-
import { showMessages } from "./controller/chat.controller.js";
5+
import { getMessages } from "./controller/chat.controller.js";
66
import { getMessageByKeyword } from "./controller/chat.controller.js";
7+
import { authenticate } from "../middlewares/auth.middleware.js";
78

89
const router = express.Router();
910

1011
// 채팅방 생성 API
11-
router.post("", createChatroom);
12+
router.post("", authenticate, createChatroom);
1213

1314
// 채팅방 삭제 API
14-
router.delete("/delete", deleteChatrooms);
15+
router.delete("/delete", authenticate, deleteChatrooms);
1516

16-
// 채팅 메시지 검색 API
17-
router.get("/search/messages", getMessageByKeyword);
17+
// 채팅 메시지 검색 API (다중)
18+
router.get("/search/messages", authenticate, getMessageByKeyword);
1819

1920
// 채팅방 조회 API
20-
router.get("/:consumerId", showChatroom);
21+
router.get("/list", authenticate, getChatroom);
2122

2223
// 채팅 메시지 조회 API
23-
router.get("/:chatroomId/messages", showMessages);
24+
router.get("/:chatroomId/messages", authenticate, getMessages);
2425

2526
export default router;

src/chat/controller/chat.controller.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { StatusCodes } from "http-status-codes";
22
import { parseWithBigInt, stringifyWithBigInt } from "../../bigintJson.js";
3-
import { ShowMessagesDto } from "../dto/chat.dto.js";
3+
import { GetMessagesDto } from "../dto/chat.dto.js";
44
import { ChatService } from "../service/chat.service.js";
55
import { FindChatroomByMessageDto } from "../dto/chat.dto.js";
66

7-
export const showMessages = async (req, res, next) => {
7+
export const getMessages = async (req, res, next) => {
88
try {
9-
const dto = new ShowMessagesDto ({
9+
const userId = BigInt(req.user.userId);
10+
11+
const dto = new GetMessagesDto ({
1012
chatroomId: BigInt(req.params.chatroomId),
1113
limit: req.query.limit,
1214
cursor: req.query.cursor,
15+
userId: userId,
1316
});
1417

1518
const messages = await ChatService.getMessagesByChatroomId(dto);
@@ -23,9 +26,14 @@ export const showMessages = async (req, res, next) => {
2326

2427
export const getMessageByKeyword = async (req, res, next) => {
2528
try {
26-
const dto = new FindChatroomByMessageDto(req.query);
29+
const userId = BigInt(req.user.userId);
30+
31+
const dto = new FindChatroomByMessageDto({
32+
keyword: req.query.keyword,
33+
userId: userId,
34+
});
2735

28-
const messages = await ChatService.searchMessagesByKeyword(dto.keyword);
36+
const messages = await ChatService.searchMessagesByKeyword(dto);
2937
const responseData = parseWithBigInt(stringifyWithBigInt(messages));
3038

3139
res.status(StatusCodes.OK).success(responseData);

src/chat/controller/chatroom.controller.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { StatusCodes } from "http-status-codes";
22
import { ChatroomService } from "../service/chatroom.service.js";
33
import { CreateChatroomDto } from "../dto/chatroom.dto.js";
4-
import { ShowChatroomDto } from "../dto/chatroom.dto.js";
4+
import { GetChatroomDto } from "../dto/chatroom.dto.js";
55
import { DeleteChatroomDto } from "../dto/chatroom.dto.js";
66
import { parseWithBigInt, stringifyWithBigInt } from "../../bigintJson.js";
77

88
export const createChatroom = async (req, res, next) => {
99
try {
10+
const consumerId = BigInt(req.user.userId);
11+
1012
const dto = new CreateChatroomDto({
11-
consumerId: BigInt(req.body.consumerId),
13+
consumerId: consumerId,
1214
artistId: BigInt(req.body.artistId),
1315
requestId: BigInt(req.body.requestId),
1416
});
@@ -22,10 +24,10 @@ export const createChatroom = async (req, res, next) => {
2224
}
2325
};
2426

25-
export const showChatroom = async (req, res, next) => {
27+
export const getChatroom = async (req, res, next) => {
2628
try {
27-
const dto = new ShowChatroomDto({
28-
consumerId: BigInt(req.params.consumerId)
29+
const dto = new GetChatroomDto({
30+
consumerId: BigInt(req.user.userId)
2931
});
3032

3133
const chatrooms = await ChatroomService.getChatroomsByUserId(dto);
@@ -39,9 +41,12 @@ export const showChatroom = async (req, res, next) => {
3941

4042
export const deleteChatrooms = async (req, res, next) => {
4143
try {
44+
const userId = BigInt(req.user.userId);
45+
4246
const dto = new DeleteChatroomDto({
4347
chatroomIds: req.body.chatroomIds,
4448
userType: req.body.userType,
49+
userId: userId,
4550
});
4651

4752
const chatrooms = await ChatroomService.softDeleteChatroomsByUser(dto);

src/chat/dto/chat.dto.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
export class ShowMessagesDto {
2-
constructor({ chatroomId, limit, cursor }) {
1+
export class GetMessagesDto {
2+
constructor({ chatroomId, limit, cursor, userId }) {
33
this.chatroomId = chatroomId;
44
this.limit = limit ? Number(limit) : 20;
55
this.cursor = cursor ? BigInt(cursor) : null;
6+
this.userId = BigInt(userId);
67
}
78
}
89

910
export class FindChatroomByMessageDto {
10-
constructor(query) {
11-
this.keyword = query.keyword;
11+
constructor({ keyword, userId }) {
12+
this.keyword = keyword;
13+
this.userId = BigInt(userId);
1214
}
1315
}

src/chat/dto/chatroom.dto.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ export class CreateChatroomDto {
66
}
77
}
88

9-
export class ShowChatroomDto {
9+
export class GetChatroomDto {
1010
constructor({ consumerId }) {
11-
this.consumerId = consumerId;
11+
this.consumerId = BigInt(consumerId);
1212
}
1313
}
1414

1515
export class DeleteChatroomDto {
16-
constructor({ chatroomIds, userType }) {
17-
this.chatroomIds = chatroomIds.map(id => BigInt(id));
18-
this.userType = userType;
19-
}
16+
constructor({ chatroomIds, userType, userId }) {
17+
this.chatroomIds = chatroomIds.map(id => BigInt(id));
18+
this.userType = userType;
19+
this.userId = BigInt(userId);
20+
}
2021
}

src/chat/repository/chatroom.repository.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@ export const ChatroomRepository = {
2626
return await prisma.chatroom.findMany({
2727
where: {
2828
consumerId: consumerId,
29-
},
29+
hiddenConsumer: false,
30+
}
3031
});
3132
},
3233

33-
async softDeleteChatrooms(chatroomIds, userType) {
34+
async softDeleteChatrooms(chatroomIds, userType, userId) {
3435
const hiddenField = userType === "consumer" ? "hiddenConsumer" : "hiddenArtist";
36+
const userField = userType === "consumer" ? "consumerId" : "artistId";
3537

3638
await prisma.chatroom.updateMany({
3739
where: {
38-
id: { in: chatroomIds }
40+
id: { in: chatroomIds },
41+
[userField]: userId
3942
},
4043
data: {
4144
[hiddenField]: true

src/chat/service/chat.service.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
import { ChatroomRepository } from "../repository/chatroom.repository.js";
22
import { ChatRepository } from "../repository/chat.repository.js";
33
import { ChatroomNotFoundError } from "../../common/errors/chat.errors.js";
4+
import { ForbiddenError } from "../../common/errors/chat.errors.js";
45

56
export const ChatService = {
67
async getMessagesByChatroomId(dto) {
78
const chatroom = await ChatroomRepository.findChatroomById(dto.chatroomId);
89
if (!chatroom) {
910
throw new ChatroomNotFoundError({ chatroomId: dto.chatroomId });
1011
}
12+
13+
if (dto.userId !== chatroom.consumerId && dto.userId !== chatroom.artistId) {
14+
throw new ForbiddenError({ consumerId: dto.userId });
15+
}
1116

1217
const messages = await ChatRepository.findMessagesWithImages(dto);
1318
return messages;
1419
},
1520

16-
async searchMessagesByKeyword(keyword) {
17-
const messages = await ChatRepository.searchByKeyword(keyword);
21+
async searchMessagesByKeyword(dto) {
22+
const userChatrooms = await ChatroomRepository.findChatroomsByUser(dto.userId);
23+
const chatroomIds = userChatrooms.map(cr => cr.id);
24+
25+
const messages = await ChatRepository.searchByKeyword(dto.keyword, chatroomIds);
1826
return messages;
1927
},
2028
};

src/chat/service/chatroom.service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const ChatroomService = {
6262
throw new ChatroomNotFoundError({ chatroomIds: dto.chatroomIds });
6363
}
6464

65-
await ChatroomRepository.softDeleteChatrooms(dto.chatroomIds, dto.userType);
65+
await ChatroomRepository.softDeleteChatrooms(dto.chatroomIds, dto.userType, dto.userId);
6666

6767
const chatrooms = await ChatroomRepository.findChatroomsByIds(dto.chatroomIds);
6868

src/common/errors/chat.errors.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,15 @@ export class ChatroomNotFoundError extends BaseError {
99
data,
1010
});
1111
}
12+
}
13+
14+
export class ForbiddenError extends BaseError {
15+
constructor(data = null) {
16+
super({
17+
errorCode: "M002",
18+
reason: "권한이 없습니다.",
19+
statusCode: 403,
20+
data,
21+
});
22+
}
1223
}

0 commit comments

Comments
 (0)