Skip to content

Commit

Permalink
feat: 유저가 대회에 입장 가능한지 확인하는 api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
rladydgn committed Dec 6, 2023
1 parent f0b0a08 commit 7722bce
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { AuthUser } from '../../user/decorators/user.decorators';
import { User } from '../../user/entities/user.entity';
import { CompetitionDto } from '../dto/competition.dto';
import { IsJoinableDto } from '../dto/competition.is.joinable.dto';
import { CompetitionProblemResponseDto } from '../dto/competition.problem.response.dto';
import { CompetitionResponseDto } from '../dto/competition.response.dto';
import { ProblemSimpleResponseDto } from '../dto/problem.simple.response.dto';
Expand Down Expand Up @@ -119,6 +120,21 @@ export class CompetitionController {
await this.competitionService.joinCompetition(competitionId, req.user.email);
}

@Get('validation/:competitionId')
@ApiOperation({
summary: '대회 입장 가능 여부 검증',
description: '선택한 대회 화면에 입장이 가능한지 확인하는 api 입니다.',
})
@ApiBearerAuth()
@ApiResponse({ type: IsJoinableDto })
@UseGuards(AuthGuard('jwt'))
async checkUserCanJoinCompetition(
@AuthUser() user: User,
@Param('competitionId') competitionId: number,
) {
return this.competitionService.checkUserCanJoinCompetition(competitionId, user);
}

// @Post('submissions')
// @UsePipes(new ValidationPipe({ transform: true }))
// createSubmission(@Body() createSubmissionDto: CreateSubmissionDto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiProperty } from '@nestjs/swagger';

export class IsJoinableDto {
constructor(isJoinable: boolean, message?: string) {
this.isJoinable = isJoinable;
this.message = message;
}

@ApiProperty({ description: 'true 면 입장 가능, false 면 입장 불가능' })
isJoinable: boolean;

@ApiProperty({ description: '입장 불가능일 경우 사유' })
message?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class CompetitionGateWay implements OnGatewayConnection, OnGatewayInit {
);
// 유저가 대회 참여자가 맞는지 검증
const user: User = await this.userService.getByEmail(authTokenPayloadDto.sub);
await this.competitionService.isUserJoinedCompetition(Number(competitionId), user.id);
await this.competitionService.isUserJoinedCompetition(Number(competitionId), user);

// 동일한 유저의 다른 연결 끊기
this.server.to(authTokenPayloadDto.sub).disconnectSockets();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as path from 'path';

import { ProblemService } from './problem.service';
import { RESULT } from '../competition.enums';
import { IsJoinableDto } from '../dto/competition.is.joinable.dto';
import {
CompetitionProblemResponseDto,
ITestcases,
Expand Down Expand Up @@ -240,10 +241,10 @@ export class CompetitionService {
this.competitionParticipantRepository.save({ competition: competition, user: user });
}

async isUserJoinedCompetition(competitionId: number, userId: number) {
this.logger.debug(competitionId, userId);
async isUserJoinedCompetition(competitionId: number, user: User) {
this.logger.debug(competitionId, user.id);
const competitionParticipant: CompetitionParticipant =
await this.competitionParticipantRepository.findOneBy({ competitionId, userId });
await this.competitionParticipantRepository.findOneBy({ competitionId, userId: user.id });

if (!competitionParticipant) throw new UnauthorizedException('대회 참여자가 아닙니다.');
return true;
Expand Down Expand Up @@ -353,6 +354,16 @@ export class CompetitionService {
throw new BadRequestException(`${competitionId}는 아직 시작하지 않은 대회입니다.`);
}

async checkUserCanJoinCompetition(competitionId: number, user: User) {
try {
await this.isCompetitionOngoing(competitionId);
await this.isUserJoinedCompetition(competitionId, user);
return new IsJoinableDto(true);
} catch (error) {
return new IsJoinableDto(false, error.message);
}
}

private assertCompetitionExists(competition: Competition) {
if (!competition)
throw new NotFoundException(
Expand Down

0 comments on commit 7722bce

Please sign in to comment.