Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading