Paiement automatique d'une course
This commit is contained in:
		@@ -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({
 | 
			
		||||
      where: { id: 1 },
 | 
			
		||||
      data: {
 | 
			
		||||
 
 | 
			
		||||
@@ -95,6 +95,7 @@ export class TrainsController {
 | 
			
		||||
   * Importation d'un trajet en train à partir de Rail Planner
 | 
			
		||||
   * 
 | 
			
		||||
   * @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
 | 
			
		||||
   */
 | 
			
		||||
  @Post("/import")
 | 
			
		||||
 
 | 
			
		||||
@@ -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 { UpdateTrainDto } from './dto/update-train.dto'
 | 
			
		||||
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 { paginate } from 'src/common/utils/pagination.utils'
 | 
			
		||||
import { ImportTrainDto } from './dto/import-train.dto'
 | 
			
		||||
@@ -54,6 +54,9 @@ export class TrainsService {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  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}`)
 | 
			
		||||
      .then(data => data.json())
 | 
			
		||||
    if (interrailResult.data.travels.length !== 1)
 | 
			
		||||
@@ -80,12 +83,10 @@ export class TrainsService {
 | 
			
		||||
          const oldCoordinates = legInfoJson.trainStopStations.at(index - 1).coordinates
 | 
			
		||||
          return distance + distanceCoordinates({ from: oldCoordinates, to: coordinates })
 | 
			
		||||
        }, 0)
 | 
			
		||||
    const cost = 10 * 10 * Math.ceil(distance / 1000)
 | 
			
		||||
 | 
			
		||||
    return this.prisma.trainTrip.upsert({
 | 
			
		||||
      where: {
 | 
			
		||||
        id: trainId,
 | 
			
		||||
      },
 | 
			
		||||
      create: {
 | 
			
		||||
    const train = await this.prisma.trainTrip.create({
 | 
			
		||||
      data: {
 | 
			
		||||
        id: trainId,
 | 
			
		||||
        playerId: player.id,
 | 
			
		||||
        distance: distance,
 | 
			
		||||
@@ -95,15 +96,16 @@ export class TrainsService {
 | 
			
		||||
        arrivalTime: arrival,
 | 
			
		||||
        infoJson: leg.infoJson,
 | 
			
		||||
      },
 | 
			
		||||
      update: {
 | 
			
		||||
    })
 | 
			
		||||
    // Ajout du trajet en points
 | 
			
		||||
    await this.prisma.moneyUpdate.create({
 | 
			
		||||
      data: {
 | 
			
		||||
        playerId: player.id,
 | 
			
		||||
        distance: distance,
 | 
			
		||||
        from: travel.from,
 | 
			
		||||
        to: travel.to,
 | 
			
		||||
        departureTime: departure,
 | 
			
		||||
        arrivalTime: arrival,
 | 
			
		||||
        infoJson: leg.infoJson,
 | 
			
		||||
        amount: -cost,
 | 
			
		||||
        reason: MoneyUpdateType.BUY_TRAIN,
 | 
			
		||||
        tripId: train.id,
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
    return train
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user