99 lines
2.9 KiB
TypeScript
99 lines
2.9 KiB
TypeScript
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common'
|
|
import { CreateChallengeDto } from './dto/create-challenge.dto'
|
|
import { UpdateChallengeDto } from './dto/update-challenge.dto'
|
|
import { Challenge, Player } 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 {
|
|
constructor(private prisma: PrismaService) { }
|
|
|
|
async create(createChallengeDto: CreateChallengeDto): Promise<Challenge> {
|
|
const data = { ...createChallengeDto }
|
|
return await this.prisma.challenge.create({ data: data })
|
|
}
|
|
|
|
async findAll(queryPagination?: QueryPaginationDto): Promise<[Challenge[], number]> {
|
|
return [
|
|
await this.prisma.challenge.findMany({
|
|
...paginate(queryPagination),
|
|
include: {
|
|
action: true,
|
|
}
|
|
}),
|
|
await this.prisma.challenge.count(),
|
|
]
|
|
}
|
|
|
|
async findOne(id: number): Promise<Challenge> {
|
|
return await this.prisma.challenge.findUnique({
|
|
where: { id },
|
|
include: {
|
|
action: true,
|
|
}
|
|
})
|
|
}
|
|
|
|
async update(id: number, updateChallengeDto: UpdateChallengeDto): Promise<Challenge> {
|
|
if (!this.findOne(id))
|
|
throw new NotFoundException(`Aucun défi n'existe avec l'identifiant ${id}`)
|
|
return await this.prisma.challenge.update({
|
|
where: { id },
|
|
data: updateChallengeDto,
|
|
include: {
|
|
action: true,
|
|
}
|
|
})
|
|
}
|
|
|
|
async remove(id: number): Promise<Challenge> {
|
|
if (!this.findOne(id))
|
|
throw new NotFoundException(`Aucun défi n'existe avec l'identifiant ${id}`)
|
|
return await this.prisma.challenge.delete({
|
|
where: { id },
|
|
include: {
|
|
action: true,
|
|
}
|
|
})
|
|
}
|
|
|
|
async drawRandom(player: Player): Promise<ChallengeEntity> {
|
|
const currentChallengeAction = await this.prisma.challengeAction.findFirst({
|
|
where: {
|
|
playerId: player.id,
|
|
active: true,
|
|
}
|
|
})
|
|
if (currentChallengeAction)
|
|
throw new ConflictException("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: {
|
|
playerId: player.id,
|
|
challengeId: challenge.id,
|
|
active: true,
|
|
success: false,
|
|
}
|
|
})
|
|
challengeEntity.action = action
|
|
return challengeEntity
|
|
}
|
|
}
|