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

초대 코드 모듈 리팩토링 #360

Merged
merged 4 commits into from
Nov 8, 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
50 changes: 13 additions & 37 deletions nestjs-BE/server/src/invite-codes/invite-codes.controller.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
import {
Controller,
Get,
Post,
Body,
Param,
NotFoundException,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { Controller, Get, Post, Body, Param, HttpStatus } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { InviteCodesService } from './invite-codes.service';
import { CreateInviteCodeDto } from './dto/create-invite-code.dto';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { SpacesService } from '../spaces/spaces.service';

@Controller('inviteCodes')
@ApiTags('inviteCodes')
export class InviteCodesController {
constructor(
private readonly inviteCodesService: InviteCodesService,
private readonly spacesService: SpacesService,
) {}
constructor(private readonly inviteCodesService: InviteCodesService) {}

@Post()
@ApiOperation({ summary: 'Create invite code' })
@ApiResponse({
status: 201,
status: HttpStatus.CREATED,
description: 'The invite code has been successfully created.',
})
@ApiResponse({
status: 400,
status: HttpStatus.BAD_REQUEST,
description: 'Space code input is missing.',
})
@ApiResponse({
status: 404,
status: HttpStatus.NOT_FOUND,
description: 'Space not found.',
})
async create(@Body() createInviteCodeDto: CreateInviteCodeDto) {
async createInviteCode(@Body() createInviteCodeDto: CreateInviteCodeDto) {
const spaceUuid = createInviteCodeDto.space_uuid;
const space = await this.spacesService.findSpaceBySpaceUuid(spaceUuid);
if (!space) throw new NotFoundException();
const inviteCode =
await this.inviteCodesService.createInviteCode(spaceUuid);
return {
statusCode: 201,
statusCode: HttpStatus.CREATED,
message: 'Created',
data: { invite_code: inviteCode.inviteCode },
};
Expand All @@ -51,28 +36,19 @@ export class InviteCodesController {
@Get(':inviteCode')
@ApiOperation({ summary: 'Find space by invite code' })
@ApiResponse({
status: 200,
status: HttpStatus.OK,
description: 'Returns a space associated with the invite code.',
})
@ApiResponse({
status: 404,
status: HttpStatus.NOT_FOUND,
description: 'Invite code not found.',
})
@ApiResponse({
status: 410,
status: HttpStatus.GONE,
description: 'Invite code has expired',
})
async findSpace(@Param('inviteCode') inviteCode: string) {
const inviteCodeData =
await this.inviteCodesService.findInviteCode(inviteCode);
if (!inviteCodeData) throw new NotFoundException();
if (this.inviteCodesService.checkExpiry(inviteCodeData.expiryDate)) {
this.inviteCodesService.deleteInviteCode(inviteCode);
throw new HttpException('Invite code has expired.', HttpStatus.GONE);
}
const space = await this.spacesService.findSpaceBySpaceUuid(
inviteCodeData.spaceUuid,
);
return { statusCode: 200, message: 'Success', data: space };
const space = await this.inviteCodesService.findSpace(inviteCode);
return { statusCode: HttpStatus.OK, message: 'Success', data: space };
}
}
27 changes: 24 additions & 3 deletions nestjs-BE/server/src/invite-codes/invite-codes.service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
import { Injectable } from '@nestjs/common';
import {
HttpException,
HttpStatus,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { InviteCode, Prisma } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import {
INVITE_CODE_EXPIRY_HOURS,
INVITE_CODE_LENGTH,
} from '../config/magic-number';
import { InviteCode, Prisma } from '@prisma/client';
import generateUuid from '../utils/uuid';
import { SpacesService } from '../spaces/spaces.service';

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

async findInviteCode(inviteCode: string): Promise<InviteCode | null> {
return this.prisma.inviteCode.findUnique({
where: { inviteCode: inviteCode },
});
}

async findSpace(inviteCode: string) {
const inviteCodeData = await this.findInviteCode(inviteCode);
if (!inviteCodeData) throw new NotFoundException();
if (this.checkExpiry(inviteCodeData.expiryDate)) {
this.deleteInviteCode(inviteCode);
throw new HttpException('Invite code has expired.', HttpStatus.GONE);
}
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: generateUuid(),
Expand Down
Loading