From 6d73359b8e377cd3dae9aed00be60ae2adef674d Mon Sep 17 00:00:00 2001 From: Roba Boru Date: Wed, 17 Jun 2026 20:22:09 +0300 Subject: [PATCH 01/21] update booking request object --- .../portal/src/app/booking/review/page.tsx | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/apps/edr-passenger-web/portal/src/app/booking/review/page.tsx b/apps/edr-passenger-web/portal/src/app/booking/review/page.tsx index d3e75d7d9..6b09ec422 100644 --- a/apps/edr-passenger-web/portal/src/app/booking/review/page.tsx +++ b/apps/edr-passenger-web/portal/src/app/booking/review/page.tsx @@ -144,9 +144,14 @@ export default function ReviewPage() { const createBookingMutation = useMutation({ mutationFn: (data: any) => { const endpoint = isAuthenticated ? '/bookings' : '/bookings/guest'; + console.log('=== API REQUEST ==='); + console.log('Endpoint:', endpoint); + console.log('Request Data:', JSON.stringify(data, null, 2)); return apiClient.post(endpoint, data); }, onSuccess: (data: any) => { + console.log('=== API RESPONSE SUCCESS ==='); + console.log('Response Data:', JSON.stringify(data, null, 2)); console.log('Booking created successfully:', data); const bookingIdValue = data.bookingId || data.id; const pnrValue = data.pnr || data.bookingReference || data.bookingRef; @@ -178,7 +183,12 @@ export default function ReviewPage() { }, 100); }, onError: (error: any) => { - console.error('Booking creation failed:', error); + console.log('=== API RESPONSE ERROR ==='); + console.error('Error Object:', error); + console.error('Error Response:', error?.response); + console.error('Error Response Data:', JSON.stringify(error?.response?.data, null, 2)); + console.error('Error Status:', error?.response?.status); + console.error('Error Message:', error?.message); const errorMessage = error?.response?.data?.message || error?.message || 'Failed to create booking. Please try again.'; alert(errorMessage); }, @@ -196,7 +206,7 @@ export default function ReviewPage() { console.log('Inbound schedule:', inboundSchedule); console.log('Passengers:', passengers); - if (!seatHold?.holdId) { + if (!seatHold?.holdId && (passengers.some(p => p.seatId) || passengers.some(p => (p as any).outboundSeatId || (p as any).inboundSeatId))) { console.error('No seat hold found'); alert('Please select seats before continuing.'); router.push('/booking/seats'); @@ -285,6 +295,7 @@ export default function ReviewPage() { const isEthiopian = p.nationality === 'ETHIOPIAN' || p.nationality === 'Ethiopian'; return { seatId: isRoundTrip ? (p as any).outboundSeatId : (p.seatId || ''), + ...(isRoundTrip && { returnSeatId: (p as any).inboundSeatId || '' }), passengerName: p.name, dateOfBirth: p.dateOfBirth, idDocumentType: isEthiopian ? 'NATIONAL_ID' : 'PASSPORT', @@ -323,6 +334,7 @@ export default function ReviewPage() { const isEthiopian = p.nationality === 'ETHIOPIAN' || p.nationality === 'Ethiopian'; return { seatId: isRoundTrip ? (p as any).outboundSeatId : (p.seatId || ''), + ...(isRoundTrip && { returnSeatId: (p as any).inboundSeatId || '' }), passengerName: p.name, dateOfBirth: p.dateOfBirth, idDocumentType: isEthiopian ? 'NATIONAL_ID' : 'PASSPORT', @@ -358,7 +370,8 @@ export default function ReviewPage() { localStorage.setItem('deviceId', bookingData.deviceId); } - console.log('Creating booking with payload:', bookingData); + console.log('Creating booking with payload:', JSON.stringify(bookingData, null, 2)); + console.log('API endpoint:', isAuthenticated ? '/bookings' : '/bookings/guest'); await createBookingMutation.mutateAsync(bookingData); } catch (error) { console.error('Error in handleConfirm:', error); From b491f39d4266dcb29b8aec02cfff5346a667b0ca Mon Sep 17 00:00:00 2001 From: Stephanos A Date: Thu, 18 Jun 2026 14:02:11 +0300 Subject: [PATCH 02/21] Booking and pricing related updates --- .../migration.sql | 78 ++- apps/edr-passenger-api/prisma/schema.prisma | 52 +- apps/edr-passenger-api/prisma/seed.ts | 29 +- .../modules/fare-engine/fare-engine.dto.ts | 3 + .../fare-engine/fare-engine.service.ts | 99 +++- .../modules/schedules/schedules.controller.ts | 141 ++--- .../modules/schedules/schedules.service.ts | 111 ++-- .../src/modules/search/search.service.ts | 1 + .../backoffice/src/app/currencies/page.tsx | 507 +++++------------- .../backoffice/src/app/pricing/page.tsx | 15 +- 10 files changed, 410 insertions(+), 626 deletions(-) diff --git a/apps/edr-passenger-api/prisma/migrations/20260626_fix_missing_booking_columns/migration.sql b/apps/edr-passenger-api/prisma/migrations/20260626_fix_missing_booking_columns/migration.sql index 4f828f075..f252642f6 100644 --- a/apps/edr-passenger-api/prisma/migrations/20260626_fix_missing_booking_columns/migration.sql +++ b/apps/edr-passenger-api/prisma/migrations/20260626_fix_missing_booking_columns/migration.sql @@ -1,28 +1,52 @@ --- Fix missing columns from 20260617 migration (failed due to missing schema prefix) +-- Create passenger schema if it doesn't exist +CREATE SCHEMA IF NOT EXISTS passenger; + +-- Move all enums from public to passenger schema +DO $$ +DECLARE + e text; +BEGIN + FOR e IN + SELECT typname FROM pg_type + JOIN pg_namespace ON pg_namespace.oid = pg_type.typnamespace + WHERE pg_namespace.nspname = 'public' AND pg_type.typtype = 'e' + LOOP + EXECUTE format('ALTER TYPE public.%I SET SCHEMA passenger', e); + END LOOP; +END $$; + +-- Move all tables from public to passenger schema +DO $$ +DECLARE + t text; +BEGIN + FOR t IN + SELECT tablename FROM pg_tables + WHERE schemaname = 'public' AND tablename NOT IN ('_prisma_migrations') + LOOP + EXECUTE format('ALTER TABLE public.%I SET SCHEMA passenger', t); + END LOOP; +END $$; + +-- Add missing columns to Booking ALTER TABLE "passenger"."Booking" ADD COLUMN IF NOT EXISTS "returnScheduleId" TEXT, ADD COLUMN IF NOT EXISTS "returnOriginStationId" TEXT, ADD COLUMN IF NOT EXISTS "returnDestinationStationId" TEXT, ADD COLUMN IF NOT EXISTS "returnHoldId" TEXT, - ADD COLUMN IF NOT EXISTS "returnSeatClassId" TEXT; + ADD COLUMN IF NOT EXISTS "returnSeatClassId" TEXT, + ADD COLUMN IF NOT EXISTS "leg2ScheduleId" TEXT, + ADD COLUMN IF NOT EXISTS "leg2OriginStationId" TEXT, + ADD COLUMN IF NOT EXISTS "leg2DestinationStationId" TEXT, + ADD COLUMN IF NOT EXISTS "leg2SeatClassId" TEXT, + ADD COLUMN IF NOT EXISTS "returnLeg2ScheduleId" TEXT, + ADD COLUMN IF NOT EXISTS "returnLeg2OriginStationId" TEXT, + ADD COLUMN IF NOT EXISTS "returnLeg2DestStationId" TEXT, + ADD COLUMN IF NOT EXISTS "returnLeg2SeatClassId" TEXT, + ADD COLUMN IF NOT EXISTS "outboundBoardedAt" TIMESTAMP(3), + ADD COLUMN IF NOT EXISTS "returnBoardedAt" TIMESTAMP(3); -ALTER TABLE "passenger"."SeatClass" ALTER COLUMN "baseFareMinor" SET DEFAULT 0; -ALTER TABLE "passenger"."Ticket" ALTER COLUMN "status" SET DEFAULT 'ACTIVE'; - -CREATE INDEX IF NOT EXISTS "Booking_bookingType_idx" ON "passenger"."Booking"("bookingType"); - --- Transit leg-2 columns (never migrated) -ALTER TABLE "passenger"."Booking" - ADD COLUMN IF NOT EXISTS "leg2ScheduleId" TEXT, - ADD COLUMN IF NOT EXISTS "leg2OriginStationId" TEXT, - ADD COLUMN IF NOT EXISTS "leg2DestinationStationId" TEXT, - ADD COLUMN IF NOT EXISTS "leg2SeatClassId" TEXT, - ADD COLUMN IF NOT EXISTS "returnLeg2ScheduleId" TEXT, - ADD COLUMN IF NOT EXISTS "returnLeg2OriginStationId" TEXT, - ADD COLUMN IF NOT EXISTS "returnLeg2DestStationId" TEXT, - ADD COLUMN IF NOT EXISTS "returnLeg2SeatClassId" TEXT; - --- ReturnLegStatus enum + columns (from 20260625 migration, may have also failed) +-- Add ReturnLegStatus enum and column DO $$ BEGIN CREATE TYPE "passenger"."ReturnLegStatus" AS ENUM ( 'NOT_APPLICABLE', 'BOTH_USED', 'OUTBOUND_ONLY', 'INBOUND_ONLY', 'NEITHER_USED' @@ -30,9 +54,15 @@ DO $$ BEGIN EXCEPTION WHEN duplicate_object THEN NULL; END $$; ALTER TABLE "passenger"."Booking" - ADD COLUMN IF NOT EXISTS "returnLegStatus" "passenger"."ReturnLegStatus" NOT NULL DEFAULT 'NOT_APPLICABLE', - ADD COLUMN IF NOT EXISTS "outboundBoardedAt" TIMESTAMP(3), - ADD COLUMN IF NOT EXISTS "returnBoardedAt" TIMESTAMP(3); + ADD COLUMN IF NOT EXISTS "returnLegStatus" "passenger"."ReturnLegStatus" NOT NULL DEFAULT 'NOT_APPLICABLE'; -ALTER TABLE "passenger"."GateValidationLog" - ADD COLUMN IF NOT EXISTS "leg" TEXT; +-- Add missing columns to other tables +ALTER TABLE "passenger"."GateValidationLog" ADD COLUMN IF NOT EXISTS "leg" TEXT; +ALTER TABLE "passenger"."BookingSeat" ADD COLUMN IF NOT EXISTS "leg" INTEGER NOT NULL DEFAULT 1; +ALTER TABLE "passenger"."BookingSeat" ADD COLUMN IF NOT EXISTS "scheduleId" TEXT; +ALTER TABLE "passenger"."Ticket" ADD COLUMN IF NOT EXISTS "boardedAt" TIMESTAMP(3); + +ALTER TABLE "passenger"."SeatClass" ALTER COLUMN "baseFareMinor" SET DEFAULT 0; +ALTER TABLE "passenger"."Ticket" ALTER COLUMN "status" SET DEFAULT 'ACTIVE'; + +CREATE INDEX IF NOT EXISTS "Booking_bookingType_idx" ON "passenger"."Booking"("bookingType"); diff --git a/apps/edr-passenger-api/prisma/schema.prisma b/apps/edr-passenger-api/prisma/schema.prisma index 1b98bd3e1..423a03ec9 100644 --- a/apps/edr-passenger-api/prisma/schema.prisma +++ b/apps/edr-passenger-api/prisma/schema.prisma @@ -1,5 +1,6 @@ generator client { - provider = "prisma-client-js" + provider = "prisma-client-js" + previewFeatures = ["multiSchema"] } datasource db { @@ -79,7 +80,6 @@ model CoachType { updatedAt DateTime @updatedAt coaches Coach[] seatClasses SeatClass[] - @@schema("passenger") } @@ -116,11 +116,11 @@ enum BookingStatus { } enum ReturnLegStatus { - NOT_APPLICABLE // one-way booking - BOTH_USED // passenger used both legs - OUTBOUND_ONLY // return leg not used (no-show on return) - INBOUND_ONLY // outbound leg not used, return leg used - NEITHER_USED // neither leg boarded yet + NOT_APPLICABLE + BOTH_USED + OUTBOUND_ONLY + INBOUND_ONLY + NEITHER_USED @@schema("passenger") } @@ -269,7 +269,6 @@ model User { fraudAlerts FraudAlert[] faydaVerificationSessions FaydaVerificationSession[] - @@schema("passenger") } @@ -283,7 +282,6 @@ model Session { lastActivityAt DateTime @default(now()) createdAt DateTime @default(now()) user User @relation(fields: [userId], references: [id], onDelete: Cascade) - @@schema("passenger") } @@ -315,7 +313,6 @@ model TravelerProfile { notes String? createdAt DateTime @default(now()) passenger Passenger @relation(fields: [passengerId], references: [id]) - @@schema("passenger") } @@ -350,7 +347,6 @@ model Train { createdAt DateTime @default(now()) updatedAt DateTime @updatedAt schedules TrainSchedule[] - @@schema("passenger") } @@ -412,7 +408,6 @@ model TripLiveStatus { platformLabel String? updatedAt DateTime @updatedAt schedule TrainSchedule @relation(fields: [scheduleId], references: [id]) - @@schema("passenger") } @@ -500,7 +495,6 @@ model FareRule { validFrom DateTime validUntil DateTime? createdAt DateTime @default(now()) - @@schema("passenger") } @@ -584,7 +578,6 @@ model BookingSeat { displayFareMinor Int? booking Booking @relation(fields: [bookingId], references: [id]) seat Seat @relation(fields: [seatId], references: [id]) - @@schema("passenger") } @@ -600,7 +593,6 @@ model PaymentMethod { sortOrder Int @default(0) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt - @@schema("passenger") } @@ -661,7 +653,6 @@ model PaymentRefund { status String createdAt DateTime @default(now()) paymentIntent PaymentIntent @relation(fields: [paymentIntentId], references: [id]) - @@schema("passenger") } @@ -681,7 +672,6 @@ model Ticket { booking Booking @relation(fields: [bookingId], references: [id]) validationLogs GateValidationLog[] seats TicketSeat[] - @@schema("passenger") } @@ -709,7 +699,6 @@ model LoyaltyAccount { passenger Passenger @relation(fields: [passengerId], references: [id]) ledger LoyaltyLedgerEntry[] rewards LoyaltyReward[] - @@schema("passenger") } @@ -722,7 +711,6 @@ model LoyaltyLedgerEntry { balanceAfter Int createdAt DateTime @default(now()) account LoyaltyAccount @relation(fields: [accountId], references: [id]) - @@schema("passenger") } @@ -734,7 +722,6 @@ model LoyaltyReward { available Boolean @default(true) description String? account LoyaltyAccount @relation(fields: [accountId], references: [id]) - @@schema("passenger") } @@ -763,7 +750,6 @@ model WalletLedgerEntry { relatedBookingId String? createdAt DateTime @default(now()) wallet WalletAccount @relation(fields: [walletId], references: [id]) - @@schema("passenger") } @@ -778,7 +764,6 @@ model Notification { metadata Json? createdAt DateTime @default(now()) passenger Passenger @relation(fields: [passengerId], references: [id]) - @@schema("passenger") } @@ -794,7 +779,6 @@ model Promotion { deepLink String? active Boolean @default(true) createdAt DateTime @default(now()) - @@schema("passenger") } @@ -808,7 +792,6 @@ model StationCrowdSignal { observedAt DateTime? updatedAt DateTime @updatedAt station Station @relation(fields: [stationId], references: [id]) - @@schema("passenger") } @@ -820,7 +803,6 @@ model WeatherAlert { message String validUntil DateTime createdAt DateTime @default(now()) - @@schema("passenger") } @@ -828,7 +810,6 @@ model MenuCategory { id String @id @default(uuid()) name String items MenuItem[] - @@schema("passenger") } @@ -843,7 +824,6 @@ model MenuItem { availableUntil DateTime? schedule TrainSchedule @relation(fields: [scheduleId], references: [id]) category MenuCategory @relation(fields: [categoryId], references: [id]) - @@schema("passenger") } @@ -858,7 +838,6 @@ model FoodOrder { createdAt DateTime @default(now()) booking Booking @relation(fields: [bookingId], references: [id]) items FoodOrderItem[] - @@schema("passenger") } @@ -871,7 +850,6 @@ model FoodOrderItem { unitPriceMinor Int? lineTotalMinor Int order FoodOrder @relation(fields: [orderId], references: [id]) - @@schema("passenger") } @@ -880,7 +858,6 @@ model FaqCategory { title String iconKey String? articles FaqArticle[] - @@schema("passenger") } @@ -891,7 +868,6 @@ model FaqArticle { answerMarkdown String rank Int @default(0) category FaqCategory @relation(fields: [categoryId], references: [id]) - @@schema("passenger") } @@ -902,7 +878,6 @@ model SupportConversation { status SupportConversationStatus @default(OPEN) createdAt DateTime @default(now()) messages SupportMessage[] - @@schema("passenger") } @@ -914,7 +889,6 @@ model SupportMessage { attachments Json? createdAt DateTime @default(now()) conversation SupportConversation @relation(fields: [conversationId], references: [id]) - @@schema("passenger") } @@ -934,7 +908,6 @@ model UserPreferences { darkMode Boolean @default(false) language String @default("en") user User @relation(fields: [userId], references: [id]) - @@schema("passenger") } @@ -947,7 +920,6 @@ model Device { trusted Boolean @default(false) lastSeenAt DateTime @default(now()) user User @relation(fields: [userId], references: [id]) - @@schema("passenger") } @@ -961,7 +933,6 @@ model SavedRoute { tripCount Int @default(0) createdAt DateTime @default(now()) passenger Passenger @relation(fields: [passengerId], references: [id]) - @@schema("passenger") } @@ -973,7 +944,6 @@ model Journey { currency String @default("ETB") createdAt DateTime @default(now()) journeySegments JourneySegment[] - @@schema("passenger") } @@ -988,7 +958,6 @@ model JourneySegment { arrivalStationId String journey Journey @relation(fields: [journeyId], references: [id]) schedule TrainSchedule @relation(fields: [scheduleId], references: [id]) - @@schema("passenger") } @@ -1032,7 +1001,6 @@ model Route { fareRules RouteFareRule[] segmentFares SegmentFareRule[] schedules TrainSchedule[] - @@schema("passenger") } @@ -1102,7 +1070,6 @@ model Agent { bookings AgentBooking[] shifts AgentShift[] commissions AgentCommission[] - @@schema("passenger") } @@ -1117,7 +1084,6 @@ model AgentBooking { createdAt DateTime @default(now()) agent Agent @relation(fields: [agentId], references: [id]) booking Booking @relation(fields: [bookingId], references: [id]) - @@schema("passenger") } @@ -1177,7 +1143,6 @@ model BookingCancellation { processedAt DateTime? createdAt DateTime @default(now()) booking Booking @relation(fields: [bookingId], references: [id]) - @@schema("passenger") } @@ -1205,7 +1170,6 @@ model BaggageAllowance { excessFeePerKg Int currency String @default("ETB") createdAt DateTime @default(now()) - @@schema("passenger") } @@ -1249,7 +1213,6 @@ model NotificationTemplate { bodyTemplate String active Boolean @default(true) createdAt DateTime @default(now()) - @@schema("passenger") } @@ -1288,7 +1251,6 @@ model FraudRule { config Json? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt - @@schema("passenger") } diff --git a/apps/edr-passenger-api/prisma/seed.ts b/apps/edr-passenger-api/prisma/seed.ts index 14907f87e..9e1fffa44 100644 --- a/apps/edr-passenger-api/prisma/seed.ts +++ b/apps/edr-passenger-api/prisma/seed.ts @@ -212,7 +212,7 @@ async function seedRoute() { create: { routeId: route.id, stationId: station!.id, sequence: i + 1, distanceKm: routeDistancesKm[i] }, }); } - + const returnRoute = await prisma.route.upsert({ where: { code: 'Route-102' }, update: {}, @@ -317,7 +317,7 @@ async function seedTrips() { const now = new Date(); const tomorrow = new Date(now); tomorrow.setDate(now.getDate() + 1); - + const schedules = []; for (let d = 0; d < 5; d++) { @@ -403,7 +403,7 @@ async function seedTrips() { const coachAssignments = []; const liveStatuses = []; - + for (const schedule of createdSchedules) { for (let p = 0; p < coaches.length; p++) { coachAssignments.push({ @@ -418,12 +418,12 @@ async function seedTrips() { progressPercent: 0, }); } - + await Promise.all([ ...coachAssignments.map(ca => prisma.coachAssignment.create({ data: ca })), ...liveStatuses.map(ls => prisma.tripLiveStatus.create({ data: ls })), ]); - + console.log(` ✅ Train with ${createdSchedules.length} upcoming trips created`); } @@ -453,7 +453,7 @@ async function seedFareRules() { validFrom, }); } - + await Promise.all( fareRules.map(fr => prisma.routeFareRule.create({ data: fr })) ); @@ -518,7 +518,7 @@ async function seedSegmentFares() { include: { stops: { orderBy: { sequence: 'asc' } } }, }); const seatClasses = await prisma.seatClass.findMany(); - const validFrom = new Date('2024-01-01'); + const validFrom = new Date('2026-01-01'); if (route && route.stops.length > 2) { for (const sc of seatClasses) { @@ -531,7 +531,7 @@ async function seedSegmentFares() { baseFareMinor: Math.floor(sc.baseFareMinor * 0.4), validFrom, }, - }).catch(() => {}); + }).catch(() => { }); await prisma.segmentFareRule.create({ data: { @@ -542,7 +542,7 @@ async function seedSegmentFares() { baseFareMinor: Math.floor(sc.baseFareMinor * 0.6), validFrom, }, - }).catch(() => {}); + }).catch(() => { }); } console.log(` ✅ ${seatClasses.length * 2} segment fare rules created`); } @@ -586,16 +586,16 @@ async function seedMenuAndFood() { const coffeeId = uuidv4(); const juiceId = uuidv4(); const sandwichId = uuidv4(); - + await prisma.menuItem.create({ data: { id: coffeeId, scheduleId: schedule.id, categoryId: beverages.id, name: 'Ethiopian Coffee', priceMinor: 50 }, - }).catch(() => {}); // ignore if exists + }).catch(() => { }); // ignore if exists await prisma.menuItem.create({ data: { id: juiceId, scheduleId: schedule.id, categoryId: beverages.id, name: 'Fresh Juice', priceMinor: 35 }, - }).catch(() => {}); // ignore if exists + }).catch(() => { }); // ignore if exists await prisma.menuItem.create({ data: { id: sandwichId, scheduleId: schedule.id, categoryId: snacks.id, name: 'Sandwich', priceMinor: 80 }, - }).catch(() => {}); // ignore if exists + }).catch(() => { }); // ignore if exists } console.log(` ✅ Menu categories and items created`); } @@ -682,6 +682,9 @@ async function main() { const steps: Array<[string, () => Promise]> = [ ['system users', seedSystemUsers], + ['fare rules', seedFareRules], + ['segment fares', seedSegmentFares], + ['currency', seedCurrency] ]; let failed = 0; diff --git a/apps/edr-passenger-api/src/modules/fare-engine/fare-engine.dto.ts b/apps/edr-passenger-api/src/modules/fare-engine/fare-engine.dto.ts index 536ddaa3b..5d3a288cd 100644 --- a/apps/edr-passenger-api/src/modules/fare-engine/fare-engine.dto.ts +++ b/apps/edr-passenger-api/src/modules/fare-engine/fare-engine.dto.ts @@ -47,6 +47,9 @@ export class FareCalculateDto { @ApiPropertyOptional({ example: 'WEEKEND15', description: 'Promo code for discount' }) @IsOptional() @IsString() promoCode?: string; + + @ApiPropertyOptional({ example: 'schedule-uuid', description: 'Schedule UUID — used to match schedule-scoped FareRules first' }) + @IsOptional() @IsString() scheduleId?: string; } export class FareBreakdownDto { diff --git a/apps/edr-passenger-api/src/modules/fare-engine/fare-engine.service.ts b/apps/edr-passenger-api/src/modules/fare-engine/fare-engine.service.ts index b111b5d67..9071f0535 100644 --- a/apps/edr-passenger-api/src/modules/fare-engine/fare-engine.service.ts +++ b/apps/edr-passenger-api/src/modules/fare-engine/fare-engine.service.ts @@ -32,20 +32,59 @@ export class FareEngineService { s => s.sequence > originStop.sequence && s.sequence <= destStop.sequence, ); - const missingDistance = legStops.filter(s => s.distanceKm === null || s.distanceKm === undefined); - if (missingDistance.length > 0) - throw new BadRequestException( - `Missing distanceKm on route stops at sequences: ${missingDistance.map(s => s.sequence).join(', ')}`, - ); - - const totalDistanceKm = legStops.reduce((sum, s) => sum + (s.distanceKm ?? 0), 0); - const seatClass = await this.prisma.seatClass.findUnique({ where: { id: dto.seatClassId } }); if (!seatClass) throw new NotFoundException('Seat class not found'); if (!seatClass.isActive) throw new BadRequestException('Seat class is not active'); - const ratePerKmMinor = seatClass.baseFareMinor; - const baseFarePerPassengerMinor = totalDistanceKm * ratePerKmMinor; + // Resolve fare: FareRule (schedule-scoped → route-scoped) takes precedence over distance×rate + const now = new Date(); + const [originStation, destStation] = await Promise.all([ + this.prisma.station.findUnique({ where: { id: dto.originStationId } }), + this.prisma.station.findUnique({ where: { id: dto.destinationStationId } }), + ]); + const segmentRoute = originStation && destStation + ? `${originStation.code}-${destStation.code}` : null; + const fullRoute = `${route.code}`; + + const fareRuleCandidates = await this.prisma.fareRule.findMany({ + where: { + seatClassId: dto.seatClassId, + validFrom: { lte: now }, + OR: [{ validUntil: null }, { validUntil: { gte: now } }], + }, + }); + + const fareRule = this.pickBestFareRule( + fareRuleCandidates, + dto.scheduleId, + segmentRoute, + fullRoute, + dto.nationality, + ); + + let baseFarePerPassengerMinor: number; + let ratePerKmMinor: number; + let totalDistanceKm: number; + let fareSource: string; + + if (fareRule) { + // Flat fare from FareRule — distance is informational only + baseFarePerPassengerMinor = fareRule.baseFareMinor; + totalDistanceKm = legStops.reduce((sum, s) => sum + (s.distanceKm ?? 0), 0); + ratePerKmMinor = totalDistanceKm > 0 ? Math.round(baseFarePerPassengerMinor / totalDistanceKm) : 0; + fareSource = fareRule.tripId ? 'SCHEDULE_FARE_RULE' : 'ROUTE_FARE_RULE'; + } else { + // Distance × rate fallback + const missingDistance = legStops.filter(s => s.distanceKm === null || s.distanceKm === undefined); + if (missingDistance.length > 0) + throw new BadRequestException( + `Missing distanceKm on route stops at sequences: ${missingDistance.map(s => s.sequence).join(', ')}`, + ); + totalDistanceKm = legStops.reduce((sum, s) => sum + (s.distanceKm ?? 0), 0); + ratePerKmMinor = seatClass.baseFareMinor; + baseFarePerPassengerMinor = totalDistanceKm * ratePerKmMinor; + fareSource = 'DISTANCE_RATE'; + } // Premium and insurance fees applied per passenger const premiumPerPassenger = seatClass.premiumMinor ?? 0; @@ -84,11 +123,6 @@ export class FareEngineService { const exchangeRate = await this.currencyService.getExchangeRate(Currency.ETB, billingCurrency); const totalInBillingCurrency = Math.round(totalEtbMinor * exchangeRate); - const [originStation, destStation] = await Promise.all([ - this.prisma.station.findUnique({ where: { id: dto.originStationId } }), - this.prisma.station.findUnique({ where: { id: dto.destinationStationId } }), - ]); - const calculation = [ `Distance: ${totalDistanceKm} km (${originStation?.name} → ${destStation?.name})`, `Rate per km: ${ratePerKmMinor} ETB minor (${seatClass.name})`, @@ -110,9 +144,11 @@ export class FareEngineService { `Nationality: ${dto.nationality ?? 'unspecified'} → ${billingCurrency}`, `Exchange rate: 1 ETB = ${exchangeRate} ${billingCurrency}`, `Total (${billingCurrency}): ${totalInBillingCurrency} ${billingCurrency} minor`, + `Fare source: ${fareSource}`, ].join('\n'); return { + fareSource, routeCode: route.code, originName: originStation?.name ?? dto.originStationId, destinationName: destStation?.name ?? dto.destinationStationId, @@ -161,6 +197,37 @@ export class FareEngineService { return results.filter(Boolean); } + private pickBestFareRule( + candidates: any[], + scheduleId?: string, + segmentRoute?: string | null, + fullRoute?: string, + nationality?: string, + ): any | null { + const nat = nationality ?? null; + const priorities = [ + { tripId: scheduleId, route: segmentRoute, nationality: nat }, + { tripId: scheduleId, route: segmentRoute, nationality: null }, + { tripId: scheduleId, route: fullRoute, nationality: nat }, + { tripId: scheduleId, route: fullRoute, nationality: null }, + { tripId: scheduleId, route: null, nationality: nat }, + { tripId: scheduleId, route: null, nationality: null }, + { tripId: null, route: segmentRoute, nationality: nat }, + { tripId: null, route: segmentRoute, nationality: null }, + { tripId: null, route: fullRoute, nationality: nat }, + { tripId: null, route: fullRoute, nationality: null }, + { tripId: null, route: null, nationality: nat }, + { tripId: null, route: null, nationality: null }, + ]; + for (const p of priorities) { + const match = candidates.find( + c => c.tripId === p.tripId && c.route === p.route && c.nationality === p.nationality, + ); + if (match) return match; + } + return null; + } + async calculateForSchedule(scheduleId: string, seatClassId: string, nationality?: string) { const schedule = await this.prisma.trainSchedule.findUnique({ where: { id: scheduleId }, @@ -175,6 +242,7 @@ export class FareEngineService { destinationStationId: schedule.destinationStationId, seatClassId, nationality, + scheduleId, }); } @@ -199,6 +267,7 @@ export class FareEngineService { destinationStationId: schedule.destinationStationId, seatClassId: sc.id, nationality, + scheduleId, }).catch(() => null), ), ); diff --git a/apps/edr-passenger-api/src/modules/schedules/schedules.controller.ts b/apps/edr-passenger-api/src/modules/schedules/schedules.controller.ts index 8cb8ea253..378bc8fa7 100644 --- a/apps/edr-passenger-api/src/modules/schedules/schedules.controller.ts +++ b/apps/edr-passenger-api/src/modules/schedules/schedules.controller.ts @@ -12,34 +12,22 @@ export class SchedulesController { @Post('bulk-generate') @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') - @ApiOperation({ - summary: 'Bulk generate repetitive schedules', - description: 'Creates multiple schedules automatically by repeating every X days for the next Y days. Example: repeat every 2 days for 30 days = 15 schedules.', - }) - @ApiResponse({ status: 201, description: 'Schedules generated successfully' }) - @ApiResponse({ status: 400, description: 'Invalid parameters or route not found' }) + @ApiOperation({ summary: 'Bulk generate repetitive schedules' }) bulkGenerateSchedules(@Body() dto: BulkCreateSchedulesDto) { return this.service.bulkGenerateSchedules(dto); } @Post() @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') - @ApiOperation({ - summary: 'Create a train schedule from a route template', - description: `Creates a schedule by referencing a Route (routeId).\nStops are automatically copied from the route's RouteStop definitions.\nYou supply the actual planned arrival/departure times per stop sequence.\nOrigin and destination are derived from the first and last route stop — no need to specify them manually.`, - }) - @ApiResponse({ status: 201, description: 'Schedule created with stops copied from route template' }) - @ApiResponse({ status: 400, description: 'Invalid times, inactive route, or missing planned times for some stops' }) - @ApiResponse({ status: 404, description: 'Train or route not found' }) + @ApiOperation({ summary: 'Create a train schedule from a route template' }) createSchedule(@Body() dto: CreateScheduleDto) { return this.service.createSchedule(dto); } @Get() @ApiOperation({ summary: 'List schedules with optional filters' }) - @ApiQuery({ name: 'date', required: false, example: '2026-06-15', description: 'Departure date (YYYY-MM-DD). Returns all schedules departing on this calendar day.' }) - @ApiQuery({ name: 'routeId', required: false, description: 'Filter by route UUID' }) - @ApiQuery({ name: 'trainId', required: false, description: 'Filter by train UUID' }) - @ApiQuery({ name: 'status', required: false, enum: TripStatus, description: 'Filter by schedule status' }) - @ApiResponse({ status: 200, description: 'Array of schedules ordered by departureAt, each with train, origin/destination, stops, and booking/assignment counts' }) + @ApiQuery({ name: 'date', required: false }) + @ApiQuery({ name: 'routeId', required: false }) + @ApiQuery({ name: 'trainId', required: false }) + @ApiQuery({ name: 'status', required: false, enum: TripStatus }) listSchedules( @Query('date') date?: string, @Query('routeId') routeId?: string, @@ -57,57 +45,63 @@ export class SchedulesController { @ApiResponse({ status: 201, description: 'Fare rule created' }) createFareRule(@Body() dto: CreateFareRuleDto) { return this.service.createFareRule(dto); } + @Patch('fares/:id') + @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') + @ApiOperation({ summary: 'Update a fare rule' }) + @ApiParam({ name: 'id', description: 'FareRule UUID' }) + @ApiResponse({ status: 200, description: 'Fare rule updated' }) + updateFareRule(@Param('id') id: string, @Body() dto: Partial) { + return this.service.updateFareRule(id, dto); + } + + @Delete('fares/:id') + @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') + @ApiOperation({ summary: 'Delete a fare rule' }) + @ApiParam({ name: 'id', description: 'FareRule UUID' }) + @ApiResponse({ status: 200, description: 'Fare rule deleted' }) + deleteFareRule(@Param('id') id: string) { return this.service.deleteFareRule(id); } + @Post('segment-fares') @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') - @ApiOperation({ summary: 'Create a segment fare rule (stop-to-stop pricing on a route)' }) - @ApiResponse({ status: 201, description: 'Segment fare rule created' }) + @ApiOperation({ summary: 'Create a segment fare rule' }) createSegmentFareRule(@Body() dto: any) { return this.service.createSegmentFareRule(dto); } @Get('routes/:routeId/segment-fares') @ApiOperation({ summary: 'List all segment fare rules for a route' }) @ApiParam({ name: 'routeId', description: 'Route UUID' }) - @ApiResponse({ status: 200, description: 'List of segment fare rules' }) getSegmentFares(@Param('routeId') routeId: string) { return this.service.getSegmentFares(routeId); } @Patch('segment-fares/:id') @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') @ApiOperation({ summary: 'Update a segment fare rule' }) @ApiParam({ name: 'id', description: 'SegmentFareRule UUID' }) - @ApiResponse({ status: 200, description: 'Segment fare rule updated' }) updateSegmentFareRule(@Param('id') id: string, @Body() dto: any) { return this.service.updateSegmentFareRule(id, dto); } @Delete('segment-fares/:id') @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') @ApiOperation({ summary: 'Delete a segment fare rule' }) @ApiParam({ name: 'id', description: 'SegmentFareRule UUID' }) - @ApiResponse({ status: 200, description: 'Segment fare rule deleted' }) deleteSegmentFareRule(@Param('id') id: string) { return this.service.deleteSegmentFareRule(id); } // ===== PARAMETRIZED ROUTES (generic :id routes come AFTER specific routes) ===== @Get(':id') - @ApiOperation({ summary: 'Get schedule with train, coaches, seats, and stop timeline' }) + @ApiOperation({ summary: 'Get schedule detail' }) @ApiParam({ name: 'id', description: 'TrainSchedule UUID' }) - @ApiResponse({ status: 200, description: 'Full schedule detail including route stops with station info' }) - @ApiResponse({ status: 404, description: 'Schedule not found' }) getSchedule(@Param('id') id: string) { return this.service.getSchedule(id); } @Patch(':id') @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') - @ApiOperation({ summary: 'Update a schedule (partial update - times, status, coaches)' }) + @ApiOperation({ summary: 'Update a schedule (partial)' }) @ApiParam({ name: 'id', description: 'TrainSchedule UUID' }) - @ApiResponse({ status: 200, description: 'Schedule updated' }) - @ApiResponse({ status: 404, description: 'Schedule not found' }) updateSchedule(@Param('id') id: string, @Body() dto: UpdateScheduleDto) { return this.service.updateSchedulePartial(id, dto); } @Patch(':id/status') @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') - @ApiOperation({ summary: 'Update schedule status (SCHEDULED → BOARDING → EN_ROUTE → ARRIVED)' }) + @ApiOperation({ summary: 'Update schedule status' }) @ApiParam({ name: 'id', description: 'TrainSchedule UUID' }) - @ApiResponse({ status: 200, description: 'Status updated' }) - @ApiResponse({ status: 404, description: 'Schedule not found' }) updateStatus(@Param('id') id: string, @Body() dto: UpdateScheduleStatusDto) { return this.service.updateScheduleStatus(id, dto); } @@ -116,26 +110,18 @@ export class SchedulesController { @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') @ApiOperation({ summary: 'Delete a schedule' }) @ApiParam({ name: 'id', description: 'TrainSchedule UUID' }) - @ApiResponse({ status: 200, description: 'Schedule deleted' }) - @ApiResponse({ status: 404, description: 'Schedule not found' }) - deleteSchedule(@Param('id') id: string) { - return this.service.deleteSchedule(id); - } + deleteSchedule(@Param('id') id: string) { return this.service.deleteSchedule(id); } @Get(':id/stops') - @ApiOperation({ summary: 'List all stops for a schedule ordered by sequence' }) + @ApiOperation({ summary: 'List all stops for a schedule' }) @ApiParam({ name: 'id', description: 'TrainSchedule UUID' }) - @ApiResponse({ status: 200, description: 'Ordered stop list with station details and planned/actual times' }) - @ApiResponse({ status: 404, description: 'Schedule not found' }) getStops(@Param('id') id: string) { return this.service.getStops(id); } @Patch(':id/stops/:sequence') @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') - @ApiOperation({ summary: 'Update planned times or live status of a specific stop' }) + @ApiOperation({ summary: 'Update a stop time' }) @ApiParam({ name: 'id', description: 'TrainSchedule UUID' }) @ApiParam({ name: 'sequence', description: 'Stop sequence number' }) - @ApiResponse({ status: 200, description: 'Stop updated' }) - @ApiResponse({ status: 404, description: 'Stop not found on schedule' }) updateStop( @Param('id') id: string, @Param('sequence', ParseIntPipe) sequence: number, @@ -145,19 +131,26 @@ export class SchedulesController { @Get(':scheduleId/fares/stored') @ApiOperation({ summary: 'Get stored fare rules for a schedule' }) @ApiParam({ name: 'scheduleId', description: 'TrainSchedule UUID' }) - @ApiResponse({ status: 200, description: 'List of stored fare rules with seat class info' }) getStoredFares(@Param('scheduleId') scheduleId: string) { return this.service.getFareRules(scheduleId); } - @Get(':scheduleId/fares') - @ApiOperation({ summary: 'Get fare for a schedule and seat class from the fare engine' }) + @Get(':scheduleId/fares/all') + @ApiOperation({ summary: 'Get fares for all active seat classes from the fare engine' }) @ApiParam({ name: 'scheduleId', description: 'TrainSchedule UUID' }) - @ApiQuery({ name: 'seatClassId', required: true, description: 'SeatClass UUID' }) - @ApiQuery({ name: 'nationality', required: false, description: 'Passenger nationality — determines billing currency (Ethiopian→ETB, Djiboutian→DJF, other→USD)' }) - @ApiResponse({ status: 200, description: 'Live fare breakdown from fare engine' }) - @ApiResponse({ status: 400, description: 'Schedule has no route or missing distanceKm on stops' }) - @ApiResponse({ status: 404, description: 'Schedule or seat class not found' }) + @ApiQuery({ name: 'nationality', required: false }) + getAllFares( + @Param('scheduleId') scheduleId: string, + @Query('nationality') nationality?: string, + ) { + return this.service.getAllFaresFromEngine(scheduleId, nationality); + } + + @Get(':scheduleId/fares') + @ApiOperation({ summary: 'Get fare for a specific seat class from the fare engine' }) + @ApiParam({ name: 'scheduleId', description: 'TrainSchedule UUID' }) + @ApiQuery({ name: 'seatClassId', required: true }) + @ApiQuery({ name: 'nationality', required: false }) getFare( @Param('scheduleId') scheduleId: string, @Query('seatClassId') seatClassId: string, @@ -166,42 +159,15 @@ export class SchedulesController { return this.service.getFareFromEngine(scheduleId, seatClassId, nationality); } - @Get(':scheduleId/fares/all') - @ApiOperation({ summary: 'Get fares for all active seat classes on a schedule' }) - @ApiParam({ name: 'scheduleId', description: 'TrainSchedule UUID' }) - @ApiQuery({ name: 'nationality', required: false, description: 'Passenger nationality — determines billing currency' }) - @ApiResponse({ status: 200, description: 'Array of fare breakdowns for every active seat class, ordered by price ascending' }) - @ApiResponse({ status: 400, description: 'Schedule has no route or missing distanceKm on stops' }) - @ApiResponse({ status: 404, description: 'Schedule not found' }) - getAllFares( - @Param('scheduleId') scheduleId: string, - @Query('nationality') nationality?: string, - ) { - return this.service.getAllFaresFromEngine(scheduleId, nationality); - } - @Post(':id/fares/sync') - @ApiOperation({ - summary: 'Sync fares from fare engine', - description: 'Recalculates fares for all active seat classes using the fare engine (km × ratePerKm + tax) and upserts them as FareRule records scoped to this schedule. Previous active rules are expired.', - }) + @ApiOperation({ summary: 'Sync fares from fare engine' }) @ApiParam({ name: 'id', description: 'TrainSchedule UUID' }) - @ApiResponse({ status: 201, description: 'Fares synced — returns count of synced rules and any errors' }) - @ApiResponse({ status: 400, description: 'Schedule has no associated route or missing distanceKm on stops' }) - @ApiResponse({ status: 404, description: 'Schedule not found' }) - syncFares(@Param('id') id: string) { - return this.service.syncFaresFromEngine(id); - } + syncFares(@Param('id') id: string) { return this.service.syncFaresFromEngine(id); } @Post(':id/coaches') @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') - @ApiOperation({ - summary: 'Assign coaches to a schedule', - description: 'Assigns selected coaches to a schedule with their position numbers. Replaces any existing coach assignments.' - }) + @ApiOperation({ summary: 'Assign coaches to a schedule' }) @ApiParam({ name: 'id', description: 'TrainSchedule UUID' }) - @ApiResponse({ status: 201, description: 'Coaches assigned successfully' }) - @ApiResponse({ status: 404, description: 'Schedule or coach not found' }) assignCoaches( @Param('id') id: string, @Body() dto: { coaches: Array<{ coachId: string; positionNumber: number }> }, @@ -212,21 +178,14 @@ export class SchedulesController { @Get(':id/coaches') @ApiOperation({ summary: 'Get assigned coaches for a schedule' }) @ApiParam({ name: 'id', description: 'TrainSchedule UUID' }) - @ApiResponse({ status: 200, description: 'List of assigned coaches with seat details' }) - getAssignedCoaches(@Param('id') id: string) { - return this.service.getAssignedCoaches(id); - } + getAssignedCoaches(@Param('id') id: string) { return this.service.getAssignedCoaches(id); } @Delete(':id/coaches/:coachId') @UseGuards(JwtGuard) @ApiBearerAuth('JWT-auth') - @ApiOperation({ summary: 'Remove a coach assignment from a schedule' }) + @ApiOperation({ summary: 'Remove a coach assignment' }) @ApiParam({ name: 'id', description: 'TrainSchedule UUID' }) @ApiParam({ name: 'coachId', description: 'Coach UUID' }) - @ApiResponse({ status: 200, description: 'Coach assignment removed' }) - removeCoachAssignment( - @Param('id') id: string, - @Param('coachId') coachId: string, - ) { + removeCoachAssignment(@Param('id') id: string, @Param('coachId') coachId: string) { return this.service.removeCoachAssignment(id, coachId); } } diff --git a/apps/edr-passenger-api/src/modules/schedules/schedules.service.ts b/apps/edr-passenger-api/src/modules/schedules/schedules.service.ts index fd0776141..0234824d1 100644 --- a/apps/edr-passenger-api/src/modules/schedules/schedules.service.ts +++ b/apps/edr-passenger-api/src/modules/schedules/schedules.service.ts @@ -18,7 +18,6 @@ export class SchedulesService { const errors: string[] = []; const scheduleIds: string[] = []; - // Validate route and get stops for plannedTimes generation const route = await this.prisma.route.findUnique({ where: { id: dto.routeId }, include: { stops: { orderBy: { sequence: 'asc' } } }, @@ -45,7 +44,6 @@ export class SchedulesService { const schedule = await this.createSchedule(createDto); scheduleIds.push(schedule.id); - // Assign coaches if provided if (dto.coachIds && dto.coachIds.length > 0) { await this.assignCoaches( schedule.id, @@ -58,15 +56,10 @@ export class SchedulesService { errors.push(`Failed to create schedule for ${currentDate.toISOString()}: ${error instanceof Error ? error.message : String(error)}`); } - // Move to next repetition currentDate = new Date(currentDate.getTime() + dto.repeatEveryDays * 24 * 60 * 60 * 1000); } - return { - schedulesCreated: scheduleCount, - errors, - scheduleIds, - }; + return { schedulesCreated: scheduleCount, errors, scheduleIds }; } async listSchedules(dto: ListSchedulesDto) { @@ -104,7 +97,6 @@ export class SchedulesService { const arr = new Date(dto.arrivalAt); if (arr <= dep) throw new BadRequestException('arrivalAt must be after departureAt'); - // Validate route exists and has stops const route = await this.prisma.route.findUnique({ where: { id: dto.routeId }, include: { stops: { orderBy: { sequence: 'asc' } } }, @@ -113,21 +105,13 @@ export class SchedulesService { if (!route.active) throw new BadRequestException('Route is not active'); if (route.stops.length < 2) throw new BadRequestException('Route must have at least 2 stops'); - // Check for duplicate schedule with same train, route, and date const depDate = new Date(dep); depDate.setHours(0, 0, 0, 0); const nextDay = new Date(depDate); nextDay.setDate(nextDay.getDate() + 1); const existingSchedule = await this.prisma.trainSchedule.findFirst({ - where: { - trainId: dto.trainId, - routeId: dto.routeId, - departureAt: { - gte: depDate, - lt: nextDay, - }, - }, + where: { trainId: dto.trainId, routeId: dto.routeId, departureAt: { gte: depDate, lt: nextDay } }, }); if (existingSchedule) { @@ -136,7 +120,6 @@ export class SchedulesService { ); } - // Auto-generate plannedTimes if not provided or empty let plannedTimes = dto.plannedTimes; if (!plannedTimes || plannedTimes.length === 0) { const totalDuration = arr.getTime() - dep.getTime(); @@ -144,7 +127,6 @@ export class SchedulesService { plannedTimes = route.stops.map((stop, index) => { let stopTime: Date; - if (index === 0) { stopTime = dep; } else if (index === route.stops.length - 1) { @@ -154,7 +136,6 @@ export class SchedulesService { const progress = totalDistance > 0 ? stopDistance / totalDistance : index / (route.stops.length - 1); stopTime = new Date(dep.getTime() + totalDuration * progress); } - return { sequence: stop.sequence, plannedArrivalAt: index === 0 ? undefined : stopTime.toISOString(), @@ -163,14 +144,12 @@ export class SchedulesService { }); } - // Validate all route stop sequences are covered by plannedTimes const providedSeqs = new Set(plannedTimes.map(t => t.sequence)); const missingSeqs = route.stops.map(s => s.sequence).filter(seq => !providedSeqs.has(seq)); if (missingSeqs.length > 0) { throw new BadRequestException(`Missing planned times for stop sequences: ${missingSeqs.join(', ')}`); } - // Derive origin and destination from first and last route stop const firstStop = route.stops[0]; const lastStop = route.stops[route.stops.length - 1]; @@ -188,9 +167,7 @@ export class SchedulesService { include: { train: true, originStation: true, destinationStation: true }, }); - const plannedTimesMap = Object.fromEntries( - plannedTimes.map(t => [t.sequence, t]), - ); + const plannedTimesMap = Object.fromEntries(plannedTimes.map(t => [t.sequence, t])); await this.routesService.applyRouteToSchedule(dto.routeId, schedule.id, plannedTimesMap); return this.getSchedule(schedule.id); @@ -230,10 +207,7 @@ export class SchedulesService { }; } - private async resolveEffectiveStatuses( - scheduleId: string, - seatIds: string[], - ): Promise> { + private async resolveEffectiveStatuses(scheduleId: string, seatIds: string[]): Promise> { const statusMap = new Map(); if (seatIds.length === 0) return statusMap; @@ -304,7 +278,6 @@ export class SchedulesService { plannedTimes = route.stops.map((stop, index) => { let stopTime: Date; - if (index === 0) { stopTime = dep; } else if (index === route.stops.length - 1) { @@ -314,7 +287,6 @@ export class SchedulesService { const progress = totalDistance > 0 ? stopDistance / totalDistance : index / (route.stops.length - 1); stopTime = new Date(dep.getTime() + totalDuration * progress); } - return { sequence: stop.sequence, plannedArrivalAt: index === 0 ? undefined : stopTime.toISOString(), @@ -323,9 +295,7 @@ export class SchedulesService { }); } - const plannedTimesMap = Object.fromEntries( - plannedTimes.map(t => [t.sequence, t]), - ); + const plannedTimesMap = Object.fromEntries(plannedTimes.map(t => [t.sequence, t])); await this.routesService.applyRouteToSchedule(dto.routeId, id, plannedTimesMap); return this.getSchedule(id); @@ -376,9 +346,35 @@ export class SchedulesService { validFrom: new Date(validFrom), validUntil: validUntil ? new Date(validUntil) : null, }, + include: { seatClass: true }, }); } + async updateFareRule(id: string, dto: Partial) { + const existing = await this.prisma.fareRule.findUnique({ where: { id } }); + if (!existing) throw new NotFoundException('Fare rule not found'); + + const { validFrom, validUntil, scheduleId, nationality, passengerCategory, ...rest } = dto; + return this.prisma.fareRule.update({ + where: { id }, + data: { + ...rest, + ...(scheduleId !== undefined && { tripId: scheduleId }), + ...(nationality !== undefined && { nationality }), + ...(validFrom && { validFrom: new Date(validFrom) }), + ...(validUntil !== undefined && { validUntil: validUntil ? new Date(validUntil) : null }), + }, + include: { seatClass: true }, + }); + } + + async deleteFareRule(id: string) { + const existing = await this.prisma.fareRule.findUnique({ where: { id } }); + if (!existing) throw new NotFoundException('Fare rule not found'); + await this.prisma.fareRule.delete({ where: { id } }); + return { deleted: true, id }; + } + createSegmentFareRule(dto: any) { const { validFrom, validUntil, passengerCategory, ...rest } = dto; return this.prisma.segmentFareRule.create({ @@ -419,7 +415,6 @@ export class SchedulesService { async getFareRules(scheduleId?: string) { const where: any = {}; if (scheduleId) where.tripId = scheduleId; - return this.prisma.fareRule.findMany({ where, include: { seatClass: true }, @@ -439,11 +434,10 @@ export class SchedulesService { }); if (!schedule) throw new NotFoundException('Schedule not found'); if (!schedule.routeId) throw new BadRequestException('Schedule has no associated route'); - return await this.fareEngine.calculateAllForSchedule(scheduleId, nationality); } catch (error) { throw new BadRequestException( - error instanceof Error ? error.message : 'Failed to calculate fares for schedule' + error instanceof Error ? error.message : 'Failed to calculate fares for schedule', ); } } @@ -483,20 +477,13 @@ export class SchedulesService { return { synced, errors }; } - async assignCoaches( - scheduleId: string, - coaches: Array<{ coachId: string; positionNumber: number }>, - ) { + async assignCoaches(scheduleId: string, coaches: Array<{ coachId: string; positionNumber: number }>) { const schedule = await this.prisma.trainSchedule.findUnique({ where: { id: scheduleId } }); if (!schedule) throw new NotFoundException('Schedule not found'); const coachIds = coaches.map(c => c.coachId); - const existingCoaches = await this.prisma.coach.findMany({ - where: { id: { in: coachIds } }, - }); - if (existingCoaches.length !== coachIds.length) { - throw new NotFoundException('One or more coaches not found'); - } + const existingCoaches = await this.prisma.coach.findMany({ where: { id: { in: coachIds } } }); + if (existingCoaches.length !== coachIds.length) throw new NotFoundException('One or more coaches not found'); await this.prisma.coachAssignment.deleteMany({ where: { scheduleId } }); @@ -508,20 +495,13 @@ export class SchedulesService { })); await this.prisma.coachAssignment.createMany({ data }); - return { message: 'Coaches assigned successfully', count: coaches.length }; } async getAssignedCoaches(scheduleId: string) { return this.prisma.coachAssignment.findMany({ where: { scheduleId }, - include: { - coach: { - include: { - seats: { orderBy: [{ row: 'asc' }, { col: 'asc' }] }, - }, - }, - }, + include: { coach: { include: { seats: { orderBy: [{ row: 'asc' }, { col: 'asc' }] } } } }, orderBy: { positionNumber: 'asc' }, }); } @@ -535,30 +515,22 @@ export class SchedulesService { if (dto.departureAt || dto.arrivalAt) { const dep = dto.departureAt ? new Date(dto.departureAt) : new Date(schedule.departureAt); const arr = dto.arrivalAt ? new Date(dto.arrivalAt) : new Date(schedule.arrivalAt); - if (arr <= dep) throw new BadRequestException('Arrival time must be after departure time'); - updateData.departureAt = dep; updateData.arrivalAt = arr; updateData.durationMinutes = Math.round((arr.getTime() - dep.getTime()) / 60_000); } - if (dto.status) { - updateData.status = dto.status; - } + if (dto.status) updateData.status = dto.status; if (Object.keys(updateData).length > 0) { - await this.prisma.trainSchedule.update({ - where: { id }, - data: updateData, - }); + await this.prisma.trainSchedule.update({ where: { id }, data: updateData }); } if (dto.coaches !== undefined) { if (dto.coaches.length > 0) { await this.assignCoaches(id, dto.coaches); } else { - // Remove all coach assignments when empty array is sent await this.prisma.coachAssignment.deleteMany({ where: { scheduleId: id } }); } } @@ -567,12 +539,9 @@ export class SchedulesService { } async removeCoachAssignment(scheduleId: string, coachId: string) { - const assignment = await this.prisma.coachAssignment.findFirst({ - where: { scheduleId, coachId }, - }); + const assignment = await this.prisma.coachAssignment.findFirst({ where: { scheduleId, coachId } }); if (!assignment) throw new NotFoundException('Coach assignment not found'); - await this.prisma.coachAssignment.delete({ where: { id: assignment.id } }); return { message: 'Coach assignment removed' }; } -} \ No newline at end of file +} diff --git a/apps/edr-passenger-api/src/modules/search/search.service.ts b/apps/edr-passenger-api/src/modules/search/search.service.ts index 9646c20ea..0ca9a8723 100644 --- a/apps/edr-passenger-api/src/modules/search/search.service.ts +++ b/apps/edr-passenger-api/src/modules/search/search.service.ts @@ -441,6 +441,7 @@ export class SearchService { destinationStationId, seatClassId: sc.id, nationality, + scheduleId: schedule.id, }); return { seatClassName: fare.seatClassName, diff --git a/apps/edr-passenger-web/backoffice/src/app/currencies/page.tsx b/apps/edr-passenger-web/backoffice/src/app/currencies/page.tsx index d57a67c83..8de163da9 100644 --- a/apps/edr-passenger-web/backoffice/src/app/currencies/page.tsx +++ b/apps/edr-passenger-web/backoffice/src/app/currencies/page.tsx @@ -2,14 +2,13 @@ import { useState } from 'react'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; -import { Plus, Trash2, Loader2, Edit, RefreshCw } from 'lucide-react'; +import { Edit, Loader2, RefreshCw } from 'lucide-react'; import DataTable from '@/components/ui/DataTable'; import Modal from '@/components/ui/Modal'; import ActionButton from '@/components/ui/ActionButton'; -import ConfirmDialog from '@/components/ui/ConfirmDialog'; import { apiClient } from '@/lib/api-client'; -interface Currency { +interface CurrencyRate { id: string; code: string; name: string; @@ -18,203 +17,104 @@ interface Currency { exchangeRate: number; isActive: boolean; createdAt: string; - updatedAt: string; } +const CURRENCY_META: Record = { + ETB: { name: 'Ethiopian Birr', symbol: 'Br' }, + DJF: { name: 'Djiboutian Franc', symbol: 'Fdj' }, + USD: { name: 'US Dollar', symbol: '$' }, +}; + export default function CurrenciesPage() { - const [showModal, setShowModal] = useState(false); - const [editingCurrency, setEditingCurrency] = useState(null); - const [deleteConfirm, setDeleteConfirm] = useState<{ isOpen: boolean; id: string | null }>({ - isOpen: false, - id: null, - }); + const [editingRate, setEditingRate] = useState(null); + const [rateInput, setRateInput] = useState(''); const [error, setError] = useState(null); const queryClient = useQueryClient(); - const [currencyForm, setCurrencyForm] = useState({ - code: '', - name: '', - symbol: '', - baseCurrencyCode: 'ETB', - exchangeRate: '', - }); - - const { data: currencies = [], isLoading } = useQuery({ + const { data: currencies = [], isLoading } = useQuery({ queryKey: ['currencies'], queryFn: () => apiClient.get('/currencies'), }); - const createMutation = useMutation({ - mutationFn: (data: any) => apiClient.post('/currencies', data), - onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ['currencies'] }); - resetForm(); - setError(null); - }, - onError: (err: any) => { - setError(err.response?.data?.message || 'Failed to create currency'); - }, - }); - const updateMutation = useMutation({ - mutationFn: (data: any) => apiClient.patch(`/currencies/${data.id}`, data), + mutationFn: ({ id, exchangeRate }: { id: string; exchangeRate: number }) => + apiClient.patch(`/currencies/${id}`, { exchangeRate }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['currencies'] }); - setEditingCurrency(null); - resetForm(); + setEditingRate(null); setError(null); }, onError: (err: any) => { - setError(err.response?.data?.message || 'Failed to update currency'); + setError(err.response?.data?.message || 'Failed to update exchange rate'); }, }); - const deleteMutation = useMutation({ - mutationFn: (id: string) => apiClient.delete(`/currencies/${id}`), - onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ['currencies'] }); - setDeleteConfirm({ isOpen: false, id: null }); - }, - onError: (err: any) => { - setError(err.response?.data?.message || 'Failed to delete currency'); - }, - }); - - const syncRatesMutation = useMutation({ + const syncMutation = useMutation({ mutationFn: () => apiClient.post('/currencies/sync-rates', {}), - onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ['currencies'] }); - setError(null); - }, - onError: (err: any) => { - setError(err.response?.data?.message || 'Failed to sync exchange rates'); - }, + onSuccess: () => queryClient.invalidateQueries({ queryKey: ['currencies'] }), + onError: (err: any) => setError(err.response?.data?.message || 'Failed to sync rates'), }); - const resetForm = () => { - setCurrencyForm({ - code: '', - name: '', - symbol: '', - baseCurrencyCode: 'ETB', - exchangeRate: '', - }); - setEditingCurrency(null); - setShowModal(false); + const handleEdit = (currency: CurrencyRate) => { + setEditingRate(currency); + setRateInput(currency.exchangeRate.toString()); setError(null); }; - const handleEditCurrency = (currency: Currency) => { - setEditingCurrency(currency); - setCurrencyForm({ - code: currency.code, - name: currency.name, - symbol: currency.symbol, - baseCurrencyCode: currency.baseCurrencyCode, - exchangeRate: currency.exchangeRate.toString(), - }); - setError(null); - setShowModal(true); - }; - - const handleSaveCurrency = async () => { - setError(null); - if (!currencyForm.code || !currencyForm.name || !currencyForm.symbol || !currencyForm.exchangeRate) { - setError('All fields are required'); - return; - } - - const rate = parseFloat(currencyForm.exchangeRate); + const handleSave = async () => { + const rate = parseFloat(rateInput); if (isNaN(rate) || rate <= 0) { setError('Exchange rate must be a positive number'); return; } - - const payload = { - code: currencyForm.code.toUpperCase(), - name: currencyForm.name, - symbol: currencyForm.symbol, - baseCurrencyCode: currencyForm.baseCurrencyCode, - exchangeRate: rate, - }; - - if (editingCurrency) { - await updateMutation.mutateAsync({ id: editingCurrency.id, ...payload }); - } else { - await createMutation.mutateAsync(payload); - } + await updateMutation.mutateAsync({ id: editingRate!.id, exchangeRate: rate }); }; - const confirmDelete = async () => { - if (deleteConfirm.id) { - await deleteMutation.mutateAsync(deleteConfirm.id); - } - }; - - const currenciesArray = Array.isArray(currencies) ? currencies : (currencies as any)?.items || []; + const currenciesArray = Array.isArray(currencies) ? currencies : (currencies as any)?.items ?? []; const columns = [ { key: 'code', - label: 'Code', - render: (currency: Currency) => ( - {currency.code} - ), - }, - { - key: 'name', - label: 'Name', - render: (currency: Currency) => ( - {currency.name} - ), - }, - { - key: 'symbol', - label: 'Symbol', - render: (currency: Currency) => ( - {currency.symbol} - ), - }, - { - key: 'baseCurrencyCode', - label: 'Base Currency', - render: (currency: Currency) => ( - {currency.baseCurrencyCode} - ), - }, - { - key: 'exchangeRate', - label: 'Exchange Rate', - render: (currency: Currency) => ( -
-
- 1 {currency.baseCurrencyCode} = {currency.exchangeRate.toFixed(4)} {currency.code} -
-
- 1 {currency.code} = {(1 / currency.exchangeRate).toFixed(6)} {currency.baseCurrencyCode} + label: 'Currency', + render: (c: CurrencyRate) => ( +
+ + {CURRENCY_META[c.code]?.symbol ?? c.symbol} + +
+
{c.code}
+
{CURRENCY_META[c.code]?.name ?? c.name}
), }, { - key: 'isActive', - label: 'Status', - render: (currency: Currency) => ( - - {currency.isActive ? 'Active' : 'Inactive'} - + key: 'baseCurrencyCode', + label: 'Base', + render: (c: CurrencyRate) => ( + {c.baseCurrencyCode} ), }, { - key: 'updatedAt', + key: 'exchangeRate', + label: 'Exchange Rate', + render: (c: CurrencyRate) => ( +
+
+ 1 {c.baseCurrencyCode} = {c.exchangeRate} {c.code} +
+
+ 1 {c.code} = {(1 / c.exchangeRate).toFixed(6)} {c.baseCurrencyCode} +
+
+ ), + }, + { + key: 'createdAt', label: 'Last Updated', - render: (currency: Currency) => ( + render: (c: CurrencyRate) => ( - {new Date(currency.updatedAt).toLocaleDateString()} + {new Date(c.createdAt).toLocaleDateString()} ), }, @@ -222,140 +122,93 @@ export default function CurrenciesPage() { const actions = [ { - label: 'Edit', - onClick: handleEditCurrency, + label: 'Edit Rate', + onClick: handleEdit, variant: 'secondary' as const, icon: Edit, }, - { - label: 'Delete', - onClick: (currency: Currency) => setDeleteConfirm({ isOpen: true, id: currency.id }), - variant: 'danger' as const, - icon: Trash2, - }, ]; return (
-

Currencies

-

Manage exchange rates and display currencies

-
-
- syncRatesMutation.mutate()} - loading={syncRatesMutation.isPending} - > - Sync Rates - - { - setError(null); - setEditingCurrency(null); - setCurrencyForm({ - code: '', - name: '', - symbol: '', - baseCurrencyCode: 'ETB', - exchangeRate: '', - }); - setShowModal(true); - }} - > - Add Currency - +

Exchange Rates

+

+ Manage ETB exchange rates for display currencies (DJF, USD) +

+ syncMutation.mutate()} + loading={syncMutation.isPending} + > + Sync Rates +
+ {error && !editingRate && ( +
+ {error} +
+ )} +
-
-
-
-
Total Currencies
-
- {currenciesArray.length} +
+ {(['ETB', 'DJF', 'USD'] as const).map((code) => { + const entry = currenciesArray.find((c: CurrencyRate) => c.code === code); + return ( +
+
+
{CURRENCY_META[code].name}
+
{code}
+
+
+ {entry ? ( + <> +
{entry.exchangeRate}
+
per ETB
+ + ) : ( + Not configured + )} +
-
-
-
Active
-
- {currenciesArray.filter((c: Currency) => c.isActive).length} -
-
-
-
Base Currency
-
ETB
-
-
-
Last Sync
-
- {currenciesArray.length > 0 - ? new Date(currenciesArray[0]?.updatedAt).toLocaleDateString() - : 'N/A'} -
-
-
- - {isLoading ? ( -
- -
- ) : currenciesArray.length === 0 ? ( -
-

No currencies configured. Click "Add Currency" to create one.

-
- ) : ( - - )} + ); + })}
+ + {isLoading ? ( +
+ +
+ ) : ( + + )}
-
-

Currency Management

-
    -
  • - • Base Currency: All exchange rates are calculated relative to this currency (typically ETB) -
  • -
  • - • Exchange Rate: How many units of the currency equal 1 unit of the base currency -
  • -
  • - • Display Currencies: Configure which currencies customers can view prices in -
  • -
  • - • Sync Rates: Automatically update exchange rates from external sources -
  • -
+
+

How it works

+

• ETB is the transaction currency — all fares are stored in ETB minor units (1 ETB = 100 minor)

+

• DJF and USD rates are used to display prices to passengers in their preferred currency

+

• Rates apply globally; changes take effect immediately on the next booking or fare quote

- {/* Delete Confirmation */} - setDeleteConfirm({ isOpen: false, id: null })} - onConfirm={confirmDelete} - title="Delete Currency" - message="Are you sure you want to delete this currency? This action cannot be undone." - confirmText="Delete" - isDanger={true} - warning="This will remove the currency from the system." - /> - - {/* Add/Edit Modal */} { setEditingRate(null); setError(null); }} + title={`Update Rate — ${editingRate?.code}`} + size="sm" >
{error && ( @@ -364,108 +217,38 @@ export default function CurrenciesPage() {
)} -
-
- - setCurrencyForm({ ...currencyForm, code: e.target.value.toUpperCase() })} - className="input w-full" - placeholder="e.g., USD" - maxLength={3} - disabled={!!editingCurrency} - required - /> -

3-letter ISO code (e.g., USD, DJF, GBP)

-
- -
- - setCurrencyForm({ ...currencyForm, name: e.target.value })} - className="input w-full" - placeholder="e.g., United States Dollar" - required - /> -
-
- -
-
- - setCurrencyForm({ ...currencyForm, symbol: e.target.value })} - className="input w-full" - placeholder="e.g., $" - maxLength={3} - required - /> -
- -
- - -

All rates relative to this currency

-
+
+ Currency: + {editingRate?.code} — {CURRENCY_META[editingRate?.code ?? '']?.name}
- -
- setCurrencyForm({ ...currencyForm, exchangeRate: e.target.value })} - className="input w-full" - placeholder="e.g., 0.018" - required - /> -
- 1 {currencyForm.baseCurrencyCode} = ? {currencyForm.code} -
-
- {currencyForm.exchangeRate && parseFloat(currencyForm.exchangeRate) > 0 && ( -

- ≈ 1 {currencyForm.code} = {(1 / parseFloat(currencyForm.exchangeRate)).toFixed(6)} {currencyForm.baseCurrencyCode} + + setRateInput(e.target.value)} + className="input w-full" + placeholder="e.g., 3.25" + autoFocus + /> + {rateInput && parseFloat(rateInput) > 0 && ( +

+ ≈ 1 {editingRate?.code} = {(1 / parseFloat(rateInput)).toFixed(6)} {editingRate?.baseCurrencyCode}

)}
-
-

Exchange Rate Example:

-

If 1 ETB = 0.018 USD, enter 0.018

-

If 1 ETB = 3.25 DJF, enter 3.25

-
- -
- +
+ { setEditingRate(null); setError(null); }}> Cancel - - {editingCurrency ? 'Update Currency' : 'Add Currency'} + + Save Rate
diff --git a/apps/edr-passenger-web/backoffice/src/app/pricing/page.tsx b/apps/edr-passenger-web/backoffice/src/app/pricing/page.tsx index a2dcae6aa..19fd44620 100644 --- a/apps/edr-passenger-web/backoffice/src/app/pricing/page.tsx +++ b/apps/edr-passenger-web/backoffice/src/app/pricing/page.tsx @@ -731,14 +731,19 @@ export default function PricingPage() {
- setFareForm({ ...fareForm, route: e.target.value })} className="input w-full" - placeholder="e.g., ADD-DJI" - /> -

e.g., ADD-DJI for full route

+ > + + {routesArray.map((route: Route) => ( + + ))} + +

Scope this fare to a specific route

From 578012dffd46fc5e331df3033bcdb163ffa2d34e Mon Sep 17 00:00:00 2001 From: Marshal Date: Thu, 18 Jun 2026 12:52:01 +0000 Subject: [PATCH 03/21] fix: improve route direction logic to align with backend derivation rules --- .../pages/bookings/new-booking-form/schema.ts | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/apps/edr-freight-web/portal/src/pages/bookings/new-booking-form/schema.ts b/apps/edr-freight-web/portal/src/pages/bookings/new-booking-form/schema.ts index ea4b1214a..0d7ebee79 100644 --- a/apps/edr-freight-web/portal/src/pages/bookings/new-booking-form/schema.ts +++ b/apps/edr-freight-web/portal/src/pages/bookings/new-booking-form/schema.ts @@ -288,22 +288,33 @@ export interface WagonConfig { type: "20ft" | "40ft"; } +/** + * Derive the trade direction from the origin/destination yard countries. + * + * Mirrors the backend's `deriveTradeDirection` exactly so the value the portal + * sends always matches what the API re-derives (the API rejects mismatches): + * - origin in Djibouti → IMPORT + * - destination in Djibouti (origin not) → EXPORT + * - everything else (e.g. Ethiopia↔Ethiopia)→ DOMESTIC + * + * Returns null only while a yard is still unselected, so the UI can wait. + */ export function getRouteDirection( origin: Freight.BookingReferenceYard | null | undefined, dest: Freight.BookingReferenceYard | null | undefined, ): Freight.ScheduleTradeDirection | null { if (!origin || !dest) return null; - if (origin.country === "Ethiopia" && dest.country === "Ethiopia") { - return "DOMESTIC"; - } - if (origin.country === "Ethiopia" && dest.country === "Djibouti") { - return "EXPORT"; - } - if (origin.country === "Djibouti" && dest.country === "Ethiopia") { + + const originCountry = origin.country?.trim(); + const destCountry = dest.country?.trim(); + + if (originCountry === "Djibouti") { return "IMPORT"; } - - return null; + if (destCountry === "Djibouti" && originCountry !== "Djibouti") { + return "EXPORT"; + } + return "DOMESTIC"; } export function calcWagons(containers: ContainerConfig[]) { From e1324719011302081e760eb45c6437d8efe9d773 Mon Sep 17 00:00:00 2001 From: Yonas Tewabe Date: Thu, 18 Jun 2026 16:26:35 +0300 Subject: [PATCH 04/21] Update docker-compose.yaml --- docker-compose.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose.yaml b/docker-compose.yaml index 3687f592a..09eb8728f 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -20,6 +20,8 @@ services: - "${FREIGHT_API_PORT:-3001}:${FREIGHT_API_PORT:-3001}" env_file: - apps/edr-freight-api/.env + extra_hosts: + - "paymentcallback.triaplc.com:10.18.7.179" passenger-api: build: From 421e0266bc43ca6d2301fc194c0c2ea51f5729ee Mon Sep 17 00:00:00 2001 From: Marshal Date: Thu, 18 Jun 2026 14:12:46 +0000 Subject: [PATCH 05/21] feat(train-scheduling): implement day-level booking pool - Added `unplaced` method in `BookingNotifierService` to log warnings for bookings that cannot be placed on any train. - Introduced `getAvailableDays` method in `TrainSchedulingService` to retrieve distinct days with open departures for a given route. - Created `AvailableDaysQueryDto` for querying available days based on origin and destination yards. - Updated `TrainSchedulingController` to expose an endpoint for available days. - Modified frontend components to support day-level booking, allowing customers to select only a day without pinning to a specific train. - Removed references to train schedules in booking forms and review steps, emphasizing day selection. - Added a database migration to create an index for efficient querying of bookings by route and day. --- .../1784100000000-AddBookingRouteDayIndex.ts | 21 ++ .../modules/bookings/bookings.repository.ts | 55 ++- .../src/modules/bookings/bookings.service.ts | 19 +- .../bookings/dto/create-booking.dto.ts | 13 +- .../bookings/entities/booking.entity.ts | 10 +- .../train-scheduling/batch-window.util.ts | 10 + .../booking-batch.service.spec.ts | 113 +++++- .../train-scheduling/booking-batch.service.ts | 227 ++++++++++-- .../booking-notifier.service.ts | 11 + .../dto/available-days-query.dto.ts | 14 + .../train-scheduling.controller.ts | 15 + .../train-scheduling.service.ts | 48 ++- .../src/pages/bookings/NewBookingPage.tsx | 18 +- .../portal/src/constants/URLS.ts | 1 + .../src/pages/bookings/EditBookingPage.tsx | 4 +- .../src/pages/bookings/NewBookingPage.tsx | 3 +- .../pages/bookings/new-booking-form/schema.ts | 6 +- .../new-booking-form/step-scheduling.tsx | 327 +++--------------- .../new-booking-form/step8-review.tsx | 8 +- .../portal/src/services/api.ts | 7 + .../portal/src/services/bookings.service.ts | 14 + packages/types/src/freight/index.ts | 14 + 22 files changed, 626 insertions(+), 332 deletions(-) create mode 100644 apps/edr-freight-api/src/migrations/1784100000000-AddBookingRouteDayIndex.ts create mode 100644 apps/edr-freight-api/src/modules/train-scheduling/dto/available-days-query.dto.ts diff --git a/apps/edr-freight-api/src/migrations/1784100000000-AddBookingRouteDayIndex.ts b/apps/edr-freight-api/src/migrations/1784100000000-AddBookingRouteDayIndex.ts new file mode 100644 index 000000000..544600bef --- /dev/null +++ b/apps/edr-freight-api/src/migrations/1784100000000-AddBookingRouteDayIndex.ts @@ -0,0 +1,21 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +/** + * Day-level booking pool: customers select a DAY (route + day), not a specific + * train. The batch engine's pool query filters bookings on + * (origin_yard_id, destination_yard_id, scheduled_date, status); this partial + * index backs that scan. + */ +export class AddBookingRouteDayIndex1784100000000 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE INDEX IF NOT EXISTS idx_bookings_route_day + ON freight.bookings (origin_yard_id, destination_yard_id, scheduled_date, status) + WHERE deleted_at IS NULL; + `); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX IF EXISTS freight.idx_bookings_route_day;`); + } +} diff --git a/apps/edr-freight-api/src/modules/bookings/bookings.repository.ts b/apps/edr-freight-api/src/modules/bookings/bookings.repository.ts index edda990de..b173bfe68 100644 --- a/apps/edr-freight-api/src/modules/bookings/bookings.repository.ts +++ b/apps/edr-freight-api/src/modules/bookings/bookings.repository.ts @@ -689,6 +689,11 @@ export class BookingsRepository extends BaseRepository { destinationStationId?: string; schedulingStatus?: string; trainScheduleId?: string; + /** + * EAT calendar day (yyyy-MM-dd). With day-level pooling the staff wizard sees + * the whole (route, day) pool rather than bookings pre-targeted to one train. + */ + day?: string; }): Promise { const qb = this.repository .createQueryBuilder('booking') @@ -706,9 +711,16 @@ export class BookingsRepository extends BaseRepository { .where('booking.status = :paidStatus', { paidStatus: 'PAID' }) .andWhere('scheduleBooking.id IS NULL'); - // Mirror the automatic batch pool: a schedule only ever considers bookings that - // targeted THAT schedule (same as findBatchPool's train_schedule_id filter). - if (options.trainScheduleId) { + // Day-level pooling: customers no longer set train_schedule_id, so the wizard + // surfaces the whole (route, EAT day) pool. Fall back to the legacy + // single-schedule filter only when no day is supplied (e.g. a staff-pinned + // booking that still carries train_schedule_id). + if (options.day) { + qb.andWhere( + `DATE(booking.scheduled_date AT TIME ZONE 'Africa/Addis_Ababa') = :day`, + { day: options.day }, + ); + } else if (options.trainScheduleId) { qb.andWhere('booking.train_schedule_id = :trainScheduleId', { trainScheduleId: options.trainScheduleId, }); @@ -765,6 +777,43 @@ export class BookingsRepository extends BaseRepository { .getMany(); } + /** + * Day-level batch pool: ready, not-yet-allocated bookings on a route for one + * EAT calendar day, regardless of which train they end up on. Same status + * rules and ordering as {@link findBatchPool}, but keyed on + * (origin, destination, day) instead of train_schedule_id — the engine then + * distributes these across all trains departing that day. + */ + findBatchPoolByRouteDay( + originYardId: string, + destinationYardId: string, + day: string, + ): Promise { + return this.repository + .createQueryBuilder('booking') + .leftJoinAndSelect('booking.company', 'company') + .leftJoinAndSelect('booking.bookingContainers', 'bookingContainer') + .leftJoin(TrainScheduleBooking, 'sb', 'sb.booking_id = booking.id') + .where('booking.origin_yard_id = :originYardId', { originYardId }) + .andWhere('booking.destination_yard_id = :destinationYardId', { + destinationYardId, + }) + .andWhere( + `DATE(booking.scheduled_date AT TIME ZONE 'Africa/Addis_Ababa') = :day`, + { day }, + ) + .andWhere('sb.id IS NULL') + .andWhere( + `((booking.is_government = false AND booking.status = 'FULLY_EXECUTED') + OR (booking.is_government = true AND booking.status IN ('APPROVED','PAID')))`, + ) + .orderBy('booking.is_government', 'DESC') + .addOrderBy('booking.priority_score', 'DESC') + .addOrderBy('booking.fully_executed_at', 'ASC') + .addOrderBy('booking.created_at', 'ASC') + .getMany(); + } + /** Every booking that targeted a schedule (any status) — for the batch monitoring board. */ findAllBySchedule(scheduleId: string): Promise { return this.repository diff --git a/apps/edr-freight-api/src/modules/bookings/bookings.service.ts b/apps/edr-freight-api/src/modules/bookings/bookings.service.ts index b32b16b32..4c8ef2cab 100644 --- a/apps/edr-freight-api/src/modules/bookings/bookings.service.ts +++ b/apps/edr-freight-api/src/modules/bookings/bookings.service.ts @@ -11,6 +11,7 @@ import { Freight, SchedulingStatus } from '@edr/types'; // import { CustomersService } from '../customers/customers.service'; import { CompaniesService } from '../companies/companies.service'; import { TrainSchedulingService } from '../train-scheduling/train-scheduling.service'; +import { eatDay } from '../train-scheduling/batch-window.util'; import { FilesService } from '../files/files.service'; import { MinioService } from '../minio/minio.service'; import { ContainerTypesService } from '../rule-engine/services/container-types.service'; @@ -273,8 +274,8 @@ export class BookingsService { companyId = company.id; } - // Schedule targeting: when provided, the schedule must be OPEN and on the same route. if (dto.trainScheduleId) { + // Staff manual pin: the schedule must be OPEN and on the same route. const schedule = await this.dataSource .getRepository(TrainSchedule) .findOne({ where: { id: dto.trainScheduleId } }); @@ -290,6 +291,22 @@ export class BookingsService { ) { throw new BadRequestException('Selected schedule is not on the booking route'); } + } else { + // Day-level pool: the customer picked a DAY — require that the route has at + // least one OPEN departure on that EAT day. The batch engine assigns the + // train later. + const day = eatDay(new Date(dto.scheduledDate)); + const hasDeparture = + await this.trainSchedulingService.existsOpenScheduleOnRouteDay( + dto.originYardId, + dto.destinationYardId, + day, + ); + if (!hasDeparture) { + throw new BadRequestException( + 'No departures available on the selected day for this route', + ); + } } const reference = dto.reference || (await this.generateReference()); diff --git a/apps/edr-freight-api/src/modules/bookings/dto/create-booking.dto.ts b/apps/edr-freight-api/src/modules/bookings/dto/create-booking.dto.ts index 3c7eca391..fa3bb6f4d 100644 --- a/apps/edr-freight-api/src/modules/bookings/dto/create-booking.dto.ts +++ b/apps/edr-freight-api/src/modules/bookings/dto/create-booking.dto.ts @@ -91,12 +91,21 @@ export class CreateBookingDto { @IsUUID() trainId?: string; - /** Target schedule this booking is created against (required by the backoffice create form). */ - @ApiPropertyOptional({ format: 'uuid', description: 'Target train schedule (pool membership)' }) + /** + * Staff-only manual pin to a specific train. Customers omit this — they pick a + * DAY via {@link scheduledDate} and the batch engine assigns a train within + * that (route, day) pool. When provided, the schedule must be OPEN and on the + * booking route. + */ + @ApiPropertyOptional({ + format: 'uuid', + description: 'Staff only: pin to a specific train schedule. Customers omit this.', + }) @IsOptional() @IsUUID() trainScheduleId?: string; + /** The day the customer wants to ship (the pool day key). */ @ApiProperty({ example: '2026-06-15T00:00:00.000Z' }) @IsDateString() scheduledDate!: string; diff --git a/apps/edr-freight-api/src/modules/bookings/entities/booking.entity.ts b/apps/edr-freight-api/src/modules/bookings/entities/booking.entity.ts index d6f7f9b55..d5e72d860 100644 --- a/apps/edr-freight-api/src/modules/bookings/entities/booking.entity.ts +++ b/apps/edr-freight-api/src/modules/bookings/entities/booking.entity.ts @@ -277,7 +277,15 @@ export class Booking extends BaseEntity { @Column({ name: 'scheduled_at', type: 'timestamptz', nullable: true }) scheduledAt?: Date | null; - /** The schedule this booking targets (pool membership), set at creation. FK to train_schedules. */ + /** + * The train this booking is assigned to. FK to train_schedules. + * + * Day-level pooling: customers no longer pick a train — they pick a DAY, and + * this stays null at creation. The batch engine sets it when it assigns the + * booking to a specific train within its (route, day) pool; staff may also + * pin it manually. The day-level pool is keyed on + * (origin_yard_id, destination_yard_id, day of scheduled_date), not this column. + */ @Column({ name: 'train_schedule_id', type: 'uuid', nullable: true }) trainScheduleId?: string | null; diff --git a/apps/edr-freight-api/src/modules/train-scheduling/batch-window.util.ts b/apps/edr-freight-api/src/modules/train-scheduling/batch-window.util.ts index 38ab407bc..7316e1610 100644 --- a/apps/edr-freight-api/src/modules/train-scheduling/batch-window.util.ts +++ b/apps/edr-freight-api/src/modules/train-scheduling/batch-window.util.ts @@ -55,6 +55,16 @@ function eatParts(date: Date): EatDateParts { }; } +/** + * The EAT calendar day a timestamp falls on, as `yyyy-MM-dd`. This is the day + * key for day-level booking pools — it must match the day the portal calendar + * renders, so always derive day keys through this (never `toISOString().slice`). + */ +export function eatDay(date: Date): string { + const { year, month, day } = eatParts(date); + return `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`; +} + /** Build a UTC Date for a given EAT local wall-clock time on a calendar day. */ function eatToUtc( year: number, diff --git a/apps/edr-freight-api/src/modules/train-scheduling/booking-batch.service.spec.ts b/apps/edr-freight-api/src/modules/train-scheduling/booking-batch.service.spec.ts index a08dd71ec..5cd06091a 100644 --- a/apps/edr-freight-api/src/modules/train-scheduling/booking-batch.service.spec.ts +++ b/apps/edr-freight-api/src/modules/train-scheduling/booking-batch.service.spec.ts @@ -20,6 +20,7 @@ describe('BookingBatchService — PAID reconcile', () => { let bookingsRepository: { findPaidUnlinkedForSchedule: jest.Mock; findBatchPool: jest.Mock; + findBatchPoolByRouteDay: jest.Mock; findReservedForSchedule: jest.Mock; update: jest.Mock; }; @@ -33,16 +34,24 @@ describe('BookingBatchService — PAID reconcile', () => { }; let trainSchedulingService: { tryAutoWagonAllocation: jest.Mock; + getBookableSchedules: jest.Mock; }; let dataSource: { getRepository: jest.Mock; transaction: jest.Mock; }; + let notifier: { + payNow: jest.Mock; + secured: jest.Mock; + expired: jest.Mock; + unplaced: jest.Mock; + }; beforeEach(() => { bookingsRepository = { findPaidUnlinkedForSchedule: jest.fn().mockResolvedValue([]), findBatchPool: jest.fn().mockResolvedValue([]), + findBatchPoolByRouteDay: jest.fn().mockResolvedValue([]), findReservedForSchedule: jest.fn().mockResolvedValue([]), update: jest.fn().mockResolvedValue(undefined), }; @@ -67,11 +76,14 @@ describe('BookingBatchService — PAID reconcile', () => { issues: [], violations: [], }), + getBookableSchedules: jest.fn().mockResolvedValue([]), }; const bookingRepo = { findOne: jest.fn().mockResolvedValue(paidBooking), update: jest.fn().mockResolvedValue(undefined), + // WagonType.find() / global-rules find() fall back to defaults when empty. + find: jest.fn().mockResolvedValue([]), }; dataSource = { getRepository: jest.fn().mockReturnValue(bookingRepo), @@ -83,12 +95,19 @@ describe('BookingBatchService — PAID reconcile', () => { }), }; + notifier = { + payNow: jest.fn(), + secured: jest.fn(), + expired: jest.fn(), + unplaced: jest.fn(), + }; + service = new BookingBatchService( dataSource as never, bookingsRepository as never, trainSchedulesRepository as never, trainScheduleBookingsRepository as never, - { payNow: jest.fn(), secured: jest.fn(), expired: jest.fn() } as never, + notifier as never, { addTimeout: jest.fn(), deleteTimeout: jest.fn(), doesExist: jest.fn() } as never, trainSchedulingService as never, ); @@ -141,4 +160,96 @@ describe('BookingBatchService — PAID reconcile', () => { expect(fillOrder).toBeLessThan(reconcileOrder); expect(reconcileOrder).toBeLessThan(wagonOrder); }); + + describe('fillRouteDay — day-level distribution', () => { + const originYardId = 'yard-origin'; + const destinationYardId = 'yard-dest'; + const day = '2026-06-20'; + // 06:00Z and 09:00Z on 2026-06-20 both land on the same EAT day. + const trainA = 'train-a'; + const trainB = 'train-b'; + + // A tiny locomotive: default wagon = 14m / 70t → exactly 1 wagon slot fits. + const smallLoco = { maxPullWeightTons: 70, maxTrainLengthMeters: 14 }; + + const commercial = (id: string, priority: number): Booking => + ({ + id, + reference: id, + isGovernment: false, + priorityScore: priority, + status: 'FULLY_EXECUTED', + wagonsRequired: 1, + cargoTotalWeightVgm: 10, + freightType: 'CONTAINER', + bookingContainers: [], + }) as unknown as Booking; + + beforeEach(() => { + // Two OPEN trains on the same route + day, train A earlier than train B. + trainSchedulingService.getBookableSchedules.mockResolvedValue([ + { + id: trainA, + scheduleDate: '2026-06-20T06:00:00.000Z', + bookingWindowStatus: 'OPEN', + }, + { + id: trainB, + scheduleDate: '2026-06-20T09:00:00.000Z', + bookingWindowStatus: 'OPEN', + }, + ]); + trainSchedulesRepository.findByIdWithFullGraph.mockImplementation((id: string) => + Promise.resolve({ + id, + maxWagons: 1, + bookingWindowStatus: 'OPEN', + trainSetId: `set-${id}`, + trainSet: { locomotive: smallLoco }, + scheduleBookings: [], + }), + ); + }); + + it('spills overflow to the next train by priority, then reports unplaced', async () => { + // 3 commercial bookings, descending priority; only 1 fits per train (2 total). + bookingsRepository.findBatchPoolByRouteDay.mockResolvedValue([ + commercial('hi', 30), + commercial('mid', 20), + commercial('lo', 10), + ]); + + const touched = await service.fillRouteDay(originYardId, destinationYardId, day); + + expect(bookingsRepository.findBatchPoolByRouteDay).toHaveBeenCalledWith( + originYardId, + destinationYardId, + day, + ); + // Both trains were processed. + expect(touched).toEqual([trainA, trainB]); + // Highest priority reserved on train A, next on train B (commercial → reserve). + const reservedOn = notifier.payNow.mock.calls.map((c) => (c[0] as Booking).id); + expect(reservedOn).toEqual(['hi', 'mid']); + // The third booking fits no train and is reported unplaced (and only it). + expect(notifier.unplaced).toHaveBeenCalledTimes(1); + expect((notifier.unplaced.mock.calls[0][0] as Booking).id).toBe('lo'); + expect(notifier.unplaced.mock.calls[0][1]).toBe(day); + }); + + it('reserves the chosen train id on each commercial booking', async () => { + bookingsRepository.findBatchPoolByRouteDay.mockResolvedValue([commercial('hi', 30)]); + + await service.fillRouteDay(originYardId, destinationYardId, day); + + // reserve() persists trainScheduleId so the settle lifecycle can find the train. + expect(bookingsRepository.update).toHaveBeenCalledWith( + 'hi', + expect.objectContaining({ + trainScheduleId: trainA, + status: 'SELECTED_FOR_BATCH', + }), + ); + }); + }); }); diff --git a/apps/edr-freight-api/src/modules/train-scheduling/booking-batch.service.ts b/apps/edr-freight-api/src/modules/train-scheduling/booking-batch.service.ts index 771422fa2..514f8572d 100644 --- a/apps/edr-freight-api/src/modules/train-scheduling/booking-batch.service.ts +++ b/apps/edr-freight-api/src/modules/train-scheduling/booking-batch.service.ts @@ -19,7 +19,7 @@ import { TrainScheduleBookingsRepository } from '../train-schedules/train-schedu import { TrainSchedulingGlobalRules } from './entities/train-scheduling-global-rules.entity'; import { BookingNotifierService } from './booking-notifier.service'; import { TrainSchedulingService } from './train-scheduling.service'; -import { groupBookingsIntoBoardWindows } from './batch-window.util'; +import { eatDay, groupBookingsIntoBoardWindows } from './batch-window.util'; import { BATCH_CRON, BATCH_TIMEZONE, @@ -42,6 +42,14 @@ interface Capacity { lengthMeters: number; } +/** A day-level pool key: all trains on this route departing on this EAT day. */ +interface RouteDayGroup { + originYardId: string; + destinationYardId: string; + /** EAT calendar day, `yyyy-MM-dd`. */ + day: string; +} + type WagonLengths = { container: number; bulk: number }; export type BatchBoardBookingState = @@ -173,16 +181,16 @@ export class BookingBatchService implements OnModuleInit { private readonly trainSchedulingService: TrainSchedulingService, ) {} - /** On boot, reconcile OPEN schedules and re-arm settle timers. */ + /** On boot, reconcile OPEN route-days and re-arm settle timers. */ async onModuleInit(): Promise { - const open = await this.trainSchedulesRepository.findAll({ - where: { bookingWindowStatus: 'OPEN' }, - }); - for (const s of open) { + const groups = await this.openRouteDayGroups(); + for (const group of groups) { try { - await this.processSchedule(s.id); + await this.processRouteDay(group); } catch (err) { - this.logger.warn(`Boot reconcile failed for ${s.id}: ${(err as Error).message}`); + this.logger.warn( + `Boot reconcile failed for ${this.groupLabel(group)}: ${(err as Error).message}`, + ); } } const reserved = await this.dataSource @@ -195,13 +203,48 @@ export class BookingBatchService implements OnModuleInit { for (const { scheduleId } of reserved) this.armSettle(scheduleId); } - /** Fire-and-forget batch pipeline for a schedule (contract sign, cron, payment). */ + /** + * Fire-and-forget batch pipeline for the (route, day) a schedule belongs to + * (contract sign, payment). Day-level pooling distributes across all of that + * day's trains, so a single schedule id maps to its whole route-day group. + */ enqueueScheduleProcessing(scheduleId: string): void { - void this.processSchedule(scheduleId).catch((err) => - this.logger.error(`processSchedule ${scheduleId} failed: ${(err as Error).message}`), + void this.processRouteDayForSchedule(scheduleId).catch((err) => + this.logger.error( + `processRouteDay for schedule ${scheduleId} failed: ${(err as Error).message}`, + ), ); } + /** Resolve a schedule's (route, day) group and run the day-level pipeline. */ + private async processRouteDayForSchedule(scheduleId: string): Promise { + const schedule = await this.trainSchedulesRepository.findById(scheduleId); + if (!schedule?.scheduledDepartureDate) return; + await this.processRouteDay({ + originYardId: schedule.originStationId, + destinationYardId: schedule.destinationStationId, + day: eatDay(schedule.scheduledDepartureDate), + }); + } + + /** + * Day-level pipeline: distribute the (route, day) pool across all its trains, + * then settle / reconcile / assign wagons per schedule (those steps stay + * schedule-scoped — only the fill is day-level). + */ + async processRouteDay(group: RouteDayGroup): Promise { + const scheduleIds = await this.fillRouteDay( + group.originYardId, + group.destinationYardId, + group.day, + ); + for (const scheduleId of scheduleIds) { + await this.settleDueReservations(scheduleId); + await this.reconcilePaidUnlinked(scheduleId); + await this.trainSchedulingService.tryAutoWagonAllocation(scheduleId); + } + } + /** Fill pool, settle due reservations, link orphaned PAID, then assign wagons. */ async processSchedule(scheduleId: string): Promise { await this.fillSchedule(scheduleId); @@ -210,6 +253,31 @@ export class BookingBatchService implements OnModuleInit { await this.trainSchedulingService.tryAutoWagonAllocation(scheduleId); } + /** Distinct (origin, destination, EAT day) groups across all OPEN schedules. */ + private async openRouteDayGroups(): Promise { + const open = await this.trainSchedulesRepository.findAll({ + where: { bookingWindowStatus: 'OPEN' }, + }); + const groups = new Map(); + for (const s of open) { + if (!s.scheduledDepartureDate) continue; + const day = eatDay(s.scheduledDepartureDate); + const key = `${s.originStationId}|${s.destinationStationId}|${day}`; + if (!groups.has(key)) { + groups.set(key, { + originYardId: s.originStationId, + destinationYardId: s.destinationStationId, + day, + }); + } + } + return [...groups.values()]; + } + + private groupLabel(group: RouteDayGroup): string { + return `${group.originYardId}→${group.destinationYardId} on ${group.day}`; + } + /** * Idempotent: link a paid batch booking to its schedule and assign wagons. * Handles SELECTED_FOR_BATCH, PAID-without-link, and PAID-already-linked cases. @@ -289,15 +357,15 @@ export class BookingBatchService implements OnModuleInit { @Cron(BATCH_CRON, { name: 'booking-batch-fill', timeZone: BATCH_TIMEZONE }) async runBatchFill(): Promise { - const open = await this.trainSchedulesRepository.findAll({ - where: { bookingWindowStatus: 'OPEN' }, - }); - this.logger.log(`Batch fill: ${open.length} OPEN schedule(s).`); - for (const s of open) { + const groups = await this.openRouteDayGroups(); + this.logger.log(`Batch fill: ${groups.length} OPEN route-day group(s).`); + for (const group of groups) { try { - await this.processSchedule(s.id); + await this.processRouteDay(group); } catch (err) { - this.logger.error(`Batch fill failed for ${s.id}: ${(err as Error).message}`); + this.logger.error( + `Batch fill failed for ${this.groupLabel(group)}: ${(err as Error).message}`, + ); } } } @@ -611,7 +679,7 @@ export class BookingBatchService implements OnModuleInit { if (booking.isGovernment) { await this.allocate(scheduleId, booking, 'gov'); } else { - await this.reserve(booking); + await this.reserve(booking, scheduleId); armed = true; } budget = this.subtract(budget, need); @@ -623,6 +691,106 @@ export class BookingBatchService implements OnModuleInit { void this.triggerWagonAllocation(scheduleId); } + /** + * Distribute one (route, day) pool across ALL of that day's OPEN trains, by + * priority, filling each train (earliest departure first) until it's full and + * spilling overflow to the next. Government bookings that fit no train preempt + * lower-priority commercial; bookings that fit no train at all stay pending and + * trigger a staff `unplaced` warning. Returns the schedule ids that were touched + * (or that had remaining pool work) so the caller can settle them per-schedule. + */ + async fillRouteDay( + originYardId: string, + destinationYardId: string, + day: string, + ): Promise { + // The day's OPEN bookable schedules on this exact corridor, earliest first. + const bookable = await this.trainSchedulingService.getBookableSchedules( + originYardId, + destinationYardId, + ); + const scheduleIds = bookable + .filter( + (s) => + s.bookingWindowStatus === 'OPEN' && + s.scheduleDate != null && + eatDay(new Date(s.scheduleDate)) === day, + ) + .sort( + (a, b) => + new Date(a.scheduleDate).getTime() - new Date(b.scheduleDate).getTime(), + ) + .map((s) => s.id); + + if (scheduleIds.length === 0) return []; + + const rules = await this.loadGlobalRules(); + const wagonLengths = await this.loadWagonLengths(); + + // Live per-schedule budget + arm flag, in departure order. + const trains: Array<{ id: string; budget: Capacity; armed: boolean }> = []; + for (const id of scheduleIds) { + const schedule = await this.trainSchedulesRepository.findByIdWithFullGraph(id); + const locomotive = schedule?.trainSet?.locomotive; + if (!schedule || !schedule.trainSetId || !locomotive) { + this.logger.warn(`Schedule ${id} has no locomotive/train set — skipped.`); + continue; + } + const limits = await this.capacityLimits(locomotive, rules); + await this.syncScheduleMaxWagons(schedule, locomotive, rules); + const budget = await this.remainingCapacity(schedule, limits, wagonLengths); + trains.push({ id, budget, armed: false }); + } + if (trains.length === 0) return []; + + const pool = await this.bookingsRepository.findBatchPoolByRouteDay( + originYardId, + destinationYardId, + day, + ); + + for (const booking of pool) { + const need = this.needFor(booking, wagonLengths); + + // First train (earliest departure) that fits this booking as-is. + let target = trains.find((t) => this.fits(need, t.budget)); + + if (!target && booking.isGovernment) { + // Government booking fits nowhere on its own — try to preempt commercial + // on each train (earliest first) until one frees enough room. + for (const t of trains) { + t.budget = await this.preemptForGovernment(t.id, need, t.budget, wagonLengths); + if (this.fits(need, t.budget)) { + target = t; + break; + } + } + } + + if (!target) { + // Fits no train this day — stays in the pool, retried next batch. + this.notifier.unplaced(booking, day); + continue; + } + + if (booking.isGovernment) { + await this.allocate(target.id, booking, 'gov'); + } else { + await this.reserve(booking, target.id); + target.armed = true; + } + target.budget = this.subtract(target.budget, need); + } + + for (const t of trains) { + if (t.budget.wagons <= 0) await this.setWindow(t.id, 'FULL'); + if (t.armed) this.armSettle(t.id); + void this.triggerWagonAllocation(t.id); + } + + return trains.map((t) => t.id); + } + /** Durable settle: allocate paid / expire overdue reservations, then top up. */ async settleDueReservations(scheduleId: string): Promise { const reserved = await this.bookingsRepository.findReservedForSchedule(scheduleId); @@ -766,15 +934,24 @@ export class BookingBatchService implements OnModuleInit { // ---- mutations ------------------------------------------------------------ - /** Reserve capacity for a commercial booking and open its pay window. */ - private async reserve(booking: Booking): Promise { + /** + * Reserve capacity for a commercial booking on a specific train and open its + * pay window. `scheduleId` is persisted so the settle/allocate lifecycle + * (settleDueReservations, settleBatch, ensurePaidBookingAllocated, markPaid), + * which is all keyed off `booking.trainScheduleId`, can find the train — with + * day-level pooling the booking arrives here with `trainScheduleId` still null, + * so the engine sets it as it picks the train. + */ + private async reserve(booking: Booking, scheduleId: string): Promise { const now = new Date(); const deadline = new Date(now.getTime() + PAYMENT_WINDOW_MS); await this.bookingsRepository.update(booking.id, { + trainScheduleId: scheduleId, status: 'SELECTED_FOR_BATCH', selectedForBatchAt: now, paymentDeadline: deadline, } as never); + booking.trainScheduleId = scheduleId; await this.notifier.payNow(booking, deadline); } @@ -807,14 +984,20 @@ export class BookingBatchService implements OnModuleInit { void this.triggerWagonAllocation(scheduleId); } - /** Expire an unpaid reservation and free its capacity. */ + /** + * Expire an unpaid reservation and free its capacity. With day-level pooling we + * also clear `trainScheduleId` so the booking is no longer pinned to the train + * it failed to pay for — it's back in the day pool for staff to act on. + */ private async expire(booking: Booking): Promise { await this.bookingsRepository.update(booking.id, { + trainScheduleId: null, status: 'EXPIRED', schedulingStatus: 'ELIGIBLE', paymentDeadline: null, selectedForBatchAt: null, } as never); + booking.trainScheduleId = null; this.notifier.expired(booking); } diff --git a/apps/edr-freight-api/src/modules/train-scheduling/booking-notifier.service.ts b/apps/edr-freight-api/src/modules/train-scheduling/booking-notifier.service.ts index e63bc1aec..e8f272123 100644 --- a/apps/edr-freight-api/src/modules/train-scheduling/booking-notifier.service.ts +++ b/apps/edr-freight-api/src/modules/train-scheduling/booking-notifier.service.ts @@ -67,6 +67,17 @@ export class BookingNotifierService { ); } + /** + * Staff-facing warning when a pooled booking fits no train on its chosen day. + * It stays pending and is retried next batch; staff can add capacity or pin it + * to a train manually. Mirrors {@link scheduleFull} — no customer notification. + */ + unplaced(b: Booking, day: string): void { + this.logger.warn( + `UNPLACED — ${this.ref(b)} could not be placed on any train for ${day}; add capacity or assign it manually.`, + ); + } + displaced(b: Booking): void { const msg = `Booking ${b.reference ?? b.id} was displaced by a government booking. Move to another schedule or cancel.`; void this.notifyContact(b, msg, 'DISPLACED'); diff --git a/apps/edr-freight-api/src/modules/train-scheduling/dto/available-days-query.dto.ts b/apps/edr-freight-api/src/modules/train-scheduling/dto/available-days-query.dto.ts new file mode 100644 index 000000000..bb55508e5 --- /dev/null +++ b/apps/edr-freight-api/src/modules/train-scheduling/dto/available-days-query.dto.ts @@ -0,0 +1,14 @@ +import { ApiPropertyOptional } from '@nestjs/swagger'; +import { IsOptional, IsUUID } from 'class-validator'; + +export class AvailableDaysQueryDto { + @ApiPropertyOptional({ format: 'uuid' }) + @IsOptional() + @IsUUID() + originYardId?: string; + + @ApiPropertyOptional({ format: 'uuid' }) + @IsOptional() + @IsUUID() + destinationYardId?: string; +} diff --git a/apps/edr-freight-api/src/modules/train-scheduling/train-scheduling.controller.ts b/apps/edr-freight-api/src/modules/train-scheduling/train-scheduling.controller.ts index 2a4c3a357..0c01dd219 100644 --- a/apps/edr-freight-api/src/modules/train-scheduling/train-scheduling.controller.ts +++ b/apps/edr-freight-api/src/modules/train-scheduling/train-scheduling.controller.ts @@ -32,6 +32,7 @@ import { PreviewTrainScheduleDto } from "./dto/preview-train-schedule.dto"; import { RecordCheckpointDto } from "./dto/record-checkpoint.dto"; import { AvailableLocomotivesQueryDto } from "./dto/available-locomotives-query.dto"; import { BookableSchedulesQueryDto } from "./dto/bookable-schedules-query.dto"; +import { AvailableDaysQueryDto } from "./dto/available-days-query.dto"; import { UpdateTrainSchedulingGlobalRulesDto } from "./dto/update-train-scheduling-global-rules.dto"; import { TrainSchedulingService } from "./train-scheduling.service"; import { BookingBatchService } from "./booking-batch.service"; @@ -108,6 +109,20 @@ export class TrainSchedulingController { ); } + @Get("available-days") + // No staff guard: customers hit this while creating a booking to find which + // DAYS have a departure on their route. Day-level pooling — no capacity is + // returned, only the list of bookable days. + @ApiOperation({ + summary: "Distinct days with an OPEN same-route departure (day-level pool)", + }) + getAvailableDays(@Query() query: AvailableDaysQueryDto) { + return this.trainSchedulingService.getAvailableDays( + query.originYardId, + query.destinationYardId, + ); + } + @Get("container/eligible-bookings") @TrainSchedulingView() @ApiOperation({ summary: "List eligible container bookings" }) diff --git a/apps/edr-freight-api/src/modules/train-scheduling/train-scheduling.service.ts b/apps/edr-freight-api/src/modules/train-scheduling/train-scheduling.service.ts index 98b6d70c2..7bfe5811e 100644 --- a/apps/edr-freight-api/src/modules/train-scheduling/train-scheduling.service.ts +++ b/apps/edr-freight-api/src/modules/train-scheduling/train-scheduling.service.ts @@ -87,6 +87,7 @@ import { DEFAULT_BULK_WAGON_LENGTH_METERS, DEFAULT_CONTAINER_WAGON_LENGTH_METERS, } from './booking-batch.constants'; +import { eatDay } from './batch-window.util'; import { TrainCheckpointEvent } from './entities/train-checkpoint-event.entity'; import { TrainCheckpointEventsRepository } from './train-checkpoint-events.repository'; import { RecordCheckpointDto } from './dto/record-checkpoint.dto'; @@ -167,12 +168,28 @@ export class TrainSchedulingService { ) {} async getEligibleBookings(query: GetEligibleBookingsDto) { + // Day-level pooling: when the wizard targets a schedule, surface the whole + // (route, EAT day) pool — not just bookings pre-pinned to that train — by + // resolving the schedule's route + day and filtering on the day instead. + let day: string | undefined; + let originStationId = query.originStationId; + let destinationStationId = query.destinationStationId; + if (query.trainScheduleId) { + const schedule = await this.trainSchedulesRepository.findById(query.trainScheduleId); + if (schedule?.scheduledDepartureDate) { + day = eatDay(schedule.scheduledDepartureDate); + originStationId = originStationId ?? schedule.originStationId; + destinationStationId = destinationStationId ?? schedule.destinationStationId; + } + } + const bookings = await this.bookingsRepository.findEligibleForScheduling({ freightType: query.freightType, - originStationId: query.originStationId, - destinationStationId: query.destinationStationId, + originStationId, + destinationStationId, schedulingStatus: query.schedulingStatus, trainScheduleId: query.trainScheduleId, + day, }); return { count: bookings.length, items: bookings.map((b) => this.mapEligibleBooking(b)) }; } @@ -1989,6 +2006,33 @@ export class TrainSchedulingService { return filteredSchedules; } + /** + * Day-level pool: the distinct EAT calendar days that have ≥1 OPEN bookable + * departure on the route. Customers pick a DAY (not a train) — so this returns + * only the day strings, no capacity, counts or train info. + */ + async getAvailableDays( + originYardId?: string, + destinationYardId?: string, + ): Promise<{ days: string[] }> { + const schedules = await this.getBookableSchedules(originYardId, destinationYardId); + const days = new Set(); + for (const s of schedules) { + if (s.scheduleDate) days.add(eatDay(new Date(s.scheduleDate))); + } + return { days: [...days].sort() }; + } + + /** Whether a route has ≥1 OPEN bookable departure on a given EAT day. */ + async existsOpenScheduleOnRouteDay( + originYardId: string, + destinationYardId: string, + day: string, + ): Promise { + const { days } = await this.getAvailableDays(originYardId, destinationYardId); + return days.includes(day); + } + private async mapScheduleDetail( schedule: import('../train-schedules/entities/train-schedule.entity').TrainSchedule, ) { diff --git a/apps/edr-freight-web/backoffice/src/pages/bookings/NewBookingPage.tsx b/apps/edr-freight-web/backoffice/src/pages/bookings/NewBookingPage.tsx index 38be6dd31..86c188d86 100644 --- a/apps/edr-freight-web/backoffice/src/pages/bookings/NewBookingPage.tsx +++ b/apps/edr-freight-web/backoffice/src/pages/bookings/NewBookingPage.tsx @@ -302,8 +302,9 @@ export default function NewBookingPage() { const allLinesValid = lines.length > 0 && lines.every(lineValid); const sameYard = Boolean(originYardId && originYardId === destinationYardId); - const scheduleSatisfied = - hasBookableSchedules ? Boolean(trainScheduleId) : Boolean(scheduledDate); + // Day-level pool: a shipment DAY is enough to proceed. Pinning a specific train + // (trainScheduleId) is an optional staff override — the batch engine otherwise + // assigns the train. A selected schedule implies its day, so either satisfies. const departureSatisfied = Boolean(selectedSchedule) || Boolean(scheduledDate); const canSubmit = @@ -311,7 +312,6 @@ export default function NewBookingPage() { Boolean(destinationYardId) && !sameYard && Boolean(tradeDirection) && - scheduleSatisfied && Boolean(serviceTypeId) && departureSatisfied && (isGovernment ? governmentInstitution.trim().length >= 2 : Boolean(companyId)) && @@ -473,25 +473,25 @@ export default function NewBookingPage() { {hasBookableSchedules ? ( - ) : originYardId && destinationYardId ? ( - - No open train schedule on this route — set a preferred departure below. The batch - engine assigns a train on that day, or staff can pin one later. - - ) : null} + - {selectedSchedule ? ( - - ) : ( - setScheduledDate(e.target.value)} - /> - )} +