Skip to content

Commit 9e2672e

Browse files
authored
Merge pull request #174 from umc-commit/refactor/173-chatroom-api
[REFACTOR] chat api 수정
2 parents fb2fc36 + d20565c commit 9e2672e

File tree

10 files changed

+138
-64
lines changed

10 files changed

+138
-64
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- 기존 FK 삭제
2+
ALTER TABLE `chatrooms` DROP FOREIGN KEY `chatrooms_consumer_id_fkey`;
3+
ALTER TABLE `chatrooms` DROP FOREIGN KEY `chatrooms_request_id_fkey`;
4+
5+
-- request_id → commission_id
6+
ALTER TABLE `chatrooms`
7+
CHANGE COLUMN `request_id` `commission_id` BIGINT NOT NULL;
8+
9+
-- consumer_id → user_id
10+
ALTER TABLE `chatrooms`
11+
CHANGE COLUMN `consumer_id` `user_id` BIGINT NOT NULL;
12+
13+
-- hidden_consumer → hidden_user
14+
ALTER TABLE `chatrooms`
15+
CHANGE COLUMN `hidden_consumer` `hidden_user` BOOLEAN NOT NULL DEFAULT false;
16+
17+
-- FK 재생성 (이름 변경된 컬럼에 맞게)
18+
ALTER TABLE `chatrooms`
19+
ADD CONSTRAINT `chatrooms_user_id_fkey`
20+
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
21+
ON DELETE RESTRICT ON UPDATE CASCADE;
22+
23+
ALTER TABLE `chatrooms`
24+
ADD CONSTRAINT `chatrooms_commission_id_fkey`
25+
FOREIGN KEY (`commission_id`) REFERENCES `requests`(`id`)
26+
ON DELETE RESTRICT ON UPDATE CASCADE;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `commission_id` on the `chatrooms` table. All the data in the column will be lost.
5+
- Added the required column `commission_Id` to the `chatrooms` table without a default value. This is not possible if the table is not empty.
6+
7+
*/
8+
-- DropForeignKey
9+
ALTER TABLE `chatrooms` DROP FOREIGN KEY `chatrooms_commission_id_fkey`;
10+
11+
-- DropIndex
12+
DROP INDEX `chatrooms_commission_id_fkey` ON `chatrooms`;
13+
14+
-- AlterTable
15+
ALTER TABLE `chatrooms` DROP COLUMN `commission_id`,
16+
ADD COLUMN `commission_Id` BIGINT NOT NULL;
17+
18+
-- AddForeignKey
19+
ALTER TABLE `chatrooms` ADD CONSTRAINT `chatrooms_commission_Id_fkey` FOREIGN KEY (`commission_Id`) REFERENCES `commissions`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

