Compare commits

...

2 Commits

5 changed files with 72 additions and 2 deletions

View File

@ -0,0 +1,12 @@
-- AlterTable
ALTER TABLE "ChallengeAction" ALTER COLUMN "end" SET DATA TYPE TIMESTAMPTZ(3),
ALTER COLUMN "penaltyEnd" SET DATA TYPE TIMESTAMPTZ(3),
ALTER COLUMN "penaltyStart" SET DATA TYPE TIMESTAMPTZ(3),
ALTER COLUMN "start" SET DATA TYPE TIMESTAMPTZ(3);
-- AlterTable
ALTER TABLE "Geolocation" ALTER COLUMN "timestamp" SET DATA TYPE TIMESTAMPTZ(3);
-- AlterTable
ALTER TABLE "TrainTrip" ALTER COLUMN "departureTime" SET DATA TYPE TIMESTAMPTZ(3),
ALTER COLUMN "arrivalTime" SET DATA TYPE TIMESTAMPTZ(3);

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 { 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<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 { 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<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 { IsOptional } from "class-validator"
export default class ChallengeActionEntity implements ChallengeAction {
export class ChallengeActionEntity implements ChallengeAction {
constructor(partial: Partial<ChallengeActionEntity>) {
Object.assign(this, partial)
}