Get fare class on search

This commit is contained in:
Roba Boru
2026-05-26 16:54:52 +03:00
parent db0af7cc00
commit 3c6bd724f2
2 changed files with 14 additions and 6 deletions

View File

@@ -2,11 +2,12 @@ import { Module } from '@nestjs/common';
import { SearchController } from './search.controller';
import { SearchService } from './search.service';
import { CurrencyModule } from '../currency/currency.module';
import { FareEngineModule } from '../fare-engine/fare-engine.module';
@Module({
imports: [CurrencyModule],
controllers: [SearchController],
providers: [SearchService],
exports: [SearchService]
@Module({
imports: [CurrencyModule, FareEngineModule],
controllers: [SearchController],
providers: [SearchService],
exports: [SearchService],
})
export class SearchModule {}

View File

@@ -2,6 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../common/prisma.service';
import { SearchTripsDto, FareQuoteDto } from './search.dto';
import { CurrencyService } from '../currency/currency.service';
import { FareEngineService } from '../fare-engine/fare-engine.service';
import { Currency } from '@prisma/client';
const POINTS_TO_MINOR = 10;
@@ -11,6 +12,7 @@ export class SearchService {
constructor(
private prisma: PrismaService,
private currencyService: CurrencyService,
private fareEngine: FareEngineService,
) {}
async searchTrips(dto: SearchTripsDto) {
@@ -68,6 +70,11 @@ export class SearchService {
const legDepartureAt = originStop.plannedDepartureAt ?? schedule.departureAt;
const legArrivalAt = destStop.plannedArrivalAt ?? schedule.arrivalAt;
// Fetch fares for all seat classes from fare engine in one call
const faresByClass = await this.fareEngine
.calculateAllForSchedule(schedule.id, dto.nationality)
.catch(() => []);
results.push({
scheduleId: schedule.id,
trainNumber: schedule.train.number,
@@ -92,7 +99,6 @@ export class SearchService {
(new Date(legArrivalAt).getTime() - new Date(legDepartureAt).getTime()) / 60_000,
),
status: schedule.status,
// Only return stops within the requested leg (origin → destination inclusive)
stops: schedule.stopTimes
.filter(st => st.sequence >= originStop.sequence && st.sequence <= destStop.sequence)
.map(st => ({
@@ -104,6 +110,7 @@ export class SearchService {
})),
availabilityByClass,
hasAvailability: Object.values(availabilityByClass).some(n => n >= totalPassengers),
faresByClass,
});
}