diff --git a/server/src/challenge-actions/challenge-actions.controller.ts b/server/src/challenge-actions/challenge-actions.controller.ts index e2e6028..672df5e 100644 --- a/server/src/challenge-actions/challenge-actions.controller.ts +++ b/server/src/challenge-actions/challenge-actions.controller.ts @@ -9,6 +9,7 @@ import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto' import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto' import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto' import { FilterChallengeActionsDto } from './dto/filter-challenge-action.dto' +import { EndChallengeActionDto } from './dto/end-challenge-action.dto' @Controller('challenge-actions') export class ChallengeActionsController { @@ -74,4 +75,16 @@ export class ChallengeActionsController { async remove(@Param('id', ParseIntPipe) id: number) { await this.challengeActionsService.remove(id) } + + @Post('/end-current') + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + @ApiOkResponse({ type: ChallengeActionEntity }) + @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) + @ApiForbiddenResponse({ description: "Permission refusée" }) + @ApiNotFoundResponse({ description: "Objet non trouvé" }) + async endCurrent(@Req() request: AuthenticatedRequest, @Body() { success }: EndChallengeActionDto): Promise { + const challengeAction = await this.challengeActionsService.endCurrentChallenge(request.user, success) + return new ChallengeActionEntity(challengeAction) + } } diff --git a/server/src/challenge-actions/challenge-actions.service.ts b/server/src/challenge-actions/challenge-actions.service.ts index 0e84269..c081287 100644 --- a/server/src/challenge-actions/challenge-actions.service.ts +++ b/server/src/challenge-actions/challenge-actions.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common' +import { Injectable, NotAcceptableException } from '@nestjs/common' import { CreateChallengeActionDto } from './dto/create-challenge-action.dto' import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto' import { ChallengeAction, User } from '@prisma/client' @@ -48,4 +48,39 @@ export class ChallengeActionsService { where: { id }, }) } + + async endCurrentChallenge(user: User, success: boolean): Promise { + const challengeAction = await this.prisma.challengeAction.findFirst({ + where: { + userId: user.id, + active: true, + } + }) + if (!challengeAction) + throw new NotAcceptableException("Aucun défi n'est en cours") + let data + const now = new Date() + if (success) { + data = { + success: success, + active: false, + end: now, + } + } + else { + data = { + success: success, + active: false, + end: now, + penaltyStart: now, + penaltyEnd: new Date(now.getTime() + 45 * 60 * 1000), + } + } + return await this.prisma.challengeAction.update({ + where: { + id: challengeAction.id, + }, + data: data, + }) + } } diff --git a/server/src/challenge-actions/dto/end-challenge-action.dto.ts b/server/src/challenge-actions/dto/end-challenge-action.dto.ts new file mode 100644 index 0000000..54341eb --- /dev/null +++ b/server/src/challenge-actions/dto/end-challenge-action.dto.ts @@ -0,0 +1,10 @@ +import { ApiProperty } from "@nestjs/swagger" +import { IsBoolean } from "class-validator" +import { BooleanTransform } from "src/common/utils/transform.utils" + +export class EndChallengeActionDto { + @IsBoolean() + @BooleanTransform() + @ApiProperty({ description: "Indique si le défi a été un succès ou non." }) + success: boolean +} \ No newline at end of file diff --git a/server/src/challenge-actions/entities/challenge-action.entity.ts b/server/src/challenge-actions/entities/challenge-action.entity.ts index 8c990d8..e669585 100644 --- a/server/src/challenge-actions/entities/challenge-action.entity.ts +++ b/server/src/challenge-actions/entities/challenge-action.entity.ts @@ -2,7 +2,7 @@ import { ApiProperty } from "@nestjs/swagger" import { ChallengeAction } from "@prisma/client" import { IsOptional } from "class-validator" -export default class ChallengeActionEntity implements ChallengeAction { +export class ChallengeActionEntity implements ChallengeAction { constructor(partial: Partial) { Object.assign(this, partial) }