mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
fix(calendar): give transport events a LOCATION and GEO from their endpoints
A car or other transport booking carried its route only in the description, so a calendar app had no place to navigate to. Without a location of its own, a transport event now takes the endpoint the traveller has to get to: the destination of a drive of their own (car, bicycle) and the departure of a ride they board, the side the per-leg events already use. A car's pickup and drop-off events are located at their own endpoints. A location the booking states keeps winning. Closes #2909
This commit is contained in:
@@ -14,6 +14,7 @@ import { addDays } from '../days/days.service';
|
||||
import { ReservationsService } from '../reservations/reservations.service';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { isTransportReservationType } from '@trek/shared';
|
||||
|
||||
/** The VCALENDAR preamble every TREK calendar starts with, single-trip or merged. */
|
||||
export const CALENDAR_HEADER =
|
||||
@@ -572,14 +573,18 @@ export class CalendarService {
|
||||
// as the pickup. Positional first/last is only a fallback when no role is
|
||||
// present at all, and only with more than one endpoint, so a lone endpoint is
|
||||
// never both sides (#1721).
|
||||
const windowSidesOf = (r: any): { start: WindowSide | null; end: WindowSide | null } => {
|
||||
const eps = endpointsMap.get(r.id);
|
||||
const endpointOf = (reservationId: number, side: 'from' | 'to') => {
|
||||
const eps = endpointsMap.get(reservationId);
|
||||
const ordered = eps && eps.length > 0 ? [...eps].sort((a, b) => a.sequence - b.sequence) : [];
|
||||
const roleFrom = ordered.find((e) => e.role === 'from');
|
||||
const roleTo = ordered.find((e) => e.role === 'to');
|
||||
const noRoles = !roleFrom && !roleTo;
|
||||
const startEp = roleFrom ?? (noRoles && ordered.length > 1 ? ordered[0] : undefined);
|
||||
const endEp = roleTo ?? (noRoles && ordered.length > 1 ? ordered[ordered.length - 1] : undefined);
|
||||
const byRole = ordered.find((e) => e.role === side);
|
||||
if (byRole) return byRole;
|
||||
const noRoles = !ordered.some((e) => e.role === 'from' || e.role === 'to');
|
||||
if (!noRoles || ordered.length < 2) return undefined;
|
||||
return side === 'from' ? ordered[0] : ordered[ordered.length - 1];
|
||||
};
|
||||
const windowSidesOf = (r: any): { start: WindowSide | null; end: WindowSide | null } => {
|
||||
const startEp = endpointOf(r.id, 'from');
|
||||
const endEp = endpointOf(r.id, 'to');
|
||||
|
||||
const fromEp = (ep: typeof startEp): WindowSide | null =>
|
||||
ep && isDate(ep.local_date) && isTime(ep.local_time)
|
||||
@@ -665,6 +670,24 @@ export class CalendarService {
|
||||
return out;
|
||||
};
|
||||
|
||||
// Where a transport takes the traveller, as a LOCATION (and GEO) a calendar can
|
||||
// navigate to (#2909): the route in the description is no address. The side is
|
||||
// the one the traveller has to get to, the destination of a drive of their own
|
||||
// and the departure of a ride they board, as the per-leg events do. A
|
||||
// location the booking states itself still wins.
|
||||
const SELF_DRIVEN_TYPES = new Set(['car', 'bicycle']);
|
||||
const locationLines = (
|
||||
r: { id: number; type?: string | null; location?: string | null },
|
||||
side: 'from' | 'to' = SELF_DRIVEN_TYPES.has(r.type ?? '') ? 'to' : 'from',
|
||||
): string => {
|
||||
if (r.location) return `LOCATION:${esc(r.location)}\r\n`;
|
||||
const ep = isTransportReservationType(r.type) ? endpointOf(r.id, side) : undefined;
|
||||
const name = ep?.name || ep?.code;
|
||||
if (!ep || !name) return '';
|
||||
const geo = Number.isFinite(ep.lat) && Number.isFinite(ep.lng) ? `GEO:${ep.lat};${ep.lng}\r\n` : '';
|
||||
return `LOCATION:${esc(name)}\r\n${geo}`;
|
||||
};
|
||||
|
||||
// Reservations as events
|
||||
for (const r of reservations) {
|
||||
// The two hand-over events below stand in for this booking entirely.
|
||||
@@ -701,7 +724,7 @@ export class CalendarService {
|
||||
|
||||
const desc = describeReservation(r);
|
||||
if (desc) ev += `DESCRIPTION:${esc(desc)}\r\n`;
|
||||
if (r.location) ev += `LOCATION:${esc(r.location)}\r\n`;
|
||||
ev += locationLines(r);
|
||||
ev += `END:VEVENT\r\n`;
|
||||
events.push(ev);
|
||||
}
|
||||
@@ -859,7 +882,8 @@ export class CalendarService {
|
||||
}
|
||||
ev += `SUMMARY:${esc(summary)}\r\n`;
|
||||
if (desc) ev += `DESCRIPTION:${esc(desc)}\r\n`;
|
||||
if (r.location) ev += `LOCATION:${esc(r.location)}\r\n`;
|
||||
// A hand-over happens where the car is collected or returned.
|
||||
ev += locationLines(r, kind === 'pickup' ? 'from' : 'to');
|
||||
ev += `END:VEVENT\r\n`;
|
||||
events.push(ev);
|
||||
};
|
||||
|
||||
@@ -1513,6 +1513,50 @@ describe('car rentals', () => {
|
||||
expect(ics).not.toContain('Pickup: Dayless');
|
||||
expect(ics).not.toContain('Drop-off: Dayless');
|
||||
});
|
||||
|
||||
// ── Where a transport goes: LOCATION and GEO from its endpoints (#2909) ─────
|
||||
|
||||
const eventOf = (ics: string, summary: string) =>
|
||||
ics.replaceAll('\r\n ', '').split('BEGIN:VEVENT').find((ev) => ev.includes(`SUMMARY:${summary}\r\n`)) ?? '';
|
||||
|
||||
it('CAL-2909a: a drive of its own is located at its destination, each hand-over where it happens', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id, { title: 'Road Trip' });
|
||||
const drive = createReservation(testDb, trip.id, { title: 'Drive', type: 'car' });
|
||||
await updateRows(orm, Reservations, { id: drive.id }, { reservation_time: null, reservation_end_time: null, location: null });
|
||||
await insertEndpoint(drive.id, 'from', 0, '1 Origin Road, Startville', 48.1, 11.5, 'Europe/Berlin', '09:00', '2026-07-07');
|
||||
await insertEndpoint(drive.id, 'to', 1, '123 Example Street, Sampletown', 47.5, 12.25, 'Europe/Berlin', '13:00', '2026-07-07');
|
||||
|
||||
const { ics } = await svc.exportICS(trip.id);
|
||||
|
||||
const event = eventOf(ics, 'Drive');
|
||||
expect(event).toContain('LOCATION:123 Example Street\\, Sampletown\r\n');
|
||||
expect(event).toContain('GEO:47.5;12.25\r\n');
|
||||
// The route stays in the description.
|
||||
expect(event).toContain('Route: 1 Origin Road\\, Startville → 123 Example Street\\, Sampletown');
|
||||
expect(eventOf(ics, 'Pickup: Drive')).toContain('LOCATION:1 Origin Road\\, Startville\r\n');
|
||||
expect(eventOf(ics, 'Drop-off: Drive')).toContain('LOCATION:123 Example Street\\, Sampletown\r\n');
|
||||
});
|
||||
|
||||
it('CAL-2909b: a ride the traveller boards is located where it departs, and a stated location still wins', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id, { title: 'Rail Trip' });
|
||||
const train = createReservation(testDb, trip.id, { title: 'ICE 571', type: 'train' });
|
||||
const ferry = createReservation(testDb, trip.id, { title: 'Ferry', type: 'ferry' });
|
||||
await updateRows(orm, Reservations, { id: train.id }, { reservation_time: null, reservation_end_time: null, location: null });
|
||||
await updateRows(orm, Reservations, { id: ferry.id }, { reservation_time: null, reservation_end_time: null, location: 'Pier 3' });
|
||||
await insertEndpoint(train.id, 'from', 0, 'Mannheim Hbf', 49.4794, 8.4692, 'Europe/Berlin', '08:00', '2026-07-08');
|
||||
await insertEndpoint(train.id, 'to', 1, 'Berlin Hbf', 52.525, 13.369, 'Europe/Berlin', '12:30', '2026-07-08');
|
||||
await insertEndpoint(ferry.id, 'from', 0, 'Mannheim Hbf', 49.4794, 8.4692, 'Europe/Berlin', '08:00', '2026-07-08');
|
||||
await insertEndpoint(ferry.id, 'to', 1, 'Berlin Hbf', 52.525, 13.369, 'Europe/Berlin', '12:30', '2026-07-08');
|
||||
|
||||
const { ics } = await svc.exportICS(trip.id);
|
||||
|
||||
expect(eventOf(ics, 'ICE 571')).toContain('LOCATION:Mannheim Hbf\r\nGEO:49.4794;8.4692\r\n');
|
||||
const ferryEvent = eventOf(ics, 'Ferry');
|
||||
expect(ferryEvent).toContain('LOCATION:Pier 3\r\n');
|
||||
expect(ferryEvent).not.toContain('GEO:');
|
||||
});
|
||||
});
|
||||
|
||||
describe('staged bookings', () => {
|
||||
|
||||
Reference in New Issue
Block a user