fix(flight-live): tell a delay only to the bookings it is late for

Bookings that share one flight state can hold different scheduled times, so the delay notice now goes only to the legs whose own departure is at least fifteen minutes late instead of telling the others it is delayed by 0 minutes.
This commit is contained in:
Maurice
2026-11-23 14:57:21 +01:00
parent 8e51410251
commit e7e3592d97
2 changed files with 23 additions and 4 deletions
@@ -323,13 +323,17 @@ export class FlightLivePollService {
// One leg after the other, as every other sender in the codebase does: a notice writes its rows.
await legs.reduce(async (previous, leg) => {
await previous;
const delay = departureDelay(leg, state, now);
// Legs sharing the state can hold different schedules: a delay is news only where it is one.
const legEvents = events.filter((event) => event !== 'flight_delayed' || (delay ?? 0) >= DELAY_NOTICE_MIN);
if (legEvents.length === 0) return;
const params: Record<string, string> = {
flight: leg.flight_number,
from: leg.origin.code,
to: leg.destination.code,
trip: leg.trip_title,
tripId: String(leg.trip_id),
delay: String(Math.max(0, departureDelay(leg, state, now) ?? 0)),
delay: String(Math.max(0, delay ?? 0)),
};
try {
const travelers = await this.states.listTravelerIds(leg.reservation_id);
@@ -337,7 +341,7 @@ export class FlightLivePollService {
travelers.length === 0
? [{ scope: 'trip', targetId: leg.trip_id }]
: travelers.map((userId) => ({ scope: 'user', targetId: userId }));
const sends = events.flatMap((event) => targets.map((target) => ({ event, ...target })));
const sends = legEvents.flatMap((event) => targets.map((target) => ({ event, ...target })));
await sends.reduce(async (sent, payload) => {
await sent;
await this.notifications.send({ ...payload, actorId: null, params });
@@ -71,7 +71,7 @@ afterAll(async () => {
db.close();
});
async function seedFlight(title = 'Berlin') {
async function seedFlight(title = 'Berlin', departs = '12:00') {
const { user } = await makeUser(orm);
const trip = await makeTrip(orm, user.id, { title });
const flight = await makeReservation(orm, trip.id, {
@@ -89,7 +89,7 @@ async function seedFlight(title = 'Berlin') {
lng: 8.5706,
timezone: 'Europe/Berlin',
local_date: '2026-07-01',
local_time: '12:00',
local_time: departs,
});
await makeReservationEndpoint(orm, flight.id, {
role: 'to',
@@ -196,6 +196,21 @@ describe('FlightLivePollService.poll', () => {
expect(notifications.send).not.toHaveBeenCalled();
});
it('FLPOLL-014: a delay is told only to the bookings whose own departure time it is late for', async () => {
const early = await seedFlight();
const later = await seedFlight('Berlin later', '12:30');
feed.lookup.mockResolvedValueOnce(seen(aircraft()));
await poll.poll(at(DEP + 20 * MIN));
const delayed = notifications.send.mock.calls
.map((call) => (call as unknown as [{ event: string; targetId: number; params: { delay: string } }])[0])
.filter((sent) => sent.event === 'flight_delayed');
expect(delayed.map((sent) => [sent.targetId, sent.params.delay])).toEqual([[early.trip.id, '20']]);
expect(notifications.send).toHaveBeenCalledWith(
expect.objectContaining({ event: 'flight_departed', scope: 'trip', targetId: later.trip.id }),
);
});
it('FLPOLL-005: landed is told once and the flight is asked about no more', async () => {
const { trip } = await seedFlight();
feed.lookup.mockResolvedValueOnce(seen(aircraft()));