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
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/51-one-add-all-to-calendar-file-on-the-cloc → main 1 commit fork
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 | ||
| 104 | 104 | opens; reopen the Closing date and the deal is back on the clock. |
| 105 | 105 | 12. **Download the calendar file.** On a deal's page, tap Add to calendar. |
| 106 | 106 | 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. | |
| 108 | 109 | 13. **Print the client timeline.** On a sample deal's page, tap Client |
| 109 | 110 | timeline: its dates in order, who owes each and whether it is done, |
| 110 | 111 | upcoming or overdue, with no controls. Print it with your browser's print |
src/deals.test.ts +33 −1
| @@ -28,7 +28,7 @@ import { | ||
| 28 | 28 | } from "./deals.ts"; |
| 29 | 29 | import { isClosed } from "./deadlines.ts"; |
| 30 | 30 | import { openDb } from "./db.ts"; |
| 31 | import { calendar, fold, icsText } from "./ics.ts"; | |
| 31 | import { book, calendar, fold, icsText } from "./ics.ts"; | |
| 32 | 32 | import { drafts, dueOn, pending, recipients } from "./reminders.ts"; |
| 33 | 33 | import type { Who } from "./who.ts"; |
| 34 | 34 | |
| @@ -280,6 +280,38 @@ test("the calendar file: one all-day event per date, alarms, escaped and folded" | ||
| 280 | 280 | expect(fold("x".repeat(80))).toBe(`${"x".repeat(75)}\r\n ${"x".repeat(5)}`); |
| 281 | 281 | }); |
| 282 | 282 | |
| 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 | ||
| 283 | 315 | test("deals are for signed-in agents: a guest sees none, and no contacts", () => { |
| 284 | 316 | const deal = open(); |
| 285 | 317 | 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 { | ||
| 35 | 35 | |
| 36 | 36 | const compact = (date: string): string => date.replaceAll("-", ""); |
| 37 | 37 | |
| 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[] = []; | |
| 48 | 43 | for (const d of deal.deadlines) { |
| 44 | if (openOnly && d.done_at !== null) continue; | |
| 49 | 45 | const owner = deal.parties.find((p) => p.role === d.owes); |
| 50 | 46 | const who = owner === undefined ? PARTY_LABEL[d.owes] : `${owner.name} (${PARTY_LABEL[d.owes]})`; |
| 51 | 47 | lines.push( |
| @@ -74,6 +70,32 @@ export function calendar(deal: DealView, host: string, stamp = new Date()): stri | ||
| 74 | 70 | ); |
| 75 | 71 | lines.push("END:VEVENT"); |
| 76 | 72 | } |
| 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 | ]; | |
| 78 | 87 | return `${lines.map(fold).join("\r\n")}\r\n`; |
| 79 | 88 | } |
| 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: { | ||
| 207 | 207 | <h1>The clock</h1> |
| 208 | 208 | <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> |
| 209 | 209 | </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>`} | |
| 211 | 211 | </section> |
| 212 | 212 | <form class="find" method="get" action="/" role="search"> |
| 213 | 213 | <label for="find-q">Find a deal</label> |
src/routes.ts +17 −1
| @@ -60,7 +60,7 @@ import { | ||
| 60 | 60 | updateDeal, |
| 61 | 61 | type NewDeal, |
| 62 | 62 | } from "./deals.ts"; |
| 63 | import { calendar } from "./ics.ts"; | |
| 63 | import { book, calendar } from "./ics.ts"; | |
| 64 | 64 | import { rpc } from "./mcp.ts"; |
| 65 | 65 | import { FIND_REFUSAL, find, query } from "./find.ts"; |
| 66 | 66 | import { clock } from "./pages/clock.ts"; |
| @@ -393,6 +393,22 @@ export const routes: readonly Route[] = [ | ||
| 393 | 393 | return html(peoplePage({ who: c.who, people: people(deals.value, today()), on: today() })); |
| 394 | 394 | }, |
| 395 | 395 | }, |
| 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 | }, | |
| 396 | 412 | { |
| 397 | 413 | method: "GET", |
| 398 | 414 | 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 | ||
| 518 | 518 | expect(guestPage).toContain(`/__forge/signin?to=${encodeURIComponent(`/deals/${deal.id}`)}`); |
| 519 | 519 | expect(guestPage).not.toContain(deal.address.replace("&", "&")); |
| 520 | 520 | expect((await fetch(`${base}/deals/${deal.id}/ics`)).status).toBe(401); |
| 521 | expect((await fetch(`${base}/deals/ics`)).status).toBe(401); | |
| 521 | 522 | const guestTimeline = await fetch(`${base}/deals/${deal.id}/timeline`); |
| 522 | 523 | expect(guestTimeline.status).toBe(401); |
| 523 | 524 | expect(await guestTimeline.text()).not.toContain(deal.address.replace("&", "&")); |
| @@ -547,6 +548,12 @@ test("a deal's page, its calendar file, and a 404 for one that is not there", as | ||
| 547 | 548 | expect(await ics.text()).toContain("BEGIN:VEVENT"); |
| 548 | 549 | expect((await api(`/deals/999999`, {}, alice)).status).toBe(404); |
| 549 | 550 | 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"'); | |
| 550 | 557 | const fresh = await (await fetch(`${base}/deals/new`)).text(); |
| 551 | 558 | expect(fresh).toContain("Sign in"); |
| 552 | 559 | const form = await (await api("/deals/new", {}, alice)).text(); |
static/app.css +5
| @@ -283,6 +283,11 @@ footer code { | ||
| 283 | 283 | gap: var(); |
| 284 | 284 | margin-bottom: var(); |
| 285 | 285 | } |
| 286 | .intro .actions { | |
| 287 | display: flex; | |
| 288 | flex-wrap: wrap; | |
| 289 | gap: var(); | |
| 290 | } | |
| 286 | 291 | .summary { |
| 287 | 292 | color: var(); |
| 288 | 293 | margin-top: var(); |
new above
offline