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

One Add all to calendar file on the clock carries every open date of every open deal, so the agent's own calendar holds the whole book #56

Merged opened this

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

Merged

mason/deal-clock:mason/51-one-add-all-to-calendar-file-on-the-cloc → main 1 commit fork

  • Merged. mason/51-one-add-all-to-calendar-file-on-the-cloc was merged into main as 31b0bd3.

Review

Sign in to review this change.
7 files changed +98 −15
README.md +2 −1
@@ -104,7 +104,8 @@ Signed out, `/` shows what deal-clock does, three short points, a still picture
104104 opens; reopen the Closing date and the deal is back on the clock.
105105 12. **Download the calendar file.** On a deal's page, tap Add to calendar.
106106 You get a calendar file with every date of the deal and an alarm three days
107 before and the morning of, to open in your own calendar app.
107 before and the morning of, to open in your own calendar app. On the clock,
108 Add all to calendar does the same for every open date of every open deal.
108109 13. **Print the client timeline.** On a sample deal's page, tap Client
109110 timeline: its dates in order, who owes each and whether it is done,
110111 upcoming or overdue, with no controls. Print it with your browser's print
src/deals.test.ts +33 −1
@@ -28,7 +28,7 @@ import {
2828 } from "./deals.ts";
2929 import { isClosed } from "./deadlines.ts";
3030 import { openDb } from "./db.ts";
31 import { calendar, fold, icsText } from "./ics.ts";
31 import { book, calendar, fold, icsText } from "./ics.ts";
3232 import { drafts, dueOn, pending, recipients } from "./reminders.ts";
3333 import type { Who } from "./who.ts";
3434
@@ -280,6 +280,38 @@ test("the calendar file: one all-day event per date, alarms, escaped and folded"
280280 expect(fold("x".repeat(80))).toBe(`${"x".repeat(75)}\r\n ${"x".repeat(5)}`);
281281 });
282282
283 test("the book: every open date of every open deal, no done dates, no closed deals, same UIDs", () => {
284 const all = listDeals(db, bob);
285 if (!all.ok) throw new Error(all.message);
286 const deals = all.value.filter((d) => d.author_id === "sample");
287 expect(deals.length).toBe(4);
288 const stamp = new Date("2026-10-12T08:00:00Z");
289 const count = (ics: string) => ics.split("BEGIN:VEVENT").length - 1;
290 const openDates = (list: readonly DealView[]) =>
291 list.filter((d) => !d.closed).flatMap((d) => d.deadlines.filter((x) => x.done_at === null));
292 const before = book(deals, "clock.example", stamp);
293 expect(count(before)).toBe(openDates(deals).length);
294 expect(before.split("BEGIN:VALARM").length - 1).toBe(2 * openDates(deals).length);
295 expect(before).not.toContain("Done: ");
296 const one = deals.find((d) => !d.closed && d.deadlines.some((x) => x.done_at === null))!;
297 const calendarUids = calendar(one, "clock.example", stamp).match(/^UID:.*$/gm)!;
298 for (const uid of one.deadlines.filter((x) => x.done_at === null).map((x) => `UID:deadline-${x.id}@clock.example`))
299 expect(calendarUids).toContain(uid);
300 // A date marked done drops out; a deal whose Closing is done leaves the file.
301 const doneDate = one.deadlines.find((x) => x.done_at === null && x.kind !== "closing")!;
302 markDone(db, bob, { id: doneDate.id, done: true });
303 const closing = deals.find((d) => d.id !== one.id && !d.closed)!;
304 markDone(db, bob, { id: closing.deadlines.find((x) => x.kind === "closing")!.id, done: true });
305 const again = listDeals(db, bob);
306 if (!again.ok) throw new Error(again.message);
307 const after = book(again.value, "clock.example", stamp);
308 expect(after).not.toContain(`UID:deadline-${doneDate.id}@`);
309 for (const d of closing.deadlines) expect(after).not.toContain(`UID:deadline-${d.id}@`);
310 expect(count(after)).toBe(openDates(again.value).length);
311 for (const line of after.split("\r\n")) expect(new TextEncoder().encode(line).length).toBeLessThanOrEqual(75);
312 expect(book([], "clock.example", stamp)).not.toContain("BEGIN:VEVENT");
313 });
314
283315 test("deals are for signed-in agents: a guest sees none, and no contacts", () => {
284316 const deal = open();
285317 addParty(db, alice, { dealId: deal.id, role: "lender", name: "Lee Lender", contact: "[email protected]" });
src/ics.ts +33 −11
@@ -35,17 +35,13 @@ export function fold(line: string): string {
3535
3636 const compact = (date: string): string => date.replaceAll("-", "");
3737
38 export function calendar(deal: DealView, host: string, stamp = new Date()): string {
39 const now = stamp.toISOString().replace(/[-:]/g, "").replace(/\.\d+/, "");
40 const lines = [
41 "BEGIN:VCALENDAR",
42 "VERSION:2.0",
43 "PRODID:-//deal-clock//EN",
44 "CALSCALE:GREGORIAN",
45 "METHOD:PUBLISH",
46 `X-WR-CALNAME:${icsText(deal.address)}`,
47 ];
38 const stampOf = (stamp: Date): string => stamp.toISOString().replace(/[-:]/g, "").replace(/\.\d+/, "");
39
40 /** One deal's events; the UID is the date's own, so a deal's file and the book never double. */
41 function events(deal: DealView, host: string, now: string, openOnly: boolean): string[] {
42 const lines: string[] = [];
4843 for (const d of deal.deadlines) {
44 if (openOnly && d.done_at !== null) continue;
4945 const owner = deal.parties.find((p) => p.role === d.owes);
5046 const who = owner === undefined ? PARTY_LABEL[d.owes] : `${owner.name} (${PARTY_LABEL[d.owes]})`;
5147 lines.push(
@@ -74,6 +70,32 @@ export function calendar(deal: DealView, host: string, stamp = new Date()): stri
7470 );
7571 lines.push("END:VEVENT");
7672 }
77 lines.push("END:VCALENDAR");
73 return lines;
74 }
75
76 function wrap(name: string, body: string[]): string {
77 const lines = [
78 "BEGIN:VCALENDAR",
79 "VERSION:2.0",
80 "PRODID:-//deal-clock//EN",
81 "CALSCALE:GREGORIAN",
82 "METHOD:PUBLISH",
83 `X-WR-CALNAME:${icsText(name)}`,
84 ...body,
85 "END:VCALENDAR",
86 ];
7887 return `${lines.map(fold).join("\r\n")}\r\n`;
7988 }
89
90 export function calendar(deal: DealView, host: string, stamp = new Date()): string {
91 return wrap(deal.address, events(deal, host, stampOf(stamp), false));
92 }
93
94 /** Every open date of every open deal in one file; done dates and closed deals are left out. */
95 export function book(deals: readonly DealView[], host: string, stamp = new Date()): string {
96 const now = stampOf(stamp);
97 return wrap(
98 "Deal clock",
99 deals.filter((deal) => !deal.closed).flatMap((deal) => events(deal, host, now, true)),
100 );
101 }
src/pages/clock.ts +1 −1
@@ -207,7 +207,7 @@ function render(input: {
207207 <h1>The clock</h1>
208208 <p class="summary"><span>${escape(spoken(on))}</span> · <span>${deals.length} ${deals.length === 1 ? "deal" : "deals"}</span>${overdue > 0 ? ` · <b class="u-overdue">${overdue} overdue</b>` : ""}${soon > 0 ? ` · <b class="u-soon">${soon} due within 3 days</b>` : ""}</p>
209209 </div>
210 ${who.role === "anon" ? "" : `<a class="button" href="/deals/new">New deal</a>`}
210 ${who.role === "anon" ? "" : `<span class="actions"><a class="button button-quiet" href="/deals/ics">Add all to calendar</a> <a class="button" href="/deals/new">New deal</a></span>`}
211211 </section>
212212 <form class="find" method="get" action="/" role="search">
213213 <label for="find-q">Find a deal</label>
src/routes.ts +17 −1
@@ -60,7 +60,7 @@ import {
6060 updateDeal,
6161 type NewDeal,
6262 } from "./deals.ts";
63 import { calendar } from "./ics.ts";
63 import { book, calendar } from "./ics.ts";
6464 import { rpc } from "./mcp.ts";
6565 import { FIND_REFUSAL, find, query } from "./find.ts";
6666 import { clock } from "./pages/clock.ts";
@@ -393,6 +393,22 @@ export const routes: readonly Route[] = [
393393 return html(peoplePage({ who: c.who, people: people(deals.value, today()), on: today() }));
394394 },
395395 },
396 {
397 method: "GET",
398 path: "/deals/ics",
399 handle: (c) => {
400 const deals = listDeals(c.db, c.who);
401 if (!deals.ok) return text(deals.message, STATUS[deals.error]);
402 return new Response(book(deals.value, new URL(publicOrigin).hostname), {
403 headers: {
404 "content-type": "text/calendar; charset=utf-8",
405 "content-disposition": 'attachment; filename="deal-clock.ics"',
406 "cache-control": "no-store",
407 ...SECURITY,
408 },
409 });
410 },
411 },
396412 {
397413 method: "GET",
398414 path: "/deals/new",
src/server.test.ts +7
@@ -518,6 +518,7 @@ test("a deal's page, its calendar file, and a 404 for one that is not there", as
518518 expect(guestPage).toContain(`/__forge/signin?to=${encodeURIComponent(`/deals/${deal.id}`)}`);
519519 expect(guestPage).not.toContain(deal.address.replace("&", "&amp;"));
520520 expect((await fetch(`${base}/deals/${deal.id}/ics`)).status).toBe(401);
521 expect((await fetch(`${base}/deals/ics`)).status).toBe(401);
521522 const guestTimeline = await fetch(`${base}/deals/${deal.id}/timeline`);
522523 expect(guestTimeline.status).toBe(401);
523524 expect(await guestTimeline.text()).not.toContain(deal.address.replace("&", "&amp;"));
@@ -547,6 +548,12 @@ test("a deal's page, its calendar file, and a 404 for one that is not there", as
547548 expect(await ics.text()).toContain("BEGIN:VEVENT");
548549 expect((await api(`/deals/999999`, {}, alice)).status).toBe(404);
549550 expect((await api(`/deals/999999/ics`, {}, alice)).status).toBe(404);
551 const all = await api("/deals/ics", {}, alice);
552 expect(all.status).toBe(200);
553 expect(all.headers.get("content-type")).toContain("text/calendar");
554 expect(all.headers.get("x-content-type-options")).toBe("nosniff");
555 expect(await all.text()).toContain("BEGIN:VEVENT");
556 expect(await (await api("/", {}, alice)).text()).toContain('href="/deals/ics"');
550557 const fresh = await (await fetch(`${base}/deals/new`)).text();
551558 expect(fresh).toContain("Sign in");
552559 const form = await (await api("/deals/new", {}, alice)).text();
static/app.css +5
@@ -283,6 +283,11 @@ footer code {
283283 gap: var(--space-3);
284284 margin-bottom: var(--space-4);
285285 }
286 .intro .actions {
287 display: flex;
288 flex-wrap: wrap;
289 gap: var(--space-2);
290 }
286291 .summary {
287292 color: var(--color-muted);
288293 margin-top: var(--space-1);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs