Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

초대 코드 모듈 개선 #370

Merged
merged 6 commits into from
Dec 13, 2024
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
16 changes: 14 additions & 2 deletions nestjs-BE/server/src/invite-codes/dto/create-invite-code.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { ApiProperty } from '@nestjs/swagger';
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsString } from 'class-validator';
import { v4 as uuid } from 'uuid';

export class CreateInviteCodeDto {
@ApiProperty({
example: 'space uuid',
example: uuid(),
description: 'Profile UUID',
})
@IsNotEmpty()
@IsString()
@Expose({ name: 'profile_uuid' })
profileUuid: string;

@ApiProperty({
example: uuid(),
description: 'Space UUID',
})
@IsNotEmpty()
@IsString()
space_uuid: string;
@Expose({ name: 'space_uuid' })
spaceUuid: string;
}
44 changes: 42 additions & 2 deletions nestjs-BE/server/src/invite-codes/invite-codes.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { GoneException, HttpStatus, NotFoundException } from '@nestjs/common';
import {
ForbiddenException,
GoneException,
HttpStatus,
NotFoundException,
} from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { Space } from '@prisma/client';
import { InviteCode, Space } from '@prisma/client';
import { InviteCodesController } from './invite-codes.controller';
import { InviteCodesService } from './invite-codes.service';
import { CreateInviteCodeDto } from './dto/create-invite-code.dto';

