Paiement automatique d'une course

This commit is contained in:
Emmy D'Anello 2024-12-08 19:27:40 +01:00
parent 481400d404
commit 8681752888
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
3 changed files with 18 additions and 15 deletions

View File

@ -27,7 +27,7 @@ export class GameService {
}) })
} }
} }
const runnerId = alreadyStarted ? game.currentRunnerId : players[Math.trunc(players.length * Math.random())].id const runnerId = alreadyStarted ? game.currentRunnerId : players[Math.floor(players.length * Math.random())].id
return await this.prisma.game.update({ return await this.prisma.game.update({
where: { id: 1 }, where: { id: 1 },
data: { data: {

View File

@ -95,6 +95,7 @@ export class TrainsController {
* Importation d'un trajet en train à partir de Rail Planner * Importation d'un trajet en train à partir de Rail Planner
* *
* @throws {401} Non authentifiée * @throws {401} Non authentifiée
* @throws {409} Le voyage Interrail est déjà importé
* @throws {422} Le voyage Interrail à importer contient plusieurs trajets ou plusieurs trains * @throws {422} Le voyage Interrail à importer contient plusieurs trajets ou plusieurs trains
*/ */
@Post("/import") @Post("/import")

View File

@ -1,8 +1,8 @@
import { Injectable, NotFoundException, UnprocessableEntityException } from '@nestjs/common' import { ConflictException, HttpException, Injectable, NotFoundException, UnprocessableEntityException } from '@nestjs/common'
import { CreateTrainDto } from './dto/create-train.dto' import { CreateTrainDto } from './dto/create-train.dto'
import { UpdateTrainDto } from './dto/update-train.dto' import { UpdateTrainDto } from './dto/update-train.dto'
import { PrismaService } from 'src/prisma/prisma.service' import { PrismaService } from 'src/prisma/prisma.service'
import { Player, TrainTrip } from '@prisma/client' import { MoneyUpdateType, Player, TrainTrip } from '@prisma/client'
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto' import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
import { paginate } from 'src/common/utils/pagination.utils' import { paginate } from 'src/common/utils/pagination.utils'
import { ImportTrainDto } from './dto/import-train.dto' import { ImportTrainDto } from './dto/import-train.dto'
@ -54,6 +54,9 @@ export class TrainsService {
} }
async import(player: Player, { id: trainId }: ImportTrainDto): Promise<TrainTrip> { async import(player: Player, { id: trainId }: ImportTrainDto): Promise<TrainTrip> {
if (this.findOne(trainId))
throw new ConflictException(`Le train avec l'identifiant ${trainId} est déjà importé`)
const interrailResult: InterrailJourney = await fetch(`https://3uiwjsimnh.execute-api.eu-central-1.amazonaws.com/Prod/journey-import?id=${trainId}`) const interrailResult: InterrailJourney = await fetch(`https://3uiwjsimnh.execute-api.eu-central-1.amazonaws.com/Prod/journey-import?id=${trainId}`)
.then(data => data.json()) .then(data => data.json())
if (interrailResult.data.travels.length !== 1) if (interrailResult.data.travels.length !== 1)
@ -80,12 +83,10 @@ export class TrainsService {
const oldCoordinates = legInfoJson.trainStopStations.at(index - 1).coordinates const oldCoordinates = legInfoJson.trainStopStations.at(index - 1).coordinates
return distance + distanceCoordinates({ from: oldCoordinates, to: coordinates }) return distance + distanceCoordinates({ from: oldCoordinates, to: coordinates })
}, 0) }, 0)
const cost = 10 * 10 * Math.ceil(distance / 1000)
return this.prisma.trainTrip.upsert({ const train = await this.prisma.trainTrip.create({
where: { data: {
id: trainId,
},
create: {
id: trainId, id: trainId,
playerId: player.id, playerId: player.id,
distance: distance, distance: distance,
@ -95,15 +96,16 @@ export class TrainsService {
arrivalTime: arrival, arrivalTime: arrival,
infoJson: leg.infoJson, infoJson: leg.infoJson,
}, },
update: { })
// Ajout du trajet en points
await this.prisma.moneyUpdate.create({
data: {
playerId: player.id, playerId: player.id,
distance: distance, amount: -cost,
from: travel.from, reason: MoneyUpdateType.BUY_TRAIN,
to: travel.to, tripId: train.id,
departureTime: departure,
arrivalTime: arrival,
infoJson: leg.infoJson,
} }
}) })
return train
} }
} }