import { Inject, Injectable } from '@nestjs/common'; import { Kysely } from 'kysely'; import { KYSELY_CLIENT_PROVIDER_TOKEN } from 'src/core/database/database.module'; import { DB } from 'src/core/database/types'; import { CarEntityService } from '../feat-business-objects/car-entity/car-entity.service'; import { CarEntity } from '../feat-business-objects/car-entity/car.entity'; @Injectable() export class PlaygroundService { constructor( @Inject(KYSELY_CLIENT_PROVIDER_TOKEN) private readonly db: Kysely, private readonly carService: CarEntityService, ) {} async getCarsByOperationDay(day: Date) { const ordinanceTypes = ['KTW', 'TSW', 'BTW']; const cars = await Promise.all( ordinanceTypes.map((ot) => this.carService.getCarsForOperationDay(day, ot), ), ); return cars .flat(2) .filter(({ carName }) => !!carName) .map((car) => this.carMapper(car)) .filter( ({ tours }) => tours.filter( ({ optimizationInMinutes }) => optimizationInMinutes !== 0, ).length > 0, ); } private carMapper(car: CarEntity) { return { name: car.carName, drivers: car['plannedTimes'].map((pt) => ({ name: `${pt.firstname} ${pt.surname}`, })), tours: car['tours'].map((tour) => ({ id: tour.id, operationId: tour.operationId, ordinanceType: tour.ordinanceType, startDate: tour.startDate, optimizationInMinutes: tour.optimizationInMinutes, departedForPickup: tour.startBegin, arrivedAtPickup: tour.startEnd, departedPickup: tour.target, arrivedAtTarget: tour.targetBegin, completed: tour.empty, })), }; } }