describe('InviteCodesController', () => {
let controller: InviteCodesController;
Expand All @@ -18,6 +24,40 @@ describe('InviteCodesController', () => {
inviteCodesService = module.get<InviteCodesService>(InviteCodesService);
});

describe('createInviteCode', () => {
const testDto: CreateInviteCodeDto = {
profileUuid: 'test profile uuid',
spaceUuid: 'test space uuid',
};
const testInviteCode: InviteCode = {
inviteCode: 'test invite code',
} as InviteCode;

beforeEach(() => {
inviteCodesService.createInviteCode = jest.fn(async () => testInviteCode);
});

it('success', async () => {
const inviteCode = controller.createInviteCode(testDto);

await expect(inviteCode).resolves.toEqual({
statusCode: HttpStatus.CREATED,
message: 'Created',
data: { invite_code: testInviteCode.inviteCode },
});
});

it('profile not in space', async () => {
(inviteCodesService.createInviteCode as jest.Mock).mockRejectedValue(
new ForbiddenException(),
);

const inviteCode = controller.createInviteCode(testDto);

await expect(inviteCode).rejects.toThrow(ForbiddenException);
});
});

describe('findSpace', () => {
const testInviteCode = 'test invite code';
const testSpace: Space = {
Expand Down
7 changes: 4 additions & 3 deletions nestjs-BE/server/src/invite-codes/invite-codes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ export class InviteCodesController {
description: 'Space not found.',
})
async createInviteCode(@Body() createInviteCodeDto: CreateInviteCodeDto) {
const spaceUuid = createInviteCodeDto.space_uuid;
const inviteCode =
await this.inviteCodesService.createInviteCode(spaceUuid);
const inviteCode = await this.inviteCodesService.createInviteCode(
createInviteCodeDto.profileUuid,
createInviteCodeDto.spaceUuid,
);
return {
statusCode: HttpStatus.CREATED,
message: 'Created',
Expand Down
3 changes: 2 additions & 1 deletion nestjs-BE/server/src/invite-codes/invite-codes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
import { InviteCodesService } from './invite-codes.service';
import { InviteCodesController } from './invite-codes.controller';
import { SpacesModule } from '../spaces/spaces.module';
import { ProfileSpaceModule } from '../profile-space/profile-space.module';

@Module({
imports: [SpacesModule],
imports: [SpacesModule, ProfileSpaceModule],
controllers: [InviteCodesController],
providers: [InviteCodesService],
})
Expand Down
68 changes: 40 additions & 28 deletions nestjs-BE/server/src/invite-codes/invite-codes.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
import { Test, TestingModule } from '@nestjs/testing';
import { GoneException, NotFoundException } from '@nestjs/common';
import {
ForbiddenException,
GoneException,
NotFoundException,
} from '@nestjs/common';
import { InviteCode, Space } from '@prisma/client';
import { InviteCodesService } from './invite-codes.service';
import { SpacesService } from '../spaces/spaces.service';
import { PrismaService } from '../prisma/prisma.service';
import { ProfileSpaceService } from '../profile-space/profile-space.service';
import * as ExpiryModule from '../utils/date';

describe('InviteCodesService', () => {
let inviteCodesService: InviteCodesService;
let prisma: PrismaService;
let spacesService: SpacesService;
let profileSpaceService: ProfileSpaceService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
InviteCodesService,
{
provide: PrismaService,
useValue: {
inviteCode: {
create: jest.fn(),
},
},
},
{
provide: SpacesService,
useValue: {
findSpaceBySpaceUuid: jest.fn(),
},
},
{ provide: PrismaService, useValue: { inviteCode: {} } },
{ provide: SpacesService, useValue: {} },
{ provide: ProfileSpaceService, useValue: {} },
],
}).compile();

inviteCodesService = module.get<InviteCodesService>(InviteCodesService);
prisma = module.get<PrismaService>(PrismaService);
spacesService = module.get<SpacesService>(SpacesService);
profileSpaceService = module.get<ProfileSpaceService>(ProfileSpaceService);
});

describe('findSpace', () => {
Expand All @@ -61,9 +57,7 @@ describe('InviteCodesService', () => {
deleteInviteCodeSpy = jest
.spyOn(inviteCodesService, 'deleteInviteCode')
.mockResolvedValue(null);
(spacesService.findSpaceBySpaceUuid as jest.Mock).mockResolvedValue(
testSpace,
);
spacesService.findSpaceBySpaceUuid = jest.fn(async () => testSpace);
});

afterEach(() => {
Expand Down Expand Up @@ -97,24 +91,42 @@ describe('InviteCodesService', () => {
});

describe('createInviteCode', () => {
const testSpace: Space = {
uuid: 'test uuid',
name: 'test space',
icon: 'test icon',
};
const testProfileUuid = 'test profile uuid';
const testSpaceUuid = 'test space uuid';
const testInviteCode = { inviteCode: 'test invite code' } as InviteCode;

beforeEach(() => {
(spacesService.findSpaceBySpaceUuid as jest.Mock).mockResolvedValue(
testSpace,
profileSpaceService.isProfileInSpace = jest.fn(async () => true);
(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,
);
});

it('created', async () => {
const inviteCode = inviteCodesService.createInviteCode(
testProfileUuid,
testSpaceUuid,
);

await expect(inviteCode).resolves.toEqual(testInviteCode);
expect(inviteCodesService.findInviteCode).toHaveBeenCalledTimes(1);
});

it('space not found', async () => {
(spacesService.findSpaceBySpaceUuid as jest.Mock).mockResolvedValue(null);
(profileSpaceService.isProfileInSpace as jest.Mock).mockResolvedValue(
false,
);

const inviteCode = inviteCodesService.createInviteCode(testSpace.uuid);
const inviteCode = inviteCodesService.createInviteCode(
testProfileUuid,
testSpaceUuid,
);

await expect(inviteCode).rejects.toThrow(NotFoundException);
await expect(inviteCode).rejects.toThrow(ForbiddenException);
expect(prisma.inviteCode.create).not.toHaveBeenCalled();
});
});
Expand Down
60 changes: 36 additions & 24 deletions nestjs-BE/server/src/invite-codes/invite-codes.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { GoneException, Injectable, NotFoundException } from '@nestjs/common';
import {
ForbiddenException,
GoneException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { InviteCode, Prisma } from '@prisma/client';
import { v4 as uuid } from 'uuid';
Expand All @@ -8,13 +13,16 @@ import {
INVITE_CODE_LENGTH,
} from '../config/constants';
import { checkExpiry, getExpiryDate } from '../utils/date';
import { generateRandomString } from '../utils/random-string';
import { SpacesService } from '../spaces/spaces.service';
import { ProfileSpaceService } from '../profile-space/profile-space.service';

@Injectable()
export class InviteCodesService {
constructor(
private readonly prisma: PrismaService,
private readonly spacesService: SpacesService,
private readonly profileSpaceService: ProfileSpaceService,
) {}

async findInviteCode(inviteCode: string): Promise<InviteCode | null> {
Expand All @@ -33,16 +41,32 @@ export class InviteCodesService {
return this.spacesService.findSpaceBySpaceUuid(inviteCodeData.spaceUuid);
}

async createInviteCode(spaceUuid: string): Promise<InviteCode> {
const space = await this.spacesService.findSpaceBySpaceUuid(spaceUuid);
if (!space) throw new NotFoundException();
return this.prisma.inviteCode.create({
data: {
uuid: uuid(),
inviteCode: await this.generateUniqueInviteCode(INVITE_CODE_LENGTH),
spaceUuid: spaceUuid,
expiryDate: getExpiryDate({ hour: INVITE_CODE_EXPIRY_HOURS }),
},
async createInviteCode(
profileUuid: string,
spaceUuid: string,
): Promise<InviteCode> {
const isProfileInSpace = await this.profileSpaceService.isProfileInSpace(
profileUuid,
spaceUuid,
);
if (!isProfileInSpace) {
throw new ForbiddenException();
}
return this.prisma.$transaction(async () => {
let inviteCode: string;

do {
inviteCode = generateRandomString(INVITE_CODE_LENGTH);
} while (await this.findInviteCode(inviteCode));

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

Expand All @@ -69,25 +93,13 @@ export class InviteCodesService {
}
}

private generateShortInviteCode(length: number) {
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let inviteCode = '';
for (let i = 0; i < length; i++) {
inviteCode += characters.charAt(
Math.floor(Math.random() * characters.length),
);
}
return inviteCode;
}

private async generateUniqueInviteCode(length: number): Promise<string> {
return this.prisma.$transaction(async () => {
let inviteCode: string;
let inviteCodeData: InviteCode | null;

do {
inviteCode = this.generateShortInviteCode(length);
inviteCode = generateRandomString(length);
inviteCodeData = await this.findInviteCode(inviteCode);
} while (inviteCodeData !== null);

Expand Down
9 changes: 9 additions & 0 deletions nestjs-BE/server/src/utils/random-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function generateRandomString(length: number) {
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let string = '';
for (let i = 0; i < length; i++) {
string += characters.charAt(Math.floor(Math.random() * characters.length));
}
return string;
}
Loading