Public: anyone can read this; the crew can change it.

This week prints as a one-page sheet of every overdue and coming date across all deals, for the agent's Monday office meeting #105

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/deal-clock:mason/93-this-week-prints-as-a-one-page-sheet-of → main 1 commit fork

  • Merged. mason/93-this-week-prints-as-a-one-page-sheet-of was merged into main as 43452f2.

Review

Sign in to review this change.
11 files changed +187 −18
README.md +4 −1
@@ -19,7 +19,10 @@ since they were last set, so the screen always looks like today. Under the
1919 clock, **This week** lists every open date that is overdue or due in the next
2020 seven days, day by day (Overdue, Today, then each day), with the deal's
2121 address, who owes it and how urgent it is; tap a row to open its sheet. It
22 reads well on a phone, with no sideways scrolling. In the terminal, press w
22 reads well on a phone, with no sideways scrolling. **Print this week** beside
23 its heading opens a one-page sheet of the same rows for the Monday office
24 meeting (with Find on, only the matching deals); press Print or your
25 browser's print. In the terminal, press w
2326 for the same list; over MCP, an agent asks for the `this_week` tool.
2427
2528 Before calling a lender or an inspector, open **People** in the header: each
docs/TECHNICAL.md +2
@@ -123,6 +123,8 @@ reads like the deal page: `deal` read through `permit`, 401 signed out, 404 for
123123 no such deal. Over MCP it is `client_timeline`; in the terminal, `t` on an open
124124 deal (`b` goes back). Its print rules in `static/app.css` apply to that page only.
125125
126 `GET /week[?q=]` is This week as a printable sheet (`src/pages/week.ts`; rows and day grouping from `src/week.ts`, shared with the clock's card): `deal` list through `permit`, a sign-in answer signed out, 400 for a bad `q`. It reuses the timeline's print rules.
127
126128 `GET /people` (and `GET /api/people`, described in `openapi.json` under the `deal` list cell) groups every party on the deals the viewer may list into people — parties with the same contact, trimmed and case-insensitive, are one, under the newest name — each with the open dates they owe (`src/people.ts`, pure). Dates of a closed deal are not listed. The terminal's `P` view shows the same.
127129
128130 A deal is **closed** when its Closing date is marked done (derived, no column; `closed` on `GET /api/deals` and `list_deals`). It leaves the clock for the Closed card, sends no reminders (`/tick`, `reminders_today`, the terminal `r` list), and the terminal date list shows its dates only after `h`. Reopening the Closing date reopens the deal.
src/a11y.test.ts +1
@@ -89,6 +89,7 @@ test("every page names its controls and headings, and the layout skips to the co
8989 ["the clock with no match", await get("/?q=zzzznomatch")],
9090 ["a deal", await get(`/deals/${id}`)],
9191 ["the client timeline", await get(`/deals/${id}/timeline`)],
92 ["this week, printed", await get("/week")],
9293 ["new deal", await get("/deals/new")],
9394 ["people", await get("/people")],
9495 ["profile", await get("/profile")],
src/pages/clock.ts +5 −15
@@ -12,7 +12,7 @@ import { escape } from "../http.ts";
1212 import { agent as defaults } from "../env.ts";
1313 import { todaysDrafts, unsent, type Agent } from "../reminders.ts";
1414 import type { Who } from "../who.ts";
15 import { thisWeek } from "../week.ts";
15 import { byDay, thisWeek } from "../week.ts";
1616 import { layout } from "./layout.ts";
1717 import { mayRestore } from "../actions.ts";
1818 import { badge, draftLink, opens, owedBy, URGENCY_LABEL } from "./sheet.ts";
@@ -87,22 +87,11 @@ export function axis(start: string, days: number, on: string): string {
8787 return out.join("");
8888 }
8989
90 /** The day heading a date sits under: overdue ones share one. */
91 const dayHeading = (due: string, on: string): string =>
92 due < on ? "Overdue" : due === on ? "Today" : spoken(due);
93
9490 const weekRows = memoizer<string>(3000);
9591
9692 /** Open dates due within the week, under day headings; each row opens its sheet. */
97 export function weekCard(deals: readonly DealView[], on: string): string {
98 const items = thisWeek(deals, on);
99 const groups: { heading: string; items: typeof items }[] = [];
100 for (const item of items) {
101 const heading = dayHeading(item.deadline.due, on);
102 const group = groups.at(-1);
103 if (group?.heading === heading) group.items.push(item);
104 else groups.push({ heading, items: [item] });
105 }
93 export function weekCard(deals: readonly DealView[], on: string, q = ""): string {
94 const groups = byDay(thisWeek(deals, on), on);
10695 const body =
10796 groups.length === 0
10897 ? `<p class="muted">Nothing due in the next seven days.</p>`
@@ -118,6 +107,7 @@ export function weekCard(deals: readonly DealView[], on: string): string {
118107 .join("");
119108 return `<section id="week" class="card" data-live="deal.changed">
120109 <h2>This week</h2>
110 <p class="small"><a href="/week${q === "" ? "" : `?q=${encodeURIComponent(q)}`}">Print this week</a></p>
121111 ${body}
122112 </section>`;
123113 }
@@ -245,7 +235,7 @@ function render(input: {
245235 </section>
246236 ${closedCard}
247237 ${restoreCard}
248 ${deals.length === 0 ? "" : weekCard(deals, on)}
238 ${deals.length === 0 ? "" : weekCard(deals, on, q)}
249239 ${none ? "" : `<section id="today" class="card" data-live="deal.changed">
250240 <h2>Reminders for today <span class="muted">· ${toSend === 0 ? "all sent" : `${toSend} to send`}</span></h2>
251241 ${toSend === 0 ? "" : `<p><a class="button" href="/reminders/today${q === "" ? "" : `?q=${encodeURIComponent(q)}`}">Review all</a></p>`}
src/pages/week.ts added +52
@@ -0,0 +1,52 @@
1 /**
2 * This week as one printed sheet at `/week`: the rows the clock's This week card
3 * lists (`src/week.ts`), under their day headings, with the deal, who owes each
4 * date and its state — and nothing to press. Its one script, `static/print.js`,
5 * only shows the Print button. Printing rules are the client timeline's.
6 */
7
8 import { urgencyOf, spoken } from "../dates.ts";
9 import type { DealView } from "../deals.ts";
10 import type { Agent } from "../reminders.ts";
11 import { agent as defaults } from "../env.ts";
12 import { asset, escape } from "../http.ts";
13 import { STATUS } from "../timeline.ts";
14 import type { Who } from "../who.ts";
15 import { byDay, owner, thisWeek } from "../week.ts";
16 import { layout } from "./layout.ts";
17
18 export function weekPage(input: { who: Who; agent?: Agent | undefined; deals: readonly DealView[]; on: string; q?: string }): string {
19 const { who, deals, on } = input;
20 const agent = input.agent ?? defaults;
21 const q = input.q ?? "";
22 const back = q === "" ? "/" : `/?q=${encodeURIComponent(q)}`;
23 const groups = byDay(thisWeek(deals, on), on);
24 const days =
25 groups.length === 0
26 ? `<p>Nothing due in the next seven days.</p>`
27 : groups
28 .map(
29 (g) => `<h2 class="week-day">${escape(g.heading)}</h2>
30 <ol class="timeline-list">${g.items
31 .map(({ deal, deadline: d }) => {
32 const { name, role } = owner(deal, d);
33 const s = STATUS[urgencyOf(d, on)];
34 return `<li class="timeline-row" data-u="${urgencyOf(d, on)}">
35 <p class="timeline-date">${escape(deal.address)}</p>
36 <p class="timeline-label"><b>${escape(d.label)}</b></p>
37 <p class="timeline-owner">${escape(role)}${name === undefined ? "" : ` · ${escape(name)}`}</p>
38 <p class="timeline-status"><span aria-hidden="true">${s.mark}</span> ${s.word}</p>
39 </li>`;
40 })
41 .join("")}</ol>`,
42 )
43 .join("");
44 const body = `<p class="crumb timeline-tools"><a href="${escape(back)}">← Back to the clock</a> · <button type="button" class="button button-small" data-print hidden>Print</button> <span class="muted">or your browser's print (Ctrl/Cmd+P)</span></p>
45 <section class="timeline">
46 <h1>This week</h1>
47 <p class="summary">${q === "" ? "Every open deal" : `Deals matching “${escape(q)}”`}, prepared by ${escape(agent.name)}, ${escape(agent.brokerage)}, printed ${escape(spoken(on))}</p>
48 ${days}
49 </section>
50 <script src="${asset("print.js")}"></script>`;
51 return layout({ body, who, agent, path: "/week", heading: "This week", plain: true, bodyClass: "timeline-page" });
52 }
src/routes.ts +20
@@ -90,6 +90,7 @@ import { reviewHref, reviewPage, type ReviewField } from "./pages/review.ts";
9090 import { peoplePage } from "./pages/people.ts";
9191 import { activityPage } from "./pages/activity.ts";
9292 import { ACTIVITY_PAGE } from "./activity.ts";
93 import { weekPage } from "./pages/week.ts";
9394 import { timelinePage } from "./pages/timeline.ts";
9495 import { welcomePage } from "./pages/welcome.ts";
9596 import { dueOn, remindsLine, todaysDrafts, unsent, waitingCount, type Agent, type Draft } from "./reminders.ts";
@@ -574,6 +575,25 @@ export const routes: readonly Route[] = [
574575 return html(clock({ who: c.who, waiting: waitingFor(c), agent: agentFor(c), deals: deals.value, on: today(), notice, open, q }), 200, clear);
575576 },
576577 },
578 {
579 method: "GET",
580 path: "/week",
581 handle: (c) => {
582 const deals = listDeals(c.db, c.who);
583 if (!deals.ok)
584 return html(
585 refusedPage({ who: c.who, agent: agentFor(c), path: "/week", heading: "This week", error: deals.error, message: deals.message }),
586 STATUS[deals.error],
587 );
588 const q = query(c.url.searchParams.get("q"));
589 if (q === undefined)
590 return html(
591 refusedPage({ who: c.who, agent: agentFor(c), path: "/week", heading: "This week", error: "invalid", message: FIND_REFUSAL }),
592 400,
593 );
594 return html(weekPage({ who: c.who, agent: agentFor(c), deals: find(deals.value, q), on: today(), q }));
595 },
596 },
577597 {
578598 method: "GET",
579599 path: "/people",
src/speed.test.ts +1
@@ -110,6 +110,7 @@ const routes: [string, () => string][] = [
110110 ["the deal list", () => "/api/deals"],
111111 ["a deal page", () => `/deals/${firstId}`],
112112 ["the client timeline", () => `/deals/${firstId}/timeline`],
113 ["the printed week", () => "/week"],
113114 ["the reminder review", () => "/reminders/today"],
114115 ];
115116
src/timeline.ts +1 −1
@@ -12,7 +12,7 @@ import { owner } from "./week.ts";
1212 export type TimelineStatus = "Done" | "Waived" | "Upcoming" | "Overdue";
1313
1414 /** Words first, so black and white reads the same as colour; the mark is only a helper. */
15 const STATUS: Readonly<Record<Urgency, { mark: string; word: TimelineStatus }>> = {
15 export const STATUS: Readonly<Record<Urgency, { mark: string; word: TimelineStatus }>> = {
1616 done: { mark: "✓", word: "Done" },
1717 waived: { mark: "–", word: "Waived" },
1818 overdue: { mark: "!", word: "Overdue" },
src/week.page.test.ts added +73
@@ -0,0 +1,73 @@
1 /** The printed week at /week: the This week rows on one sheet, for signed-in users only. */
2 import { afterAll, 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 { addDays } from "./dates.ts";
7 import { forgetRationing } from "./security.ts";
8 import { serve } from "./server.ts";
9 import { sign } from "./who.ts";
10
11 const KEY = "a-key-only-the-forge-holds";
12 const server = serve({ port: 0, dataDir: mkdtempSync(join(tmpdir(), "app-weekpage-")), identityKey: KEY });
13 const base = `http://${server.hostname}:${server.port}`;
14 afterAll(() => server.stop(true));
15 const identity = `u1|alice|user|${Date.now() + 600_000}`;
16 const alice = { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) };
17 const get = (path: string, headers: Record<string, string> = alice) => fetch(base + path, { headers });
18
19 test("lists overdue and coming dates with deal, owner and state, and no controls", async () => {
20 forgetRationing();
21 const res = await get("/week");
22 expect(res.status).toBe(200);
23 const html = await res.text();
24 expect(html).toContain("<h2 class=\"week-day\">Overdue</h2>");
25 expect(html).toContain("timeline-label");
26 expect(html).toContain("Upcoming");
27 expect(html).toContain("Dana Whitfield");
28 expect(html).toContain("data-print");
29 expect(html).not.toContain("<form");
30 expect(html).not.toContain("<input");
31 const days = [...html.matchAll(/class="week-day">([^<]*)</g)].map((m) => m[1]);
32 expect(days[0]).toBe("Overdue");
33 // Same rows as the clock's This week card.
34 const clock = await (await get("/")).text();
35 const card = clock.slice(clock.indexOf('id="week"'));
36 expect(card).toContain('href="/week"');
37 const labels = (h: string) => [...h.matchAll(/<b>([^<]+)<\/b>/g)].map((m) => m[1]);
38 for (const l of labels(html.slice(html.indexOf("timeline-list"))).filter((l) => !days.includes(l))) expect(clock).toContain(l!);
39 });
40
41 test("omits done dates and closed deals: one row per open date in the window", async () => {
42 forgetRationing();
43 const deals = (await (await get("/api/deals")).json()) as { closed?: boolean; deadlines: { due: string; done_at: string | null; waived_at: string | null }[] }[];
44 const last = addDays(new Date().toISOString().slice(0, 10), 7);
45 const expected = deals
46 .filter((d) => d.closed !== true)
47 .flatMap((d) => d.deadlines)
48 .filter((d) => d.done_at === null && d.waived_at === null && d.due <= last).length;
49 const html = await (await get("/week")).text();
50 expect(expected).toBeGreaterThan(0);
51 expect(html.match(/class="timeline-row"/g)?.length).toBe(expected);
52 expect(html).not.toContain('data-u="done"');
53 });
54
55 test("a stranger is asked to sign in", async () => {
56 forgetRationing();
57 const res = await get("/week", {});
58 expect(res.status).not.toBe(200);
59 const html = await res.text();
60 expect(html).toContain("/__forge/signin");
61 expect(html).not.toContain("timeline-row");
62 });
63
64 test("Find narrows the sheet and a bad q is refused", async () => {
65 forgetRationing();
66 const all = await (await get("/week")).text();
67 const some = await (await get("/week?q=harbor")).text();
68 expect(some.match(/timeline-row/g)?.length ?? 0).toBeLessThan(all.match(/timeline-row/g)!.length);
69 expect(some).toContain("matching “harbor”");
70 expect(some).toContain('href="/?q=harbor"');
71 const bad = await get(`/week?q=${"x".repeat(101)}`);
72 expect(bad.status).toBe(400);
73 });
src/week.ts +20 −1
@@ -3,7 +3,7 @@
33 * within the next seven days, oldest first, then by address.
44 */
55
6 import { addDays } from "./dates.ts";
6 import { addDays, spoken } from "./dates.ts";
77 import { isOpen, PARTY_LABEL, type PartyRole } from "./deadlines.ts";
88
99 /** How many days past today still count as this week. */
@@ -44,3 +44,22 @@ export function owner(
4444 ): { name: string | undefined; role: string } {
4545 return { name: deal.parties.find((p) => p.role === d.owes)?.name, role: PARTY_LABEL[d.owes] };
4646 }
47
48 /** The day heading a date sits under: overdue ones share one. */
49 const dayHeading = (due: string, on: string): string =>
50 due < on ? "Overdue" : due === on ? "Today" : spoken(due);
51
52 /** The week's items grouped under their day headings, in the order given. */
53 export function byDay<T extends { deadline: { due: string } }>(
54 items: readonly T[],
55 on: string,
56 ): { heading: string; items: T[] }[] {
57 const groups: { heading: string; items: T[] }[] = [];
58 for (const item of items) {
59 const heading = dayHeading(item.deadline.due, on);
60 const group = groups.at(-1);
61 if (group?.heading === heading) group.items.push(item);
62 else groups.push({ heading, items: [item] });
63 }
64 return groups;
65 }
static/app.css +8
@@ -997,6 +997,14 @@ a.marker {
997997 .timeline-page .timeline-row[data-u="overdue"] .timeline-status {
998998 color: black;
999999 }
1000 .timeline-page .week-day {
1001 break-after: avoid;
1002 margin: var(--space-3) 0 0;
1003 font-size: 1rem;
1004 }
1005 .timeline-page .week-day + .timeline-list .timeline-row {
1006 padding: var(--space-1) 0;
1007 }
10001008 @page {
10011009 margin: 15mm;
10021010 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs