Skip to content

Commit

Permalink
Merge pull request #374 from boostcampwm2023/BE-feature/invite-codes
Browse files Browse the repository at this point in the history
초대 코드 중복 트랜잭션 제거, create로 확인하기
  • Loading branch information
Conut-1 authored Dec 30, 2024
2 parents 4c73868 + 8a62275 commit 5bdf581
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
8 changes: 8 additions & 0 deletions nestjs-BE/server/src/invite-codes/invite-codes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class InviteCodesController {
status: HttpStatus.BAD_REQUEST,
description: 'Space code input is missing.',
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: 'need access token',
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'Space not found.',
Expand All @@ -50,6 +54,10 @@ export class InviteCodesController {
status: HttpStatus.OK,
description: 'Returns a space associated with the invite code.',
})
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: 'need access token',
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'Invite code not found.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ describe('InviteCodesService', () => {
const testInviteCode = { inviteCode: 'test invite code' } as InviteCode;

beforeEach(() => {
(prisma.$transaction as jest.Mock) = jest.fn(async (callback) =>
callback(),
);
jest.spyOn(inviteCodesService, 'findInviteCode').mockResolvedValue(null);
(prisma.inviteCode.create as jest.Mock) = jest.fn(
async () => testInviteCode,
);
Expand All @@ -100,7 +96,7 @@ describe('InviteCodesService', () => {
const inviteCode = inviteCodesService.createInviteCode(testSpaceUuid);

await expect(inviteCode).resolves.toEqual(testInviteCode);
expect(inviteCodesService.findInviteCode).toHaveBeenCalledTimes(1);
expect(prisma.inviteCode.create).toHaveBeenCalledTimes(1);
});
});
});
31 changes: 17 additions & 14 deletions nestjs-BE/server/src/invite-codes/invite-codes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,25 @@ export class InviteCodesService {
}

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

do {
inviteCode = generateRandomString(INVITE_CODE_LENGTH);
} while (await this.findInviteCode(inviteCode));
do {
try {
inviteCode = await this.prisma.inviteCode.create({
data: {
uuid: newUuid,
inviteCode: generateRandomString(INVITE_CODE_LENGTH),
spaceUuid,
expiryDate: getExpiryDate({ hour: INVITE_CODE_EXPIRY_HOURS }),
},
});
} catch (err) {
if (err.code !== 'P2002') throw err;
}
} while (!inviteCode);

return this.prisma.inviteCode.create({
data: {
uuid: uuid(),
inviteCode,
spaceUuid,
expiryDate: getExpiryDate({ hour: INVITE_CODE_EXPIRY_HOURS }),
},
});
});
return inviteCode;
}

@Cron(CronExpression.EVERY_HOUR)
Expand Down

0 comments on commit 5bdf581

Please sign in to comment.