Ajout endpoint pour terminer ses challenges et gérer les pénalités

This commit is contained in:
Emmy D'Anello 2024-12-07 21:36:09 +01:00
parent ab2d07cd18
commit 4349a1b61a
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
4 changed files with 60 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto' import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto'
import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto' import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto'
import { FilterChallengeActionsDto } from './dto/filter-challenge-action.dto' import { FilterChallengeActionsDto } from './dto/filter-challenge-action.dto'
import { EndChallengeActionDto } from './dto/end-challenge-action.dto'
@Controller('challenge-actions') @Controller('challenge-actions')
export class ChallengeActionsController { export class ChallengeActionsController {
@ -74,4 +75,16 @@ export class ChallengeActionsController {
async remove(@Param('id', ParseIntPipe) id: number) { async remove(@Param('id', ParseIntPipe) id: number) {
await this.challengeActionsService.remove(id) 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<ChallengeActionEntity> {
const challengeAction = await this.challengeActionsService.endCurrentChallenge(request.user, success)
return new ChallengeActionEntity(challengeAction)
}
} }

View File

@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common' import { Injectable, NotAcceptableException } from '@nestjs/common'
import { CreateChallengeActionDto } from './dto/create-challenge-action.dto' import { CreateChallengeActionDto } from './dto/create-challenge-action.dto'
import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto' import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto'
import { ChallengeAction, User } from '@prisma/client' import { ChallengeAction, User } from '@prisma/client'
@ -48,4 +48,39 @@ export class ChallengeActionsService {
where: { id }, where: { id },
}) })
} }
async endCurrentChallenge(user: User, success: boolean): Promise<ChallengeAction> {
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,
})
}
} }

View File

@ -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
}

View File

@ -2,7 +2,7 @@ import { ApiProperty } from "@nestjs/swagger"
import { ChallengeAction } from "@prisma/client" import { ChallengeAction } from "@prisma/client"
import { IsOptional } from "class-validator" import { IsOptional } from "class-validator"
export default class ChallengeActionEntity implements ChallengeAction { export class ChallengeActionEntity implements ChallengeAction {
constructor(partial: Partial<ChallengeActionEntity>) { constructor(partial: Partial<ChallengeActionEntity>) {
Object.assign(this, partial) Object.assign(this, partial)
} }