Public: anyone can read this; the crew can change it.
The deal's page shows when Owed by was changed on a date: from whom, to whom, by whom and on what day #112
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/108-the-deal-s-page-shows-when-owed-by-was-c → main 1 commit fork
7 files
changed +111
−5
contract/openapi.json +50
| @@ -758,6 +758,31 @@ | ||
| 758 | 758 | ], |
| 759 | 759 | "description": "Who recorded the waiver." |
| 760 | 760 | }, |
| 761 | "owes_changed": { | |
| 762 | "type": [ | |
| 763 | "object", | |
| 764 | "null" | |
| 765 | ], | |
| 766 | "description": "The latest change of Owed by: the role it had (from), who changed it (by) and the day (on). Null when it was never changed.", | |
| 767 | "properties": { | |
| 768 | "from": { | |
| 769 | "enum": [ | |
| 770 | "buyer", | |
| 771 | "seller", | |
| 772 | "lender", | |
| 773 | "title", | |
| 774 | "other_agent" | |
| 775 | ] | |
| 776 | }, | |
| 777 | "by": { | |
| 778 | "type": "string" | |
| 779 | }, | |
| 780 | "on": { | |
| 781 | "type": "string", | |
| 782 | "format": "date" | |
| 783 | } | |
| 784 | } | |
| 785 | }, | |
| 761 | 786 | "moves": { |
| 762 | 787 | "type": "array", |
| 763 | 788 | "items": { |
| @@ -2560,6 +2585,31 @@ | ||
| 2560 | 2585 | ], |
| 2561 | 2586 | "description": "Who recorded the waiver." |
| 2562 | 2587 | }, |
| 2588 | "owes_changed": { | |
| 2589 | "type": [ | |
| 2590 | "object", | |
| 2591 | "null" | |
| 2592 | ], | |
| 2593 | "description": "The latest change of Owed by: the role it had (from), who changed it (by) and the day (on). Null when it was never changed.", | |
| 2594 | "properties": { | |
| 2595 | "from": { | |
| 2596 | "enum": [ | |
| 2597 | "buyer", | |
| 2598 | "seller", | |
| 2599 | "lender", | |
| 2600 | "title", | |
| 2601 | "other_agent" | |
| 2602 | ] | |
| 2603 | }, | |
| 2604 | "by": { | |
| 2605 | "type": "string" | |
| 2606 | }, | |
| 2607 | "on": { | |
| 2608 | "type": "string", | |
| 2609 | "format": "date" | |
| 2610 | } | |
| 2611 | } | |
| 2612 | }, | |
| 2563 | 2613 | "moves": { |
| 2564 | 2614 | "type": "array", |
| 2565 | 2615 | "items": { |
src/db.ts +4
| @@ -374,6 +374,10 @@ export const migrations: string[] = [ | ||
| 374 | 374 | // A date the buyer gave up on purpose: neither done nor gone. Who waived it is kept with the day. |
| 375 | 375 | `ALTER TABLE deadline ADD COLUMN waived_at TEXT`, |
| 376 | 376 | `ALTER TABLE deadline ADD COLUMN waived_by TEXT`, |
| 377 | // The latest change of who owes a date: the role it had, who changed it and the day. Kept with the date, not read from the audit trail. | |
| 378 | `ALTER TABLE deadline ADD COLUMN owes_from TEXT`, | |
| 379 | `ALTER TABLE deadline ADD COLUMN owes_by TEXT`, | |
| 380 | `ALTER TABLE deadline ADD COLUMN owes_on TEXT`, | |
| 377 | 381 | // A reminder the agent holds until a later morning. It is keyed like a |
| 378 | 382 | // sent or declined one, so a moved date's notice is held once, not twice. |
| 379 | 383 | `CREATE TABLE IF NOT EXISTS reminder_hold ( |
src/deals.ts +10 −3
| @@ -136,10 +136,16 @@ export interface Deadline { | ||
| 136 | 136 | /** When the buyer gave this date up on purpose, and who recorded it; null while it stands. */ |
| 137 | 137 | readonly waived_at: string | null; |
| 138 | 138 | readonly waived_by: string | null; |
| 139 | /** The latest change of Owed by, as stored: the role it had, who changed it and the day. Null columns when never changed. */ | |
| 140 | readonly owes_from: PartyRole | null; | |
| 141 | readonly owes_by: string | null; | |
| 142 | readonly owes_on: string | null; | |
| 139 | 143 | /** Days before it is due that its first reminder goes out. */ |
| 140 | 144 | readonly lead_days: number; |
| 141 | 145 | } |
| 142 | export interface DeadlineView extends Deadline { | |
| 146 | export interface DeadlineView extends Omit<Deadline, "owes_from" | "owes_by" | "owes_on"> { | |
| 147 | /** The latest change of who owes this date; null when it was never changed. */ | |
| 148 | readonly owes_changed: { readonly from: PartyRole; readonly by: string; readonly on: string } | null; | |
| 143 | 149 | readonly moves: readonly Move[]; |
| 144 | 150 | /** An open date due on a weekend or US federal holiday: the business day it rolls to. Null otherwise. */ |
| 145 | 151 | readonly nonBusinessDay: { readonly next: string } | null; |
| @@ -242,8 +248,9 @@ function assemble(deal: Deal, rows: Children): DealView { | ||
| 242 | 248 | closed: isClosed(rows.deadlines), |
| 243 | 249 | fell_through: deal.fell_at !== null, |
| 244 | 250 | parties: rows.parties, |
| 245 | deadlines: rows.deadlines.map((d) => ({ | |
| 251 | deadlines: rows.deadlines.map(({ owes_from, owes_by, owes_on, ...d }) => ({ | |
| 246 | 252 | ...d, |
| 253 | owes_changed: owes_from === null || owes_by === null || owes_on === null ? null : { from: owes_from, by: owes_by, on: owes_on }, | |
| 247 | 254 | moves: moves.filter((m) => m.deadline_id === d.id), |
| 248 | 255 | nonBusinessDay: isOpen(d) && !isBusinessDay(d.due) ? { next: nextBusinessDay(d.due) } : null, |
| 249 | 256 | })), |
| @@ -783,7 +790,7 @@ export function setOwes( | ||
| 783 | 790 | return within(db, who, OWES_WHAT, dealId, fail("invalid", `${PARTY_LABEL[input.owes]} already owes it.`, "owes")); |
| 784 | 791 | const told = [input.owes, ...KIND[deadline.kind].notify]; |
| 785 | 792 | db.transaction(() => { |
| 786 | db.query("UPDATE deadline SET owes = ?1 WHERE id = ?2").run(input.owes as string, deadline.id); | |
| 793 | db.query("UPDATE deadline SET owes = ?1, owes_from = ?2, owes_by = ?3, owes_on = ?4 WHERE id = ?5").run(input.owes as string, deadline.owes, authorOf(who).name, today(), deadline.id); | |
| 787 | 794 | for (const h of db.query<{ id: number; moment: string; role: string }, [number]>( |
| 788 | 795 | "SELECT h.id, h.moment, p.role FROM reminder_hold h JOIN party p ON p.id = h.party_id WHERE h.deadline_id = ?1", |
| 789 | 796 | ).all(deadline.id)) |
src/owes.test.ts +33
| @@ -117,3 +117,36 @@ test("the form, the API and the sheet agree, and another origin is refused", asy | ||
| 117 | 117 | const activity = await (await fetch(`${base}/activity`, { headers: { "x-forge-identity": root, "x-forge-signature": sign(root, KEY) } })).text(); |
| 118 | 118 | expect(activity).toContain("changed Owed by from Buyer to Seller"); |
| 119 | 119 | }); |
| 120 | ||
| 121 | test("the deal page and sheet say who handed a date to whom and when; only the latest change shows", async () => { | |
| 122 | const json = { "content-type": "application/json" }; | |
| 123 | const created = await fetch(base + "/api/deals", { | |
| 124 | method: "POST", | |
| 125 | headers: { ...me, ...json }, | |
| 126 | body: JSON.stringify({ address: "7 Trail Ter", open_anyway: true, buyer: "B", seller: "S", contract_date: "2026-10-01", closing_date: "2026-12-20" }), | |
| 127 | }); | |
| 128 | const d = (await created.json()) as DealView; | |
| 129 | const insp = d.deadlines.find((x) => x.kind === "inspection")!; | |
| 130 | expect(insp.owes_changed).toBeNull(); | |
| 131 | const as = (name: string) => { | |
| 132 | const who = `u${name.length}|${name}|user|${Date.now() + 600_000}`; | |
| 133 | return { "x-forge-identity": who, "x-forge-signature": sign(who, KEY), ...json }; | |
| 134 | }; | |
| 135 | const hand = (name: string, owes: string) => | |
| 136 | fetch(`${base}/api/deals/${d.id}/deadlines/${insp.id}`, { method: "PATCH", headers: as(name), body: JSON.stringify({ owes }) }); | |
| 137 | const page = async () => (await fetch(`${base}/deals/${d.id}`, { headers: me })).text(); | |
| 138 | const read = async () => ((await (await fetch(`${base}/api/deals/${d.id}`, { headers: me })).json()) as DealView).deadlines; | |
| 139 | expect((await page()).includes("Owed by changed")).toBe(false); | |
| 140 | expect((await hand("Bob <b>", "seller")).status).toBe(200); | |
| 141 | expect((await read()).find((x) => x.id === insp.id)!.owes_changed).toEqual({ from: "buyer", by: "Bob <b>", on: today() }); | |
| 142 | expect((await hand("alice", "lender")).status).toBe(200); | |
| 143 | const text = await page(); | |
| 144 | expect(text.split("Owed by changed from").length - 1).toBe(2); // the date's row and its sheet | |
| 145 | expect(text).toContain("Owed by changed from Seller to Lender by alice on"); | |
| 146 | expect(text).not.toContain("from Buyer to Seller"); | |
| 147 | await hand("Bob <b>", "buyer"); | |
| 148 | expect(await page()).toContain("from Lender to Buyer by Bob <b> on"); | |
| 149 | const dates = await read(); | |
| 150 | expect(dates.find((x) => x.kind === "title")!.owes_changed).toBeNull(); | |
| 151 | expect(Object.keys(dates[0]!)).not.toContain("owes_from"); | |
| 152 | }); |
src/pages/deal.ts +2 −1
| @@ -16,7 +16,7 @@ import { partyContact } from "./contact.ts"; | ||
| 16 | 16 | import { confirmRemove } from "./confirm.ts"; |
| 17 | 17 | import { layout } from "./layout.ts"; |
| 18 | 18 | import { progressLine } from "./progress.ts"; |
| 19 | import { badge, conflictLines, nonBusinessLine, rollForm, draftItem, dueLine, outboxLine, owedBy, releaseForm, sheet, sheetId } from "./sheet.ts"; | |
| 19 | import { badge, conflictLines, nonBusinessLine, owesChangedLine, rollForm, draftItem, dueLine, outboxLine, owedBy, releaseForm, sheet, sheetId } from "./sheet.ts"; | |
| 20 | 20 | |
| 21 | 21 | const roleOptions = (selected?: string): string => |
| 22 | 22 | PARTY_ROLES.map( |
| @@ -120,6 +120,7 @@ export function dealPage(input: { | ||
| 120 | 120 | <p>${badge(d, on)} <b>${escape(d.label)}</b></p> |
| 121 | 121 | <p class="muted small">${escape(dueLine(d, on))}${d.moves.length > 0 ? ` · moved ${d.moves.length}×` : ""}</p> |
| 122 | 122 | <p class="small">${owedBy(deal, d)}</p> |
| 123 | ${owesChangedLine(d)} | |
| 123 | 124 | ${nonBusinessLine(d)}${may ? rollForm(d, path) : ""} |
| 124 | 125 | </div> |
| 125 | 126 | <button class="button button-quiet" popovertarget="${sheetId(d)}">Open</button> |
src/pages/sheet.ts +7 −1
| @@ -44,6 +44,12 @@ export function dueLine(d: DeadlineView, on: string): string { | ||
| 44 | 44 | return `Due ${spoken(d.due)} · ${relative(d.due, on)}`; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | /** "Owed by changed from Buyer to Seller by Ann on Mon, Nov 2" for a date whose Owed by was changed, else nothing. */ | |
| 48 | export const owesChangedLine = (d: DeadlineView): string => | |
| 49 | d.owes_changed === null | |
| 50 | ? "" | |
| 51 | : `<p class="muted small">${escape(`Owed by changed from ${PARTY_LABEL[d.owes_changed.from]} to ${PARTY_LABEL[d.owes]} by ${d.owes_changed.by} on ${spoken(d.owes_changed.on)}`)}</p>`; | |
| 52 | ||
| 47 | 53 | /** "Sat, Nov 28 · not a business day · next is Mon, Nov 30" for a flagged date, else nothing. */ |
| 48 | 54 | export const nonBusinessLine = (d: DeadlineView): string => |
| 49 | 55 | d.nonBusinessDay === null |
| @@ -263,7 +269,7 @@ export function sheet(deal: DealView, d: DeadlineView, who: Who, on: string, to: | ||
| 263 | 269 | </div> |
| 264 | 270 | <button class="button button-quiet" popovertarget="${id}" popovertargetaction="hide" aria-label="Close">✕</button> |
| 265 | 271 | </header> |
| 266 | <section><h3>Who owes what</h3><p>${owedBy(deal, d)}</p>${d.what === "" ? "" : `<p>${escape(d.what)}</p>`}</section> | |
| 272 | <section><h3>Who owes what</h3><p>${owedBy(deal, d)}</p>${owesChangedLine(d)}${d.what === "" ? "" : `<p>${escape(d.what)}</p>`}</section> | |
| 267 | 273 | ${conflictLines(deal, d)} |
| 268 | 274 | ${nonBusinessLine(d)}${may ? rollForm(d, to) : ""} |
| 269 | 275 | ${moves} |
src/routes.ts +5
| @@ -243,6 +243,11 @@ export const DEAL: Schema = { | ||
| 243 | 243 | done_at: { type: ["string", "null"] }, |
| 244 | 244 | waived_at: { type: ["string", "null"], description: "When the buyer waived this date on purpose; null while it stands. A waived date is settled: no reminders, flags or conflicts." }, |
| 245 | 245 | waived_by: { type: ["string", "null"], description: "Who recorded the waiver." }, |
| 246 | owes_changed: { | |
| 247 | type: ["object", "null"], | |
| 248 | description: "The latest change of Owed by: the role it had (from), who changed it (by) and the day (on). Null when it was never changed.", | |
| 249 | properties: { from: ROLE, by: { type: "string" }, on: DATE }, | |
| 250 | }, | |
| 246 | 251 | moves: { type: "array", items: { type: "object" } }, |
| 247 | 252 | nonBusinessDay: { |
| 248 | 253 | type: ["object", "null"], |
new above
offline