diff --git a/server/src/challenge-actions/challenge-actions.service.ts b/server/src/challenge-actions/challenge-actions.service.ts index e1015a3..0e84269 100644 --- a/server/src/challenge-actions/challenge-actions.service.ts +++ b/server/src/challenge-actions/challenge-actions.service.ts @@ -25,7 +25,7 @@ export class ChallengeActionsService { ...paginate(queryPagination), where: filterChallengeActions, }), - await this.prisma.challenge.count(), + await this.prisma.challengeAction.count(), ] } @@ -36,6 +36,7 @@ export class ChallengeActionsService { } async update(id: number, updateChallengeActionDto: UpdateChallengeActionDto): Promise { + console.log(updateChallengeActionDto) return await this.prisma.challengeAction.update({ where: { id }, data: updateChallengeActionDto, diff --git a/server/src/challenges/challenges.controller.ts b/server/src/challenges/challenges.controller.ts index 7f35c09..8c62a09 100644 --- a/server/src/challenges/challenges.controller.ts +++ b/server/src/challenges/challenges.controller.ts @@ -2,7 +2,7 @@ import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, HttpCo import { ChallengesService } from './challenges.service' import { CreateChallengeDto } from './dto/create-challenge.dto' import { UpdateChallengeDto } from './dto/update-challenge.dto' -import { JwtAuthGuard } from 'src/auth/jwt-auth.guard' +import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard' import { ApiBearerAuth, ApiCreatedResponse, ApiForbiddenResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger' import { ChallengeEntity } from './entities/challenge.entity' import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils' @@ -72,4 +72,16 @@ export class ChallengesController { async remove(@Param('id', ParseIntPipe) id: number) { await this.challengesService.remove(id) } + + @Post('/draw-random') + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + @ApiOkResponse({ type: ChallengeEntity }) + @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) + @ApiForbiddenResponse({ description: "Permission refusée" }) + @ApiNotFoundResponse({ description: "Objet non trouvé" }) + async drawRandom(@Req() request: AuthenticatedRequest): Promise { + const challenge = await this.challengesService.drawRandom(request.user) + return new ChallengeEntity(challenge) + } } diff --git a/server/src/challenges/challenges.service.ts b/server/src/challenges/challenges.service.ts index 4c76b7d..2f7c758 100644 --- a/server/src/challenges/challenges.service.ts +++ b/server/src/challenges/challenges.service.ts @@ -1,10 +1,11 @@ -import { Injectable } from '@nestjs/common' +import { Injectable, NotAcceptableException, NotFoundException } from '@nestjs/common' import { CreateChallengeDto } from './dto/create-challenge.dto' import { UpdateChallengeDto } from './dto/update-challenge.dto' -import { Challenge } from '@prisma/client' +import { Challenge, User } from '@prisma/client' import { PrismaService } from 'src/prisma/prisma.service' import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto' import { paginate } from 'src/common/utils/pagination.utils' +import { ChallengeEntity } from './entities/challenge.entity' @Injectable() export class ChallengesService { @@ -54,4 +55,40 @@ export class ChallengesService { } }) } + + async drawRandom(user: User): Promise { + const currentChallengeAction = await this.prisma.challengeAction.findFirst({ + where: { + userId: user.id, + active: true, + } + }) + if (currentChallengeAction) + throw new NotAcceptableException("Un défi est déjà en cours d'accomplissement") + const remaningChallenges = await this.prisma.challenge.count({ + where: { + action: null, + } + }) + const challenge = await this.prisma.challenge.findFirst({ + where: { + action: null, + }, + take: 1, + skip: Math.random() * remaningChallenges, + }) + if (!challenge) + throw new NotFoundException("Plus aucun défi disponible") + const challengeEntity: ChallengeEntity = new ChallengeEntity(challenge) + const action = await this.prisma.challengeAction.create({ + data: { + userId: user.id, + challengeId: challenge.id, + active: true, + success: false, + } + }) + challengeEntity.action = action + return challengeEntity + } }