Skip to content

Commit 8c4a5ca

Browse files
authored
Merge pull request #91 from boostcampwm-2022/fix/#70-S
Fix/#70-S: μ›Œν¬μŠ€νŽ˜μ΄μŠ€ 쀑볡 κ°€μž…λ˜λŠ” 버그 μˆ˜μ •
2 parents a5a4b59 + 8b84065 commit 8c4a5ca

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

β€Žserver/apis/workspace/service.test.tsβ€Ž

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ describe('create', () => {
5757

5858
describe('join', () => {
5959
const USER_ID = 1;
60+
const WORKSPACE_ID = 1;
61+
const WORKSPACE_NAME = 'Wab';
6062

6163
it('μœ νš¨ν•œ μ°Έμ—¬μ½”λ“œκ°€ μ£Όμ–΄μ§„ 경우 μ„±κ³΅ν•œλ‹€.', async () => {
62-
const WORKSPACE_ID = 1;
63-
const WORKSPACE_NAME = 'Wab';
64-
6564
workspaceModel.findOne.mockResolvedValueOnce({
6665
id: WORKSPACE_ID,
6766
name: WORKSPACE_NAME,
@@ -70,6 +69,8 @@ describe('join', () => {
7069
moms: [],
7170
});
7271

72+
workspaceModel.updateOne.mockResolvedValueOnce({ modifiedCount: 1 });
73+
7374
expect(workspaceService.join(USER_ID, VALID_CODE)).resolves.toEqual({
7475
id: WORKSPACE_ID,
7576
name: WORKSPACE_NAME,
@@ -91,6 +92,22 @@ describe('join', () => {
9192
).rejects.toThrow(InvalidJoinError);
9293
});
9394

95+
it('이미 μ°Έμ—¬ν•œ μ›Œν¬μŠ€νŽ˜μ΄μŠ€μΈ 경우 μ‹€νŒ¨ν•œλ‹€.', async () => {
96+
workspaceModel.findOne.mockResolvedValueOnce({
97+
id: WORKSPACE_ID,
98+
name: WORKSPACE_NAME,
99+
code: VALID_CODE,
100+
users: [],
101+
moms: [],
102+
});
103+
104+
workspaceModel.updateOne.mockResolvedValueOnce({ modifiedCount: 0 });
105+
106+
expect(() => workspaceService.join(USER_ID, VALID_CODE)).rejects.toThrow(
107+
InvalidJoinError,
108+
);
109+
});
110+
94111
it('db μ—…λ°μ΄νŠΈ 쀑 μ—λŸ¬κ°€ λ°œμƒν•˜λ©΄ μ‹€νŒ¨ν•œλ‹€.', async () => {
95112
workspaceModel.updateOne.mockRejectedValueOnce(
96113
new Error('Some error in database operation'),

β€Žserver/apis/workspace/service.tsβ€Ž

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

2828
const { id, name } = workspace;
2929

30-
await workspaceModel.updateOne({ id }, { $push: { users: userId } });
31-
await userModel.updateOne({ id: userId }, { $push: { workspaces: id } });
30+
const res = await workspaceModel.updateOne(
31+
{ id },
32+
{ $addToSet: { users: userId } },
33+
);
34+
35+
if (!res.modifiedCount) {
36+
throw new InvalidJoinError('이미 μ°Έμ—¬ν•œ μ›Œν¬μŠ€νŽ˜μ΄μŠ€μ—μš” ^^');
37+
}
38+
39+
await userModel.updateOne({ id: userId }, { $addToSet: { workspaces: id } });
3240

3341
return { id, name, code };
3442
};

0 commit comments

Comments
Β (0)