Public: anyone can read this; the crew can change it.
A date that slips past due without being marked done drafts a follow-up to whoever owes it, which the agent approves like any reminder #35
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/25-a-date-that-slips-past-due-without-being → main 3 commits fork 3 threads · 3 unresolved
14 files
changed +227
−38
README.md +4 −2
| @@ -29,8 +29,10 @@ for the same list; over MCP, an agent asks for the `this_week` tool. | ||
| 29 | 29 | still ahead). |
| 30 | 30 | 2. **Open a marker.** Tap the red inspection marker on Harbor View. A sheet |
| 31 | 31 | opens with the date, who owes it (the buyer), what is owed, and the reminders |
| 32 | for each party, grouped as three days before and the morning of. It also | |
| 33 | has two buttons: Mark done, and Move this date. | |
| 32 | for each party, grouped as three days before, the morning of, and an | |
| 33 | Overdue follow-up to the buyer, because the date passed without being marked | |
| 34 | done (it waits under Reminders for today until you send or decline it). It | |
| 35 | also has two buttons: Mark done, and Move this date. | |
| 34 | 36 | 3. **Read and edit a reminder.** In the sheet, tap a party's reminder. You |
| 35 | 37 | see the words it would send, in a subject field and a message field, |
| 36 | 38 | already filled in. Change the message; the subject can run to 150 |
contract/openapi.json +10 −5
| @@ -528,7 +528,8 @@ | ||
| 528 | 528 | "moment": { |
| 529 | 529 | "enum": [ |
| 530 | 530 | "before", |
| 531 | "day_of" | |
| 531 | "day_of", | |
| 532 | "late" | |
| 532 | 533 | ] |
| 533 | 534 | }, |
| 534 | 535 | "reason": { |
| @@ -621,7 +622,8 @@ | ||
| 621 | 622 | "moment": { |
| 622 | 623 | "enum": [ |
| 623 | 624 | "before", |
| 624 | "day_of" | |
| 625 | "day_of", | |
| 626 | "late" | |
| 625 | 627 | ] |
| 626 | 628 | } |
| 627 | 629 | }, |
| @@ -1068,7 +1070,8 @@ | ||
| 1068 | 1070 | "moment": { |
| 1069 | 1071 | "enum": [ |
| 1070 | 1072 | "before", |
| 1071 | "day_of" | |
| 1073 | "day_of", | |
| 1074 | "late" | |
| 1072 | 1075 | ] |
| 1073 | 1076 | }, |
| 1074 | 1077 | "due": { |
| @@ -1117,7 +1120,8 @@ | ||
| 1117 | 1120 | "moment": { |
| 1118 | 1121 | "enum": [ |
| 1119 | 1122 | "before", |
| 1120 | "day_of" | |
| 1123 | "day_of", | |
| 1124 | "late" | |
| 1121 | 1125 | ] |
| 1122 | 1126 | }, |
| 1123 | 1127 | "due": { |
| @@ -1192,7 +1196,8 @@ | ||
| 1192 | 1196 | "moment": { |
| 1193 | 1197 | "enum": [ |
| 1194 | 1198 | "before", |
| 1195 | "day_of" | |
| 1199 | "day_of", | |
| 1200 | "late" | |
| 1196 | 1201 | ] |
| 1197 | 1202 | }, |
| 1198 | 1203 | "on": { |
src/db.test.ts +23
| @@ -88,3 +88,26 @@ test("a database whose reminder columns came from another migration order still | ||
| 88 | 88 | expect(check(db).version).toBe(migrations.length); |
| 89 | 89 | expect(db.query("SELECT subject, body FROM reminder WHERE sample = 0").all()).toEqual([]); |
| 90 | 90 | }); |
| 91 | ||
| 92 | test("the migration that accepts the late moment keeps the rows of the old schema", () => { | |
| 93 | const dir = mkdtempSync(join(tmpdir(), "app-db-late-")); | |
| 94 | const raw = new Database(join(dir, "app.db")); | |
| 95 | raw.exec("PRAGMA foreign_keys = ON"); | |
| 96 | const last = migrations.length - 1; | |
| 97 | raw.exec("CREATE TABLE schema_version (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')))"); | |
| 98 | migrations.slice(0, last).forEach((sql, i) => { | |
| 99 | raw.exec(sql); | |
| 100 | raw.query("INSERT INTO schema_version (version) VALUES (?1)").run(i + 1); | |
| 101 | }); | |
| 102 | raw.exec(`INSERT INTO deal (id, address, contract_date, author_id, author_name) VALUES (1, 'A St', '2026-01-01', 'u', 'U'); | |
| 103 | INSERT INTO party (id, deal_id, role, name) VALUES (1, 1, 'buyer', 'Ann'); | |
| 104 | INSERT INTO deadline (id, deal_id, kind, label, due, owes) VALUES (1, 1, 'inspection', 'Inspection', '2026-01-10', 'buyer'); | |
| 105 | INSERT INTO reminder (deal_id, deadline_id, party_id, label, party_name, party_role, moment, due, by_name, subject, body) VALUES (1, 1, 1, 'Inspection', 'Ann', 'buyer', 'before', '2026-01-10', 'Dana', 'S', 'B'); | |
| 106 | INSERT INTO reminder_declined (deal_id, deadline_id, party_id, label, party_name, moment, due, reason, by_name) VALUES (1, 1, 1, 'Inspection', 'Ann', 'day_of', '2026-01-10', 'Spoke by phone', 'Dana')`); | |
| 107 | expect(() => raw.exec("INSERT INTO reminder (deal_id, deadline_id, party_id, label, party_name, moment, due, by_name) VALUES (1, 1, 1, 'I', 'Ann', 'late', '2026-01-10', 'D')")).toThrow(); | |
| 108 | migrate(raw); | |
| 109 | expect(raw.query("SELECT moment, subject, body, party_role FROM reminder").all()).toEqual([{ moment: "before", subject: "S", body: "B", party_role: "buyer" }]); | |
| 110 | expect(raw.query("SELECT moment, reason FROM reminder_declined").all()).toEqual([{ moment: "day_of", reason: "Spoke by phone" }]); | |
| 111 | raw.exec("INSERT INTO reminder (deal_id, deadline_id, party_id, label, party_name, moment, due, by_name) VALUES (1, 1, 1, 'I', 'Ann', 'late', '2026-01-10', 'D')"); | |
| 112 | expect(migrate(raw)).toBe(migrations.length); | |
| 113 | }); |
src/db.ts +45
| @@ -153,6 +153,51 @@ export const migrations: string[] = [ | ||
| 153 | 153 | // person's. |
| 154 | 154 | `ALTER TABLE reminder ADD COLUMN sample INTEGER NOT NULL DEFAULT 0; |
| 155 | 155 | UPDATE reminder SET sample = 1 WHERE by_name = 'Sample data' AND deal_id IN (SELECT id FROM deal WHERE author_id = 'sample')`, |
| 156 | // An overdue follow-up is a third moment, `late`. SQLite cannot alter a | |
| 157 | // CHECK, so both tables are rebuilt with their rows kept; the old tables | |
| 158 | // are named only inside this one migration, and schema_version means it runs | |
| 159 | // once. | |
| 160 | `CREATE TABLE reminder_new ( | |
| 161 | id INTEGER PRIMARY KEY, | |
| 162 | deal_id INTEGER NOT NULL REFERENCES deal (id) ON DELETE CASCADE, | |
| 163 | deadline_id INTEGER NOT NULL, | |
| 164 | party_id INTEGER NOT NULL, | |
| 165 | label TEXT NOT NULL, | |
| 166 | party_name TEXT NOT NULL, | |
| 167 | moment TEXT NOT NULL CHECK (moment IN ('before', 'day_of', 'late')), | |
| 168 | due TEXT NOT NULL, | |
| 169 | by_name TEXT NOT NULL, | |
| 170 | subject TEXT NOT NULL DEFAULT '', | |
| 171 | body TEXT NOT NULL DEFAULT '', | |
| 172 | sent_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')), | |
| 173 | party_role TEXT NOT NULL DEFAULT '', | |
| 174 | sample INTEGER NOT NULL DEFAULT 0, | |
| 175 | UNIQUE (deadline_id, party_id, moment, due) | |
| 176 | ); | |
| 177 | INSERT INTO reminder_new (id, deal_id, deadline_id, party_id, label, party_name, moment, due, by_name, subject, body, sent_at, party_role, sample) | |
| 178 | SELECT id, deal_id, deadline_id, party_id, label, party_name, moment, due, by_name, subject, body, sent_at, party_role, sample FROM reminder; | |
| 179 | DROP TABLE reminder; | |
| 180 | ALTER TABLE reminder_new RENAME TO reminder; | |
| 181 | CREATE INDEX reminder_deal ON reminder (deal_id); | |
| 182 | CREATE TABLE reminder_declined_new ( | |
| 183 | id INTEGER PRIMARY KEY, | |
| 184 | deal_id INTEGER NOT NULL REFERENCES deal (id) ON DELETE CASCADE, | |
| 185 | deadline_id INTEGER NOT NULL, | |
| 186 | party_id INTEGER NOT NULL, | |
| 187 | label TEXT NOT NULL, | |
| 188 | party_name TEXT NOT NULL, | |
| 189 | moment TEXT NOT NULL CHECK (moment IN ('before', 'day_of', 'late')), | |
| 190 | due TEXT NOT NULL, | |
| 191 | reason TEXT NOT NULL CHECK (length(reason) BETWEEN 1 AND 200), | |
| 192 | by_name TEXT NOT NULL, | |
| 193 | at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')), | |
| 194 | UNIQUE (deadline_id, party_id, moment, due) | |
| 195 | ); | |
| 196 | INSERT INTO reminder_declined_new (id, deal_id, deadline_id, party_id, label, party_name, moment, due, reason, by_name, at) | |
| 197 | SELECT id, deal_id, deadline_id, party_id, label, party_name, moment, due, reason, by_name, at FROM reminder_declined; | |
| 198 | DROP TABLE reminder_declined; | |
| 199 | ALTER TABLE reminder_declined_new RENAME TO reminder_declined; | |
| 200 | CREATE INDEX reminder_declined_deal ON reminder_declined (deal_id)`, | |
| 156 | 201 | ]; |
| 157 | 202 | |
| 158 | 203 | export function openDb(dir = dataDir): Database { |
src/deals.test.ts +54 −1
| @@ -28,7 +28,7 @@ import { | ||
| 28 | 28 | import { isClosed } from "./deadlines.ts"; |
| 29 | 29 | import { openDb } from "./db.ts"; |
| 30 | 30 | import { calendar, fold, icsText } from "./ics.ts"; |
| 31 | import { drafts, dueOn, recipients } from "./reminders.ts"; | |
| 31 | import { drafts, dueOn, pending, recipients } from "./reminders.ts"; | |
| 32 | 32 | import type { Who } from "./who.ts"; |
| 33 | 33 | |
| 34 | 34 | const db = openDb(mkdtempSync(join(tmpdir(), "app-deals-"))); |
| @@ -199,6 +199,7 @@ test("reminders: three days before and the morning of, drafted in words, marked | ||
| 199 | 199 | ["Sam Seller", "before", today()], |
| 200 | 200 | ["Ann Buyer", "day_of", inspection.due], |
| 201 | 201 | ["Sam Seller", "day_of", inspection.due], |
| 202 | ["Ann Buyer", "late", addDays(inspection.due, 1)], | |
| 202 | 203 | ]); |
| 203 | 204 | const buyer = mine[0]!; |
| 204 | 205 | expect(buyer.body).toContain("Hi Ann,"); |
| @@ -438,3 +439,55 @@ test("a deal is closed when its Closing date is marked done, and open again when | ||
| 438 | 439 | expect(isClosed([{ kind: "closing", done_at: "2026-11-02T10:00:00.000Z" }])).toBe(true); |
| 439 | 440 | expect(isClosed([{ kind: "closing", done_at: null }, { kind: "custom", done_at: "x" }])).toBe(false); |
| 440 | 441 | }); |
| 442 | ||
| 443 | test("an overdue follow-up: the morning after, to whoever owes the date alone, until it is done or moved", () => { | |
| 444 | const deal = open({ contractDate: addDays(today(), -10), closingDate: addDays(today(), 30), lengths: { inspection: "8" } }); | |
| 445 | const inspection = due(deal, "inspection"); // due two days ago | |
| 446 | const lates = (d: DealView) => drafts(d, agent).filter((r) => r.deadline_id === inspection.id && r.moment === "late"); | |
| 447 | expect(lates(deal).map((r) => [r.party_name, r.on, r.sent_at])).toEqual([["Ann Buyer", addDays(inspection.due, 1), null]]); | |
| 448 | const draft = lates(deal)[0]!; | |
| 449 | expect(draft.body).toContain("was due"); | |
| 450 | expect(draft.body).toContain("update"); | |
| 451 | expect(draft.body).toContain("Dana Whitfield, Harbor & Pine Realty"); | |
| 452 | expect(dueOn([deal], addDays(inspection.due, 1), agent).filter((r) => r.moment === "late").length).toBe(1); | |
| 453 | ||
| 454 | const ask = { deadlineId: inspection.id, partyId: draft.party_id, moment: "late" }; | |
| 455 | expect(sendReminder(db, alice, { ...ask, subject: "Where is it?", body: "My own words" }).ok).toBe(true); | |
| 456 | const kept = lates(fresh(deal.id))[0]!; | |
| 457 | expect(kept.sent_at).not.toBeNull(); | |
| 458 | expect(kept.body).toBe("My own words"); | |
| 459 | expect(db.query("SELECT moment FROM reminder WHERE deadline_id = ?1").all(inspection.id)).toEqual([{ moment: "late" }]); | |
| 460 | ||
| 461 | // Moving the date makes a fresh follow-up for the new date; a done date drafts none. | |
| 462 | const to = addDays(today(), -1); | |
| 463 | expect(moveDeadline(db, alice, { id: inspection.id, due: to, reason: "Needs more time" }).ok).toBe(true); | |
| 464 | const moved = lates(fresh(deal.id)); | |
| 465 | expect(moved.map((r) => [r.due, r.sent_at])).toEqual([[to, null]]); | |
| 466 | expect(declineReminder(db, alice, { ...ask, reason: "Called them" }).ok).toBe(true); | |
| 467 | expect(lates(fresh(deal.id))[0]!.declined?.reason).toBe("Called them"); | |
| 468 | expect(markDone(db, alice, { id: inspection.id, done: true }).ok).toBe(true); | |
| 469 | expect(lates(fresh(deal.id))).toEqual([]); | |
| 470 | }); | |
| 471 | ||
| 472 | test("pending: an overdue follow-up stays listed while unsent; the earlier moments drop once the date passes", () => { | |
| 473 | const deal = open({ contractDate: addDays(today(), -10), closingDate: addDays(today(), 30), lengths: { inspection: "8" } }); | |
| 474 | const inspection = due(deal, "inspection"); // due two days ago | |
| 475 | const mine = (d: DealView) => drafts(d, agent).filter((r) => r.deadline_id === inspection.id); | |
| 476 | const day = addDays(inspection.due, 3); | |
| 477 | const late = mine(deal).find((r) => r.moment === "late")!; | |
| 478 | expect(pending(late, late.on)).toBe(true); | |
| 479 | expect(pending(late, day)).toBe(true); | |
| 480 | expect(pending(late, addDays(late.on, -1))).toBe(false); | |
| 481 | for (const r of mine(deal).filter((r) => r.moment !== "late")) expect(pending(r, addDays(inspection.due, 1))).toBe(false); | |
| 482 | expect(pending(mine(deal).find((r) => r.moment === "day_of")!, inspection.due)).toBe(true); | |
| 483 | ||
| 484 | const ask = { deadlineId: inspection.id, partyId: late.party_id, moment: "late" }; | |
| 485 | expect(declineReminder(db, alice, { ...ask, reason: "Spoke by phone" }).ok).toBe(true); | |
| 486 | expect(pending(mine(fresh(deal.id)).find((r) => r.moment === "late")!, day)).toBe(false); | |
| 487 | expect(undoDecline(db, alice, ask).ok).toBe(true); | |
| 488 | expect(sendReminder(db, alice, ask).ok).toBe(true); | |
| 489 | expect(pending(mine(fresh(deal.id)).find((r) => r.moment === "late")!, day)).toBe(false); | |
| 490 | ||
| 491 | expect(markDone(db, alice, { id: inspection.id, done: true }).ok).toBe(true); | |
| 492 | expect(mine(fresh(deal.id)).filter((r) => pending(r, day))).toEqual([]); | |
| 493 | }); |
src/deals.ts +4 −4
| @@ -114,9 +114,9 @@ export interface DealView extends Deal { | ||
| 114 | 114 | readonly declined: readonly Declined[]; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | export type Moment = "before" | "day_of"; | |
| 117 | export type Moment = "before" | "day_of" | "late"; | |
| 118 | 118 | export const isMoment = (v: unknown): v is Moment => |
| 119 | v === "before" || v === "day_of"; | |
| 119 | v === "before" || v === "day_of" || v === "late"; | |
| 120 | 120 | |
| 121 | 121 | /** Text from a person: trimmed, one line, within bounds — or undefined. */ |
| 122 | 122 | export function line(value: unknown, min: number, max: number): string | undefined { |
| @@ -652,7 +652,7 @@ export function sendReminder( | ||
| 652 | 652 | const { dealId, found } = deadlineFor(db, who, input.deadlineId); |
| 653 | 653 | if (!found.ok) return within(db, who, dealId, found); |
| 654 | 654 | if (!isMoment(input.moment)) |
| 655 | return within(db, who, dealId, fail("invalid", "A reminder is for three days before, or the day of.")); | |
| 655 | return within(db, who, dealId, fail("invalid", "A reminder is for three days before, the day of, or the day after (overdue).")); | |
| 656 | 656 | const party = Number.isInteger(input.partyId) |
| 657 | 657 | ? db |
| 658 | 658 | .query<Party, [number, number]>("SELECT * FROM party WHERE id = ?1 AND deal_id = ?2") |
| @@ -693,7 +693,7 @@ function draftOf( | ||
| 693 | 693 | const { dealId, found } = deadlineFor(db, who, input.deadlineId, input.dealId); |
| 694 | 694 | if (!found.ok) return { dealId, found }; |
| 695 | 695 | if (!isMoment(input.moment)) |
| 696 | return { dealId, found: fail("invalid", "A reminder is for three days before, or the day of.") }; | |
| 696 | return { dealId, found: fail("invalid", "A reminder is for three days before, the day of, or the day after (overdue).") }; | |
| 697 | 697 | const { deal, deadline } = found.value; |
| 698 | 698 | const party = Number.isInteger(input.partyId) |
| 699 | 699 | ? db.query<Party, [number, number]>("SELECT * FROM party WHERE id = ?1 AND deal_id = ?2").get(input.partyId, deal.id) |
src/mcp.ts +3 −3
| @@ -129,7 +129,7 @@ export const tools: readonly Tool[] = [ | ||
| 129 | 129 | properties: { |
| 130 | 130 | deadline_id: { type: "integer" }, |
| 131 | 131 | party_id: { type: "integer" }, |
| 132 | moment: { enum: ["before", "day_of"] }, | |
| 132 | moment: { enum: ["before", "day_of", "late"] }, | |
| 133 | 133 | subject: { type: "string", minLength: 1, maxLength: 150 }, |
| 134 | 134 | body: { type: "string", minLength: 1, maxLength: 2000 }, |
| 135 | 135 | }, |
| @@ -147,7 +147,7 @@ export const tools: readonly Tool[] = [ | ||
| 147 | 147 | properties: { |
| 148 | 148 | deadline_id: { type: "integer" }, |
| 149 | 149 | party_id: { type: "integer" }, |
| 150 | moment: { enum: ["before", "day_of"] }, | |
| 150 | moment: { enum: ["before", "day_of", "late"] }, | |
| 151 | 151 | reason: { type: "string", minLength: 1, maxLength: 200 }, |
| 152 | 152 | }, |
| 153 | 153 | required: ["deadline_id", "party_id", "moment", "reason"], |
| @@ -160,7 +160,7 @@ export const tools: readonly Tool[] = [ | ||
| 160 | 160 | description: "Take back a decline: the reminder is a draft again, to send or decline.", |
| 161 | 161 | inputSchema: { |
| 162 | 162 | type: "object", |
| 163 | properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of"] } }, | |
| 163 | properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late"] } }, | |
| 164 | 164 | required: ["deadline_id", "party_id", "moment"], |
| 165 | 165 | }, |
| 166 | 166 | run: (db, who, args) => |
src/pages/clock.ts +3 −3
| @@ -10,7 +10,7 @@ import type { DealView } from "../deals.ts"; | ||
| 10 | 10 | import { KIND } from "../deadlines.ts"; |
| 11 | 11 | import { agent } from "../env.ts"; |
| 12 | 12 | import { escape } from "../http.ts"; |
| 13 | import { drafts, unsent } from "../reminders.ts"; | |
| 13 | import { drafts, pending, unsent } from "../reminders.ts"; | |
| 14 | 14 | import type { Who } from "../who.ts"; |
| 15 | 15 | import { thisWeek } from "../week.ts"; |
| 16 | 16 | import { layout } from "./layout.ts"; |
| @@ -127,7 +127,7 @@ export function clock(input: { | ||
| 127 | 127 | const { start, days } = span(deals, on); |
| 128 | 128 | const today = deals.flatMap((deal) => |
| 129 | 129 | drafts(deal, agent) |
| 130 | .filter((r) => r.on === on || (r.on < on && unsent(r) && r.due >= on)) | |
| 130 | .filter((r) => pending(r, on)) | |
| 131 | 131 | .map((r) => ({ r, may: canChange(who, deal) })), |
| 132 | 132 | ); |
| 133 | 133 | const toSend = today.filter(({ r }) => unsent(r)).length; |
| @@ -175,7 +175,7 @@ export function clock(input: { | ||
| 175 | 175 | ? `<p class="muted">Nothing goes out today.</p>` |
| 176 | 176 | : `<ul class="drafts">${today.map(({ r, may }) => draftItem(r, on, may, "/", true)).join("")}</ul>` |
| 177 | 177 | } |
| 178 | <p class="muted small">Each date reminds its parties three days before and the morning of. Sending is simulated: the draft is shown and marked sent.</p> | |
| 178 | <p class="muted small">Each date reminds its parties three days before and the morning of, and the one who owes a date is asked for an update the morning after it slips. Sending is simulated: the draft is shown and marked sent.</p> | |
| 179 | 179 | </section>`; |
| 180 | 180 | return layout({ body, who, path: "/", notice }); |
| 181 | 181 | } |
src/pages/sheet.ts +1
| @@ -114,6 +114,7 @@ export function sheet(deal: DealView, d: DeadlineView, who: Who, on: string, to: | ||
| 114 | 114 | ? `<p class="muted">No one to remind yet — <a href="/deals/${deal.id}#parties">add the parties</a>.</p>` |
| 115 | 115 | : MOMENTS.map((m) => { |
| 116 | 116 | const these = drafts.filter((r) => r.moment === m); |
| 117 | if (these.length === 0) return ""; | |
| 117 | 118 | const head = `<h4>${MOMENT_LABEL[m]} <span class="muted">· ${escape(spoken(these[0]!.on))}</span></h4>`; |
| 118 | 119 | return `${head}<ul class="drafts">${these.map((r) => draftItem(r, on, may, to)).join("")}</ul>`; |
| 119 | 120 | }).join(""); |
src/reminders.ts +34 −13
| @@ -1,9 +1,10 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * Reminders, drafted. Every open date of a deal reminds each party it |
| 3 | * concerns twice: three days before, and the morning of. A draft is plain | |
| 4 | * data — who, when, the words — computed from a deal; nothing is sent | |
| 5 | * anywhere. `sendReminder` (`deals.ts`) records that the agent sent one, with | |
| 6 | * the words approved; a sent draft reads back those words. | |
| 3 | * concerns three days before and the morning of; a date that slips past due | |
| 4 | * while still open gets a third, the morning after, to the party who owes it | |
| 5 | * alone. A draft is plain data — who, when, the words — computed from a deal; | |
| 6 | * nothing is sent anywhere. `sendReminder` (`deals.ts`) records that the agent | |
| 7 | * sent one, with the words approved; a sent draft reads back those words. | |
| 7 | 8 | */ |
| 8 | 9 | |
| 9 | 10 | import { addDays, spoken } from "./dates.ts"; |
| @@ -36,17 +37,23 @@ export interface Draft { | ||
| 36 | 37 | readonly declined: { readonly reason: string; readonly by_name: string } | null; |
| 37 | 38 | } |
| 38 | 39 | |
| 39 | export const MOMENTS: readonly Moment[] = ["before", "day_of"]; | |
| 40 | export const MOMENTS: readonly Moment[] = ["before", "day_of", "late"]; | |
| 40 | 41 | export const MOMENT_LABEL: Readonly<Record<Moment, string>> = { |
| 41 | 42 | before: "3 days before", |
| 42 | 43 | day_of: "Morning of", |
| 44 | late: "Overdue follow-up", | |
| 43 | 45 | }; |
| 44 | export const sendOn = (due: string, moment: Moment): string => | |
| 45 | moment === "before" ? addDays(due, -3) : due; | |
| 46 | const OFFSET: Readonly<Record<Moment, number>> = { before: -3, day_of: 0, late: 1 }; | |
| 47 | export const sendOn = (due: string, moment: Moment): string => addDays(due, OFFSET[moment]); | |
| 46 | 48 | |
| 47 | /** The parties a date concerns: whoever owes it, then whoever its kind tells. */ | |
| 48 | export function recipients(deal: DealView, deadline: DeadlineView): Party[] { | |
| 49 | const roles = new Set<PartyRole>([deadline.owes, ...KIND[deadline.kind].notify]); | |
| 49 | /** | |
| 50 | * The parties a moment tells: whoever owes the date, then whoever its kind | |
| 51 | * notifies; an overdue follow-up goes to whoever owes it alone. | |
| 52 | */ | |
| 53 | export function recipients(deal: DealView, deadline: DeadlineView, moment: Moment = "day_of"): Party[] { | |
| 54 | const roles = new Set<PartyRole>( | |
| 55 | moment === "late" ? [deadline.owes] : [deadline.owes, ...KIND[deadline.kind].notify], | |
| 56 | ); | |
| 50 | 57 | return deal.parties.filter((p) => roles.has(p.role)); |
| 51 | 58 | } |
| 52 | 59 | |
| @@ -59,10 +66,16 @@ function words( | ||
| 59 | 66 | moment: Moment, |
| 60 | 67 | agent: Agent, |
| 61 | 68 | ): { subject: string; body: string } { |
| 62 | const when = moment === "before" ? `in three days, on ${spoken(deadline.due)}` : `today, ${spoken(deadline.due)}`; | |
| 63 | 69 | const owner = deal.parties.find((p) => p.role === deadline.owes); |
| 64 | const yours = party.role === deadline.owes; | |
| 65 | 70 | const owed = deadline.what === "" ? "" : `${deadline.what}. `; |
| 71 | if (moment === "late") { | |
| 72 | return { | |
| 73 | subject: `${deadline.label} — ${deal.address} — overdue`, | |
| 74 | body: `Hi ${first(party.name)}, the ${deadline.label.toLowerCase()} for ${deal.address} was due ${spoken(deadline.due)} and has passed without being marked done. ${owed}Could you send me an update today on where it stands and when it will be done?\n\n${agent.name}, ${agent.brokerage}`, | |
| 75 | }; | |
| 76 | } | |
| 77 | const when = moment === "before" ? `in three days, on ${spoken(deadline.due)}` : `today, ${spoken(deadline.due)}`; | |
| 78 | const yours = party.role === deadline.owes; | |
| 66 | 79 | const ask = yours |
| 67 | 80 | ? "This one is yours — please let me know right away if anything could hold it up." |
| 68 | 81 | : `${owner === undefined ? PARTY_LABEL[deadline.owes] : owner.name} (${PARTY_LABEL[deadline.owes].toLowerCase()}) is on it; nothing is needed from you unless you hear otherwise.`; |
| @@ -83,7 +96,7 @@ export function drafts(deal: DealView, agent: Agent): Draft[] { | ||
| 83 | 96 | /** The reminders one date has, sent or not. */ |
| 84 | 97 | export function draftsFor(deal: DealView, deadline: DeadlineView, agent: Agent): Draft[] { |
| 85 | 98 | return MOMENTS.flatMap((moment) => |
| 86 | recipients(deal, deadline).map((party) => { | |
| 99 | recipients(deal, deadline, moment).map((party) => { | |
| 87 | 100 | const sent = deal.sent.find( |
| 88 | 101 | (s) => |
| 89 | 102 | s.deadline_id === deadline.id && |
| @@ -127,3 +140,11 @@ export const unsent = (d: Draft): boolean => d.sent_at === null && d.declined == | ||
| 127 | 140 | /** What goes out on `day` and has not gone yet: `POST /tick`'s answer. */ |
| 128 | 141 | export const dueOn = (deals: readonly DealView[], day: string, agent: Agent): Draft[] => |
| 129 | 142 | deals.flatMap((deal) => drafts(deal, agent)).filter((d) => d.on === day && d.sent_at === null && d.declined === null); |
| 143 | ||
| 144 | /** | |
| 145 | * Waiting on `day`: goes out that morning, or was missed and still matters — | |
| 146 | * a before/day-of draft until its date passes, an overdue follow-up for as | |
| 147 | * long as the date stays open. | |
| 148 | */ | |
| 149 | export const pending = (d: Draft, day: string): boolean => | |
| 150 | d.on === day || (d.on < day && unsent(d) && (d.due >= day || d.moment === "late")); |
src/routes.ts +5 −5
| @@ -159,7 +159,7 @@ export const DEAL: Schema = { | ||
| 159 | 159 | id: { type: "integer" }, |
| 160 | 160 | deadline_id: { type: "integer" }, |
| 161 | 161 | party_id: { type: "integer" }, |
| 162 | moment: { enum: ["before", "day_of"] }, | |
| 162 | moment: { enum: ["before", "day_of", "late"] }, | |
| 163 | 163 | due: DATE, |
| 164 | 164 | reason: { type: "string", description: "Why the agent chose not to send it." }, |
| 165 | 165 | by_name: { type: "string" }, |
| @@ -180,7 +180,7 @@ export const DEAL: Schema = { | ||
| 180 | 180 | label: { type: "string", description: "The date it was about, as named when sent." }, |
| 181 | 181 | party_name: { type: "string" }, |
| 182 | 182 | party_role: { type: "string", description: "The party's role when sent; empty on a reminder sent before roles were kept." }, |
| 183 | moment: { enum: ["before", "day_of"] }, | |
| 183 | moment: { enum: ["before", "day_of", "late"] }, | |
| 184 | 184 | due: DATE, |
| 185 | 185 | by_name: { type: "string" }, |
| 186 | 186 | sent_at: { type: "string" }, |
| @@ -221,7 +221,7 @@ export const DRAFT: Schema = { | ||
| 221 | 221 | party_name: { type: "string" }, |
| 222 | 222 | party_role: ROLE, |
| 223 | 223 | contact: { type: "string" }, |
| 224 | moment: { enum: ["before", "day_of"] }, | |
| 224 | moment: { enum: ["before", "day_of", "late"] }, | |
| 225 | 225 | on: DATE, |
| 226 | 226 | subject: { type: "string" }, |
| 227 | 227 | body: { type: "string" }, |
| @@ -644,7 +644,7 @@ export const routes: readonly Route[] = [ | ||
| 644 | 644 | "Choose not to send a reminder, with a reason (1 to 200 characters). It is kept as a record, no longer counts as to send, and leaves /tick.", |
| 645 | 645 | body: { |
| 646 | 646 | type: "object", |
| 647 | properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of"] }, reason: { type: "string", minLength: 1, maxLength: 200 } }, | |
| 647 | properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late"] }, reason: { type: "string", minLength: 1, maxLength: 200 } }, | |
| 648 | 648 | required: ["deadline_id", "party_id", "moment", "reason"], |
| 649 | 649 | }, |
| 650 | 650 | answer: { type: "object", properties: { id: { type: "integer" }, reason: { type: "string" }, by_name: { type: "string" } }, required: ["id", "reason"] }, |
| @@ -666,7 +666,7 @@ export const routes: readonly Route[] = [ | ||
| 666 | 666 | summary: "Take back a decline: the reminder is a draft again.", |
| 667 | 667 | body: { |
| 668 | 668 | type: "object", |
| 669 | properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of"] } }, | |
| 669 | properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late"] } }, | |
| 670 | 670 | required: ["deadline_id", "party_id", "moment"], |
| 671 | 671 | }, |
| 672 | 672 | answer: { type: "object", properties: { id: { type: "integer" } }, required: ["id"] }, |
src/sample.test.ts +17
| @@ -1,4 +1,11 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | import { mkdtempSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { openDb } from "./db.ts"; | |
| 6 | import { listDeals } from "./deals.ts"; | |
| 7 | import { drafts, unsent } from "./reminders.ts"; | |
| 8 | import type { Who } from "./who.ts"; | |
| 2 | 9 | import { isUntouched, shiftSample, type SampleDates } from "./sample.ts"; |
| 3 | 10 | |
| 4 | 11 | const dates: SampleDates = { |
| @@ -41,3 +48,13 @@ test("a sample a person changed is not untouched", () => { | ||
| 41 | 48 | expect(isUntouched({ ...state, doneAts: ["2026-09-30T17:00:00.000Z", "2026-10-02T08:00:00.000Z", null] }, "2026-10-01")).toBe(false); |
| 42 | 49 | expect(isUntouched({ ...state, address: "Mine" }, "2026-10-01")).toBe(false); |
| 43 | 50 | }); |
| 51 | ||
| 52 | test("the sample deals hold an overdue date with its follow-up waiting", () => { | |
| 53 | const db = openDb(mkdtempSync(join(tmpdir(), "app-sample-late-"))); | |
| 54 | const alice: Who = { role: "user", id: "u1", name: "alice" }; | |
| 55 | const listed = listDeals(db, alice); | |
| 56 | if (!listed.ok) throw new Error(listed.message); | |
| 57 | const agent = { name: "Dana", brokerage: "HP" }; | |
| 58 | const late = listed.value.flatMap((d) => drafts(d, agent)).filter((r) => r.moment === "late" && unsent(r)); | |
| 59 | expect(late.length).toBeGreaterThan(0); | |
| 60 | }); |
src/terminal.test.ts +22
| @@ -347,3 +347,25 @@ test("s on a deal lists what was sent, newest first, with the words; b comes bac | ||
| 347 | 347 | expect(lines(guest).join("\n")).not.toContain("First <b>words</b>"); |
| 348 | 348 | expect(lines(guest).join("\n")).not.toContain("Messages —"); |
| 349 | 349 | }); |
| 350 | ||
| 351 | test("r lists an overdue follow-up as late, as the clock page does, until it is sent", () => { | |
| 352 | const slipped = createDeal(db, alice, { | |
| 353 | address: "5 Late Ln", | |
| 354 | buyer: "Lena Buyer", | |
| 355 | seller: "Sid Seller", | |
| 356 | contractDate: addDays(today(), -20), | |
| 357 | closingDate: addDays(today(), 30), | |
| 358 | }); | |
| 359 | if (!slipped.ok) throw new Error(slipped.message); | |
| 360 | const missed = addDeadline(db, alice, { dealId: slipped.value.id, label: "Appraisal back", due: addDays(today(), -3), owes: "buyer" }); | |
| 361 | if (!missed.ok) throw new Error(missed.message); | |
| 362 | const t = terminal(db, alice, context); | |
| 363 | t.press("r"); | |
| 364 | const row = () => lines(t).find((l) => l.includes("Lena Buyer") && l.includes("Appraisal back") && l.includes("overdue follow-up")); | |
| 365 | expect(row()).toContain("late"); | |
| 366 | expect(lines(t).filter((l) => l.includes("Sid Seller") && l.includes("Appraisal back") && l.includes("overdue follow-up"))).toEqual([]); | |
| 367 | expect(sendReminder(db, alice, { deadlineId: missed.value.id, partyId: slipped.value.parties.find((p) => p.name === "Lena Buyer")!.id, moment: "late" }).ok).toBe(true); | |
| 368 | const again = terminal(db, alice, context); | |
| 369 | again.press("r"); | |
| 370 | expect(lines(again).some((l) => l.includes("Appraisal back") && l.includes("overdue follow-up"))).toBe(false); | |
| 371 | }); |
src/terminal.ts +2 −2
| @@ -45,7 +45,7 @@ import { | ||
| 45 | 45 | type DeadlineView, |
| 46 | 46 | } from "./deals.ts"; |
| 47 | 47 | import { PARTY_LABEL, PARTY_ROLES, type PartyRole } from "./deadlines.ts"; |
| 48 | import { drafts, MOMENT_LABEL, unsent, type Agent, type Draft } from "./reminders.ts"; | |
| 48 | import { drafts, MOMENT_LABEL, pending, unsent, type Agent, type Draft } from "./reminders.ts"; | |
| 49 | 49 | import type { Result } from "./result.ts"; |
| 50 | 50 | import type { Who } from "./who.ts"; |
| 51 | 51 | import { owner, thisWeek } from "./week.ts"; |
| @@ -121,7 +121,7 @@ export function terminal( | ||
| 121 | 121 | const day = on(); |
| 122 | 122 | return deals().flatMap((deal) => |
| 123 | 123 | drafts(deal, context.agent) |
| 124 | .filter((r) => r.on === day || (r.on < day && unsent(r) && r.due >= day)) | |
| 124 | .filter((r) => pending(r, day)) | |
| 125 | 125 | .map((draft) => ({ deal, draft })), |
| 126 | 126 | ); |
| 127 | 127 | }; |
new above
offline