Skip to content

Commit 8b84065

Browse files
committed
refactor: 중복 참여 확인 로직 개선
중복 참여 여부를 db 접근을 통해 확인하던 방식에서 addToSet 연산 결과로 확인하도록 변경
1 parent fb71556 commit 8b84065

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

server/apis/workspace/service.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const userModel = require('@apis/user/model');
21
const workspaceModel = require('./model');
32
const workspaceService = require('./service');
43
const { default: InvalidJoinError } = require('@errors/invalid-join-error');
@@ -69,7 +68,8 @@ describe('join', () => {
6968
users: [],
7069
moms: [],
7170
});
72-
userModel.find.mockResolvedValueOnce([{ workspaces: [] }]);
71+
72+
workspaceModel.updateOne.mockResolvedValueOnce({ modifiedCount: 1 });
7373

7474
expect(workspaceService.join(USER_ID, VALID_CODE)).resolves.toEqual({
7575
id: WORKSPACE_ID,
@@ -100,7 +100,8 @@ describe('join', () => {
100100
users: [],
101101
moms: [],
102102
});
103-
userModel.find.mockResolvedValueOnce([{ workspaces: [WORKSPACE_ID] }]);
103+
104+
workspaceModel.updateOne.mockResolvedValueOnce({ modifiedCount: 0 });
104105

105106
expect(() => workspaceService.join(USER_ID, VALID_CODE)).rejects.toThrow(
106107
InvalidJoinError,

server/apis/workspace/service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ export const join = async (userId: number, code: string) => {
2727

2828
const { id, name } = workspace;
2929

30-
const userWorkspaces = (
31-
await userModel.find({ id: userId }, { workspaces: 1, _id: 0 })
32-
)[0].workspaces;
30+
const res = await workspaceModel.updateOne(
31+
{ id },
32+
{ $addToSet: { users: userId } },
33+
);
3334

34-
if (userWorkspaces.includes(id)) {
35+
if (!res.modifiedCount) {
3536
throw new InvalidJoinError('이미 참여한 워크스페이스에요 ^^');
3637
}
3738

38-
await workspaceModel.updateOne({ id }, { $addToSet: { users: userId } });
3939
await userModel.updateOne({ id: userId }, { $addToSet: { workspaces: id } });
4040

4141
return { id, name, code };

0 commit comments

Comments
 (0)