Tirage de défi aléatoire
This commit is contained in:
parent
54e002e1f8
commit
97d61497bc
@ -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<ChallengeAction> {
|
||||
console.log(updateChallengeActionDto)
|
||||
return await this.prisma.challengeAction.update({
|
||||
where: { id },
|
||||
data: updateChallengeActionDto,
|
||||
|
@ -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<ChallengeEntity> {
|
||||
const challenge = await this.challengesService.drawRandom(request.user)
|
||||
return new ChallengeEntity(challenge)
|
||||
}
|
||||
}
|
||||
|
@ -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<ChallengeEntity> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user