Public: anyone can read this; the crew can change it.
The day before a showing, a "see you tomorrow" confirmation naming the time and address waits on the desk for the agent to approve #79
Check · Review · Land · Live done — Landed and live in production.
mason/speed-to-lead:mason/73-the-day-before-a-showing-a-see-you-tomor → main 2 commits fork
10 files
changed +389
−34
README.md +7
| @@ -65,6 +65,13 @@ what you see right after a restart. Do step 1 first. | ||
| 65 | 65 | lead's plan as "After the showing". The sample **Rosa Alvarez** had a |
| 66 | 66 | showing yesterday, so her note is already waiting on a fresh desk. A |
| 67 | 67 | cancelled showing has none; a second showing gets a second note. |
| 68 | The day before, at 8:00 in your time zone, a "see you tomorrow" | |
| 69 | confirmation waits on the desk too: it names the showing's time and the | |
| 70 | address, and asks them to reply if the time no longer works (a showing | |
| 71 | booked less than a day ahead gets it at once). It is a draft you approve | |
| 72 | or edit, shows on the plan as "Before the showing", and moves or goes | |
| 73 | with the showing. The sample **Ethan Brooks** has a showing tomorrow, so | |
| 74 | his confirmation is on a fresh desk. | |
| 68 | 75 | A fresh desk also lists upcoming showings above the leads (soonest first, at most five, each linked to its lead): **Aisha Bello** has one in two days. |
| 69 | 76 | 8. Set your profile (admin to save). Press "Your profile" at the top, fill |
| 70 | 77 | in your name, brokerage, license number, phone and email, and save. Every |
src/before-showing.test.ts added +218
| @@ -0,0 +1,218 @@ | ||
| 1 | /** | |
| 2 | * The "see you tomorrow" confirmation the day before a showing: drafted for | |
| 3 | * the agent to approve from 8:00 the day before in their zone, at once when | |
| 4 | * the showing is closer than that, none once it has passed, one per showing. | |
| 5 | */ | |
| 6 | import { expect, test } from "bun:test"; | |
| 7 | import { mkdtempSync } from "node:fs"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { addLead, bookShowing, cancelShowing, getLead, sendReply, setState, optOutTexts, turn } from "./actions.ts"; | |
| 11 | import { openDb } from "./db.ts"; | |
| 12 | import { BEFORE_SHOWING_DAY, MAX_BODY, ANSWER_DAY, AFTER_SHOWING_DAY, dayLabel, confirmationDraft } from "./drafts.ts"; | |
| 13 | import { leadPage } from "./pages/lead.ts"; | |
| 14 | import { currentAgent, storeAgent } from "./profile.ts"; | |
| 15 | import { seed } from "./sample.ts"; | |
| 16 | import { morningBefore } from "./showings.ts"; | |
| 17 | import type { Who } from "./who.ts"; | |
| 18 | ||
| 19 | const alice: Who = { role: "user", id: "u1", name: "alice" }; | |
| 20 | const T0 = new Date("2026-03-01T10:00:00Z"); | |
| 21 | const DAY = 86_400_000; | |
| 22 | const at = (days: number) => new Date(T0.getTime() + days * DAY); | |
| 23 | ||
| 24 | const fresh = (zone = "UTC") => { | |
| 25 | const db = openDb(mkdtempSync(join(tmpdir(), "app-before-"))); | |
| 26 | storeAgent(db, { ...currentAgent(db), timeZone: zone }, "test", T0.toISOString()); | |
| 27 | return db; | |
| 28 | }; | |
| 29 | const value = <T>(r: { ok: boolean; value?: T; message?: string }): T => { | |
| 30 | if (!r.ok) throw new Error(r.message); | |
| 31 | return r.value as T; | |
| 32 | }; | |
| 33 | const answered = (db: ReturnType<typeof fresh>, consent = false, name = "Dana Reyes") => { | |
| 34 | const id = value( | |
| 35 | addLead(db, alice, { | |
| 36 | name, | |
| 37 | email: `${name.split(" ")[0]!.toLowerCase()}@example.com`, | |
| 38 | phone: `(206) 555-01${String(10 + name.length).padStart(2, "0")}`, | |
| 39 | source: "zillow", | |
| 40 | message: "Can I see 12 Oak Street?", | |
| 41 | consent, | |
| 42 | }, T0), | |
| 43 | ).id; | |
| 44 | value(sendReply(db, alice, { id }, T0)); | |
| 45 | return id; | |
| 46 | }; | |
| 47 | const confirmations = (db: ReturnType<typeof fresh>, id: number) => | |
| 48 | value(getLead(db, alice, { id })).messages.filter((m) => m.day >= BEFORE_SHOWING_DAY && m.day < AFTER_SHOWING_DAY); | |
| 49 | const book = (db: ReturnType<typeof fresh>, id: number, when: string, address = "12 Example St", now = at(0.5)) => | |
| 50 | value(bookShowing(db, alice, { id, when, address }, now)); | |
| 51 | ||
| 52 | test("the range sits between the cadence and the after-showing note", () => { | |
| 53 | expect(BEFORE_SHOWING_DAY).toBeLessThan(AFTER_SHOWING_DAY); | |
| 54 | expect(AFTER_SHOWING_DAY).toBeLessThan(ANSWER_DAY); | |
| 55 | expect(dayLabel(BEFORE_SHOWING_DAY)).toBe("Before the showing"); | |
| 56 | }); | |
| 57 | ||
| 58 | test("a draft from 8:00 the day before, naming the time and address, asking for a reply", () => { | |
| 59 | const db = fresh(); | |
| 60 | const id = answered(db); | |
| 61 | book(db, id, "2026-03-05T10:00"); | |
| 62 | const [m] = confirmations(db, id); | |
| 63 | expect(m!.due_at).toBe("2026-03-04T08:00:00.000Z"); | |
| 64 | expect(m!.status).toBe("scheduled"); | |
| 65 | turn(db, new Date("2026-03-04T07:59:00Z")); | |
| 66 | expect(confirmations(db, id)[0]!.status).toBe("scheduled"); | |
| 67 | turn(db, new Date("2026-03-04T08:00:00Z")); | |
| 68 | const [due] = confirmations(db, id); | |
| 69 | expect(due!.status).toBe("due"); | |
| 70 | expect(due!.body).toContain("Thursday, Mar 5 at 10:00 AM"); | |
| 71 | expect(due!.body).toContain("12 Example St"); | |
| 72 | expect(due!.body).toMatch(/reply/); | |
| 73 | expect(due!.signature).toContain(currentAgent(db).name); | |
| 74 | expect(value(getLead(db, alice, { id })).messages.filter((m) => m.status === "sent")).toHaveLength(1); | |
| 75 | }); | |
| 76 | ||
| 77 | test("the time is the agent's, and 8:00 is read in their zone", () => { | |
| 78 | expect(morningBefore("2026-03-05T10:00:00.000Z", "America/Chicago")).toBe("2026-03-04T14:00:00.000Z"); | |
| 79 | const db = fresh("America/Chicago"); | |
| 80 | const id = answered(db); | |
| 81 | book(db, id, "2026-03-05T10:00"); | |
| 82 | expect(confirmations(db, id)[0]!.body).toContain("10:00 AM"); | |
| 83 | expect(confirmations(db, id)[0]!.due_at).toBe("2026-03-04T14:00:00.000Z"); | |
| 84 | }); | |
| 85 | ||
| 86 | test("less than a day ahead it is due at once; a passed showing gets none", () => { | |
| 87 | const db = fresh(); | |
| 88 | const id = answered(db); | |
| 89 | book(db, id, "2026-03-01T18:00", "12 Example St", T0); | |
| 90 | const [m] = confirmations(db, id); | |
| 91 | expect(m!.status).toBe("due"); | |
| 92 | expect(m!.body).toContain("Sunday, Mar 1 at 6:00 PM"); | |
| 93 | const other = answered(db, false, "Eli Park"); | |
| 94 | db.query("INSERT INTO showing (lead_id, at, address, hold_until, booked_at) VALUES (?1, '2026-02-20T10:00:00.000Z', '', '2026-02-21T10:00:00.000Z', ?2)").run(other, T0.toISOString()); | |
| 95 | expect(confirmations(db, other)).toHaveLength(0); | |
| 96 | }); | |
| 97 | ||
| 98 | test("moving rewrites the unsent one, cancelling removes it, one per showing", () => { | |
| 99 | const db = fresh(); | |
| 100 | const id = answered(db); | |
| 101 | book(db, id, "2026-03-05T10:00"); | |
| 102 | book(db, id, "2026-03-09T15:30", "9 Moved Ln"); | |
| 103 | const list = confirmations(db, id); | |
| 104 | expect(list).toHaveLength(1); | |
| 105 | expect(list[0]!.due_at).toBe("2026-03-08T08:00:00.000Z"); | |
| 106 | expect(list[0]!.body).toContain("3:30 PM"); | |
| 107 | expect(list[0]!.body).toContain("9 Moved Ln"); | |
| 108 | value(cancelShowing(db, alice, { id }, at(1))); | |
| 109 | expect(confirmations(db, id)).toHaveLength(0); | |
| 110 | }); | |
| 111 | ||
| 112 | test("it is not held behind the showing's own hold, and the cadence hold still works", () => { | |
| 113 | const db = fresh(); | |
| 114 | const id = answered(db); | |
| 115 | book(db, id, "2026-03-05T10:00"); | |
| 116 | turn(db, new Date("2026-03-04T09:00:00Z")); | |
| 117 | const status = Object.fromEntries(value(getLead(db, alice, { id })).messages.map((m) => [m.day, m.status])); | |
| 118 | expect(status[BEFORE_SHOWING_DAY]).toBe("due"); | |
| 119 | expect(status[3]).toBe("scheduled"); | |
| 120 | expect(status[AFTER_SHOWING_DAY]).toBe("scheduled"); | |
| 121 | }); | |
| 122 | ||
| 123 | test("a sent one gives way to a second for a second showing; it survives a restart", () => { | |
| 124 | const dir = mkdtempSync(join(tmpdir(), "app-before-restart-")); | |
| 125 | const db = openDb(dir); | |
| 126 | storeAgent(db, { ...currentAgent(db), timeZone: "UTC" }, "test", T0.toISOString()); | |
| 127 | const id = answered(db); | |
| 128 | book(db, id, "2026-03-05T10:00"); | |
| 129 | turn(db, new Date("2026-03-04T08:00:00Z")); | |
| 130 | db.close(); | |
| 131 | const again = openDb(dir); | |
| 132 | expect(confirmations(again, id)).toHaveLength(1); | |
| 133 | again.query("UPDATE message SET status = 'sent', sent_at = ?1 WHERE day = ?2").run("2026-03-04T09:00:00.000Z", BEFORE_SHOWING_DAY); | |
| 134 | value(bookShowing(again, alice, { id, when: "2026-03-12T10:00", address: "7 Second Ave" }, new Date("2026-03-05T12:00:00Z"))); | |
| 135 | expect(confirmations(again, id).map((m) => m.status)).toEqual(["sent", "scheduled"]); | |
| 136 | }); | |
| 137 | ||
| 138 | test("channelFor decides: text with consent, email after STOP", () => { | |
| 139 | const db = fresh(); | |
| 140 | const id = answered(db, true); | |
| 141 | book(db, id, "2026-03-05T10:00"); | |
| 142 | expect(confirmations(db, id)[0]!.channel).toBe("text"); | |
| 143 | expect(confirmations(db, id)[0]!.signature).toContain("STOP"); | |
| 144 | value(optOutTexts(db, alice, { id }, at(1))); | |
| 145 | expect(confirmations(db, id)[0]!.channel).toBe("email"); | |
| 146 | expect(confirmations(db, id)[0]!.body).toContain("12 Example St"); | |
| 147 | }); | |
| 148 | ||
| 149 | test("a qualified, lost or unreachable lead gets none", () => { | |
| 150 | const db = fresh(); | |
| 151 | const id = answered(db); | |
| 152 | book(db, id, "2026-03-05T10:00"); | |
| 153 | value(setState(db, alice, { id, state: "qualified" }, at(1))); | |
| 154 | expect(confirmations(db, id).map((m) => m.status)).toEqual(["skipped"]); | |
| 155 | turn(db, new Date("2026-03-04T09:00:00Z")); | |
| 156 | expect(confirmations(db, id).map((m) => m.status)).toEqual(["skipped"]); | |
| 157 | value(setState(db, alice, { id, state: "open" }, at(2))); | |
| 158 | expect(confirmations(db, id)).toHaveLength(1); | |
| 159 | expect(confirmations(db, id)[0]!.status).toBe("scheduled"); | |
| 160 | ||
| 161 | const nowhere = answered(db, false, "Quin Vale"); | |
| 162 | db.query("UPDATE lead SET email = '' WHERE id = ?1").run(nowhere); | |
| 163 | book(db, nowhere, "2026-03-05T10:00"); | |
| 164 | expect(confirmations(db, nowhere)).toHaveLength(0); | |
| 165 | }); | |
| 166 | ||
| 167 | test("it can be edited when sent, and the plan calls it 'Before the showing'", () => { | |
| 168 | const db = fresh(); | |
| 169 | const id = answered(db); | |
| 170 | book(db, id, "2026-03-05T10:00"); | |
| 171 | turn(db, new Date("2026-03-04T08:00:00Z")); | |
| 172 | const m = confirmations(db, id)[0]!; | |
| 173 | value(sendReply(db, alice, { id, body: "Hi Dana, see you tomorrow at ten!" }, new Date("2026-03-04T08:05:00Z"))); | |
| 174 | const lead = value(getLead(db, alice, { id })); | |
| 175 | expect(lead.messages.find((x) => x.id === m.id)!.body).toContain("see you tomorrow at ten"); | |
| 176 | const html = leadPage({ who: alice, agent: currentAgent(db), lead, mayUpdate: true, next: null, now: Date.parse("2026-03-04T08:05:00Z") }); | |
| 177 | expect(html).toContain("Before the showing"); | |
| 178 | }); | |
| 179 | ||
| 180 | test("a script-laden address is escaped and the text stays within its limit", () => { | |
| 181 | const db = fresh(); | |
| 182 | const id = answered(db, true); | |
| 183 | book(db, id, "2026-03-05T10:00", `<script>alert(1)</script>${"x".repeat(170)}`); | |
| 184 | expect(confirmations(db, id)[0]!.body.length).toBeLessThanOrEqual(MAX_BODY.text); | |
| 185 | const short = fresh(); | |
| 186 | const sid = answered(short, false); | |
| 187 | book(short, sid, "2026-03-05T10:00", "<script>alert(1)</script>"); | |
| 188 | turn(short, new Date("2026-03-04T08:00:00Z")); | |
| 189 | const lead = value(getLead(short, alice, { id: sid })); | |
| 190 | const html = leadPage({ who: alice, agent: currentAgent(short), lead, mayUpdate: true, next: null, now: Date.parse("2026-03-04T08:01:00Z") }); | |
| 191 | expect(html).not.toContain("<script>alert(1)"); | |
| 192 | expect(html).toContain("<script>alert(1)"); | |
| 193 | const long = confirmationDraft({ name: "A B", phone: "1", source: "zillow", message: "", consent: true }, "Thursday, Mar 5", "10:00 AM", "y".repeat(200) + "z".repeat(400)); | |
| 194 | expect(long.length).toBeLessThanOrEqual(MAX_BODY.text); | |
| 195 | }); | |
| 196 | ||
| 197 | test("the sample desk has a lead with a showing tomorrow and its confirmation", () => { | |
| 198 | const db = openDb(mkdtempSync(join(tmpdir(), "app-before-sample-"))); | |
| 199 | seed(db, new Date("2026-03-04T15:00:00Z")); | |
| 200 | const rows = db | |
| 201 | .query<{ name: string; body: string }, [number]>( | |
| 202 | "SELECT lead.name, message.body FROM message JOIN lead ON lead.id = message.lead_id WHERE message.day = ?1", | |
| 203 | ) | |
| 204 | .all(BEFORE_SHOWING_DAY); | |
| 205 | expect(rows.map((r) => r.name)).toContain("Ethan Brooks"); | |
| 206 | }); | |
| 207 | ||
| 208 | test("a confirmation not yet approved is withdrawn once its showing begins", () => { | |
| 209 | const db = fresh(); | |
| 210 | const id = answered(db); | |
| 211 | book(db, id, "2026-03-05T10:00"); | |
| 212 | turn(db, new Date("2026-03-04T09:00:00Z")); | |
| 213 | expect(confirmations(db, id)[0]!.status).toBe("due"); | |
| 214 | turn(db, new Date("2026-03-05T10:00:00Z")); | |
| 215 | expect(confirmations(db, id)[0]!.status).toBe("skipped"); | |
| 216 | const ready = value(getLead(db, alice, { id })).messages.filter((m) => m.status === "due"); | |
| 217 | expect(ready.map((m) => m.day)).not.toContain(BEFORE_SHOWING_DAY); | |
| 218 | }); |
src/drafts.ts +37 −1
| @@ -49,6 +49,14 @@ export const isAnswer = (day: number): boolean => day >= ANSWER_DAY; | ||
| 49 | 49 | * cadence day, so it meets neither: the `(lead, day)` pair stays unique per showing. |
| 50 | 50 | */ |
| 51 | 51 | export const AFTER_SHOWING_DAY = 500; |
| 52 | /** | |
| 53 | * The "see you tomorrow" confirmation before a showing is numbered from here | |
| 54 | * (400, 401, …, one per showing), above every cadence day and below | |
| 55 | * `AFTER_SHOWING_DAY`, so it meets neither. | |
| 56 | */ | |
| 57 | export const BEFORE_SHOWING_DAY = 400; | |
| 58 | export const isBeforeShowing = (day: number): boolean => | |
| 59 | day >= BEFORE_SHOWING_DAY && day < AFTER_SHOWING_DAY; | |
| 52 | 60 | export const isAfterShowing = (day: number): boolean => |
| 53 | 61 | day >= AFTER_SHOWING_DAY && day < ANSWER_DAY; |
| 54 | 62 | |
| @@ -60,7 +68,13 @@ export const dayLabel = (day: number): string => | ||
| 60 | 68 | ? "Answer to their reply" |
| 61 | 69 | : isAfterShowing(day) |
| 62 | 70 | ? "After the showing" |
| 63 | : `Day ${day} follow-up`; | |
| 71 | : isBeforeShowing(day) | |
| 72 | ? "Before the showing" | |
| 73 | : `Day ${day} follow-up`; | |
| 74 | ||
| 75 | /** A message's short name in the follow-up plan: "Day 7", "Before the showing". */ | |
| 76 | export const planLabel = (day: number): string => | |
| 77 | isBeforeShowing(day) ? "Before the showing" : isAfterShowing(day) ? "After the showing" : `Day ${day}`; | |
| 64 | 78 | |
| 65 | 79 | export interface Asker { |
| 66 | 80 | readonly name: string; |
| @@ -312,6 +326,28 @@ export function afterShowingDraft(lead: Asker, address: string): string { | ||
| 312 | 326 | return `Hi ${first},\n\nIt was a pleasure showing you around — ${asked(named)}\n\nBest,`; |
| 313 | 327 | } |
| 314 | 328 | |
| 329 | /** | |
| 330 | * The day before a showing: the time (as `time`, on the agent's clock) and | |
| 331 | * the address when there is one, and a way to say it no longer works. | |
| 332 | * `day` is the weekday and date ("Thursday, Mar 5"), not "tomorrow", so the | |
| 333 | * words stay true whenever the agent approves them. An address too long for a text is left out of it. | |
| 334 | */ | |
| 335 | export function confirmationDraft( | |
| 336 | lead: Asker, | |
| 337 | day: string, | |
| 338 | time: string, | |
| 339 | address: string, | |
| 340 | ): string { | |
| 341 | const first = firstName(lead.name); | |
| 342 | const line = (place: string): string => | |
| 343 | `I'll see you ${day} at ${time}${place === "" ? "" : ` at ${place}`}. If the time no longer works, just reply and we'll find another.`; | |
| 344 | if (channelFor(lead) === "text") { | |
| 345 | const text = `Hi ${first}, ${line(address)}`; | |
| 346 | return text.length <= MAX_BODY.text ? text : `Hi ${first}, ${line("")}`.slice(0, MAX_BODY.text); | |
| 347 | } | |
| 348 | return `Hi ${first},\n\nA quick note before your showing: ${line(address)}\n\nBest,`; | |
| 349 | } | |
| 350 | ||
| 315 | 351 | const AGREES = /\b(yes|yeah|yep|sure|sounds good|works for me|that works|perfect|great)\b/i; |
| 316 | 352 | |
| 317 | 353 | /** |
src/leads.ts +103 −22
| @@ -15,6 +15,9 @@ import type { Database } from "bun:sqlite"; | ||
| 15 | 15 | import { |
| 16 | 16 | AFTER_SHOWING_DAY, |
| 17 | 17 | afterShowingDraft, |
| 18 | BEFORE_SHOWING_DAY, | |
| 19 | confirmationDraft, | |
| 20 | isBeforeShowing, | |
| 18 | 21 | ANSWER_DAY, |
| 19 | 22 | answerDraft, |
| 20 | 23 | CADENCE, |
| @@ -31,7 +34,7 @@ import { GREEN_MS } from "./clock.ts"; | ||
| 31 | 34 | import type { Agent } from "./env.ts"; |
| 32 | 35 | import { isQuiet, nextOpening } from "./quiet.ts"; |
| 33 | 36 | import { type LostReason } from "./lost.ts"; |
| 34 | import { holdUntil, morningAfter, type Showing } from "./showings.ts"; | |
| 37 | import { clockTime, dayName, holdUntil, morningAfter, morningBefore, type Showing } from "./showings.ts"; | |
| 35 | 38 | |
| 36 | 39 | export type State = "new" | "contacted" | "qualified" | "lost"; |
| 37 | 40 | export const CLOSED: readonly State[] = ["qualified", "lost"]; |
| @@ -717,7 +720,7 @@ function respace(db: Database, id: number, at: string): void { | ||
| 717 | 720 | .query<{ id: number; day: number }, [number, number]>( |
| 718 | 721 | "SELECT id, day FROM message WHERE lead_id = ?1 AND status = 'scheduled' AND day < ?2", |
| 719 | 722 | ) |
| 720 | .all(id, AFTER_SHOWING_DAY); | |
| 723 | .all(id, BEFORE_SHOWING_DAY); | |
| 721 | 724 | for (const m of waiting) |
| 722 | 725 | db.query("UPDATE message SET due_at = ?1 WHERE id = ?2").run( |
| 723 | 726 | later(at, m.day), |
| @@ -796,16 +799,17 @@ export function redraftUnsent(db: Database, agent: Agent): void { | ||
| 796 | 799 | })(); |
| 797 | 800 | } |
| 798 | 801 | |
| 799 | /** The words for an unsent touch or after-showing note: the cadence's, or the note for the lead's showing as it stands. */ | |
| 800 | const wordsFor = (db: Database, found: Lead, day: number, agent: Agent): string => | |
| 801 | isAfterShowing(day) | |
| 802 | ? afterShowingDraft( | |
| 803 | found, | |
| 804 | db | |
| 805 | .query<{ address: string }, [number]>("SELECT address FROM showing WHERE lead_id = ?1") | |
| 806 | .get(found.id)?.address ?? "", | |
| 807 | ) | |
| 808 | : draft(found, day, agent); | |
| 802 | /** The words for an unsent touch or showing note: the cadence's, or the note for the lead's showing as it stands. */ | |
| 803 | const wordsFor = (db: Database, found: Lead, day: number, agent: Agent): string => { | |
| 804 | if (!isAfterShowing(day) && !isBeforeShowing(day)) return draft(found, day, agent); | |
| 805 | const showing = showingOf(db, found.id); | |
| 806 | if (isAfterShowing(day)) return afterShowingDraft(found, showing?.address ?? ""); | |
| 807 | return showing === null ? "" : confirmation(found, showing.at, showing.address, agent); | |
| 808 | }; | |
| 809 | ||
| 810 | /** The confirmation for the showing at `at`, naming its weekday and date so it reads true whenever it is approved. */ | |
| 811 | const confirmation = (found: Lead, at: string, address: string, agent: Agent): string => | |
| 812 | confirmationDraft(found, dayName(at, agent.timeZone), clockTime(at, agent.timeZone), address); | |
| 809 | 813 | |
| 810 | 814 | /** The answer to the reply it was drafted for, drafted again for the lead as they are now. */ |
| 811 | 815 | const answerAgain = (db: Database, found: Lead, day: number): string => |
| @@ -933,7 +937,7 @@ export function reopen(db: Database, found: Lead, at: string, agent: Agent): voi | ||
| 933 | 937 | const channel = channelFor(found); |
| 934 | 938 | const skipped = db |
| 935 | 939 | .query<{ id: number; day: number; due_at: string }, [number, number, number]>( |
| 936 | "SELECT id, day, due_at FROM message WHERE lead_id = ?1 AND status = 'skipped' AND skipped_by_hand = 0 AND day < ?2 AND NOT (day = 0 AND ?3 = 1)", | |
| 940 | `SELECT id, day, due_at FROM message WHERE lead_id = ?1 AND status = 'skipped' AND skipped_by_hand = 0 AND day < ?2 AND NOT (day >= ${BEFORE_SHOWING_DAY} AND day < ${AFTER_SHOWING_DAY}) AND NOT (day = 0 AND ?3 = 1)`, | |
| 937 | 941 | ) |
| 938 | 942 | .all(found.id, ANSWER_DAY, replied ? 1 : 0); |
| 939 | 943 | for (const m of skipped) |
| @@ -948,6 +952,12 @@ export function reopen(db: Database, found: Lead, at: string, agent: Agent): voi | ||
| 948 | 952 | m.id, |
| 949 | 953 | ); |
| 950 | 954 | respace(db, found.id, at); |
| 955 | db.query( | |
| 956 | `DELETE FROM message WHERE lead_id = ?1 AND day >= ${BEFORE_SHOWING_DAY} AND day < ${AFTER_SHOWING_DAY} AND status = 'skipped'`, | |
| 957 | ).run(found.id); | |
| 958 | const showing = showingOf(db, found.id); | |
| 959 | if (showing !== null) | |
| 960 | confirmShowing(db, findLead(db, found.id)!, showing.at, showing.address, at, agent); | |
| 951 | 961 | })(); |
| 952 | 962 | } |
| 953 | 963 | |
| @@ -996,13 +1006,21 @@ export function restoreMessage(db: Database, message: Message, at: string): void | ||
| 996 | 1006 | |
| 997 | 1007 | /** Every scheduled touch whose day has come is due now. Returns the ids of the leads it touched. */ |
| 998 | 1008 | export function advance(db: Database, at: string, only?: number): number[] { |
| 1009 | // A confirmation not yet sent is no use once its showing has begun: set it aside. | |
| 1010 | db.query( | |
| 1011 | `UPDATE message SET status = 'skipped', goes_at = NULL | |
| 1012 | WHERE day >= ${BEFORE_SHOWING_DAY} AND day < ${AFTER_SHOWING_DAY} AND status IN ('draft', 'scheduled', 'due') | |
| 1013 | AND lead_id IN (SELECT lead_id FROM showing WHERE at <= ?1)`, | |
| 1014 | ).run(at); | |
| 999 | 1015 | return db |
| 1000 | 1016 | .query<{ lead_id: number }, [string, number]>( |
| 1001 | 1017 | `UPDATE message SET status = 'due' |
| 1002 | 1018 | WHERE status = 'scheduled' AND due_at <= ?1 AND (?2 = 0 OR id = ?2) |
| 1003 | 1019 | AND lead_id IN (SELECT id FROM lead WHERE state = 'contacted') |
| 1004 | AND (day >= ${AFTER_SHOWING_DAY} AND day < ${ANSWER_DAY} | |
| 1020 | AND (day >= ${BEFORE_SHOWING_DAY} AND day < ${ANSWER_DAY} | |
| 1005 | 1021 | OR lead_id NOT IN (SELECT lead_id FROM showing WHERE hold_until > ?1)) |
| 1022 | AND NOT (day >= ${BEFORE_SHOWING_DAY} AND day < ${AFTER_SHOWING_DAY} | |
| 1023 | AND lead_id IN (SELECT lead_id FROM showing WHERE at <= ?1)) | |
| 1006 | 1024 | AND lead_id NOT IN (SELECT lead_id FROM message WHERE day >= ${ANSWER_DAY} AND status IN ('draft', 'held')) |
| 1007 | 1025 | RETURNING lead_id`, |
| 1008 | 1026 | ) |
| @@ -1029,9 +1047,10 @@ export function bookShowing( | ||
| 1029 | 1047 | ON CONFLICT (lead_id) DO UPDATE SET at = ?2, address = ?3, hold_until = ?4, booked_at = ?5`, |
| 1030 | 1048 | ).run(found.id, when, address, holdUntil(when), at); |
| 1031 | 1049 | db.query( |
| 1032 | "UPDATE message SET status = 'scheduled' WHERE lead_id = ?1 AND status = 'due'", | |
| 1050 | `UPDATE message SET status = 'scheduled' WHERE lead_id = ?1 AND status = 'due' AND NOT (day >= ${BEFORE_SHOWING_DAY} AND day < ${AFTER_SHOWING_DAY})`, | |
| 1033 | 1051 | ).run(found.id); |
| 1034 | 1052 | noteAfterShowing(db, found, when, address, agent); |
| 1053 | confirmShowing(db, found, when, address, at, agent); | |
| 1035 | 1054 | db.query("UPDATE lead SET updated_at = ?1 WHERE id = ?2").run(at, found.id); |
| 1036 | 1055 | })(); |
| 1037 | 1056 | } |
| @@ -1052,31 +1071,93 @@ function noteAfterShowing( | ||
| 1052 | 1071 | when: string, |
| 1053 | 1072 | address: string, |
| 1054 | 1073 | agent: Agent, |
| 1074 | ): void { | |
| 1075 | placeShowingMessage( | |
| 1076 | db, | |
| 1077 | found, | |
| 1078 | AFTER_SHOWING_DAY, | |
| 1079 | ANSWER_DAY, | |
| 1080 | () => afterShowingDraft(found, address), | |
| 1081 | morningAfter(when, agent.timeZone), | |
| 1082 | agent, | |
| 1083 | ); | |
| 1084 | } | |
| 1085 | ||
| 1086 | /** | |
| 1087 | * The "see you tomorrow" confirmation for the showing at `when`: due at 8:00 | |
| 1088 | * the day before in the agent's zone, or at once (`at`) when the showing is | |
| 1089 | * less than that far off; none for a showing already past. Moving the | |
| 1090 | * showing rewrites the unsent one. It is a draft until the agent approves it. | |
| 1091 | */ | |
| 1092 | function confirmShowing( | |
| 1093 | db: Database, | |
| 1094 | found: Lead, | |
| 1095 | when: string, | |
| 1096 | address: string, | |
| 1097 | at: string, | |
| 1098 | agent: Agent, | |
| 1099 | ): void { | |
| 1100 | const open = found.state !== "qualified" && found.state !== "lost"; | |
| 1101 | const ahead = Date.parse(when) > Date.parse(at); | |
| 1102 | const due = morningBefore(when, agent.timeZone); | |
| 1103 | const goes = due > at ? due : at; | |
| 1104 | placeShowingMessage( | |
| 1105 | db, | |
| 1106 | found, | |
| 1107 | BEFORE_SHOWING_DAY, | |
| 1108 | AFTER_SHOWING_DAY, | |
| 1109 | () => confirmation(found, when, address, agent), | |
| 1110 | goes, | |
| 1111 | agent, | |
| 1112 | !open || !ahead, | |
| 1113 | ); | |
| 1114 | const mine = db | |
| 1115 | .query<{ id: number }, [number, number, number]>( | |
| 1116 | "SELECT id FROM message WHERE lead_id = ?1 AND day >= ?2 AND day < ?3 AND status = 'scheduled' ORDER BY day DESC LIMIT 1", | |
| 1117 | ) | |
| 1118 | .get(found.id, BEFORE_SHOWING_DAY, AFTER_SHOWING_DAY); | |
| 1119 | if (mine !== null) advance(db, at, mine.id); | |
| 1120 | } | |
| 1121 | ||
| 1122 | /** | |
| 1123 | * Write the one message a showing owns in the days `from`..`end`: an unsent | |
| 1124 | * one is rewritten, one approved and held is left alone, and one already | |
| 1125 | * sent gives way to a new one on the next day. `none` (or a lead who can be | |
| 1126 | * reached nowhere) removes the unsent one instead. | |
| 1127 | */ | |
| 1128 | function placeShowingMessage( | |
| 1129 | db: Database, | |
| 1130 | found: Lead, | |
| 1131 | from: number, | |
| 1132 | end: number, | |
| 1133 | words: (due: string) => string, | |
| 1134 | due: string, | |
| 1135 | agent: Agent, | |
| 1136 | none = false, | |
| 1055 | 1137 | ): void { |
| 1056 | 1138 | const last = db |
| 1057 | 1139 | .query<{ id: number; day: number; status: string }, [number, number, number]>( |
| 1058 | 1140 | "SELECT id, day, status FROM message WHERE lead_id = ?1 AND day >= ?2 AND day < ?3 ORDER BY day DESC LIMIT 1", |
| 1059 | 1141 | ) |
| 1060 | .get(found.id, AFTER_SHOWING_DAY, ANSWER_DAY); | |
| 1142 | .get(found.id, from, end); | |
| 1061 | 1143 | // Approved and held for 8:00 (quiet hours): it goes as approved, and a moved showing does not get a second. |
| 1062 | 1144 | if (last?.status === "held") return; |
| 1063 | 1145 | const unsent = last !== null && UNSENT.includes(last.status); |
| 1064 | if (!reachable(found)) { | |
| 1146 | if (none || !reachable(found)) { | |
| 1065 | 1147 | if (unsent) db.query("DELETE FROM message WHERE id = ?1").run(last.id); |
| 1066 | 1148 | return; |
| 1067 | 1149 | } |
| 1068 | 1150 | const channel = channelFor(found); |
| 1069 | const body = afterShowingDraft(found, address); | |
| 1151 | const body = words(due); | |
| 1070 | 1152 | const sig = signature(channel, agent); |
| 1071 | const due = morningAfter(when, agent.timeZone); | |
| 1072 | 1153 | if (unsent) { |
| 1073 | 1154 | db.query( |
| 1074 | 1155 | "UPDATE message SET status = 'scheduled', skipped_by_hand = 0, channel = ?1, body = ?2, signature = ?3, due_at = ?4 WHERE id = ?5", |
| 1075 | 1156 | ).run(channel, body, sig, due, last.id); |
| 1076 | 1157 | return; |
| 1077 | 1158 | } |
| 1078 | const day = last === null ? AFTER_SHOWING_DAY : last.day + 1; | |
| 1079 | if (day >= ANSWER_DAY) return; | |
| 1159 | const day = last === null ? from : last.day + 1; | |
| 1160 | if (day >= end) return; | |
| 1080 | 1161 | db.query( |
| 1081 | 1162 | "INSERT INTO message (lead_id, day, channel, body, signature, status, due_at) VALUES (?1, ?2, ?3, ?4, ?5, 'scheduled', ?6)", |
| 1082 | 1163 | ).run(found.id, day, channel, body, sig, due); |
| @@ -1091,7 +1172,7 @@ export function cancelShowing(db: Database, found: Lead, at: string): void { | ||
| 1091 | 1172 | db.transaction(() => { |
| 1092 | 1173 | db.query("DELETE FROM showing WHERE lead_id = ?1").run(found.id); |
| 1093 | 1174 | db.query( |
| 1094 | `DELETE FROM message WHERE lead_id = ?1 AND day >= ${AFTER_SHOWING_DAY} AND day < ${ANSWER_DAY} AND status IN ${UNSENT_SQL}`, | |
| 1175 | `DELETE FROM message WHERE lead_id = ?1 AND day >= ${BEFORE_SHOWING_DAY} AND day < ${ANSWER_DAY} AND status IN ${UNSENT_SQL}`, | |
| 1095 | 1176 | ).run(found.id); |
| 1096 | 1177 | db.query("UPDATE lead SET updated_at = ?1 WHERE id = ?2").run(at, found.id); |
| 1097 | 1178 | advance(db, at); |
src/pages/desk.ts +2 −2
| @@ -7,7 +7,7 @@ import { day, clock, consent, source, STATE_LABEL } from "./parts.ts"; | ||
| 7 | 7 | import { deskTitle, layout } from "./layout.ts"; |
| 8 | 8 | import { escape } from "../http.ts"; |
| 9 | 9 | import { recordLine, sourceLine } from "../clock.ts"; |
| 10 | import { isAfterShowing, isAnswer } from "../drafts.ts"; | |
| 10 | import { isAfterShowing, isBeforeShowing, isAnswer } from "../drafts.ts"; | |
| 11 | 11 | import { goesLabel, RECORD_NOTE } from "../quiet.ts"; |
| 12 | 12 | import { MAX_LISTED, showingWhen } from "../showings.ts"; |
| 13 | 13 | import { MAX_EMAIL_TEXT, missingLine } from "../intake.ts"; |
| @@ -186,7 +186,7 @@ function status(lead: DeskLead, now: number, zone: string): string { | ||
| 186 | 186 | if (lead.ready !== null && isAnswer(lead.ready.day)) |
| 187 | 187 | return `<span class="status" data-ready>They wrote back — answer drafted</span>`; |
| 188 | 188 | if (lead.ready !== null) |
| 189 | return `<span class="status" data-ready>${isAfterShowing(lead.ready.day) ? "After the showing — note ready" : `Day ${lead.ready.day} follow-up ready`}</span>`; | |
| 189 | return `<span class="status" data-ready>${isBeforeShowing(lead.ready.day) ? "Before the showing — confirmation ready" : isAfterShowing(lead.ready.day) ? "After the showing — note ready" : `Day ${lead.ready.day} follow-up ready`}</span>`; | |
| 190 | 190 | if (lead.next_at !== null) |
| 191 | 191 | return `<span class="status">Next follow-up ${escape(day(held(lead, lead.next_at), zone))}</span>`; |
| 192 | 192 | return `<span class="status">Follow-ups done</span>`; |
src/pages/lead.ts +2 −2
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import { callLine, MAX_NOTE, OUTCOME_LABEL, OUTCOMES } from "../calls.ts"; |
| 8 | import { channelFor, dayLabel, isAfterShowing, isAnswer, MAX_BODY } from "../drafts.ts"; | |
| 8 | import { channelFor, dayLabel, isAnswer, planLabel, MAX_BODY } from "../drafts.ts"; | |
| 9 | 9 | import { escape } from "../http.ts"; |
| 10 | 10 | import { handoff } from "../handoff.ts"; |
| 11 | 11 | import { MAX_ADDRESS, MAX_AHEAD_DAYS, localInput, showingLine, showingWhen } from "../showings.ts"; |
| @@ -170,7 +170,7 @@ export function leadPage(input: { | ||
| 170 | 170 | }<ol class="plan">${cadence |
| 171 | 171 | .map( |
| 172 | 172 | (m) => `<li data-status="${m.status}"> |
| 173 | <span class="plan-day">${isAfterShowing(m.day) ? "After the showing" : `Day ${m.day}`}</span> | |
| 173 | <span class="plan-day">${planLabel(m.day)}</span> | |
| 174 | 174 | <span class="plan-when">${escape(day(m.sent_at ?? m.due_at, agent.timeZone))}</span> |
| 175 | 175 | <span class="plan-status">${waits(m) ? "Waits for the showing" : PLAN[m.status]}</span> |
| 176 | 176 | ${m.status === "scheduled" ? `<details class="peek"><summary>Preview</summary>${words(m)}</details>` : ""} |
src/sample.ts +1 −1
| @@ -76,7 +76,7 @@ export const SAMPLES: readonly Sample[] = [ | ||
| 76 | 76 | person("Lena Novak", "open_house", false, "Signed in at the open house on Birch St. Could you send me the disclosures and HOA details?", { ago: 26 * H }), |
| 77 | 77 | person("Marcus Lee", "zillow", true, "Can I tour 92 Harbor View Dr this week? Available most evenings after 6.", { ago: 2 * D + 3 * H, answeredIn: 48 * S, touched: [1], note: "Works nights: call after 6. Cash buyer, relocating from Austin; wants a short closing." }), |
| 78 | 78 | person("Rosa Alvarez", "referral", false, "My cousin Elena recommended you. We need a bigger place before the baby comes in March.", { ago: 4 * D + 2 * H, answeredIn: 3 * M + 20 * S, touched: [1], showedAgo: D }), |
| 79 | person("Ethan Brooks", "realtor_com", true, "What would my 2-bed condo on Seaview Ave sell for? Considering upsizing next year.", { ago: 8 * D + 5 * H, answeredIn: 2 * M + 5 * S, touched: [1, 3] }), | |
| 79 | person("Ethan Brooks", "realtor_com", true, "What would my 2-bed condo on Seaview Ave sell for? Considering upsizing next year.", { ago: 8 * D + 5 * H, answeredIn: 2 * M + 5 * S, touched: [1, 3], showingInDays: 1 }), | |
| 80 | 80 | person("Grace Kim", "zillow", false, "Is the price on 7 Larkspur Ct negotiable? We're pre-approved and ready to move.", { ago: 15 * D, answeredIn: 1 * M + 30 * S, touched: [1, 3, 7], closed: { state: "qualified" } }), |
| 81 | 81 | person("Hannah Wright", "open_house", false, "Thanks for the tour of the Maple Ave home. Still deciding whether we want to stay in the city.", { ago: 31 * D, answeredIn: 4 * M + 15 * S, touched: [1, 3, 7, 14] }), |
| 82 | 82 | person("Owen Hart", "zillow", false, "Is 55 Cedar Ln still on the market? We'd like a second showing.", { ago: 9 * D, answeredIn: 2 * M + 40 * S, touched: [1, 3], closed: { state: "lost", reason: "bought_elsewhere" } }), |
src/showings.test.ts +2 −1
| @@ -171,7 +171,8 @@ test("follow-ups due before the showing wait until the day after; cancelling rel | ||
| 171 | 171 | value(bookShowing(db, alice, { id, when: "2026-03-06T10:00" }, at(0.5))); |
| 172 | 172 | turn(db, at(4)); |
| 173 | 173 | expect(statuses(db, id)).toMatchObject({ 1: "scheduled", 3: "scheduled" }); |
| 174 | expect(value(listLeads(db, alice, { show: "needs" })).leads).toHaveLength(0); | |
| 174 | // Only the day-before confirmation is ready; no follow-up is. | |
| 175 | expect(value(getLead(db, alice, { id })).messages.filter((m) => m.status === "due").map((m) => m.day)).toEqual([400]); | |
| 175 | 176 | turn(db, at(5.2)); // the showing's day: still held |
| 176 | 177 | expect(statuses(db, id)[1]).toBe("scheduled"); |
| 177 | 178 | turn(db, at(6.1)); // the day after the showing |
src/showings.ts +12
| @@ -131,6 +131,18 @@ export function wallAt(ms: number, days: number, hour: number, zone: string): nu | ||
| 131 | 131 | export const morningAfter = (at: string, zone: string): string => |
| 132 | 132 | new Date(wallAt(Date.parse(at), 1, 8, zone) ?? Date.parse(at) + DAY_MS).toISOString(); |
| 133 | 133 | |
| 134 | /** When the confirmation before a showing at `at` comes due: 8:00 the day before in `zone`, the agent's. */ | |
| 135 | export const morningBefore = (at: string, zone: string): string => | |
| 136 | new Date(wallAt(Date.parse(at), -1, 8, zone) ?? Date.parse(at) - 2 * DAY_MS / 3).toISOString(); | |
| 137 | ||
| 138 | /** The time of day of `at` on the wall clock of `zone`: "10:00 AM". */ | |
| 139 | export const clockTime = (at: string, zone: string): string => | |
| 140 | new Date(at).toLocaleTimeString("en-US", { hour: "numeric", minute: "2-digit", timeZone: zone }); | |
| 141 | ||
| 142 | /** The day of `at` on the wall clock of `zone`: "Thursday, Mar 5". */ | |
| 143 | export const dayName = (at: string, zone: string): string => | |
| 144 | new Date(at).toLocaleDateString("en-US", { weekday: "long", month: "short", day: "numeric", timeZone: zone }); | |
| 145 | ||
| 134 | 146 | /** In `zone`, the agent's: "Sat 10:00", or "Sat Dec 12 10:00" beyond the coming week. */ |
| 135 | 147 | export function showingWhen(at: string, now: number, zone: string): string { |
| 136 | 148 | const d = new Date(at); |
src/terminal.ts +5 −5
| @@ -59,7 +59,7 @@ import { between, elapsed, recordLine, sourceLine, tone } from "./clock.ts"; | ||
| 59 | 59 | import { permit } from "./contract.ts"; |
| 60 | 60 | import { goesLabel, isQuiet, nextOpening, RECORD_NOTE } from "./quiet.ts"; |
| 61 | 61 | import { said as saidOf } from "./sentences.ts"; |
| 62 | import { SOURCE_LABEL, SOURCES, channelFor, dayLabel, isAfterShowing, isAnswer, type Channel, type Source } from "./drafts.ts"; | |
| 62 | import { SOURCE_LABEL, SOURCES, channelFor, dayLabel, isAfterShowing, isAnswer, isBeforeShowing, planLabel, type Channel, type Source } from "./drafts.ts"; | |
| 63 | 63 | import { ssh, title, where } from "./env.ts"; |
| 64 | 64 | import { LOST_REASONS, REASON_LABEL, lostSaid } from "./lost.ts"; |
| 65 | 65 | import { answerSince, CLOSED, MAX_QUERY, VIEWS, queryOf, type DeskLead, type LeadDetail, type Message, type View as DeskView } from "./leads.ts"; |
| @@ -281,7 +281,7 @@ export function terminal( | ||
| 281 | 281 | ? "reply drafted" |
| 282 | 282 | : isAnswer(l.ready.day) |
| 283 | 283 | ? "wrote back · answer drafted" |
| 284 | : isAfterShowing(l.ready.day) ? "after the showing · note ready" : `day ${l.ready.day} follow-up ready`; | |
| 284 | : isBeforeShowing(l.ready.day) ? "before the showing · confirmation ready" : isAfterShowing(l.ready.day) ? "after the showing · note ready" : `day ${l.ready.day} follow-up ready`; | |
| 285 | 285 | const name = clean(l.name).padEnd(18).slice(0, 18); |
| 286 | 286 | const showing = |
| 287 | 287 | l.showing === null |
| @@ -415,7 +415,7 @@ export function terminal( | ||
| 415 | 415 | dim( |
| 416 | 416 | `${sent} sent · plan: ${l.messages |
| 417 | 417 | .filter((m) => m.day > 0 && !isAnswer(m.day)) |
| 418 | .map((m) => `${isAfterShowing(m.day) ? "after the showing" : `day ${m.day}`} ${m.status}`) | |
| 418 | .map((m) => `${planLabel(m.day).toLowerCase()} ${m.status}`) | |
| 419 | 419 | .join(", ") || "starts with the first reply"}`, |
| 420 | 420 | ), |
| 421 | 421 | "", |
| @@ -793,11 +793,11 @@ export function terminal( | ||
| 793 | 793 | } else if (input === "k" && skippable(l) !== undefined) { |
| 794 | 794 | const m = skippable(l)!; |
| 795 | 795 | const skipped = skipFollowUp(db, me, { id, messageId: m.id }); |
| 796 | said = skipped.ok ? `${isAfterShowing(m.day) ? "The note after the showing" : `Day ${m.day}`} skipped. The rest of the plan is as it was.` : skipped.message; | |
| 796 | said = skipped.ok ? `${isBeforeShowing(m.day) ? "The confirmation before the showing" : isAfterShowing(m.day) ? "The note after the showing" : `Day ${m.day}`} skipped. The rest of the plan is as it was.` : skipped.message; | |
| 797 | 797 | } else if (input === "K" && restorable(l) !== undefined) { |
| 798 | 798 | const m = restorable(l)!; |
| 799 | 799 | const back = restoreFollowUp(db, me, { id, messageId: m.id }); |
| 800 | said = back.ok ? `${isAfterShowing(m.day) ? "The note after the showing" : `Day ${m.day}`} is back in the plan.` : back.message; | |
| 800 | said = back.ok ? `${isBeforeShowing(m.day) ? "The confirmation before the showing" : isAfterShowing(m.day) ? "The note after the showing" : `Day ${m.day}`} is back in the plan.` : back.message; | |
| 801 | 801 | } else if (input === "e") { |
| 802 | 802 | const r = ready(l); |
| 803 | 803 | if (r === undefined) said = "Nothing is waiting to go to this lead right now."; |
new above
offline