217 lines
7.4 KiB
JavaScript
217 lines
7.4 KiB
JavaScript
/* ═══════════════════════════════════════════════════════════════
|
|
HISTORY ROUTE — read-only, mounted under /api
|
|
|
|
GET /history every published timeline entry
|
|
|
|
v_timeline has already done the resolution: an entry with no title
|
|
of its own carries the referenced record's name, an entry with no
|
|
date carries the event's starts_on, and org_kind rides along
|
|
because /regions, /chapters and /partners are three routes and only
|
|
the database knows which a slug is.
|
|
|
|
What's left here is shaping, and three things the view can't do:
|
|
|
|
· rosters. A 'people' entry naming a team resolves through
|
|
v_org_leadership; one with an editorial list reads
|
|
timeline_entry_people. Both are batched, so the number of
|
|
queries doesn't grow with the number of entries.
|
|
|
|
· precision that outruns the date. An entry can hold '2012' with
|
|
precision 'day' — the admin doesn't stop you. Trusting that
|
|
pair would put the entry in a month node built from a month
|
|
that isn't there, so precision is capped at what the string
|
|
actually carries.
|
|
|
|
· entries with no date at all. They can't be placed on a rail, so
|
|
they're dropped rather than crashing the page, and counted so
|
|
the omission is visible rather than silent.
|
|
|
|
Filenames only, as everywhere else in this API. Where the images
|
|
live is the component's business.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
import { Hono } from "hono";
|
|
|
|
const history = new Hono();
|
|
|
|
const CACHE = "public, max-age=60, stale-while-revalidate=300";
|
|
|
|
const json = (c, body) => c.json(body, 200, { "Cache-Control": CACHE });
|
|
|
|
const marks = (n) => Array(n).fill("?").join(",");
|
|
|
|
/* 'YYYY' → year, 'YYYY-MM' → month, 'YYYY-MM-DD' → day. */
|
|
function precisionOfString(date) {
|
|
const parts = String(date).split("-");
|
|
if (parts.length >= 3) return "day";
|
|
if (parts.length === 2) return "month";
|
|
return "year";
|
|
}
|
|
|
|
const RANK = { year: 0, month: 1, day: 2 };
|
|
|
|
/* The stored precision is a claim about how much to trust the date. It
|
|
can't be more precise than the date itself, and a row that claims
|
|
otherwise is a data error the page shouldn't have to survive. */
|
|
function effectivePrecision(stored, date) {
|
|
const actual = precisionOfString(date);
|
|
return RANK[stored] < RANK[actual] ? stored : actual;
|
|
}
|
|
|
|
function shapeEntry(row, rosters) {
|
|
const precision = effectivePrecision(row.precision, row.effective_date);
|
|
|
|
const item = {
|
|
id: String(row.id),
|
|
date: row.effective_date,
|
|
precision,
|
|
kind: row.kind,
|
|
title: row.effective_title,
|
|
featured: row.is_featured === 1,
|
|
};
|
|
|
|
if (row.effective_blurb) item.blurb = row.effective_blurb;
|
|
if (row.meta) item.meta = row.meta;
|
|
|
|
// An explicit link wins on the client too, but sending it only when
|
|
// set keeps "no override" distinguishable from "override to empty".
|
|
if (row.link_url) item.href = row.link_url;
|
|
|
|
if (row.ref_kind && row.ref_id) {
|
|
item.ref = { kind: row.ref_kind, id: row.ref_id };
|
|
// Only organizations need it, and only they have it.
|
|
if (row.org_kind) item.ref.orgKind = row.org_kind;
|
|
}
|
|
|
|
if (row.effective_logo && row.ref_kind) {
|
|
item.logo = { file: row.effective_logo, kind: row.ref_kind };
|
|
}
|
|
|
|
if (row.ref_kind === "team") {
|
|
item.team = {
|
|
id: row.ref_id,
|
|
name: row.team_name,
|
|
orgId: row.team_org_id ?? undefined,
|
|
};
|
|
}
|
|
|
|
const people = rosters.get(row.id);
|
|
if (people?.length) item.people = people;
|
|
|
|
return item;
|
|
}
|
|
|
|
history.get("/history", (c) => {
|
|
const db = c.get("db");
|
|
|
|
const rows = db
|
|
.prepare(
|
|
`SELECT * FROM v_timeline
|
|
WHERE is_published = 1
|
|
-- A standalone milestone reports null here and is unaffected.
|
|
AND (ref_is_published IS NULL OR ref_is_published = 1)
|
|
AND effective_date IS NOT NULL
|
|
ORDER BY effective_date DESC, sort_order, id`,
|
|
)
|
|
.all();
|
|
|
|
// How many entries exist but can't be placed. Worth knowing about —
|
|
// an entry nobody gave a date to is invisible, and silence is how it
|
|
// stays that way.
|
|
const undated = db
|
|
.prepare(
|
|
`SELECT COUNT(*) AS n FROM v_timeline
|
|
WHERE is_published = 1 AND effective_date IS NULL`,
|
|
)
|
|
.get().n;
|
|
|
|
const rosters = loadRosters(db, rows);
|
|
|
|
return json(c, {
|
|
items: rows.map((row) => shapeEntry(row, rosters)),
|
|
undated,
|
|
});
|
|
});
|
|
|
|
/* ── Rosters ───────────────────────────────────────────────────
|
|
Two queries total, whatever the number of entries. A team entry
|
|
reads the team's current public membership; anything else reads
|
|
the entry's own curated list.
|
|
───────────────────────────────────────────────────────────── */
|
|
|
|
function loadRosters(db, rows) {
|
|
const rosters = new Map();
|
|
|
|
const teamEntries = rows.filter(
|
|
(row) => row.kind === "people" && row.ref_kind === "team" && row.ref_id,
|
|
);
|
|
const listEntries = rows.filter(
|
|
(row) => row.kind === "people" && row.ref_kind !== "team",
|
|
);
|
|
|
|
if (teamEntries.length) {
|
|
const teamIds = [...new Set(teamEntries.map((row) => row.ref_id))];
|
|
|
|
// v_org_leadership already decides who counts as current and
|
|
// public — affiliation still open, marked public, person
|
|
// published. Restating those conditions here is how they drift.
|
|
const members = db
|
|
.prepare(
|
|
`SELECT team_id, person_id, display_name, photo, title
|
|
FROM v_org_leadership
|
|
WHERE team_id IN (${marks(teamIds.length)})
|
|
ORDER BY is_owner DESC, sort_order,
|
|
COALESCE(sort_name, display_name)`,
|
|
)
|
|
.all(...teamIds);
|
|
|
|
const byTeam = new Map();
|
|
for (const member of members) {
|
|
const list = byTeam.get(member.team_id) ?? [];
|
|
list.push({
|
|
id: member.person_id,
|
|
name: member.display_name,
|
|
...(member.photo ? { photo: member.photo } : {}),
|
|
...(member.title ? { title: member.title } : {}),
|
|
});
|
|
byTeam.set(member.team_id, list);
|
|
}
|
|
|
|
for (const row of teamEntries) {
|
|
const list = byTeam.get(row.ref_id);
|
|
if (list) rosters.set(row.id, list);
|
|
}
|
|
}
|
|
|
|
if (listEntries.length) {
|
|
const ids = listEntries.map((row) => row.id);
|
|
|
|
const listed = db
|
|
.prepare(
|
|
`SELECT tep.entry_id, tep.person_id, tep.note,
|
|
p.display_name, p.photo
|
|
FROM timeline_entry_people tep
|
|
JOIN people p ON p.id = tep.person_id AND p.is_published = 1
|
|
WHERE tep.entry_id IN (${marks(ids.length)})
|
|
ORDER BY tep.entry_id, tep.sort_order`,
|
|
)
|
|
.all(...ids);
|
|
|
|
for (const person of listed) {
|
|
const list = rosters.get(person.entry_id) ?? [];
|
|
list.push({
|
|
id: person.person_id,
|
|
name: person.display_name,
|
|
...(person.photo ? { photo: person.photo } : {}),
|
|
// The note is the person's standing in this entry, which is
|
|
// what `title` means on the client.
|
|
...(person.note ? { title: person.note } : {}),
|
|
});
|
|
rosters.set(person.entry_id, list);
|
|
}
|
|
}
|
|
|
|
return rosters;
|
|
}
|
|
|
|
export default history;
|