prisma/schema.prisma

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ model Commission {
112112
category Category @relation(fields: [categoryId], references: [id])
113113
requests Request[]
114114
bookmarks Bookmark[]
115+
chatrooms Chatroom[]
115116
commissionTags CommissionTag[]
116117
117118
@@map("commissions")
@@ -145,7 +146,6 @@ model Request {
145146
146147
user User @relation(fields: [userId], references: [id])
147148
commission Commission @relation(fields: [commissionId], references: [id])
148-
chatrooms Chatroom[]
149149
reviews Review[]
150150
PointTransaction PointTransaction[]
151151
@@ -212,16 +212,16 @@ model PointTransaction {
212212

213213
model Chatroom {
214214
id BigInt @id @default(autoincrement())
215-
requestId BigInt @map("request_id")
216-
consumerId BigInt @map("consumer_id")
215+
commissionId BigInt @map("commission_Id")
216+
userId BigInt @map("user_id")
217217
artistId BigInt @map("artist_id")
218218
hiddenArtist Boolean @default(false) @map("hidden_artist")
219-
hiddenConsumer Boolean @default(false) @map("hidden_consumer")
219+
hiddenUser Boolean @default(false) @map("hidden_user")
220220
createdAt DateTime @default(now()) @map("created_at")
221221
222-
request Request @relation(fields: [requestId], references: [id])
223-
consumer User @relation("ConsumerChatrooms", fields: [consumerId], references: [id])
224-
artist Artist @relation("ArtistChatrooms", fields: [artistId], references: [id])
222+
commission Commission @relation(fields: [commissionId], references: [id])
223+
user User @relation("ConsumerChatrooms", fields: [userId], references: [id])
224+
artist Artist @relation("ArtistChatrooms", fields: [artistId], references: [id])
225225
chatMessages ChatMessage[]
226226
227227
@@map("chatrooms")

prisma/seed.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,38 @@ async function main() {
5454
},
5555
});
5656

57+
// Artist 2 생성
58+
const artistAccount2 = await prisma.account.create({
59+
data: {
60+
provider: "kakao",
61+
oauthId: "artist2_oauth_id",
62+
},
63+
});
64+
const artist2 = await prisma.artist.create({
65+
data: {
66+
accountId: artistAccount2.id,
67+
nickname: "artist_two",
68+
description: "두 번째 테스트용 작가입니다.",
69+
profileImage: "https://example.com/artist2.png",
70+
},
71+
});
72+
73+
// Artist 3 생성
74+
const artistAccount3 = await prisma.account.create({
75+
data: {
76+
provider: "kakao",
77+
oauthId: "artist3_oauth_id",
78+
},
79+
});
80+
const artist3 = await prisma.artist.create({
81+
data: {
82+
accountId: artistAccount3.id,
83+
nickname: "artist_three",
84+
description: "세 번째 테스트용 작가입니다.",
85+
profileImage: "https://example.com/artist3.png",
86+
},
87+
});
88+
5789
// Category 생성
5890
const category1 = await prisma.category.create({
5991
data: {

src/chat/controller/chatroom.controller.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { parseWithBigInt, stringifyWithBigInt } from "../../bigintJson.js";
77

88
export const createChatroom = async (req, res, next) => {
99
try {
10-
const consumerId = BigInt(req.user.userId);
10+
const userId = BigInt(req.user.userId);
1111

1212
const dto = new CreateChatroomDto({
13-
consumerId: consumerId,
13+
userId,
1414
artistId: BigInt(req.body.artistId),
15-
requestId: BigInt(req.body.requestId),
15+
commissionId: BigInt(req.body.commissionId),
1616
});
1717

1818
const chatroom = await ChatroomService.createChatroom(dto);
@@ -26,8 +26,10 @@ export const createChatroom = async (req, res, next) => {
2626

2727
export const getChatroom = async (req, res, next) => {
2828
try {
29+
const userId = BigInt(req.user.userId);
30+
2931
const dto = new GetChatroomDto({
30-
consumerId: BigInt(req.user.userId),
32+
userId,
3133
accountId: BigInt(req.user.accountId),
3234
});
3335

@@ -47,7 +49,7 @@ export const deleteChatrooms = async (req, res, next) => {
4749
const dto = new DeleteChatroomDto({
4850
chatroomIds: req.body.chatroomIds,
4951
userType: req.body.userType,
50-
userId: userId,
52+
userId,
5153
});
5254

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

src/chat/dto/chatroom.dto.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
export class CreateChatroomDto {
2-
constructor({ consumerId, artistId, requestId }) {
3-
this.consumerId = consumerId;
2+
constructor({ userId, artistId, commissionId }) {
3+
this.userId = userId;
44
this.artistId = artistId;
5-
this.requestId = requestId;
5+
this.commissionId = commissionId;
66
}
77
}
88

99
export class GetChatroomDto {
10-
constructor({ consumerId, accountId }) {
11-
this.consumerId = BigInt(consumerId);
10+
constructor({ userId, accountId }) {
11+
this.userId = BigInt(userId);
1212
this.accountId = BigInt(accountId);
1313
}
1414
}
@@ -19,8 +19,8 @@ export class ChatroomListResponseDto {
1919
this.artist_id = room.artist.id;
2020
this.artist_nickname = room.artist.nickname;
2121
this.artist_profile_image = room.artist.profileImage;
22-
this.request_id = room.request.id;
23-
this.request_title = room.request.commission.title;
22+
this.commission_id = room.commission.id;
23+
this.commission_title = room.commission.title;
2424
// 이미지가 있으면 이미지 URL, 없으면 텍스트 content
2525
const lastMsg = room.chatMessages[0];
2626
this.last_message = lastMsg?.imageUrl || lastMsg?.content || null;

src/chat/repository/chatroom.repository.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@ export const ChatroomRepository = {
55
console.log(data)
66
return await prisma.chatroom.create({
77
data: {
8-
consumerId: data.consumerId,
8+
userId: data.userId,
99
artistId: data.artistId,
10-
requestId: data.requestId,
10+
commissionId: data.commissionId,
1111
},
1212
});
1313
},
1414

15-
async findChatroomByUsersAndCommission(consumerId, artistId, requestId) {
15+
async findChatroomByUsersAndCommission(userId, artistId, commissionId) {
1616
return await prisma.chatroom.findFirst({
1717
where: {
18-
consumerId,
18+
userId,
1919
artistId,
20-
requestId,
20+
commissionId,
2121
},
2222
});
2323
},
2424

25-
async findChatroomsByUser(consumerId) {
25+
async findChatroomsByUser(userId) {
2626
// 1. 채팅방 기본 정보 + 마지막 메시지(내용, 생성시간, id) 조회
2727
const chatrooms = await prisma.chatroom.findMany({
28-
where: { consumerId },
28+
where: { userId },
2929
include: {
3030
artist: {
3131
select: {
@@ -34,14 +34,10 @@ export const ChatroomRepository = {
3434
profileImage: true,
3535
}
3636
},
37-
request: {
37+
commission: {
3838
select: {
3939
id: true,
40-
commission: {
41-
select: {
42-
title: true,
43-
}
44-
}
40+
title: true,
4541
}
4642
},
4743
chatMessages: {
@@ -91,8 +87,8 @@ export const ChatroomRepository = {
9187
},
9288

9389
async softDeleteChatrooms(chatroomIds, userType, userId) {
94-
const hiddenField = userType === "consumer" ? "hiddenConsumer" : "hiddenArtist";
95-
const userField = userType === "consumer" ? "consumerId" : "artistId";
90+
const hiddenField = userType === "user" ? "hiddenUser" : "hiddenArtist";
91+
const userField = userType === "user" ? "userId" : "artistId";
9692

9793
await prisma.chatroom.updateMany({
9894
where: {

src/chat/service/chat.service.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export const ChatService = {
1010
throw new ChatroomNotFoundError({ chatroomId: dto.chatroomId });
1111
}
1212

13-
if (dto.userId !== chatroom.consumerId && dto.userId !== chatroom.artistId) {
14-
throw new ForbiddenError({ consumerId: dto.userId });
13+
if (dto.userId !== chatroom.userId && dto.userId !== chatroom.artistId) {
14+
throw new ForbiddenError({ userId: dto.userId });
1515
}
1616

1717
const messages = await ChatRepository.findMessagesWithImages(dto);

src/chat/service/chatroom.service.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import { ChatroomRepository } from "../repository/chatroom.repository.js";
22
import { ChatRepository } from "../repository/chat.repository.js";
33
import { UserRepository } from "../../user/repository/user.repository.js";
4-
import { RequestRepository } from "../../request/repository/request.repository.js";
4+
import { CommissionRepository } from "../../commission/repository/commission.repository.js";
55
import { UserNotFoundError } from "../../common/errors/user.errors.js";
66
import { ArtistNotFoundError } from "../../common/errors/artist.errors.js";
7-
import { RequestNotFoundError } from "../../common/errors/request.errors.js";
7+
import { CommissionNotFoundError } from "../../common/errors/commission.errors.js";
88
import { ChatroomNotFoundError } from "../../common/errors/chat.errors.js";
99
import { ChatroomListResponseDto } from "../dto/chatroom.dto.js";
1010

1111
export const ChatroomService = {
1212
async createChatroom(dto) {
13-
const user = await UserRepository.findUserById(dto.consumerId);
13+
const user = await UserRepository.findUserById(dto.userId);
1414
if (!user) {
15-
throw new UserNotFoundError({ consumerId: dto.consumerId });
15+
throw new UserNotFoundError({ userId: dto.userId });
1616
}
1717

1818
const artist = await UserRepository.findArtistById(dto.artistId);
1919
if (!artist) {
2020
throw new ArtistNotFoundError({ artistId: dto.artistId });
2121
}
2222

23-
const request = await RequestRepository.findRequestById(dto.requestId);
24-
if (!request) {
25-
throw new RequestNotFoundError({ requestId: dto.requestId });
23+
const commission = await CommissionRepository.findCommissionById(dto.commissionId);
24+
if (!commission) {
25+
throw new CommissionNotFoundError({ commissionId: dto.commissionId });
2626
}
2727

2828
// 채팅방 중복 확인
2929
const existing = await ChatroomRepository.findChatroomByUsersAndCommission(
30-
dto.consumerId,
30+
dto.userId,
3131
dto.artistId,
32-
dto.requestId
32+
dto.commissionId
3333
);
3434

3535
// 기존 채팅방 반환
@@ -39,21 +39,21 @@ export const ChatroomService = {
3939

4040
// 채팅방이 없을 시 생성
4141
const chatroom = await ChatroomRepository.createChatroom({
42-
consumerId: dto.consumerId,
42+
userId: dto.userId,
4343
artistId: dto.artistId,
44-
requestId: dto.requestId,
44+
commissionId: dto.commissionId,
4545
});
4646

4747
return chatroom;
4848
},
4949

5050
async getChatroomsByUserId(dto) {
51-
const user = await UserRepository.findUserById(dto.consumerId);
51+
const user = await UserRepository.findUserById(dto.userId);
5252
if (!user) {
53-
throw new UserNotFoundError({ consumerId: dto.consumerId });
53+
throw new UserNotFoundError({ userId: dto.userId });
5454
}
5555

56-
const chatrooms = await ChatroomRepository.findChatroomsByUser(dto.consumerId);
56+
const chatrooms = await ChatroomRepository.findChatroomsByUser(dto.userId);
5757
console.log(dto.accountId)
5858

5959
const result = [];
@@ -76,7 +76,7 @@ export const ChatroomService = {
7676
const chatrooms = await ChatroomRepository.findChatroomsByIds(dto.chatroomIds);
7777

7878
const chatroomIdsToDelete = chatrooms
79-
.filter(cr => cr.hiddenConsumer && cr.hiddenArtist)
79+
.filter(cr => cr.hiddenUser && cr.hiddenArtist)
8080
.map(cr => cr.id);
8181

8282
if (chatroomIdsToDelete.length > 0) {

0 commit comments

Comments
 (0)