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

Merged opened this

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

Merged

mason/deal-clock:mason/108-the-deal-s-page-shows-when-owed-by-was-c → main 1 commit fork

  • Merged. mason/108-the-deal-s-page-shows-when-owed-by-was-c was merged into main as 49144bd.

Review

Sign in to review this change.
7 files changed +111 −5
contract/openapi.json +50
@@ -758,6 +758,31 @@
758758 ],
759759 "description": "Who recorded the waiver."
760760 },
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 },
761786 "moves": {
762787 "type": "array",
763788 "items": {
@@ -2560,6 +2585,31 @@
25602585 ],
25612586 "description": "Who recorded the waiver."
25622587 },
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 },
25632613 "moves": {
25642614 "type": "array",
25652615 "items": {
src/db.ts +4
@@ -374,6 +374,10 @@ export const migrations: string[] = [
374374 // A date the buyer gave up on purpose: neither done nor gone. Who waived it is kept with the day.
375375 `ALTER TABLE deadline ADD COLUMN waived_at TEXT`,
376376 `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`,
377381 // A reminder the agent holds until a later morning. It is keyed like a
378382 // sent or declined one, so a moved date's notice is held once, not twice.
379383 `CREATE TABLE IF NOT EXISTS reminder_hold (
src/deals.ts +10 −3
@@ -136,10 +136,16 @@ export interface Deadline {
136136 /** When the buyer gave this date up on purpose, and who recorded it; null while it stands. */
137137 readonly waived_at: string | null;
138138 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;
139143 /** Days before it is due that its first reminder goes out. */
140144 readonly lead_days: number;
141145 }
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;
143149 readonly moves: readonly Move[];
144150 /** An open date due on a weekend or US federal holiday: the business day it rolls to. Null otherwise. */
145151 readonly nonBusinessDay: { readonly next: string } | null;
@@ -242,8 +248,9 @@ function assemble(deal: Deal, rows: Children): DealView {
242248 closed: isClosed(rows.deadlines),
243249 fell_through: deal.fell_at !== null,
244250 parties: rows.parties,
245 deadlines: rows.deadlines.map((d) => ({
251 deadlines: rows.deadlines.map(({ owes_from, owes_by, owes_on, ...d }) => ({
246252 ...d,
253 owes_changed: owes_from === null || owes_by === null || owes_on === null ? null : { from: owes_from, by: owes_by, on: owes_on },
247254 moves: moves.filter((m) => m.deadline_id === d.id),
248255 nonBusinessDay: isOpen(d) && !isBusinessDay(d.due) ? { next: nextBusinessDay(d.due) } : null,
249256 })),
@@ -783,7 +790,7 @@ export function setOwes(
783790 return within(db, who, OWES_WHAT, dealId, fail("invalid", `${PARTY_LABEL[input.owes]} already owes it.`, "owes"));
784791 const told = [input.owes, ...KIND[deadline.kind].notify];
785792 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);
787794 for (const h of db.query<{ id: number; moment: string; role: string }, [number]>(
788795 "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",
789796 ).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
117117 const activity = await (await fetch(`${base}/activity`, { headers: { "x-forge-identity": root, "x-forge-signature": sign(root, KEY) } })).text();
118118 expect(activity).toContain("changed Owed by from Buyer to Seller");
119119 });
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 &lt;b&gt; 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";
1616 import { confirmRemove } from "./confirm.ts";
1717 import { layout } from "./layout.ts";
1818 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";
2020
2121 const roleOptions = (selected?: string): string =>
2222 PARTY_ROLES.map(
@@ -120,6 +120,7 @@ export function dealPage(input: {
120120 <p>${badge(d, on)} <b>${escape(d.label)}</b></p>
121121 <p class="muted small">${escape(dueLine(d, on))}${d.moves.length > 0 ? ` · moved ${d.moves.length}×` : ""}</p>
122122 <p class="small">${owedBy(deal, d)}</p>
123 ${owesChangedLine(d)}
123124 ${nonBusinessLine(d)}${may ? rollForm(d, path) : ""}
124125 </div>
125126 <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 {
4444 return `Due ${spoken(d.due)} · ${relative(d.due, on)}`;
4545 }
4646
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
4753 /** "Sat, Nov 28 · not a business day · next is Mon, Nov 30" for a flagged date, else nothing. */
4854 export const nonBusinessLine = (d: DeadlineView): string =>
4955 d.nonBusinessDay === null
@@ -263,7 +269,7 @@ export function sheet(deal: DealView, d: DeadlineView, who: Who, on: string, to:
263269 </div>
264270 <button class="button button-quiet" popovertarget="${id}" popovertargetaction="hide" aria-label="Close">✕</button>
265271 </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>
267273 ${conflictLines(deal, d)}
268274 ${nonBusinessLine(d)}${may ? rollForm(d, to) : ""}
269275 ${moves}
src/routes.ts +5
@@ -243,6 +243,11 @@ export const DEAL: Schema = {
243243 done_at: { type: ["string", "null"] },
244244 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." },
245245 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 },
246251 moves: { type: "array", items: { type: "object" } },
247252 nonBusinessDay: {
248253 type: ["object", "null"],

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs