Skip to content

Commit 5bdf581

Browse files
authoredDec 30, 2024
Merge pull request #374 from boostcampwm2023/BE-feature/invite-codes
초대 코드 중복 트랜잭션 제거, create로 확인하기
2 parents 4c73868 + 8a62275 commit 5bdf581

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed
 

‎nestjs-BE/server/src/invite-codes/invite-codes.controller.ts

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export class InviteCodesController {
3030
status: HttpStatus.BAD_REQUEST,
3131
description: 'Space code input is missing.',
3232
})
33+
@ApiResponse({
34+
status: HttpStatus.UNAUTHORIZED,
35+
description: 'need access token',
36+
})
3337
@ApiResponse({
3438
status: HttpStatus.NOT_FOUND,
3539
description: 'Space not found.',
@@ -50,6 +54,10 @@ export class InviteCodesController {
5054
status: HttpStatus.OK,
5155
description: 'Returns a space associated with the invite code.',
5256
})
57+
@ApiResponse({
58+
status: HttpStatus.UNAUTHORIZED,
59+
description: 'need access token',
60+
})
5361
@ApiResponse({
5462
status: HttpStatus.NOT_FOUND,
5563
description: 'Invite code not found.',

‎nestjs-BE/server/src/invite-codes/invite-codes.service.spec.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ describe('InviteCodesService', () => {
8787
const testInviteCode = { inviteCode: 'test invite code' } as InviteCode;
8888

8989
beforeEach(() => {
90-
(prisma.$transaction as jest.Mock) = jest.fn(async (callback) =>
91-
callback(),
92-
);
93-
jest.spyOn(inviteCodesService, 'findInviteCode').mockResolvedValue(null);
9490
(prisma.inviteCode.create as jest.Mock) = jest.fn(
9591
async () => testInviteCode,
9692
);
@@ -100,7 +96,7 @@ describe('InviteCodesService', () => {
10096
const inviteCode = inviteCodesService.createInviteCode(testSpaceUuid);
10197

10298
await expect(inviteCode).resolves.toEqual(testInviteCode);
103-
expect(inviteCodesService.findInviteCode).toHaveBeenCalledTimes(1);
99+
expect(prisma.inviteCode.create).toHaveBeenCalledTimes(1);
104100
});
105101
});
106102
});

‎nestjs-BE/server/src/invite-codes/invite-codes.service.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,25 @@ export class InviteCodesService {
3535
}
3636

3737
async createInviteCode(spaceUuid: string): Promise<InviteCode> {
38-
return this.prisma.$transaction(async () => {
39-
let inviteCode: string;
38+
let inviteCode: InviteCode;
39+
const newUuid = uuid();
4040

41-
do {
42-
inviteCode = generateRandomString(INVITE_CODE_LENGTH);
43-
} while (await this.findInviteCode(inviteCode));
41+
do {
42+
try {
43+
inviteCode = await this.prisma.inviteCode.create({
44+
data: {
45+
uuid: newUuid,
46+
inviteCode: generateRandomString(INVITE_CODE_LENGTH),
47+
spaceUuid,
48+
expiryDate: getExpiryDate({ hour: INVITE_CODE_EXPIRY_HOURS }),
49+
},
50+
});
51+
} catch (err) {
52+
if (err.code !== 'P2002') throw err;
53+
}
54+
} while (!inviteCode);
4455

45-
return this.prisma.inviteCode.create({
46-
data: {
47-
uuid: uuid(),
48-
inviteCode,
49-
spaceUuid,
50-
expiryDate: getExpiryDate({ hour: INVITE_CODE_EXPIRY_HOURS }),
51-
},
52-
});
53-
});
56+
return inviteCode;
5457
}
5558

5659
@Cron(CronExpression.EVERY_HOUR)

0 commit comments

Comments
 (0)
Please sign in to comment.