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

Thirty days after a deal closes, the agent gets a drafted "How is the new home?" check-in to their clients, approved like any reminder, so past clients turn into referrals #156

Merged opened this

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

Merged

mason/deal-clock:mason/139-thirty-days-after-a-deal-closes-the-agen → main 2 commits fork 1 thread · 1 unresolved

  • Merged. mason/139-thirty-days-after-a-deal-closes-the-agen was merged into main as 2462189.

Review

Sign in to review this change.
14 files changed +322 −37
README.md +3
@@ -155,6 +155,9 @@ Signed out, `/` shows what deal-clock does, three short points, a still picture
155155 mark it done. The deal steps off the clock into the Closed card below it,
156156 its reminders stop, and its page says "Closed on …". Everything on it still
157157 opens; reopen the Closing date and the deal is back on the clock.
158 A month after you marked it done, a drafted "How is the new home?" check-in
159 to your past clients (the buyers, the sellers, or both, by the deal's side)
160 waits in Review and the header count; send, edit, hold or decline it like any reminder.
158161 12. **Download the calendar file.** On a deal's page, tap Add to calendar.
159162 You get a calendar file with every date of the deal and two
160163 alarms for each: one as many days ahead as that date's Reminds N days
contract/openapi.json +22 −12
@@ -253,11 +253,11 @@
253253 },
254254 "/tick": {
255255 "post": {
256 "summary": "The reminders that go out today and have not been sent: call it each morning. It sends nothing and changes nothing.",
256 "summary": "The reminders that go out today and have not been sent, including the check-in (moment checkin) owed to past clients 30 days after a closing was marked done: call it each morning. It sends nothing and changes nothing.",
257257 "operationId": "postTick",
258258 "responses": {
259259 "200": {
260 "description": "The reminders that go out today and have not been sent: call it each morning. It sends nothing and changes nothing.",
260 "description": "The reminders that go out today and have not been sent, including the check-in (moment checkin) owed to past clients 30 days after a closing was marked done: call it each morning. It sends nothing and changes nothing.",
261261 "content": {
262262 "application/json": {
263263 "schema": {
@@ -1074,7 +1074,8 @@
10741074 "day_of",
10751075 "late",
10761076 "moved",
1077 "status"
1077 "status",
1078 "checkin"
10781079 ]
10791080 },
10801081 "due": {
@@ -1126,7 +1127,8 @@
11261127 "day_of",
11271128 "late",
11281129 "moved",
1129 "status"
1130 "status",
1131 "checkin"
11301132 ]
11311133 },
11321134 "due": {
@@ -1728,7 +1730,8 @@
17281730 "day_of",
17291731 "late",
17301732 "moved",
1731 "status"
1733 "status",
1734 "checkin"
17321735 ]
17331736 },
17341737 "reason": {
@@ -1823,7 +1826,8 @@
18231826 "before",
18241827 "day_of",
18251828 "late",
1826 "moved"
1829 "moved",
1830 "checkin"
18271831 ]
18281832 },
18291833 "subject": {
@@ -1920,7 +1924,8 @@
19201924 "before",
19211925 "day_of",
19221926 "late",
1923 "moved"
1927 "moved",
1928 "checkin"
19241929 ]
19251930 }
19261931 },
@@ -2003,7 +2008,8 @@
20032008 "day_of",
20042009 "late",
20052010 "moved",
2006 "status"
2011 "status",
2012 "checkin"
20072013 ]
20082014 }
20092015 },
@@ -3062,7 +3068,8 @@
30623068 "day_of",
30633069 "late",
30643070 "moved",
3065 "status"
3071 "status",
3072 "checkin"
30663073 ]
30673074 },
30683075 "due": {
@@ -3114,7 +3121,8 @@
31143121 "day_of",
31153122 "late",
31163123 "moved",
3117 "status"
3124 "status",
3125 "checkin"
31183126 ]
31193127 },
31203128 "due": {
@@ -3214,8 +3222,10 @@
32143222 "day_of",
32153223 "late",
32163224 "moved",
3217 "status"
3218 ]
3225 "status",
3226 "checkin"
3227 ],
3228 "description": "Why it goes out. checkin is the \"How is the new home?\" message to a past client 30 days after the deal's closing was marked done (buyers on a buyer-side deal, sellers on a listing, both on a both-sides deal); it hangs on the closing's deadline_id, its due is the day the closing was marked done, and a reopened or fell-through deal has none."
32193229 },
32203230 "on": {
32213231 "type": "string",
docs/TECHNICAL.md +7
@@ -31,6 +31,13 @@ drafts no reminders, follow-up, business-day flag or after-closing conflict.
3131 the MCP tool `waive_date` and `W` in the terminal all call `waiveDate`, an
3232 update of the deal, audited as `date.waive`.
3333
34 A closed deal drafts one check-in (`moment: "checkin"`, `src/checkin.ts`) per
35 client party with a contact (buyers on a buyer-side deal, sellers on a listing,
36 both on a both-sides deal), on the morning 30 days after the day the closing
37 was marked done. It hangs on the closing's `deadline_id`, is keyed by that day
38 (so reopening and closing again is a new one), and is sent, held, declined and
39 listed in `/tick` like any reminder. A reopened or fell-through deal has none.
40
3441 A deal that falls through has `fell_at`, `fell_reason` and `fell_by` set
3542 (`deal.fell_through` in the JSON of `GET /api/deals`); `closed` keeps meaning
3643 "Closing marked done". It is off the clock like a closed deal: no reminders,
src/checkin.test.ts added +95
@@ -0,0 +1,95 @@
1 /** A month after a closing is marked done, each past client is owed a check-in, approved like any reminder. */
2 import { expect, test } from "bun:test";
3 import { mkdtempSync } from "node:fs";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createDeal, declineReminder, getDeal, holdReminder, markDone, sendReminder, listDeals } from "./deals.ts";
7 import { openDb } from "./db.ts";
8 import { checkinDrafts, dueOn, todaysDrafts, waitingCount, unsent } from "./reminders.ts";
9 import { dealPage } from "./pages/deal.ts";
10 import type { Who } from "./who.ts";
11
12 const alice: Who = { role: "user", id: "u1", name: "alice" };
13 const dana = { name: "Dana", brokerage: "HP", phone: "(555) 010-0142", email: "[email protected]" };
14 const db = openDb(mkdtempSync(join(tmpdir(), "app-checkin-")));
15
16 function closed(address: string, side: string, doneOn = "2026-10-01") {
17 const made = createDeal(db, alice, { openAnyway: true, address, buyer: "Ann Buyer", seller: "Sam Seller", contractDate: "2026-09-01", closingDate: "2026-10-01", side, salePrice: 612000 });
18 if (!made.ok) throw new Error(made.message);
19 db.query("UPDATE party SET email = lower(substr(name, 1, 3)) || '@example.com', contact = lower(substr(name, 1, 3)) || '@example.com' WHERE deal_id = ?1").run(made.value.id);
20 const closing = getDeal(db, alice, { id: made.value.id }).value!.deadlines.find((d) => d.kind === "closing")!;
21 markDone(db, alice, { id: closing.id, done: true });
22 db.query("UPDATE deadline SET done_at = ?1 WHERE id = ?2").run(`${doneOn}T12:00:00.000Z`, closing.id);
23 return { id: made.value.id, closing: closing.id };
24 }
25 const view = (id: number) => getDeal(db, alice, { id }).value!;
26 const all = () => listDeals(db, alice).value!;
27
28 test("drafted on day 30 and not day 29, once per client the side names", () => {
29 const buyer = closed("1 Buyer Side", "buyer");
30 const listing = closed("2 Listing", "listing");
31 const both = closed("3 Both", "both");
32 const who = (id: number) => checkinDrafts(view(id), dana).map((d) => d.party_role);
33 expect(who(buyer.id)).toEqual(["buyer"]);
34 expect(who(listing.id)).toEqual(["seller"]);
35 expect(who(both.id)).toEqual(["buyer", "seller"]);
36 const mine = (day: string) => todaysDrafts(all(), day, dana).filter((d) => d.moment === "checkin");
37 expect(mine("2026-10-30")).toEqual([]);
38 expect(mine("2026-10-31").map((d) => d.address).sort()).toEqual(["1 Buyer Side", "2 Listing", "3 Both", "3 Both"]);
39 expect(dueOn(all(), "2026-10-31", dana).filter((d) => d.moment === "checkin")).toHaveLength(4);
40 expect(waitingCount(all(), "2026-10-31", dana)).toBe(4);
41 expect(waitingCount(all(), "2026-10-30", dana)).toBe(0);
42 });
43
44 test("the words are warm, name the address and never the price", () => {
45 const d = checkinDrafts(view(closed("4 Words Lane", "buyer").id), dana)[0]!;
46 expect(d.subject).toContain("4 Words Lane");
47 expect(d.body).toContain("Hi Ann,");
48 expect(d.body).toContain("how the move went");
49 expect(d.body).toContain("No pressure");
50 expect(d.body).not.toMatch(/612|\$/);
51 expect(d.body.endsWith("\n\nDana, HP\n(555) 010-0142 · [email protected]")).toBe(true);
52 });
53
54 test("a reopened deal and a fell-through deal get none", () => {
55 const a = closed("5 Reopened", "buyer");
56 expect(checkinDrafts(view(a.id), dana)).toHaveLength(1);
57 markDone(db, alice, { id: a.closing, done: false });
58 expect(checkinDrafts(view(a.id), dana)).toEqual([]);
59 expect(todaysDrafts(all(), "2026-10-31", dana).filter((d) => d.address === "5 Reopened" && d.moment === "checkin")).toEqual([]);
60 const b = closed("6 Fell", "buyer");
61 db.query("UPDATE deal SET fell_at = '2026-10-05T00:00:00.000Z', fell_reason = 'x' WHERE id = ?1").run(b.id);
62 expect(checkinDrafts(view(b.id), dana)).toEqual([]);
63 });
64
65 test("sending records the approved words; sent, it is not offered again", () => {
66 const c = closed("7 Sent", "buyer");
67 const d = checkinDrafts(view(c.id), dana)[0]!;
68 const sent = sendReminder(db, alice, { deadlineId: c.closing, partyId: d.party_id, moment: "checkin", body: "Hi Ann, how is it?", agent: dana });
69 expect(sent).toMatchObject({ ok: true, value: { moment: "checkin", body: "Hi Ann, how is it?" } });
70 const after = checkinDrafts(view(c.id), dana)[0]!;
71 expect(after.sent_at).not.toBeNull();
72 expect(after.body).toBe("Hi Ann, how is it?");
73 expect(todaysDrafts(all(), "2026-10-31", dana).filter(unsent).some((x) => x.address === "7 Sent")).toBe(false);
74 expect(view(c.id).sent[0]).toMatchObject({ moment: "checkin", body: "Hi Ann, how is it?" });
75 expect(dealPage({ who: alice, deal: view(c.id), on: "2026-10-31" })).toContain("Check-in");
76 });
77
78 test("the deal page offers the check-in on day 30, not before", () => {
79 const c = closed("10 Early", "buyer");
80 expect(dealPage({ who: alice, deal: view(c.id), on: "2026-10-30" })).not.toContain("Check-in");
81 expect(dealPage({ who: alice, deal: view(c.id), on: "2026-10-31" })).toContain("Check-in");
82 });
83
84 test("declining stops it; holding moves it to tomorrow", () => {
85 const c = closed("8 Declined", "buyer");
86 const d = checkinDrafts(view(c.id), dana)[0]!;
87 expect(declineReminder(db, alice, { deadlineId: c.closing, partyId: d.party_id, moment: "checkin", reason: "Moved abroad", agent: dana }).ok).toBe(true);
88 expect(todaysDrafts(all(), "2026-10-31", dana).some((x) => x.address === "8 Declined" && unsent(x))).toBe(false);
89 const h = closed("9 Held", "buyer");
90 const hd = checkinDrafts(view(h.id), dana)[0]!;
91 expect(holdReminder(db, alice, { deadlineId: h.closing, partyId: hd.party_id, moment: "checkin", on: "2026-10-31", agent: dana }).ok).toBe(true);
92 const mine = (day: string) => todaysDrafts(all(), day, dana).filter((x) => x.address === "9 Held" && unsent(x));
93 expect(mine("2026-10-31")).toEqual([]);
94 expect(mine("2026-11-01")).toHaveLength(1);
95 });
src/checkin.ts added +28
@@ -0,0 +1,28 @@
1 /**
2 * The check-in: a month after a closing is marked done, each past client
3 * gets a short, warm "How is the new home?" draft. This module holds the day
4 * it goes out, who it goes to and the words; `reminders.ts` makes the draft.
5 */
6
7 import { addDays } from "./dates.ts";
8 import type { PartyRole } from "./deadlines.ts";
9 import type { Side } from "./side.ts";
10
11 /** Days after the closing was marked done that the check-in goes out. */
12 export const CHECKIN_DAYS = 30;
13
14 /** The morning the check-in goes out, given the day the closing was marked done. */
15 export const checkinOn = (doneOn: string): string => addDays(doneOn, CHECKIN_DAYS);
16
17 /** The client parties a deal's side checks in on: the buyers, the sellers, or both. */
18 export function checkinRoles(side: Side): readonly PartyRole[] {
19 return side === "buyer" ? ["buyer"] : side === "listing" ? ["seller"] : ["buyer", "seller"];
20 }
21
22 /** The words of a check-in to one client; never the price. `signOff` is the agent's usual ending. */
23 export function checkinWords(address: string, firstName: string, signOff: string): { subject: string; body: string } {
24 return {
25 subject: `How is the new home? — ${address}`,
26 body: `Hi ${firstName}, it has been about a month since ${address}, and I wanted to ask how the move went. Is the home treating you well?\n\nIf anything has come up since closing, I'm happy to help with it, whether that's a question about the paperwork or a good name for a plumber.\n\nAnd if you know someone thinking of buying or selling, I'd be glad to look after them the way I looked after you. No pressure at all; I'm just glad to have been part of it.\n\n${signOff}`,
27 };
28 }
src/db.ts +66
@@ -433,6 +433,72 @@ export const migrations: string[] = [
433433 // Repair: the Profile-line change inserted the profile line mid-list, so databases already past
434434 // that position never got it. Tolerated as a duplicate where it exists.
435435 `ALTER TABLE profile ADD COLUMN line TEXT NOT NULL DEFAULT '' CHECK (length(line) <= 200)`,
436 // A check-in a month after a closing is a sixth moment, `checkin`. SQLite cannot alter a
437 // CHECK, so the three tables that hold a moment are rebuilt as before.
438 `CREATE TABLE reminder_new (
439 id INTEGER PRIMARY KEY,
440 deal_id INTEGER NOT NULL REFERENCES deal (id) ON DELETE CASCADE,
441 deadline_id INTEGER NOT NULL,
442 party_id INTEGER NOT NULL,
443 label TEXT NOT NULL,
444 party_name TEXT NOT NULL,
445 moment TEXT NOT NULL CHECK (moment IN ('before', 'day_of', 'late', 'moved', 'status', 'checkin')),
446 due TEXT NOT NULL,
447 by_name TEXT NOT NULL,
448 subject TEXT NOT NULL DEFAULT '',
449 body TEXT NOT NULL DEFAULT '',
450 sent_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
451 party_role TEXT NOT NULL DEFAULT '',
452 sample INTEGER NOT NULL DEFAULT 0,
453 move_id INTEGER NOT NULL DEFAULT 0,
454 UNIQUE (deadline_id, party_id, moment, due, move_id)
455 );
456 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, move_id)
457 SELECT id, deal_id, deadline_id, party_id, label, party_name, moment, due, by_name, subject, body, sent_at, party_role, sample, move_id FROM reminder;
458 DROP TABLE reminder;
459 ALTER TABLE reminder_new RENAME TO reminder;
460 CREATE INDEX reminder_deal ON reminder (deal_id);
461 CREATE TABLE reminder_declined_new (
462 id INTEGER PRIMARY KEY,
463 deal_id INTEGER NOT NULL REFERENCES deal (id) ON DELETE CASCADE,
464 deadline_id INTEGER NOT NULL,
465 party_id INTEGER NOT NULL,
466 label TEXT NOT NULL,
467 party_name TEXT NOT NULL,
468 moment TEXT NOT NULL CHECK (moment IN ('before', 'day_of', 'late', 'moved', 'status', 'checkin')),
469 due TEXT NOT NULL,
470 reason TEXT NOT NULL CHECK (length(reason) BETWEEN 1 AND 200),
471 by_name TEXT NOT NULL,
472 at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
473 move_id INTEGER NOT NULL DEFAULT 0,
474 UNIQUE (deadline_id, party_id, moment, due, move_id)
475 );
476 INSERT INTO reminder_declined_new (id, deal_id, deadline_id, party_id, label, party_name, moment, due, reason, by_name, at, move_id)
477 SELECT id, deal_id, deadline_id, party_id, label, party_name, moment, due, reason, by_name, at, move_id FROM reminder_declined;
478 DROP TABLE reminder_declined;
479 ALTER TABLE reminder_declined_new RENAME TO reminder_declined;
480 CREATE INDEX reminder_declined_deal ON reminder_declined (deal_id);
481 CREATE TABLE reminder_hold_new (
482 id INTEGER PRIMARY KEY,
483 deal_id INTEGER NOT NULL REFERENCES deal (id) ON DELETE CASCADE,
484 deadline_id INTEGER NOT NULL,
485 party_id INTEGER NOT NULL,
486 moment TEXT NOT NULL CHECK (moment IN ('before', 'day_of', 'late', 'moved', 'checkin')),
487 due TEXT NOT NULL,
488 move_id INTEGER NOT NULL DEFAULT 0,
489 until TEXT NOT NULL,
490 held_on TEXT NOT NULL,
491 by_name TEXT NOT NULL,
492 at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
493 subject TEXT NOT NULL DEFAULT '',
494 body TEXT NOT NULL DEFAULT '',
495 UNIQUE (deadline_id, party_id, moment, due, move_id)
496 );
497 INSERT INTO reminder_hold_new (id, deal_id, deadline_id, party_id, moment, due, move_id, until, held_on, by_name, at, subject, body)
498 SELECT id, deal_id, deadline_id, party_id, moment, due, move_id, until, held_on, by_name, at, subject, body FROM reminder_hold;
499 DROP TABLE reminder_hold;
500 ALTER TABLE reminder_hold_new RENAME TO reminder_hold;
501 CREATE INDEX reminder_hold_deal ON reminder_hold (deal_id)`,
436502 ];
437503
438504 export function openDb(dir = dataDir): Database {
src/deals.ts +4 −4
@@ -198,9 +198,9 @@ export interface DealView extends Deal {
198198 readonly conflicts: readonly Conflict[];
199199 }
200200
201 export type Moment = "before" | "day_of" | "late" | "moved" | "status";
201 export type Moment = "before" | "day_of" | "late" | "moved" | "status" | "checkin";
202202 export const isMoment = (v: unknown): v is Moment =>
203 v === "before" || v === "day_of" || v === "late" || v === "moved" || v === "status";
203 v === "before" || v === "day_of" || v === "late" || v === "moved" || v === "status" || v === "checkin";
204204
205205 /** Text from a person: trimmed, one line, within bounds — or undefined. */
206206 export function line(value: unknown, min: number, max: number): string | undefined {
@@ -1197,7 +1197,7 @@ export function sendReminder(
11971197 const { dealId, found } = deadlineFor(db, who, input.deadlineId);
11981198 if (!found.ok) return within(db, who, "reminder.send", dealId, found);
11991199 if (!isMoment(input.moment))
1200 return within(db, who, "reminder.send", dealId, fail("invalid", "A reminder is for ahead of a date, the day of, the day after (overdue), a date that changed, or a status update."));
1200 return within(db, who, "reminder.send", dealId, fail("invalid", "A reminder is for ahead of a date, the day of, the day after (overdue), a date that changed, a status update, or a check-in."));
12011201 const party = Number.isInteger(input.partyId)
12021202 ? db
12031203 .query<Party, [number, number]>("SELECT * FROM party WHERE id = ?1 AND deal_id = ?2")
@@ -1258,7 +1258,7 @@ function draftOf(
12581258 const { dealId, found } = deadlineFor(db, who, input.deadlineId, input.dealId);
12591259 if (!found.ok) return { dealId, found };
12601260 if (!isMoment(input.moment))
1261 return { dealId, found: fail("invalid", "A reminder is for ahead of a date, the day of, the day after (overdue), a date that changed, or a status update.") };
1261 return { dealId, found: fail("invalid", "A reminder is for ahead of a date, the day of, the day after (overdue), a date that changed, a status update, or a check-in.") };
12621262 const { deal, deadline } = found.value;
12631263 const party = Number.isInteger(input.partyId)
12641264 ? db.query<Party, [number, number]>("SELECT * FROM party WHERE id = ?1 AND deal_id = ?2").get(input.partyId, deal.id)
src/mcp.ts +6 −6
@@ -95,7 +95,7 @@ export const tools: readonly Tool[] = [
9595 },
9696 {
9797 name: "reminders_today",
98 description: "The reminder drafts that go out today and have not been sent, including ones held from an earlier day that come back today.",
98 description: "The reminder drafts that go out today and have not been sent, including ones held from an earlier day that come back today and the check-ins (moment checkin, on the closing's deadline_id) owed to past clients 30 days after a closing was marked done. Send, hold or decline them like any reminder; nothing goes without approval.",
9999 inputSchema: { type: "object", properties: {} },
100100 run: (db, who) => {
101101 const deals = listDeals(db, who);
@@ -238,7 +238,7 @@ export const tools: readonly Tool[] = [
238238 properties: {
239239 deadline_id: { type: "integer" },
240240 party_id: { type: "integer" },
241 moment: { enum: ["before", "day_of", "late", "moved", "status"] },
241 moment: { enum: ["before", "day_of", "late", "moved", "status", "checkin"] },
242242 subject: { type: "string", minLength: 1, maxLength: 150 },
243243 body: { type: "string", minLength: 1, maxLength: 2000 },
244244 },
@@ -280,7 +280,7 @@ export const tools: readonly Tool[] = [
280280 properties: {
281281 deadline_id: { type: "integer" },
282282 party_id: { type: "integer" },
283 moment: { enum: ["before", "day_of", "late", "moved", "status"] },
283 moment: { enum: ["before", "day_of", "late", "moved", "status", "checkin"] },
284284 reason: { type: "string", minLength: 1, maxLength: 200 },
285285 },
286286 required: ["deadline_id", "party_id", "moment", "reason"],
@@ -294,7 +294,7 @@ export const tools: readonly Tool[] = [
294294 "Hold a reminder until tomorrow, for a message the agent is not ready to approve: it leaves reminders_today and comes back the next morning with its words, labelled held. Give subject (up to 150 characters) and body (up to 2000) to hold the agent's edited words; leave them out to keep any already held. Held again, it waits one more day. A status update is not held.",
295295 inputSchema: {
296296 type: "object",
297 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved"] }, subject: { type: "string" }, body: { type: "string" } },
297 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved", "checkin"] }, subject: { type: "string" }, body: { type: "string" } },
298298 required: ["deadline_id", "party_id", "moment"],
299299 },
300300 run: (db, who, args) =>
@@ -305,7 +305,7 @@ export const tools: readonly Tool[] = [
305305 description: "Bring a held reminder back to today, for when the agent is ready to send it after all: the hold is deleted and it returns to reminders_today. Refused when it is not held, or was already sent or declined.",
306306 inputSchema: {
307307 type: "object",
308 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved"] } },
308 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved", "checkin"] } },
309309 required: ["deadline_id", "party_id", "moment"],
310310 },
311311 run: (db, who, args) =>
@@ -316,7 +316,7 @@ export const tools: readonly Tool[] = [
316316 description: "Take back a decline: the reminder is a draft again, to send or decline.",
317317 inputSchema: {
318318 type: "object",
319 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved", "status"] } },
319 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved", "status", "checkin"] } },
320320 required: ["deadline_id", "party_id", "moment"],
321321 },
322322 run: (db, who, args) =>
src/migrations.pin.json +2 −1
@@ -51,5 +51,6 @@
5151 "cf6972801626419644e27b9bc4e6d338367e1c6fe67bb5ca338dc0c9bb408fc1",
5252 "fd1b67b66fb8ff34326ca5cb86ab04678619dfea420f21d8f85b2d05c999907f",
5353 "f836a1da11c1f51a255301c4ee62c441612baada0345c1310f4149e5d5ccf5b9",
54 "72f67a8e8f06f9b9fcac58ff22eb3f4222c77a0968da5981d4e7dce8d4b8ce7d"
54 "72f67a8e8f06f9b9fcac58ff22eb3f4222c77a0968da5981d4e7dce8d4b8ce7d",
55 "166c5e5ec18272d556b87786396c5c3a17da8b56185643e8efeeb83742cc2b5b"
5556 ]
src/pages/clock.ts +2 −1
@@ -188,7 +188,8 @@ function render(input: {
188188 const overdue = open.filter((d) => urgency(d.due, on, false) === "overdue").length;
189189 const soon = open.filter((d) => urgency(d.due, on, false) === "soon").length;
190190 const { start, days } = span(deals, on);
191 const today = deals.map((deal) => reminderRows(deal, on, agent));
191 // A closed deal is not on the clock, but its check-in is a reminder for today.
192 const today = [...deals, ...closed].map((deal) => reminderRows(deal, on, agent));
192193 const todayCount = today.reduce((n, t) => n + t.count, 0);
193194 const none = (q !== "" || side !== "") && found.length === 0;
194195 const toSend = today.reduce((n, t) => n + t.toSend, 0);
src/pages/deal.ts +14 −1
@@ -12,7 +12,7 @@ import { DEFAULT_LENGTHS, PARTY_LABEL, PARTY_ROLES, SUGGESTED_DATES } from "../d
1212 import { escape } from "../http.ts";
1313 import { closingLine } from "../closing.ts";
1414 import { commission, dollars, percent } from "../money.ts";
15 import { drafts, sentLabel, statusDrafts, type Agent } from "../reminders.ts";
15 import { checkinDrafts, drafts, sentLabel, statusDrafts, type Agent } from "../reminders.ts";
1616 import type { Who } from "../who.ts";
1717 import { amendForm, type AmendState } from "./amend.ts";
1818 import { partyContact } from "./contact.ts";
@@ -96,6 +96,18 @@ function statusCard(deal: DealView, agent: Agent, on: string, may: boolean, to:
9696 </section>`;
9797 }
9898
99 /** A closed deal's check-in: the message to each past client a month after the closing, waiting for the agent's word. */
100 function checkinCard(deal: DealView, agent: Agent, on: string, may: boolean, to: string): string {
101 // Offered on its day, or once it has a record (sent, declined, held); never before.
102 const these = checkinDrafts(deal, agent).filter((d) => d.on <= on || d.sent_at !== null || d.declined !== null || d.held !== null);
outdated

Filter to drafts that are due (d.on <= on) or already sent, declined or held; as written the card and its Send form appear the day the closing is marked done, 30 days early. Add a before/after-day-30 test on dealPage.

103 if (these.length === 0) return "";
104 return `<section class="card" id="checkin">
105 <h2>Check-in</h2>
106 <p class="muted small">A month after closing: how the move went, and a gentle ask for a referral. Nothing goes out until you send it.</p>
107 <ul class="drafts">${these.map((d) => draftItem(d, on, may, to)).join("")}</ul>
108 </section>`;
109 }
110
99111 /** The record of what went out: each sent reminder with the words approved, newest first. */
100112 function messages(deal: DealView, may: boolean, agent: Agent, to: string): string {
101113 const holding = drafts(deal, agent).flatMap((d) => (d.held === null ? [] : [{ d, held: d.held }]));
@@ -347,6 +359,7 @@ export function dealPage(input: {
347359 ${parties}
348360 </section>
349361 ${statusCard(deal, agent ?? defaults, on, may, path)}
362 ${checkinCard(deal, agent ?? defaults, on, may, path)}
350363 <section class="card" id="messages">
351364 <h2>Messages</h2>
352365 ${messages(deal, may, agent ?? defaults, path)}
src/pages/sheet.ts +3 −1
@@ -182,7 +182,9 @@ export function draftItem(draft: Draft, on: string, may: boolean, to: string): s
182182 * for, where the draft is edited and sent. The clock carries no forms.
183183 */
184184 export const draftLink = (draft: Draft, on: string): string =>
185 `<li class="draft"><a class="draft-open" href="/deals/${draft.deal_id}#sheet-${draft.deadline_id}" data-sheet="sheet-${draft.deadline_id}" data-sheets="/deals/${draft.deal_id}/sheets">${draftLine(draft, on, true)}</a></li>`;
185 draft.moment === "checkin"
186 ? `<li class="draft"><a class="draft-open" href="/deals/${draft.deal_id}#checkin">${draftLine(draft, on, true)}</a></li>`
187 : `<li class="draft"><a class="draft-open" href="/deals/${draft.deal_id}#sheet-${draft.deadline_id}" data-sheet="sheet-${draft.deadline_id}" data-sheets="/deals/${draft.deal_id}/sheets">${draftLine(draft, on, true)}</a></li>`;
186188
187189 export function sheet(deal: DealView, d: DeadlineView, who: Who, on: string, to: string, agent: Agent = defaults, typed?: { id: number; note: string }): string {
188190 const id = sheetId(d);
src/reminders.ts +58 −3
@@ -10,6 +10,7 @@
1010
1111 import { addDays, relative, short, spoken, today, weekday } from "./dates.ts";
1212 import { closingAfterDate } from "./closing.ts";
13 import { checkinOn, checkinRoles, checkinWords } from "./checkin.ts";
1314 import { dollars } from "./money.ts";
1415 import { memoizer, print } from "./memo.ts";
1516 import { DEFAULT_LEAD, isOpen, KIND, PARTY_LABEL, type PartyRole } from "./deadlines.ts";
@@ -59,6 +60,7 @@ export interface Draft {
5960
6061 export const MOMENTS: readonly Moment[] = ["before", "day_of", "late", "moved"];
6162 const MOMENT_LABEL: Readonly<Record<Exclude<Moment, "before">, string>> = {
63 checkin: "Check-in",
6264 status: "Status update",
6365 day_of: "Morning of",
6466 late: "Overdue follow-up",
@@ -186,8 +188,12 @@ function words(
186188 };
187189 }
188190
189 /** Every reminder an open date of this deal has, sent or not; a closed deal reminds no one. */
191 /**
192 * Every reminder an open date of this deal has, sent or not; a closed deal
193 * reminds no one of a date, but has its check-ins a month after the closing.
194 */
190195 export function drafts(deal: DealView, agent: Agent): readonly Draft[] {
196 if (deal.closed && !deal.fell_through) return checkinDrafts(deal, agent);
191197 if (deal.closed || deal.fell_through) return [];
192198 return remembered(`${JSON.stringify(agent)}|${print(deal)}`, () =>
193199 deal.deadlines
@@ -243,6 +249,54 @@ export function draftsFor(deal: DealView, deadline: DeadlineView, agent: Agent):
243249 );
244250 }
245251
252 // ---- the check-in: a month after the closing, to the clients ----
253
254 /**
255 * The check-in each client with a contact is owed once the closing has been
256 * marked done: the buyers, the sellers or both, by the deal's side, the
257 * morning 30 days after that day. Keyed by the closing and the day it was
258 * marked done, so a deal reopened and closed again is a new check-in; a
259 * reopened deal, or one that fell through, has none.
260 */
261 export function checkinDrafts(deal: DealView, agent: Agent): Draft[] {
262 const closing = deal.deadlines.find((d) => d.kind === "closing");
263 if (!deal.closed || deal.fell_through || closing === undefined || closing.done_on === null) return [];
264 const doneOn = closing.done_on;
265 const roles = checkinRoles(deal.side);
266 return deal.parties
267 .filter((p) => roles.includes(p.role) && p.contact !== "")
268 .map((party) => {
269 const mine = (s: { deadline_id: number; party_id: number; moment: Moment; due: string; move_id: number }): boolean =>
270 s.deadline_id === closing.id && s.party_id === party.id && s.moment === "checkin" && s.due === doneOn && s.move_id === 0;
271 const sent = deal.sent.find(mine);
272 const declined = deal.declined.find(mine);
273 const held = sent === undefined && declined === undefined ? deal.held.find(mine) : undefined;
274 const drafted = checkinWords(deal.address, first(party.name), signOff(agent));
275 return {
276 deal_id: deal.id,
277 address: deal.address,
278 deadline_id: closing.id,
279 label: closing.label,
280 due: doneOn,
281 party_id: party.id,
282 party_name: party.name,
283 party_role: party.role,
284 contact: party.contact,
285 email: party.email,
286 phone: party.phone,
287 moment: "checkin" as const,
288 lead_days: closing.lead_days,
289 move_id: 0,
290 on: checkinOn(doneOn),
291 subject: sent !== undefined && sent.subject !== "" ? sent.subject : held !== undefined && held.subject !== "" ? held.subject : drafted.subject,
292 body: sent !== undefined && sent.subject !== "" ? sent.body : held !== undefined && held.body !== "" ? held.body : drafted.body,
293 sent_at: sent?.sent_at ?? null,
294 declined: declined === undefined ? null : { reason: declined.reason, by_name: declined.by_name },
295 held: held === undefined ? null : { until: held.until, from: held.held_on, by_name: held.by_name },
296 };
297 });
298 }
299
246300 // ---- the status update: one message about the deal as a whole ----
247301
248302 /** At most this many open dates are named as what comes next. */
@@ -331,6 +385,7 @@ export function statusDrafts(deal: DealView, agent: Agent): Draft[] {
331385
332386 /** The draft of `moment` that `party` has on `deadline`, if any: a reminder, or the deal's status update on its closing date. */
333387 export function draftOfMoment(deal: DealView, deadline: DeadlineView, agent: Agent, party: Party, moment: Moment): Draft | undefined {
388 if (moment === "checkin") return deadline.kind === "closing" ? checkinDrafts(deal, agent).find((r) => r.party_id === party.id) : undefined;
334389 if (moment !== "status" && !isOpen(deadline)) return undefined;
335390 const these = moment === "status" ? (deadline.kind === "closing" ? statusDrafts(deal, agent) : []) : draftsFor(deal, deadline, agent);
336391 return these.find((r) => r.party_id === party.id && r.moment === moment);
@@ -350,8 +405,8 @@ export const dueOn = (deals: readonly DealView[], day: string, agent: Agent): Dr
350405 */
351406 export const pending = (d: Draft, day: string): boolean =>
352407 d.held !== null
353 ? d.held.until <= day && (d.on === day || d.due >= day || d.moment === "late")
354 : d.on === day || (d.on < day && unsent(d) && (d.due >= day || d.moment === "late"));
408 ? d.held.until <= day && (d.on === day || d.due >= day || d.moment === "late" || d.moment === "checkin")
409 : d.on === day || (d.on < day && unsent(d) && (d.due >= day || d.moment === "late" || d.moment === "checkin"));
355410
356411 /**
357412 * The drafts waiting on `day` across these deals, in the order the clock lists
src/routes.ts +12 −8
@@ -320,7 +320,7 @@ export const DEAL: Schema = {
320320 id: { type: "integer" },
321321 deadline_id: { type: "integer" },
322322 party_id: { type: "integer" },
323 moment: { enum: ["before", "day_of", "late", "moved", "status"] },
323 moment: { enum: ["before", "day_of", "late", "moved", "status", "checkin"] },
324324 due: DATE,
325325 reason: { type: "string", description: "Why the agent chose not to send it." },
326326 by_name: { type: "string" },
@@ -341,7 +341,7 @@ export const DEAL: Schema = {
341341 label: { type: "string", description: "The date it was about, as named when sent." },
342342 party_name: { type: "string" },
343343 party_role: { type: "string", description: "The party's role when sent; empty on a reminder sent before roles were kept." },
344 moment: { enum: ["before", "day_of", "late", "moved", "status"] },
344 moment: { enum: ["before", "day_of", "late", "moved", "status", "checkin"] },
345345 due: DATE,
346346 by_name: { type: "string" },
347347 sent_at: { type: "string" },
@@ -404,7 +404,11 @@ export const DRAFT: Schema = {
404404 email: { type: "string" },
405405 phone: { type: "string" },
406406 contact: CONTACT,
407 moment: { enum: ["before", "day_of", "late", "moved", "status"] },
407 moment: {
408 enum: ["before", "day_of", "late", "moved", "status", "checkin"],
409 description:
410 "Why it goes out. checkin is the \"How is the new home?\" message to a past client 30 days after the deal's closing was marked done (buyers on a buyer-side deal, sellers on a listing, both on a both-sides deal); it hangs on the closing's deadline_id, its due is the day the closing was marked done, and a reopened or fell-through deal has none.",
411 },
408412 on: DATE,
409413 subject: { type: "string" },
410414 body: { type: "string" },
@@ -1345,7 +1349,7 @@ export const routes: readonly Route[] = [
13451349 },
13461350 api: {
13471351 summary:
1348 "The reminders that go out today and have not been sent: call it each morning. It sends nothing and changes nothing.",
1352 "The reminders that go out today and have not been sent, including the check-in (moment checkin) owed to past clients 30 days after a closing was marked done: call it each morning. It sends nothing and changes nothing.",
13491353 answer: {
13501354 type: "object",
13511355 properties: { on: { type: "string", format: "date" }, reminders: list(DRAFT) },
@@ -1539,7 +1543,7 @@ export const routes: readonly Route[] = [
15391543 "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.",
15401544 body: {
15411545 type: "object",
1542 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved", "status"] }, reason: { type: "string", minLength: 1, maxLength: 200 } },
1546 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved", "status", "checkin"] }, reason: { type: "string", minLength: 1, maxLength: 200 } },
15431547 required: ["deadline_id", "party_id", "moment", "reason"],
15441548 },
15451549 answer: { type: "object", properties: { id: { type: "integer" }, reason: { type: "string" }, by_name: { type: "string" } }, required: ["id", "reason"] },
@@ -1560,7 +1564,7 @@ export const routes: readonly Route[] = [
15601564 "Hold a reminder until tomorrow: it leaves today's list and /tick, and comes back the next morning with its words. Give subject (up to 150 characters) and body (up to 2000) to hold the agent's edited words; leave them out to keep any words already held. Held again, it waits one more day. A status update is not held.",
15611565 body: {
15621566 type: "object",
1563 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved"] }, subject: { type: "string", minLength: 1, maxLength: 150 }, body: { type: "string", minLength: 1, maxLength: 2000 } },
1567 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved", "checkin"] }, subject: { type: "string", minLength: 1, maxLength: 150 }, body: { type: "string", minLength: 1, maxLength: 2000 } },
15641568 required: ["deadline_id", "party_id", "moment"],
15651569 },
15661570 answer: { type: "object", properties: { id: { type: "integer" }, until: { type: "string" }, by_name: { type: "string" } }, required: ["id", "until"] },
@@ -1580,7 +1584,7 @@ export const routes: readonly Route[] = [
15801584 summary: "Bring a held reminder back to today: the hold is deleted and the reminder returns to today's list and /tick. Refused as a conflict when it is not held, or was already sent or declined.",
15811585 body: {
15821586 type: "object",
1583 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved"] } },
1587 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved", "checkin"] } },
15841588 required: ["deadline_id", "party_id", "moment"],
15851589 },
15861590 answer: { type: "object", properties: { id: { type: "integer" } }, required: ["id"] },
@@ -1601,7 +1605,7 @@ export const routes: readonly Route[] = [
16011605 summary: "Take back a decline: the reminder is a draft again.",
16021606 body: {
16031607 type: "object",
1604 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved", "status"] } },
1608 properties: { deadline_id: { type: "integer" }, party_id: { type: "integer" }, moment: { enum: ["before", "day_of", "late", "moved", "status", "checkin"] } },
16051609 required: ["deadline_id", "party_id", "moment"],
16061610 },
16071611 answer: { type: "object", properties: { id: { type: "integer" } }, required: ["id"] },

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs