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.4 KB
Raw Blame
import { JOURNEY_ENTRY_PROS_CONS, JOURNEY_ENTRY_TAGS } from '../../db/json-columns';
import type { JourneyEntry } from '../../types';
import { decodeJson } from '../../utils/json-column';

/**
 * A journey entry on the way out.
 *
 * `tags` and `pros_cons` are JSON held in TEXT columns, so a row read straight
 * out of SQLite carries them as strings — and `JourneyEntry` types them that
 * way, because it describes the row. What every consumer wants is the decoded
 * shape, and it wants it under the same field names, so the two are one type
 * apart and only this module knows the difference.
 *
 * That distinction is the whole bug this exists for. Three read paths decoded
 * the columns by hand and the create and update paths did not, so an edit
 * answered with `tags` as a string, the store spread it into an entry the
 * client had typed as `string[]`, and the page threw `tags.map is not a
 * function`. Neither compiler could see it: both sides were reading the same
 * field name and only one of them was right.
 *
 * The broadcast is decoded for the same reason rather than because anything
 * breaks on it today — the journey page reloads on any event and ignores the
 * payload — but a payload that contradicts what the read paths answer is a trap
 * for the first listener that decides to trust it.
 *
 * `stats_excluded` is the same gap one column over: an INTEGER holding 0 or 1
 * that the wire carries as a boolean (discussion #2064). It crosses here for
 * the reason the JSON does, so that a client comparing it with `=== true` is
 * comparing against something that can be true on every path.
 */
export interface JourneyEntryWire extends Omit<
  JourneyEntry,
  'tags' | 'pros_cons' | 'stats_excluded' | 'dismissed' | 'is_draft'
> {
  tags: string[];
  pros_cons: { pros: string[]; cons: string[] } | null;
  stats_excluded: boolean;
  dismissed: boolean;
  is_draft: boolean;
}

/** Decode a row's JSON columns and its flags. The one place that knows how they are stored. */
export function decodeEntryRow(row: JourneyEntry): JourneyEntryWire {
  const { tags, pros_cons, stats_excluded, dismissed, is_draft, ...rest } = row;
  return {
    ...rest,
    tags: decodeJson(JOURNEY_ENTRY_TAGS, tags, `journey entry ${row.id}`),
    pros_cons: decodeJson(
      JOURNEY_ENTRY_PROS_CONS,
      pros_cons,
      `journey entry ${row.id}`,
    ) as JourneyEntryWire['pros_cons'],
    stats_excluded: !!stats_excluded,
    dismissed: !!dismissed,
    is_draft: !!is_draft,
  };
}