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.

2.3 KB
Raw Blame
import { TRAVEL_RESERVATION_TYPES } from '@trek/shared';

/**
 * The two narrowing options of a public share link (#1712), as pure functions over
 * the snapshot the service has already read, so they can be tested apart from SQL.
 */

/** Getting there and sleeping there: every transport type, and the hotel (the shared catalog's travel set). */
const TRAVEL_BOOKING_TYPES: ReadonlySet<string> = new Set(TRAVEL_RESERVATION_TYPES);

export function isTravelBooking(type: unknown): boolean {
  return typeof type === 'string' && TRAVEL_BOOKING_TYPES.has(type);
}

interface ShareSnapshot {
  assignments: Record<
    number,
    Array<{ accommodation_id?: number | null; place?: { id: number; image_url?: string | null } }>
  >;
  dayNotes: Record<number, unknown[]>;
  days: Array<{ title?: string | null }>;
  places: Array<{ id: number; image_url?: string | null }>;
  reservations: Array<{ type?: unknown }>;
}

/**
 * "Only travel and stays": what family or an emergency contact needs to know,
 * without the museum and the restaurant. A day keeps only the stop its night's
 * booking wrote, the notes go, the place pool shrinks to where the trip sleeps,
 * and the bookings to transport and hotels. Day titles go too ("Louvre and
 * lunch at Marie's" says what the link leaves out); the page then reads "Day N".
 */
export function travelOnly<T extends ShareSnapshot>(data: T, stayPlaceIds: ReadonlySet<number>): T {
  const assignments: T['assignments'] = {};
  for (const [dayId, rows] of Object.entries(data.assignments)) {
    const kept = rows.filter((a) => a.accommodation_id != null);
    if (kept.length) assignments[Number(dayId)] = kept;
  }
  return {
    ...data,
    assignments,
    dayNotes: {},
    days: data.days.map((d) => ({ ...d, title: null })),
    places: data.places.filter((p) => stayPlaceIds.has(p.id)),
    reservations: data.reservations.filter((r) => isTravelBooking(r.type)),
  };
}

/** Without the place photos: a link to pass around without the preview images. */
export function withoutImages<T extends ShareSnapshot>(data: T): T {
  const assignments: T['assignments'] = {};
  for (const [dayId, rows] of Object.entries(data.assignments)) {
    assignments[Number(dayId)] = rows.map((a) => (a.place ? { ...a, place: { ...a.place, image_url: null } } : a));
  }
  return { ...data, assignments, places: data.places.map((p) => ({ ...p, image_url: null })) };
}