diff --git a/nestjs-BE/server/src/invite-codes/invite-codes.controller.ts b/nestjs-BE/server/src/invite-codes/invite-codes.controller.ts index 312572c2..a0796bcc 100644 --- a/nestjs-BE/server/src/invite-codes/invite-codes.controller.ts +++ b/nestjs-BE/server/src/invite-codes/invite-codes.controller.ts @@ -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.', @@ -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.', diff --git a/nestjs-BE/server/src/invite-codes/invite-codes.service.spec.ts b/nestjs-BE/server/src/invite-codes/invite-codes.service.spec.ts index 026f8a99..e6735dd7 100644 --- a/nestjs-BE/server/src/invite-codes/invite-codes.service.spec.ts +++ b/nestjs-BE/server/src/invite-codes/invite-codes.service.spec.ts @@ -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, ); @@ -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); }); }); }); diff --git a/nestjs-BE/server/src/invite-codes/invite-codes.service.ts b/nestjs-BE/server/src/invite-codes/invite-codes.service.ts index b55c9082..8c34e949 100644 --- a/nestjs-BE/server/src/invite-codes/invite-codes.service.ts +++ b/nestjs-BE/server/src/invite-codes/invite-codes.service.ts @@ -35,22 +35,25 @@ export class InviteCodesService { } async createInviteCode(spaceUuid: string): Promise { - 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)