87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
import { Injectable, NotAcceptableException } from '@nestjs/common'
|
|
import { CreateChallengeActionDto } from './dto/create-challenge-action.dto'
|
|
import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto'
|
|
import { ChallengeAction, 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 { FilterChallengeActionsDto } from './dto/filter-challenge-action.dto'
|
|
|
|
@Injectable()
|
|
export class ChallengeActionsService {
|
|
constructor(private prisma: PrismaService) { }
|
|
|
|
async create(authenticatedPlayer: Player, createChallengeActionDto: CreateChallengeActionDto): Promise<ChallengeAction> {
|
|
const data = { ...createChallengeActionDto, playerId: authenticatedPlayer.id }
|
|
return await this.prisma.challengeAction.create({
|
|
data: data,
|
|
})
|
|
}
|
|
|
|
async findAll(queryPagination: QueryPaginationDto, filterChallengeActions: FilterChallengeActionsDto): Promise<[ChallengeAction[], number]> {
|
|
console.log(filterChallengeActions)
|
|
return [
|
|
await this.prisma.challengeAction.findMany({
|
|
...paginate(queryPagination),
|
|
where: filterChallengeActions,
|
|
}),
|
|
await this.prisma.challengeAction.count(),
|
|
]
|
|
}
|
|
|
|
async findOne(id: number): Promise<ChallengeAction> {
|
|
return await this.prisma.challengeAction.findUnique({
|
|
where: { id },
|
|
})
|
|
}
|
|
|
|
async update(id: number, updateChallengeActionDto: UpdateChallengeActionDto): Promise<ChallengeAction> {
|
|
console.log(updateChallengeActionDto)
|
|
return await this.prisma.challengeAction.update({
|
|
where: { id },
|
|
data: updateChallengeActionDto,
|
|
})
|
|
}
|
|
|
|
async remove(id: number): Promise<ChallengeAction> {
|
|
return await this.prisma.challengeAction.delete({
|
|
where: { id },
|
|
})
|
|
}
|
|
|
|
async endCurrentChallenge(player: Player, success: boolean): Promise<ChallengeAction> {
|
|
const challengeAction = await this.prisma.challengeAction.findFirst({
|
|
where: {
|
|
playerId: player.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,
|
|
})
|
|
}
|
|
}
|