Public: anyone can read this; the crew can change it.
The Activity page downloads as a spreadsheet of who approved, declined or changed what, so a broker can file the record that a person approved every message #137
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/133-the-activity-page-downloads-as-a-spreads → main 1 commit fork
5 files
changed +137
−4
README.md +3 −1
| @@ -215,7 +215,9 @@ refused, why. **Messages** narrows it to sends, declines and undos, so the | ||
| 215 | 215 | sales meeting can show that every message that went out was approved by a |
| 216 | 216 | named person at a stated time; **Older** pages back. In the terminal press L, |
| 217 | 217 | then m for only messages, o for older and y for newer. A signed-in user who is |
| 218 | not an admin is told no; a stranger is asked to sign in. | |
| 218 | not an admin is told no; a stranger is asked to sign in. **Download | |
| 219 | spreadsheet** saves the same trail (every row, or only messages) as a CSV file | |
| 220 | for a compliance file. | |
| 219 | 221 | |
| 220 | 222 | ## Make it yours |
| 221 | 223 |
src/activitycsv.test.ts added +73
| @@ -0,0 +1,73 @@ | ||
| 1 | /** Activity as a spreadsheet: for whoever may list the audit, guarded, filtered, every row. */ | |
| 2 | import { afterAll, beforeAll, beforeEach, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { today } from "./dates.ts"; | |
| 7 | import { createDeal, sendReminder } from "./deals.ts"; | |
| 8 | import { openDb } from "./db.ts"; | |
| 9 | import { forgetRationing } from "./security.ts"; | |
| 10 | import { serve } from "./server.ts"; | |
| 11 | import { sign, type Who } from "./who.ts"; | |
| 12 | ||
| 13 | const KEY = "a-key-only-the-forge-holds"; | |
| 14 | const dir = mkdtempSync(join(tmpdir(), "app-activitycsv-")); | |
| 15 | const db = openDb(dir); | |
| 16 | const alice: Who = { role: "user", id: "u1", name: "alice" }; | |
| 17 | const server = serve({ port: 0, dataDir: dir, identityKey: KEY }); | |
| 18 | const base = `http://${server.hostname}:${server.port}`; | |
| 19 | afterAll(() => server.stop(true)); | |
| 20 | beforeEach(forgetRationing); | |
| 21 | ||
| 22 | const as = (id: string, name: string, role: "user" | "admin"): Record<string, string> => { | |
| 23 | const identity = `${id}|${name}|${role}|${Date.now() + 600_000}`; | |
| 24 | return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 25 | }; | |
| 26 | const root = as("u0", "root", "admin"); | |
| 27 | const get = (path: string, who: Record<string, string> = {}) => fetch(base + path, { headers: who }); | |
| 28 | ||
| 29 | beforeAll(() => { | |
| 30 | const made = createDeal(db, alice, { openAnyway: true, address: "=1+1 Formula Way", buyer: "Ann", seller: "Sam", contractDate: "2026-10-01", closingDate: "2026-11-06" }); | |
| 31 | if (!made.ok) throw new Error(made.message); | |
| 32 | const inspection = made.value.deadlines.find((d) => d.kind === "inspection")!; | |
| 33 | const party = db.query<{ id: number }, [number]>("SELECT id FROM party WHERE deal_id = ?1 LIMIT 1").get(made.value.id)!; | |
| 34 | expect(sendReminder(db, alice, { deadlineId: inspection.id, partyId: party.id, moment: "before" }).ok).toBe(true); | |
| 35 | }); | |
| 36 | ||
| 37 | test("an admin gets the file, with the header, a guarded address and the wording", async () => { | |
| 38 | const res = await get("/activity.csv", root); | |
| 39 | expect(res.status).toBe(200); | |
| 40 | expect(res.headers.get("content-type")).toBe("text/csv; charset=utf-8"); | |
| 41 | expect(res.headers.get("content-disposition")).toBe(`attachment; filename="deal-clock-activity-${today()}.csv"`); | |
| 42 | const body = await res.text(); | |
| 43 | expect(body.startsWith("when,who,what,deal,outcome\r\n")).toBe(true); | |
| 44 | expect(body).toContain("'=1+1 Formula Way"); | |
| 45 | expect(body).toContain("sent a reminder"); | |
| 46 | expect(body).toContain("added a deal"); | |
| 47 | }); | |
| 48 | ||
| 49 | test("a user is refused, a stranger is asked to sign in", async () => { | |
| 50 | expect((await get("/activity.csv", as("u1", "alice", "user"))).status).toBe(403); | |
| 51 | const stranger = await get("/activity.csv"); | |
| 52 | expect(stranger.status).toBe(401); | |
| 53 | expect(await stranger.text()).toContain("Sign in"); | |
| 54 | }); | |
| 55 | ||
| 56 | test("the Messages filter leaves out other rows, and an unknown filter is a 400", async () => { | |
| 57 | const body = await (await get("/activity.csv?only=messages", root)).text(); | |
| 58 | expect(body).toContain("sent a reminder"); | |
| 59 | expect(body).not.toContain("added a deal"); | |
| 60 | expect((await get("/activity.csv?only=x", root)).status).toBe(400); | |
| 61 | }); | |
| 62 | ||
| 63 | test("the page links the download, keeping the filter, for an admin only", async () => { | |
| 64 | expect(await (await get("/activity", root)).text()).toContain('href="/activity.csv"'); | |
| 65 | expect(await (await get("/activity?only=messages", root)).text()).toContain('href="/activity.csv?only=messages"'); | |
| 66 | }); | |
| 67 | ||
| 68 | test("it covers more rows than one page", async () => { | |
| 69 | const ins = db.query("INSERT INTO audit (at, who_id, who_name, role, resource, verb, target, allowed, outcome, what) VALUES (?1,'u1','alice','user','deal','update','1',1,'ok','date.note')"); | |
| 70 | for (let i = 0; i < 1200; i++) ins.run(new Date().toISOString()); | |
| 71 | const lines = (await (await get("/activity.csv", root)).text()).trim().split("\r\n"); | |
| 72 | expect(lines.length).toBeGreaterThan(1200); | |
| 73 | }); |
src/csv.ts +28
| @@ -4,6 +4,8 @@ | ||
| 4 | 4 | * would read as a formula is prefixed with `'`. Pure text out. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { wording } from "./activity.ts"; | |
| 8 | import type { AuditRow } from "./audit.ts"; | |
| 7 | 9 | import { today } from "./dates.ts"; |
| 8 | 10 | import { PARTY_LABEL } from "./deadlines.ts"; |
| 9 | 11 | import type { DealView } from "./deals.ts"; |
| @@ -39,3 +41,29 @@ export function sheet(deals: readonly DealView[]): string { | ||
| 39 | 41 | ]); |
| 40 | 42 | return `${rows.map((r) => r.map(cell).join(",")).join("\r\n")}\r\n`; |
| 41 | 43 | } |
| 44 | ||
| 45 | export const ACTIVITY_COLUMNS = ["when", "who", "what", "deal", "outcome"] as const; | |
| 46 | ||
| 47 | const pad = (n: number): string => String(n).padStart(2, "0"); | |
| 48 | ||
| 49 | /** "2026-11-08 14:05" in this process's zone; the stored text itself if it does not read. */ | |
| 50 | const localTime = (at: string): string => { | |
| 51 | const d = new Date(at); | |
| 52 | return Number.isNaN(d.getTime()) ? at : `${today(d)} ${pad(d.getHours())}:${pad(d.getMinutes())}`; | |
| 53 | }; | |
| 54 | ||
| 55 | /** The audit rows as a file, header first, in the order given: when, who, what, deal address, outcome. */ | |
| 56 | export function activitySheet(rows: readonly AuditRow[], addresses: ReadonlyMap<string, string>): string { | |
| 57 | const out: string[][] = [[...ACTIVITY_COLUMNS]]; | |
| 58 | for (const r of rows) { | |
| 59 | const named = r.resource === "deal" && r.target !== null; | |
| 60 | out.push([ | |
| 61 | localTime(r.at), | |
| 62 | r.who_name ?? "someone not signed in", | |
| 63 | wording(r), | |
| 64 | named ? (addresses.get(r.target!) ?? "no longer here") : "", | |
| 65 | r.outcome, | |
| 66 | ]); | |
| 67 | } | |
| 68 | return `${out.map((r) => r.map(cell).join(",")).join("\r\n")}\r\n`; | |
| 69 | } |
src/pages/activity.ts +3 −1
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | import type { Database } from "bun:sqlite"; |
| 9 | 9 | import type { AuditRow } from "../audit.ts"; |
| 10 | 10 | import { ACTIVITY_PAGE, addresses, refusal, when, wording } from "../activity.ts"; |
| 11 | import { permit } from "../contract.ts"; | |
| 11 | 12 | import { escape } from "../http.ts"; |
| 12 | 13 | import type { Agent } from "../reminders.ts"; |
| 13 | 14 | import type { Who } from "../who.ts"; |
| @@ -46,6 +47,7 @@ export function activityPage(input: { | ||
| 46 | 47 | const body = `<h1>Activity</h1> |
| 47 | 48 | <p class="summary">Who approved, declined or changed what, newest first. ${filter}</p> |
| 48 | 49 | ${rows.length === 0 ? `<p class="muted">Nothing yet.</p>` : `<ul class="stack">${rows.map(line).join("")}</ul>`} |
| 49 | ${next}`; | |
| 50 | ${next} | |
| 51 | ${permit(who, "audit", "list") === "yes" ? `<p><a class="button button-quiet" href="/activity.csv${only ? "?only=messages" : ""}">Download spreadsheet</a></p>` : ""}`; | |
| 50 | 52 | return layout({ body, who, agent, waiting: input.waiting, path: "/activity", heading: "Activity", live: false }); |
| 51 | 53 | } |
src/routes.ts +30 −2
| @@ -82,11 +82,12 @@ import { | ||
| 82 | 82 | updateDeal, |
| 83 | 83 | type NewDeal, |
| 84 | 84 | } from "./deals.ts"; |
| 85 | import { sheet } from "./csv.ts"; | |
| 85 | import { activitySheet, sheet } from "./csv.ts"; | |
| 86 | 86 | import { book, calendar } from "./ics.ts"; |
| 87 | 87 | import { agentOf, getProfile, setProfile, useDefaults, type Profile } from "./profile.ts"; |
| 88 | 88 | import { profilePage } from "./pages/profile.ts"; |
| 89 | 89 | import { permit } from "./contract.ts"; |
| 90 | import type { AuditRow } from "./audit.ts"; | |
| 90 | 91 | import { rpc } from "./mcp.ts"; |
| 91 | 92 | import { FIND_REFUSAL, find, query } from "./find.ts"; |
| 92 | 93 | import { SIDES, SIDE_REFUSAL, onSide, sideFilter } from "./side.ts"; |
| @@ -99,7 +100,7 @@ import { knownPeople, people } from "./people.ts"; | ||
| 99 | 100 | import { reviewHref, reviewPage, type ReviewError, type ReviewField } from "./pages/review.ts"; |
| 100 | 101 | import { peoplePage } from "./pages/people.ts"; |
| 101 | 102 | import { activityPage } from "./pages/activity.ts"; |
| 102 | import { ACTIVITY_PAGE } from "./activity.ts"; | |
| 103 | import { ACTIVITY_PAGE, addresses } from "./activity.ts"; | |
| 103 | 104 | import { weekPage } from "./pages/week.ts"; |
| 104 | 105 | import { timelinePage } from "./pages/timeline.ts"; |
| 105 | 106 | import { welcomePage } from "./pages/welcome.ts"; |
| @@ -730,6 +731,33 @@ export const routes: readonly Route[] = [ | ||
| 730 | 731 | return html(activityPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), db: c.db, rows: got.value, only: only === "messages" })); |
| 731 | 732 | }, |
| 732 | 733 | }, |
| 734 | { | |
| 735 | method: "GET", | |
| 736 | path: "/activity.csv", | |
| 737 | handle: (c) => { | |
| 738 | const only = c.url.searchParams.get("only"); | |
| 739 | const path = "/activity.csv"; | |
| 740 | if (only !== null && only !== "messages") | |
| 741 | return html(refusedPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), path, heading: "Activity", error: "invalid", message: "The only filter is messages." }), 400); | |
| 742 | // Every row, a page of the largest size at a time; the first call is the permit check the page makes. | |
| 743 | const rows: AuditRow[] = []; | |
| 744 | for (let before: number | undefined; ; ) { | |
| 745 | const got = listAudit(c.db, c.who, { limit: 500, ...(before === undefined ? {} : { before }), ...(only === "messages" ? { only } : {}) }); | |
| 746 | if (!got.ok) return html(refusedPage({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), path, heading: "Activity", error: got.error, message: got.message }), STATUS[got.error]); | |
| 747 | rows.push(...got.value); | |
| 748 | if (got.value.length < 500) break; | |
| 749 | before = got.value[got.value.length - 1]!.id; | |
| 750 | } | |
| 751 | return new Response(activitySheet(rows, addresses(c.db, rows)), { | |
| 752 | headers: { | |
| 753 | "content-type": "text/csv; charset=utf-8", | |
| 754 | "content-disposition": `attachment; filename="deal-clock-activity-${today()}.csv"`, | |
| 755 | "cache-control": "no-store", | |
| 756 | ...SECURITY, | |
| 757 | }, | |
| 758 | }); | |
| 759 | }, | |
| 760 | }, | |
| 733 | 761 | { |
| 734 | 762 | method: "GET", |
| 735 | 763 | path: "/deals/ics", |
new above
offline