Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(calendar): a booking linked to a plan stop gets its zone and is exported once

Maurice8ab7547edf3d
diff --git a/server/src/db/repositories/Reservations.repository.ts b/server/src/db/repositories/Reservations.repository.ts
index 299a7618b..4e6c29c57 100644
--- a/server/src/db/repositories/Reservations.repository.ts
+++ b/server/src/db/repositories/Reservations.repository.ts
@@ -136,8 +136,8 @@ export interface CalendarReservationRow extends ReservationAllColumnsRow {
type CalendarReservationKyselyDB = Pick<DB, 'reservations' | 'places' | 'day_accommodations' | 'days'>;
/**
- * CL4 (`CalendarService.buildTripCalendar`'s assignment read, for every dated
- * day at once) — `SELECT da.*, p.name as place_name, p.address as
+ * CL4 (`CalendarService.buildTripCalendar`'s assignment read, for every day
+ * of the trip at once, undated ones for a linked booking's zone) — `SELECT da.*, p.name as place_name, p.address as
* place_address, p.lat as place_lat, p.lng as place_lng, COALESCE(da.stay_minutes,
* p.duration_minutes) as place_duration_minutes, COALESCE(da.assignment_time, p.place_time) as
* effective_time, COALESCE(da.assignment_end_time, p.end_time) as
diff --git a/server/src/nest/calendar/calendar-timezones.helpers.ts b/server/src/nest/calendar/calendar-timezones.helpers.ts
new file mode 100644
index 000000000..e2f6c0ccf
--- /dev/null
+++ b/server/src/nest/calendar/calendar-timezones.helpers.ts
@@ -0,0 +1,133 @@
+/**
+ * The time-zone half of the trip calendar: which zones an event may name, and
+ * the VTIMEZONE block each named zone gets. Pure functions, kept apart from
+ * CalendarService so the service stays about which events a trip has.
+ */
+import { offsetMinutesAt } from '../common/timezoneService';
+
+// ── ICS time-zone helpers ────────────────────────────────────────────────────
+// Timed events must carry an explicit IANA zone; a bare "YYYYMMDDTHHMMSS" is an
+// RFC 5545 "floating" time that clients render in the *subscriber's* zone (#1453).
+
+// A stored/plugin-provided timezone (e.g. a transport endpoint's `timezone`) is a
+// free string that need not be a real IANA zone. Intl.DateTimeFormat throws a
+// RangeError on an unknown zone, which (via buildVTimezone and offsetMinutesAt)
+// would crash the whole ICS export (and drop the trip from the all-trips feed).
+// Validate once so an invalid zone degrades to a floating local time instead.
+// Module-scoped on purpose (like the permissions/FX caches): the bridge instance
+// and the DI singleton share one cache.
+const _tzValidCache = new Map<string, boolean>();
+export function isValidTimeZone(zone: string): boolean {
+ const cached = _tzValidCache.get(zone);
+ if (cached !== undefined) return cached;
+ let ok = false;
+ try {
+ new Intl.DateTimeFormat('en-US', { timeZone: zone });
+ ok = true;
+ } catch {
+ // Unknown/invalid zone → ok stays false.
+ }
+ // Bound the cache: the key is a free-form (plugin/importer-written) zone string,
+ // so cap distinct entries rather than growing for the process lifetime.
+ if (_tzValidCache.size >= 1000) _tzValidCache.clear();
+ _tzValidCache.set(zone, ok);
+ return ok;
+}
+
+/** An offset in minutes as iCalendar writes it: "+0200", "-0330". */
+function formatOffset(minutes: number): string {
+ const abs = Math.abs(minutes);
+ return `${minutes < 0 ? '-' : '+'}${String(Math.floor(abs / 60)).padStart(2, '0')}${String(abs % 60).padStart(2, '0')}`;
+}
+
+/** Midnight UTC of a YYYYMMDD date, or NaN when it is no date. */
+function utcDay(yyyymmdd: string): number {
+ return Date.parse(`${yyyymmdd.slice(0, 4)}-${yyyymmdd.slice(4, 6)}-${yyyymmdd.slice(6, 8)}T00:00:00Z`);
+}
+
+interface ZoneTransition {
+ /** The instant the new offset starts, ms since the epoch. */
+ at: number;
+ from: number;
+ to: number;
+}
+
+const DAY_MS = 86_400_000;
+
+/** Ten years at most: a span longer than that is a typo, not a trip. */
+const MAX_SCAN_DAYS = 3660;
+
+/** Every offset change in [startMs, endMs), to the minute. */
+function zoneTransitions(zone: string, startMs: number, endMs: number): ZoneTransition[] {
+ const found: ZoneTransition[] = [];
+ const stop = Math.min(endMs, startMs + MAX_SCAN_DAYS * DAY_MS);
+ let before = offsetMinutesAt(zone, startMs);
+ for (let t = startMs; t < stop; t += DAY_MS) {
+ const after = offsetMinutesAt(zone, t + DAY_MS);
+ if (after === before) continue;
+ let lo = t;
+ let hi = t + DAY_MS;
+ while (hi - lo > 60_000) {
+ const mid = lo + Math.floor((hi - lo) / 120_000) * 60_000;
+ if (offsetMinutesAt(zone, mid) === before) lo = mid;
+ else hi = mid;
+ }
+ found.push({ at: hi, from: before, to: after });
+ before = after;
+ }
+ return found;
+}
+
+/** A local wall-clock time as iCalendar writes it, from an instant and the offset in force. */
+function localStamp(ms: number, offsetMinutes: number): string {
+ return new Date(ms + offsetMinutes * 60_000).toISOString().slice(0, 19).replace(/[-:]/g, '');
+}
+
+function observance(kind: 'STANDARD' | 'DAYLIGHT', dtstart: string, from: number, to: number, zone: string): string {
+ return (
+ `BEGIN:${kind}\r\n` +
+ `DTSTART:${dtstart}\r\n` +
+ `TZOFFSETFROM:${formatOffset(from)}\r\n` +
+ `TZOFFSETTO:${formatOffset(to)}\r\n` +
+ `TZNAME:${zone}\r\n` +
+ `END:${kind}\r\n`
+ );
+}
+
+/**
+ * The zone's VTIMEZONE for the dates its events fall on (YYYYMMDD, inclusive).
+ *
+ * Smart clients replace it with their own IANA rules, but a client that reads
+ * it literally shows every event at the offset written here. One fixed offset
+ * put a June flight in Paris an hour off once the trip crossed the change to
+ * summer time, so each change inside the span is written out as its own
+ * observance, with the offset that held before it as the first one. A zone
+ * without changes in the span, or a span with no readable date, keeps the one
+ * fixed offset it always had.
+ */
+export function buildVTimezone(zone: string, firstDay: string, lastDay: string): string {
+ const start = utcDay(firstDay);
+ const end = utcDay(lastDay);
+ let body: string;
+ if (Number.isNaN(start)) {
+ body = observance('STANDARD', '19700101T000000', 0, 0, zone);
+ } else {
+ // Whole calendar years around the events, so a client that expands the
+ // rules a little past the last event still finds the offset in force.
+ const from = Date.UTC(new Date(start).getUTCFullYear(), 0, 1);
+ const to = Date.UTC(new Date(Number.isNaN(end) ? start : Math.max(start, end)).getUTCFullYear() + 1, 0, 1);
+ const changes = zoneTransitions(zone, from, to);
+ const initial = offsetMinutesAt(zone, from);
+ if (changes.length === 0) {
+ const at = offsetMinutesAt(zone, start + DAY_MS / 2);
+ body = observance('STANDARD', '19700101T000000', at, at, zone);
+ } else {
+ const firstIsSummer = changes[0].to < changes[0].from;
+ body = observance(firstIsSummer ? 'DAYLIGHT' : 'STANDARD', '19700101T000000', initial, initial, zone);
+ for (const c of changes) {
+ body += observance(c.to > c.from ? 'DAYLIGHT' : 'STANDARD', localStamp(c.at, c.from), c.from, c.to, zone);
+ }
+ }
+ }
+ return `BEGIN:VTIMEZONE\r\nTZID:${zone}\r\n${body}END:VTIMEZONE\r\n`;
+}
diff --git a/server/src/nest/calendar/calendar.service.ts b/server/src/nest/calendar/calendar.service.ts
index cde71f556..e991e11a2 100644
--- a/server/src/nest/calendar/calendar.service.ts
+++ b/server/src/nest/calendar/calendar.service.ts
@@ -5,13 +5,18 @@ import { Trips } from '../../db/entities/Trips.entity';
import { RESERVATION_METADATA } from '../../db/json-columns';
import type { DayNotesRepository } from '../../db/repositories/DayNotes.repository';
import type { DaysRepository } from '../../db/repositories/Days.repository';
-import type { ReservationsRepository } from '../../db/repositories/Reservations.repository';
+import type {
+ CalendarReservationRow,
+ CalendarStopRow,
+ ReservationsRepository,
+} from '../../db/repositories/Reservations.repository';
import type { TripsRepository } from '../../db/repositories/Trips.repository';
import { decodeJson } from '../../utils/json-column';
import { NotFoundError } from '../common/domain-errors';
-import { offsetMinutesAt, resolveTimeZone } from '../common/timezoneService';
+import { resolveTimeZone } from '../common/timezoneService';
import { addDays } from '../days/days.service';
import { ReservationsService } from '../reservations/reservations.service';
+import { buildVTimezone, isValidTimeZone } from './calendar-timezones.helpers';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
import { isTransportReservationType, type TripCalendarQuery } from '@trek/shared';
@@ -70,55 +75,6 @@ export function foldICS(ics: string): string {
return ics.split('\r\n').map(foldLine).join('\r\n');
}
-// ── ICS time-zone helpers ────────────────────────────────────────────────────
-// Timed events must carry an explicit IANA zone; a bare "YYYYMMDDTHHMMSS" is an
-// RFC 5545 "floating" time that clients render in the *subscriber's* zone (#1453).
-
-// A stored/plugin-provided timezone (e.g. a transport endpoint's `timezone`) is a
-// free string that need not be a real IANA zone. Intl.DateTimeFormat throws a
-// RangeError on an unknown zone, which — via buildVTimezone → offsetMinutesAt —
-// would crash the whole ICS export (and drop the trip from the all-trips feed).
-// Validate once so an invalid zone degrades to a floating local time instead.
-// Module-scoped on purpose (like the permissions/FX caches): the bridge instance
-// and the DI singleton share one cache.
-const _tzValidCache = new Map<string, boolean>();
-function isValidTimeZone(zone: string): boolean {
- const cached = _tzValidCache.get(zone);
- if (cached !== undefined) return cached;
- let ok = false;
- try {
- new Intl.DateTimeFormat('en-US', { timeZone: zone });
- ok = true;
- } catch {
- // Unknown/invalid zone → ok stays false.
- }
- // Bound the cache — the key is a free-form (plugin/importer-written) zone string,
- // so cap distinct entries rather than growing for the process lifetime.
- if (_tzValidCache.size >= 1000) _tzValidCache.clear();
- _tzValidCache.set(zone, ok);
- return ok;
-}
-
-/** An offset in minutes as iCalendar writes it: "+0200", "-0330". */
-function formatOffset(minutes: number): string {
- const abs = Math.abs(minutes);
- return `${minutes < 0 ? '-' : '+'}${String(Math.floor(abs / 60)).padStart(2, '0')}${String(abs % 60).padStart(2, '0')}`;
-}
-
-/** Midnight UTC of a YYYYMMDD date, or NaN when it is no date. */
-function utcDay(yyyymmdd: string): number {
- return Date.parse(`${yyyymmdd.slice(0, 4)}-${yyyymmdd.slice(4, 6)}-${yyyymmdd.slice(6, 8)}T00:00:00Z`);
-}
-
-interface ZoneTransition {
- /** The instant the new offset starts, ms since the epoch. */
- at: number;
- from: number;
- to: number;
-}
-
-const DAY_MS = 86_400_000;
-
/** Rows of several days, by day id; each day keeps the order the rows came in. */
function groupByDay<T extends { day_id: number }>(rows: T[]): Map<number, T[]> {
const byDay = new Map<number, T[]>();
@@ -142,83 +98,18 @@ function plusMinutes(date: string, time: string, minutes: number): { date: strin
};
}
-/** Ten years at most: a span longer than that is a typo, not a trip. */
-const MAX_SCAN_DAYS = 3660;
-
-/** Every offset change in [startMs, endMs), to the minute. */
-function zoneTransitions(zone: string, startMs: number, endMs: number): ZoneTransition[] {
- const found: ZoneTransition[] = [];
- const stop = Math.min(endMs, startMs + MAX_SCAN_DAYS * DAY_MS);
- let before = offsetMinutesAt(zone, startMs);
- for (let t = startMs; t < stop; t += DAY_MS) {
- const after = offsetMinutesAt(zone, t + DAY_MS);
- if (after === before) continue;
- let lo = t;
- let hi = t + DAY_MS;
- while (hi - lo > 60_000) {
- const mid = lo + Math.floor((hi - lo) / 120_000) * 60_000;
- if (offsetMinutesAt(zone, mid) === before) lo = mid;
- else hi = mid;
- }
- found.push({ at: hi, from: before, to: after });
- before = after;
- }
- return found;
-}
-
-/** A local wall-clock time as iCalendar writes it, from an instant and the offset in force. */
-function localStamp(ms: number, offsetMinutes: number): string {
- return new Date(ms + offsetMinutes * 60_000).toISOString().slice(0, 19).replace(/[-:]/g, '');
-}
-
-function observance(kind: 'STANDARD' | 'DAYLIGHT', dtstart: string, from: number, to: number, zone: string): string {
- return (
- `BEGIN:${kind}\r\n` +
- `DTSTART:${dtstart}\r\n` +
- `TZOFFSETFROM:${formatOffset(from)}\r\n` +
- `TZOFFSETTO:${formatOffset(to)}\r\n` +
- `TZNAME:${zone}\r\n` +
- `END:${kind}\r\n`
- );
-}
-
/**
- * The zone's VTIMEZONE for the dates its events fall on (YYYYMMDD, inclusive).
- *
- * Smart clients replace it with their own IANA rules, but a client that reads
- * it literally shows every event at the offset written here. One fixed offset
- * put a June flight in Paris an hour off once the trip crossed the change to
- * summer time, so each change inside the span is written out as its own
- * observance, with the offset that held before it as the first one. A zone
- * without changes in the span, or a span with no readable date, keeps the one
- * fixed offset it always had.
+ * Where a timed stop ends. Without an end the event would have no length, and
+ * clients put their own default (mostly an hour) in its place, so a 20 minute
+ * stop came out as an hour (#2641). The place's duration says how long it lasts.
*/
-function buildVTimezone(zone: string, firstDay: string, lastDay: string): string {
- const start = utcDay(firstDay);
- const end = utcDay(lastDay);
- let body: string;
- if (Number.isNaN(start)) {
- body = observance('STANDARD', '19700101T000000', 0, 0, zone);
- } else {
- // Whole calendar years around the events, so a client that expands the
- // rules a little past the last event still finds the offset in force.
- const from = Date.UTC(new Date(start).getUTCFullYear(), 0, 1);
- const to = Date.UTC(new Date(Number.isNaN(end) ? start : Math.max(start, end)).getUTCFullYear() + 1, 0, 1);
- const changes = zoneTransitions(zone, from, to);
- const initial = offsetMinutesAt(zone, from);
- if (changes.length === 0) {
- const at = offsetMinutesAt(zone, start + DAY_MS / 2);
- body = observance('STANDARD', '19700101T000000', at, at, zone);
- } else {
- const firstIsSummer = changes[0].to < changes[0].from;
- body = observance(firstIsSummer ? 'DAYLIGHT' : 'STANDARD', '19700101T000000', initial, initial, zone);
- for (const c of changes) {
- body += observance(c.to > c.from ? 'DAYLIGHT' : 'STANDARD', localStamp(c.at, c.from), c.from, c.to, zone);
- }
- }
- }
- return `BEGIN:VTIMEZONE\r\nTZID:${zone}\r\n${body}END:VTIMEZONE\r\n`;
+function stopEnd(a: CalendarStopRow, date: string): { date: string; time: string } | null {
+ if (a.effective_end_time) return { date, time: a.effective_end_time };
+ const start = a.effective_time ?? '';
+ const minutes = a.place_duration_minutes;
+ return minutes && minutes > 0 && /^\d{2}:\d{2}/.test(start) ? plusMinutes(date, start, minutes) : null;
}
+
/**
* Everything TREK knows how to say in iCalendar. Moved out of TripsService
* unchanged: same statements, same escaping, same folding, same VTIMEZONE
@@ -297,6 +188,9 @@ export class CalendarService {
// used on, the span the VTIMEZONE has to cover. Populated by dtLine; emitted
// once as VTIMEZONE blocks.
const usedZones = new Map<string, { first: string; last: string }>();
+ // Off while the bookings are only looked at (see startsAtClock), so each zone
+ // is recorded once, in the order the events are emitted.
+ let recordZones = true;
// Emit a DTSTART/DTEND line, attaching TZID when the event's zone is known so
// subscribers see the time in TREK's zone. Falls back to a floating local time
@@ -306,8 +200,8 @@ export class CalendarService {
if (zone && isValidTimeZone(zone) && /^\d{8}T\d{6}$/.test(val)) {
const day = val.slice(0, 8);
const span = usedZones.get(zone);
- if (!span) usedZones.set(zone, { first: day, last: day });
- else {
+ if (recordZones && !span) usedZones.set(zone, { first: day, last: day });
+ else if (recordZones && span) {
if (day < span.first) span.first = day;
if (day > span.last) span.last = day;
}
@@ -329,89 +223,24 @@ export class CalendarService {
events.push(ev);
}
- // Days with assignments and notes, read for all dated days at once.
+ // Days with their stops and notes, each read for all days at once.
const days = await this.daysRepo.listByTrip(trip.id);
const datedDayIds = days.filter((d) => d.date).map((d) => d.id);
// A booked night puts a stop of its own on its check-in day, so the route
// can reach the hotel. That stop is the booking, not a place the traveller
// planned to visit, and the booking already comes through below as the
// stay block or its check-in and check-out markers. Read here as well it
- // would put the hotel on the day a second time.
- const stopsByDay = groupByDay(await this.reservationsRepo.listCalendarStopsForDays(datedDayIds));
+ // would put the hotel on the day a second time. The stops of undated days
+ // are read too, since a booking linked to one takes its zone from it (#3150);
+ // those days are skipped when emitting.
+ const stops = await this.reservationsRepo.listCalendarStopsForDays(days.map((d) => d.id));
+ const stopsByDay = groupByDay(stops);
const notesByDay = groupByDay(await this.dayNotesRepo.listByDayIds(datedDayIds));
- for (const day of days) {
- if (!day.date) continue;
- const assignments = stopsByDay.get(day.id) ?? [];
- const notes = notesByDay.get(day.id) ?? [];
-
- const timed = assignments.filter((a) => a.effective_time);
- const untimed = assignments.filter((a) => !a.effective_time);
-
- // Timed assignments → individual events
- for (const a of timed) {
- const zone = resolveTimeZone(a.place_lat, a.place_lng);
- let ev = `BEGIN:VEVENT\r\nUID:${uid(a.id, 'assign')}\r\nDTSTAMP:${now}\r\n`;
- ev += dtLine('DTSTART', a.effective_time, zone, day.date + 'T00:00');
- if (a.effective_end_time) {
- ev += dtLine('DTEND', a.effective_end_time, zone, day.date + 'T00:00');
- } else if (a.place_duration_minutes && a.place_duration_minutes > 0) {
- // Without an end the event would have no length, and clients put their
- // own default (mostly an hour) in its place, so a 20 minute stop came out
- // as an hour (#2641). The place's duration says how long it lasts.
- const start = a.effective_time ?? '';
- if (/^\d{2}:\d{2}/.test(start)) {
- const stop = plusMinutes(day.date, start, a.place_duration_minutes);
- ev += dtLine('DTEND', stop.time, zone, stop.date + 'T00:00');
- }
- }
- ev += `SUMMARY:${esc(a.place_name)}\r\n`;
- let desc = '';
- if (a.notes) desc += a.notes;
- if (a.place_address) desc += (desc ? '\n' : '') + a.place_address;
- if (desc) ev += `DESCRIPTION:${esc(desc)}\r\n`;
- if (a.place_address) ev += `LOCATION:${esc(a.place_address)}\r\n`;
- ev += `END:VEVENT\r\n`;
- events.push(ev);
- }
-
- // Build all-day summary event if there are untimed activities or notes,
- // unless the subscriber asked for a calendar without them: the summary
- // sits above every day and can crowd out everything else there (#2602).
- if (options.dayEvents !== false && (untimed.length > 0 || notes.length > 0)) {
- const dayTitle = day.title || `Day ${day.day_number}`;
- const endStr = fmtDate(addDays(day.date, 1));
-
- let ev = `BEGIN:VEVENT\r\nUID:${uid(day.id, 'day')}\r\nDTSTAMP:${now}\r\n`;
- ev += `DTSTART;VALUE=DATE:${fmtDate(day.date)}\r\nDTEND;VALUE=DATE:${endStr}\r\n`;
- ev += `SUMMARY:${esc(dayTitle)}\r\n`;
-
- let desc = '';
- if (untimed.length > 0) {
- desc += untimed
- .map((a) => {
- let line = `• ${a.place_name}`;
- if (a.place_address) line += ` (${a.place_address})`;
- if (a.notes) line += ` — ${a.notes}`;
- return line;
- })
- .join('\n');
- }
- if (notes.length > 0) {
- if (desc) desc += '\n\n';
- desc +=
- 'Notes:\n' +
- notes
- .map((n) => {
- const line = n.time ? `${n.time} — ${n.text}` : `• ${n.text}`;
- return line;
- })
- .join('\n');
- }
- if (desc) ev += `DESCRIPTION:${esc(desc)}\r\n`;
- ev += `END:VEVENT\r\n`;
- events.push(ev);
- }
- }
+ const stopById = new Map(stops.map((a) => [a.id, a]));
+ const linkedStop = (r: CalendarReservationRow) =>
+ r.assignment_id == null ? undefined : stopById.get(Number(r.assignment_id));
+ // The bookings whose event stands in for their linked stop, decided below.
+ const stopOfBooking = new Map<number, CalendarStopRow>();
// Transport/flight reservations carry no top-level reservation_time; their
// times live per endpoint (local_date + local_time) in reservation_endpoints.
@@ -434,6 +263,27 @@ export class CalendarService {
return t && isTime(t) ? t : null;
};
+ // The one zone a set of stops agrees on, or null when they name none or several.
+ const soleZone = (list: CalendarStopRow[]): string | null => {
+ const zones = new Set(list.map((a) => resolveTimeZone(a.place_lat, a.place_lng)).filter((z) => z !== null));
+ return zones.size === 1 ? [...zones][0] : null;
+ };
+ const tripZone = soleZone(stops);
+ // A booking's own place gives its zone. One without, a restaurant booked
+ // against a plan stop, takes the stop's instead of floating (#3150). One
+ // with neither takes the zone its day's stops agree on, else the one the
+ // whole trip agrees on. A transport is left out of that guess: its two
+ // ends can sit in different zones, and its endpoints say which.
+ const placeZoneOf = (r: CalendarReservationRow): string | null => {
+ const stop = linkedStop(r);
+ const own =
+ resolveTimeZone(r.place_lat, r.place_lng) ?? (stop ? resolveTimeZone(stop.place_lat, stop.place_lng) : null);
+ if (own || isTransportReservationType(r.type)) return own;
+ const startDate = dateOf(r.reservation_time);
+ const dayId = r.day_id ?? days.find((d) => startDate && d.date === startDate)?.id;
+ return (dayId == null ? null : soleZone(stopsByDay.get(Number(dayId)) ?? [])) ?? tripZone;
+ };
+
// Per stay, the booking whose all-day block its two markers replace. Decided
// once here rather than twice, because the markers have to hand on exactly
// what the block they replaced would have said (#2136).
@@ -515,8 +365,8 @@ export class CalendarService {
const datePart = r.reservation_time.includes('T') ? r.reservation_time.split('T')[0] : r.reservation_time;
if (!isDate(datePart)) return null; // time-only (relative "Day N" trips)
if (r.reservation_time.includes('T')) {
- // Hotels/restaurants: derive the zone from the linked place, if any.
- const zone = resolveTimeZone(r.place_lat, r.place_lng);
+ // Hotels/restaurants: the zone of the booking's place, else its stop's.
+ const zone = placeZoneOf(r);
let out = dtLine('DTSTART', r.reservation_time, zone);
if (r.reservation_end_time) {
const endDt = fmtDateTime(r.reservation_end_time, r.reservation_time);
@@ -604,7 +454,7 @@ export class CalendarService {
? { date: ep.local_date!, time: ep.local_time!, zone: ep.timezone || resolveTimeZone(ep.lat, ep.lng) }
: null;
- const placeZone = resolveTimeZone(r.place_lat, r.place_lng);
+ const placeZone = placeZoneOf(r);
const start =
fromEp(startEp) ??
(() => {
@@ -696,11 +546,116 @@ export class CalendarService {
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;
+ // A booking that replaced its plan stop is where the stop was (#3150), by
+ // its address or, for a place without one, by its name.
+ const stop = stopOfBooking.get(r.id);
+ const stopAddress = stop ? stop.place_address || stop.place_name : null;
+ if ((!ep || !name) && stopAddress) return `LOCATION:${esc(stopAddress)}\r\n`;
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}`;
};
+ // A booking linked to a plan stop and the stop are one appointment (#3150).
+ // Where the booking starts at a clock on the stop's own day, its event wins
+ // and the stop is left out, as a booked night's stop is; the booking then
+ // carries what the stop said and it does not (place, zone, notes, end). A
+ // booking without a clock, or on another day, leaves the stop alone, since
+ // the stop is then the only record of the visit on its day.
+ const clockStartDate = (r: CalendarReservationRow): string | null => {
+ const split = windowSplit.get(r.id);
+ if (split) return split.start.time === null ? null : split.start.date;
+ const legs = legsOf(r);
+ if (legs) return legs[0].dep.date;
+ const lines = buildReservationTimeLines(r);
+ const start = lines ? /^DTSTART(?:;TZID=[^:]+)?:(\d{4})(\d{2})(\d{2})T/.exec(lines) : null;
+ return start ? `${start[1]}-${start[2]}-${start[3]}` : null;
+ };
+ recordZones = false;
+ for (const r of reservations) {
+ const stop = linkedStop(r);
+ const date = stop ? clockStartDate(r) : null;
+ if (stop && date && date === dayDate.get(Number(stop.day_id))) stopOfBooking.set(r.id, stop);
+ }
+ recordZones = true;
+ const replacedStops = new Set([...stopOfBooking.values()].map((a) => a.id));
+
+ // The stop's end for the booking that replaced it and records none, when
+ // both start at the same moment; without it the booking is only a point.
+ const borrowedEnd = (timeLines: string, a: CalendarStopRow): string => {
+ const start = /^DTSTART(?:;TZID=([^:]+))?:(\d{8}T\d{4})/.exec(timeLines);
+ const date = dayDate.get(Number(a.day_id));
+ if (!start || timeLines.includes('DTEND') || !date || !a.effective_time) return '';
+ if (start[2] !== `${fmtDate(date)}T${a.effective_time.slice(0, 5).replace(':', '')}`) return '';
+ const end = stopEnd(a, date);
+ return end ? dtLine('DTEND', end.time, start[1] || null, `${end.date}T00:00`) : '';
+ };
+
+ // Each dated day: its timed stops as events, the rest in one all-day summary.
+ for (const day of days) {
+ if (!day.date) continue;
+ const assignments = (stopsByDay.get(day.id) ?? []).filter((a) => !replacedStops.has(a.id));
+ const notes = notesByDay.get(day.id) ?? [];
+
+ const timed = assignments.filter((a) => a.effective_time);
+ const untimed = assignments.filter((a) => !a.effective_time);
+
+ // Timed assignments → individual events
+ for (const a of timed) {
+ const zone = resolveTimeZone(a.place_lat, a.place_lng);
+ let ev = `BEGIN:VEVENT\r\nUID:${uid(a.id, 'assign')}\r\nDTSTAMP:${now}\r\n`;
+ ev += dtLine('DTSTART', a.effective_time, zone, day.date + 'T00:00');
+ const end = stopEnd(a, day.date);
+ if (end) ev += dtLine('DTEND', end.time, zone, end.date + 'T00:00');
+ ev += `SUMMARY:${esc(a.place_name)}\r\n`;
+ let desc = '';
+ if (a.notes) desc += a.notes;
+ if (a.place_address) desc += (desc ? '\n' : '') + a.place_address;
+ if (desc) ev += `DESCRIPTION:${esc(desc)}\r\n`;
+ if (a.place_address) ev += `LOCATION:${esc(a.place_address)}\r\n`;
+ ev += `END:VEVENT\r\n`;
+ events.push(ev);
+ }
+
+ // Build all-day summary event if there are untimed activities or notes,
+ // unless the subscriber asked for a calendar without them: the summary
+ // sits above every day and can crowd out everything else there (#2602).
+ if (options.dayEvents !== false && (untimed.length > 0 || notes.length > 0)) {
+ const dayTitle = day.title || `Day ${day.day_number}`;
+ const endStr = fmtDate(addDays(day.date, 1));
+
+ let ev = `BEGIN:VEVENT\r\nUID:${uid(day.id, 'day')}\r\nDTSTAMP:${now}\r\n`;
+ ev += `DTSTART;VALUE=DATE:${fmtDate(day.date)}\r\nDTEND;VALUE=DATE:${endStr}\r\n`;
+ ev += `SUMMARY:${esc(dayTitle)}\r\n`;
+
+ let desc = '';
+ if (untimed.length > 0) {
+ desc += untimed
+ .map((a) => {
+ let line = `• ${a.place_name}`;
+ if (a.place_address) line += ` (${a.place_address})`;
+ if (a.notes) line += ` — ${a.notes}`;
+ return line;
+ })
+ .join('\n');
+ }
+ if (notes.length > 0) {
+ if (desc) desc += '\n\n';
+ desc +=
+ 'Notes:\n' +
+ notes
+ .map((n) => {
+ const line = n.time ? `${n.time} — ${n.text}` : `• ${n.text}`;
+ return line;
+ })
+ .join('\n');
+ }
+ if (desc) ev += `DESCRIPTION:${esc(desc)}\r\n`;
+ ev += `END:VEVENT\r\n`;
+ events.push(ev);
+ }
+ }
+
// Reservations as events
for (const r of reservations) {
// The two hand-over events below stand in for this booking entirely.
@@ -732,7 +687,8 @@ export class CalendarService {
if (!timeLines) continue;
let ev = `BEGIN:VEVENT\r\nUID:${uid(r.id, 'res')}\r\nDTSTAMP:${now}\r\n`;
- ev += timeLines;
+ const stop = stopOfBooking.get(r.id);
+ ev += timeLines + (stop ? borrowedEnd(timeLines, stop) : '');
ev += `SUMMARY:${esc(r.title)}\r\n`;
const desc = describeReservation(r);
@@ -780,6 +736,8 @@ export class CalendarService {
}
if (meta.train_number) desc += `\nTrain: ${meta.train_number}`;
if (r.notes) desc += `\n${r.notes}`;
+ const stopNotes = stopOfBooking.get(r.id)?.notes;
+ if (stopNotes && stopNotes !== r.notes) desc += (desc ? '\n' : '') + stopNotes;
return desc;
}
diff --git a/server/tests/e2e/trips.e2e.test.ts b/server/tests/e2e/trips.e2e.test.ts
index 4d29be6d5..051327681 100644
--- a/server/tests/e2e/trips.e2e.test.ts
+++ b/server/tests/e2e/trips.e2e.test.ts
@@ -26,6 +26,7 @@ import { BudgetItemMembers } from '../../src/db/entities/BudgetItemMembers.entit
import { Days } from '../../src/db/entities/Days.entity';
import { JourneyEntries } from '../../src/db/entities/JourneyEntries.entity';
import { Journeys } from '../../src/db/entities/Journeys.entity';
+import { Reservations } from '../../src/db/entities/Reservations.entity';
import { Settings } from '../../src/db/entities/Settings.entity';
import { TripMembers } from '../../src/db/entities/TripMembers.entity';
import { Trips } from '../../src/db/entities/Trips.entity';
@@ -382,6 +383,40 @@ describe('Trips e2e (real auth guard + temp SQLite)', () => {
);
});
+ it('200 export.ics gives a booking linked to a stop the stop zone and exports the lunch once (#3150)', async () => {
+ const tripId = await insertRow(orm, Trips, {
+ user: 1,
+ title: 'London',
+ start_date: '2026-12-09',
+ end_date: '2026-12-09',
+ });
+ const day = await makeDay(orm, tripId, { date: '2026-12-09' });
+ const place = await makePlace(orm, tripId, { name: 'Restaurant X', lat: 51.5074, lng: -0.1278 });
+ const stop = await makeDayAssignment(orm, day.id, place.id, {
+ assignment_time: '14:00',
+ assignment_end_time: '15:30',
+ });
+ await insertRow(orm, Reservations, {
+ trip: tripId,
+ title: 'Restaurant X',
+ type: 'restaurant',
+ assignment: stop.id,
+ reservation_time: '2026-12-09T14:00',
+ reservation_end_time: '2026-12-09T15:30',
+ });
+
+ const res = await request(server).get(`/api/trips/${tripId}/export.ics`).set('Cookie', sessionCookie(1));
+
+ expect(res.status).toBe(200);
+ expect(res.text).toContain(
+ 'DTSTART;TZID=Europe/London:20261209T140000\r\nDTEND;TZID=Europe/London:20261209T153000\r\n',
+ );
+ // The booking used to float (no TZID) and repeat the stop as a second event.
+ expect(res.text).not.toContain('DTSTART:20261209T140000');
+ expect(res.text).not.toContain(`UID:trek-assign-${stop.id}@trek`);
+ expect(res.text.match(/SUMMARY:Restaurant X/g)).toHaveLength(1);
+ });
+
it('200 bundle for an accessible trip (real member list)', async () => {
const tripId = await seedTrip('B');
const res = await request(server).get(`/api/trips/${tripId}/bundle`).set('Cookie', sessionCookie(1));
diff --git a/server/tests/unit/nest/calendar-linked-stops.test.ts b/server/tests/unit/nest/calendar-linked-stops.test.ts
new file mode 100644
index 000000000..f94abacc1
--- /dev/null
+++ b/server/tests/unit/nest/calendar-linked-stops.test.ts
@@ -0,0 +1,226 @@
+/**
+ * A booking linked to a plan stop in the trip calendar (#3150). The booking and
+ * the stop are one appointment: the booking takes its zone from the stop's place
+ * when it has no place of its own, and where the booking has a clock the stop is
+ * not exported a second time. The service comes out of the container over a
+ * snapshot test db and is seeded through the ORM factories.
+ */
+import { CalendarModule } from '../../../src/nest/calendar/calendar.module';
+import { CalendarService } from '../../../src/nest/calendar/calendar.service';
+import { createSnapshotTestDb } from '../../helpers/db-mock';
+import { makeDayAssignment } from '../../helpers/factories/itinerary';
+import { makePlace } from '../../helpers/factories/places';
+import { makeReservation } from '../../helpers/factories/reservations';
+import { makeDay, makeTrip } from '../../helpers/factories/trips';
+import { makeUser } from '../../helpers/factories/users';
+import { createTestModule, type TestModule } from '../../helpers/test-module';
+import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
+
+import { afterAll, beforeAll, describe, expect, it } from 'vitest';
+
+const db = createSnapshotTestDb();
+let t: TestModule;
+let orm: TestOrm;
+
+beforeAll(async () => {
+ t = await createTestModule({ db, imports: [CalendarModule] });
+ orm = await createTestOrm(db);
+});
+
+afterAll(async () => {
+ await orm.close();
+ await t.close();
+ db.close();
+});
+
+const exportIcs = async (tripId: number) => (await t.get(CalendarService).exportICS(tripId)).ics.replace(/\r\n /g, '');
+/** The VEVENT blocks; the VTIMEZONE observances before the first one are left out. */
+const eventsOf = (ics: string) => ics.split('BEGIN:VEVENT').slice(1);
+const lunchEvents = (ics: string) => eventsOf(ics).filter((ev) => ev.includes('SUMMARY:Restaurant X'));
+
+/** A London trip with a lunch stop on the 9th of December, and a booking linked to it. */
+async function lunch(
+ stop: Parameters<typeof makeDayAssignment>[3],
+ booking: Parameters<typeof makeReservation>[2],
+ type = 'restaurant',
+) {
+ const { user } = await makeUser(orm);
+ const trip = await makeTrip(orm, user.id, { title: 'London' });
+ const day = await makeDay(orm, trip.id, { date: '2026-12-09' });
+ const place = await makePlace(orm, trip.id, {
+ name: 'Restaurant X',
+ lat: 51.5074,
+ lng: -0.1278,
+ address: '1 Strand',
+ });
+ const assignment = await makeDayAssignment(orm, day.id, place.id, stop);
+ const reservation = await makeReservation(orm, trip.id, {
+ title: 'Restaurant X',
+ type,
+ assignment: assignment.id,
+ ...booking,
+ });
+ return { ics: await exportIcs(trip.id), assignment, reservation };
+}
+
+describe('bookings linked to a plan stop (#3150)', () => {
+ it('CAL-3150a: the booking takes the zone of its stop and the stop is not exported a second time', async () => {
+ const { ics, assignment, reservation } = await lunch(
+ { assignment_time: '14:00', assignment_end_time: '15:30' },
+ { reservation_time: '2026-12-09T14:00', reservation_end_time: '2026-12-09T15:30' },
+ );
+
+ const booking = eventsOf(ics).find((ev) => ev.includes(`UID:trek-res-${reservation.id}@trek`));
+ expect(booking).toContain('DTSTART;TZID=Europe/London:20261209T140000\r\n');
+ expect(booking).toContain('DTEND;TZID=Europe/London:20261209T153000\r\n');
+ // What the stop event said and the booking lacks moves over with it.
+ expect(booking).toContain('LOCATION:1 Strand\r\n');
+ expect(eventsOf(ics).join('')).not.toMatch(/DTSTART:\d{8}T/);
+ expect(ics).toContain('BEGIN:VTIMEZONE\r\nTZID:Europe/London');
+ expect(ics).not.toContain(`UID:trek-assign-${assignment.id}@trek`);
+ expect(lunchEvents(ics)).toHaveLength(1);
+ });
+
+ it('CAL-3150b: a booking without an end keeps the length and notes of the stop it replaces', async () => {
+ const { ics } = await lunch(
+ { assignment_time: '14:00', assignment_end_time: '15:30', notes: 'Ask for the terrace' },
+ { reservation_time: '2026-12-09T14:00', confirmation_number: 'R-1' },
+ );
+
+ expect(lunchEvents(ics)).toHaveLength(1);
+ const [ev] = lunchEvents(ics);
+ expect(ev).toContain('DTSTART;TZID=Europe/London:20261209T140000\r\nDTEND;TZID=Europe/London:20261209T153000\r\n');
+ expect(ev).toContain('DESCRIPTION:Type: restaurant\\nConfirmation: R-1\\nAsk for the terrace\r\n');
+ });
+
+ it('CAL-3150c: a booking at another time than its stop keeps its own, without borrowing the stop end', async () => {
+ const { ics } = await lunch(
+ { assignment_time: '13:00', assignment_end_time: '14:30' },
+ { reservation_time: '2026-12-09T14:00' },
+ );
+
+ expect(lunchEvents(ics)).toHaveLength(1);
+ const [ev] = lunchEvents(ics);
+ expect(ev).toContain('DTSTART;TZID=Europe/London:20261209T140000\r\n');
+ expect(ev).not.toContain('DTEND');
+ });
+
+ it('CAL-3150d: an untimed stop leaves the day summary once its booking has a clock', async () => {
+ const { ics } = await lunch({}, { reservation_time: '2026-12-09T14:00', reservation_end_time: '15:30' }, 'event');
+
+ expect(ics).not.toContain('• Restaurant X');
+ expect(ics).toContain('DTSTART;TZID=Europe/London:20261209T140000\r\nDTEND;TZID=Europe/London:20261209T153000\r\n');
+ });
+
+ it('CAL-3150e: a booking without a clock leaves its stop as the timed event', async () => {
+ const { ics, assignment } = await lunch({ assignment_time: '14:00' }, { reservation_time: '2026-12-09' });
+
+ expect(ics).toContain(`UID:trek-assign-${assignment.id}@trek`);
+ expect(ics).toContain('DTSTART;TZID=Europe/London:20261209T140000\r\n');
+ expect(ics).toContain('DTSTART;VALUE=DATE:20261209\r\n');
+ });
+
+ it('CAL-3150f: a staged booking does not take the stop out of the calendar', async () => {
+ const { ics, assignment } = await lunch(
+ { assignment_time: '14:00' },
+ { reservation_time: '2026-12-09T14:00', ingest_state: 'staged' },
+ );
+
+ expect(ics).toContain(`UID:trek-assign-${assignment.id}@trek`);
+ expect(lunchEvents(ics)).toHaveLength(1);
+ });
+
+ it('CAL-3150g: a booking with a place of its own keeps that place zone', async () => {
+ const { user } = await makeUser(orm);
+ const trip = await makeTrip(orm, user.id, { title: 'Two Cities' });
+ const day = await makeDay(orm, trip.id, { date: '2026-12-09' });
+ const stopPlace = await makePlace(orm, trip.id, { name: 'Stop', lat: 51.5074, lng: -0.1278 });
+ const ownPlace = await makePlace(orm, trip.id, { name: 'Own', lat: 48.8566, lng: 2.3522 });
+ const assignment = await makeDayAssignment(orm, day.id, stopPlace.id, { assignment_time: '14:00' });
+ await makeReservation(orm, trip.id, {
+ title: 'Show',
+ type: 'event',
+ assignment: assignment.id,
+ place: ownPlace.id,
+ reservation_time: '2026-12-09T20:00',
+ });
+
+ const ics = await exportIcs(trip.id);
+
+ expect(ics).toContain('DTSTART;TZID=Europe/Paris:20261209T200000\r\n');
+ expect(ics).not.toContain(`UID:trek-assign-${assignment.id}@trek`);
+ });
+
+ it('CAL-3150h: a booking on a stop of an undated day still takes the zone of that stop', async () => {
+ const { user } = await makeUser(orm);
+ const trip = await makeTrip(orm, user.id, { title: 'Undated' });
+ const day = await makeDay(orm, trip.id, { date: null });
+ const place = await makePlace(orm, trip.id, { name: 'Tokyo Tower', lat: 35.6586, lng: 139.7454 });
+ const assignment = await makeDayAssignment(orm, day.id, place.id);
+ await makeReservation(orm, trip.id, {
+ title: 'Observation deck',
+ type: 'tour',
+ assignment: assignment.id,
+ reservation_time: '2026-12-09T10:00',
+ });
+
+ const ics = await exportIcs(trip.id);
+
+ expect(ics).toContain('DTSTART;TZID=Asia/Tokyo:20261209T100000\r\n');
+ });
+
+ it('CAL-3150i: a booking on the day after its stop keeps both events', async () => {
+ const { ics, assignment } = await lunch({ assignment_time: '14:00' }, { reservation_time: '2026-12-10T14:00' });
+
+ expect(ics).toContain(`UID:trek-assign-${assignment.id}@trek`);
+ expect(lunchEvents(ics)).toHaveLength(2);
+ expect(ics).toContain('DTSTART;TZID=Europe/London:20261210T140000\r\n');
+ });
+
+ it('CAL-3150j: a stop whose place has no address hands the booking its name as the location', async () => {
+ const { user } = await makeUser(orm);
+ const trip = await makeTrip(orm, user.id, { title: 'London' });
+ const day = await makeDay(orm, trip.id, { date: '2026-12-09' });
+ const place = await makePlace(orm, trip.id, { name: 'The Ivy', lat: 51.5074, lng: -0.1278, address: null });
+ const assignment = await makeDayAssignment(orm, day.id, place.id, { assignment_time: '19:00' });
+ await makeReservation(orm, trip.id, {
+ title: 'Dinner',
+ type: 'restaurant',
+ assignment: assignment.id,
+ reservation_time: '2026-12-09T19:00',
+ });
+
+ const ics = await exportIcs(trip.id);
+
+ expect(ics).not.toContain(`UID:trek-assign-${assignment.id}@trek`);
+ expect(ics).toContain('SUMMARY:Dinner\r\nDESCRIPTION:Type: restaurant\r\nLOCATION:The Ivy\r\n');
+ });
+
+ /** A London trip with one stop on the 9th of December and a booking that names no place and no stop. */
+ async function unlinked(booking: Parameters<typeof makeReservation>[2]) {
+ const { user } = await makeUser(orm);
+ const trip = await makeTrip(orm, user.id, { title: 'London' });
+ const day = await makeDay(orm, trip.id, { date: '2026-12-09' });
+ const place = await makePlace(orm, trip.id, { name: 'Museum', lat: 51.5194, lng: -0.127 });
+ await makeDayAssignment(orm, day.id, place.id);
+ await makeReservation(orm, trip.id, { title: 'Booked', ...booking });
+ return exportIcs(trip.id);
+ }
+
+ it('CAL-3150k: a booking with no place and no stop takes the zone of the stops on its day', async () => {
+ const ics = await unlinked({
+ type: 'restaurant',
+ reservation_time: '2026-12-09T14:00',
+ reservation_end_time: '15:30',
+ });
+
+ expect(ics).toContain('DTSTART;TZID=Europe/London:20261209T140000\r\nDTEND;TZID=Europe/London:20261209T153000\r\n');
+ expect(eventsOf(ics).join('')).not.toMatch(/DTSTART:\d{8}T/);
+ });
+
+ it('CAL-3150l: a transport with no endpoints is not given a zone guessed from the stops', async () => {
+ const ics = await unlinked({ type: 'train', reservation_time: '2026-12-09T14:00' });
+
+ expect(ics).toContain('DTSTART:20261209T140000\r\n');
+ });
+});