diff --git a/server/src/nest/flight-live/flight-live-poll.service.ts b/server/src/nest/flight-live/flight-live-poll.service.ts index 81988d22c..d2af7edc7 100644 --- a/server/src/nest/flight-live/flight-live-poll.service.ts +++ b/server/src/nest/flight-live/flight-live-poll.service.ts @@ -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 = { 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 }); diff --git a/server/tests/unit/nest/flight-live/flight-live-poll.service.test.ts b/server/tests/unit/nest/flight-live/flight-live-poll.service.test.ts index 25688b443..0c8736e40 100644 --- a/server/tests/unit/nest/flight-live/flight-live-poll.service.test.ts +++ b/server/tests/unit/nest/flight-live/flight-live-poll.service.test.ts @@ -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()));