export flow and fix intercity issue

This commit is contained in:
Marshal
2026-07-30 13:15:56 +00:00
parent e80aac877f
commit 2cdf8bd161
2 changed files with 105 additions and 19 deletions

View File

@@ -1088,6 +1088,15 @@ export class BookingsRepository extends BaseRepository<Booking> {
* the whole (route, day) pool rather than bookings pre-targeted to one train.
*/
day?: string;
/**
* The schedule's ordered route stops. When given, the corridor filter
* replaces the exact origin/destination match: any booking whose BOTH yards
* lie on the route qualifies (sub-corridor bookings like Dire→DCT on a
* GMT→Dire→DCT train — the caller still checks stop ORDER). Dateless
* DOMESTIC (intercity) bookings also join the pool: they ride any train on
* their corridor.
*/
corridorYardIds?: string[];
}): Promise<Booking[]> {
const qb = this.repository
.createQueryBuilder('booking')
@@ -1111,8 +1120,11 @@ export class BookingsRepository extends BaseRepository<Booking> {
// single-schedule filter only when no day is supplied (e.g. a staff-pinned
// booking that still carries train_schedule_id).
if (options.day) {
// Dateless DOMESTIC (intercity) bookings ride any train on their corridor
// — no scheduled_date to match, so the day filter must not hide them.
qb.andWhere(
`DATE(booking.scheduled_date AT TIME ZONE 'Africa/Addis_Ababa') = :day`,
`(DATE(booking.scheduled_date AT TIME ZONE 'Africa/Addis_Ababa') = :day
OR (booking.trade_direction = 'DOMESTIC' AND booking.scheduled_date IS NULL))`,
{ day: options.day },
);
} else if (options.trainScheduleId) {
@@ -1125,15 +1137,23 @@ export class BookingsRepository extends BaseRepository<Booking> {
qb.andWhere('booking.freightType = :freightType', { freightType: options.freightType });
}
if (options.originStationId) {
qb.andWhere('booking.originYardId = :originStationId', {
originStationId: options.originStationId,
});
}
if (options.destinationStationId) {
qb.andWhere('booking.destinationYardId = :destinationStationId', {
destinationStationId: options.destinationStationId,
if (options.corridorYardIds?.length) {
qb.andWhere('booking.originYardId IN (:...corridorYardIds)', {
corridorYardIds: options.corridorYardIds,
}).andWhere('booking.destinationYardId IN (:...corridorYardIds)', {
corridorYardIds: options.corridorYardIds,
});
} else {
if (options.originStationId) {
qb.andWhere('booking.originYardId = :originStationId', {
originStationId: options.originStationId,
});
}
if (options.destinationStationId) {
qb.andWhere('booking.destinationYardId = :destinationStationId', {
destinationStationId: options.destinationStationId,
});
}
}
if (options.schedulingStatus) {
qb.andWhere('booking.scheduling_status = :schedulingStatus', {