Public: anyone can read this; the crew can change it.
The morning after a showing, a "how did you like it?" follow-up naming the address waits on the desk for the agent to approve #71
Check · Review · Land · Live done — Landed and live in production.
mason/speed-to-lead:mason/68-the-morning-after-a-showing-a-how-did-yo → main 2 commits fork 1 thread · 1 unresolved
11 files
changed +411
−33
AGENTS.md +5 −1
| @@ -123,7 +123,11 @@ Speed to Lead is one realtor's desk. The concepts, one module each: | ||
| 123 | 123 | lead's follow-ups scheduled while its `hold_until` (the day after the |
| 124 | 124 | showing) is ahead; booking sets due ones back, `cancelShowing` deletes the |
| 125 | 125 | row and advances. It is the same hold `advance` already keeps for a pending |
| 126 | answer, not a second mechanism. | |
| 126 | answer, not a second mechanism. Booking also schedules a note for 8:00 the | |
| 127 | next morning in the agent's zone (`morningAfter`, message day | |
| 128 | `AFTER_SHOWING_DAY` + n, one per showing, between the cadence and | |
| 129 | `ANSWER_DAY`); `advance` does not hold it, rebooking before it goes moves it, | |
| 130 | cancelling deletes it, closing skips it. | |
| 127 | 131 | - `src/drafts.ts` — the words: templates filled from the inquiry and the |
| 128 | 132 | agent's profile (`src/profile.ts`: the saved one, else the `AGENT_*` |
| 129 | 133 | defaults of `agent` in `src/env.ts`; its `openers` are the agent's own |
README.md +8 −1
| @@ -57,7 +57,14 @@ what you see right after a restart. Do step 1 first. | ||
| 57 | 57 | a day and time later this week (in your time zone from the profile), type an address and book it. The |
| 58 | 58 | history says "Showing booked", his row on the desk carries a *Showing* |
| 59 | 59 | badge, and his follow-up plan says which follow-ups wait until the day |
| 60 | after. Press "Cancel showing" and they return to their usual place. | |
| 60 | after. Press "Cancel showing" and they return to their usual place. A | |
| 61 | booked showing also drafts a note for the morning after it, at 8:00 in | |
| 62 | your time zone: it asks how the home was, names the address, and offers | |
| 63 | another showing or talking numbers, by the lead's channel and signed with | |
| 64 | your profile. It is only a draft until you approve it, and it shows on the | |
| 65 | lead's plan as "After the showing". The sample **Rosa Alvarez** had a | |
| 66 | showing yesterday, so her note is already waiting on a fresh desk. A | |
| 67 | cancelled showing has none; a second showing gets a second note. | |
| 61 | 68 | 8. Set your profile (admin to save). Press "Your profile" at the top, fill |
| 62 | 69 | in your name, brokerage, license number, phone and email, and save. Every |
| 63 | 70 | draft not yet sent takes it at once. |
src/actions.ts +1 −1
| @@ -731,7 +731,7 @@ function booked( | ||
| 731 | 731 | return fail("invalid", `An address is at most ${MAX_ADDRESS} characters.`); |
| 732 | 732 | if (hasControl(address, false)) |
| 733 | 733 | return fail("invalid", "An address is one line of plain text."); |
| 734 | bookShowingFor(db, lead, time.at, address, now); | |
| 734 | bookShowingFor(db, lead, time.at, address, now, currentAgent(db)); | |
| 735 | 735 | emit("lead.changed", { id: lead.id }); |
| 736 | 736 | return ok(detail(db, lead.id)!); |
| 737 | 737 | } |
src/after-showing.test.ts added +235
| @@ -0,0 +1,235 @@ | ||
| 1 | /** | |
| 2 | * The note the morning after a showing: drafted for the agent to approve, | |
| 3 | * due at 8:00 in their time zone, one per showing, none for a cancelled one, | |
| 4 | * by the lead's channel, and escaped wherever it is shown. | |
| 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 { | |
| 11 | addLead, | |
| 12 | bookShowing, | |
| 13 | cancelShowing, | |
| 14 | getLead, | |
| 15 | sendReply, | |
| 16 | setState, | |
| 17 | optOutTexts, | |
| 18 | turn, | |
| 19 | } from "./actions.ts"; | |
| 20 | import { openDb } from "./db.ts"; | |
| 21 | import { AFTER_SHOWING_DAY, MAX_BODY, dayLabel } from "./drafts.ts"; | |
| 22 | import { leadPage } from "./pages/lead.ts"; | |
| 23 | import { currentAgent, storeAgent } from "./profile.ts"; | |
| 24 | import { seed } from "./sample.ts"; | |
| 25 | import { morningAfter } from "./showings.ts"; | |
| 26 | import type { Who } from "./who.ts"; | |
| 27 | ||
| 28 | const alice: Who = { role: "user", id: "u1", name: "alice" }; | |
| 29 | const T0 = new Date("2026-03-01T10:00:00Z"); | |
| 30 | const DAY = 86_400_000; | |
| 31 | const at = (days: number) => new Date(T0.getTime() + days * DAY); | |
| 32 | ||
| 33 | const fresh = (zone = "UTC") => { | |
| 34 | const db = openDb(mkdtempSync(join(tmpdir(), "app-after-"))); | |
| 35 | storeAgent(db, { ...currentAgent(db), timeZone: zone }, "test", T0.toISOString()); | |
| 36 | return db; | |
| 37 | }; | |
| 38 | const value = <T>(r: { ok: boolean; value?: T; message?: string }): T => { | |
| 39 | if (!r.ok) throw new Error(r.message); | |
| 40 | return r.value as T; | |
| 41 | }; | |
| 42 | const answered = (db: ReturnType<typeof fresh>, consent = false, name = "Dana Reyes") => { | |
| 43 | const id = value( | |
| 44 | addLead( | |
| 45 | db, | |
| 46 | alice, | |
| 47 | { | |
| 48 | name, | |
| 49 | email: `${name.split(" ")[0]!.toLowerCase()}@example.com`, | |
| 50 | phone: `(206) 555-01${String(10 + name.length).padStart(2, "0")}`, | |
| 51 | source: "zillow", | |
| 52 | message: "Can I see 12 Oak Street?", | |
| 53 | consent, | |
| 54 | }, | |
| 55 | T0, | |
| 56 | ), | |
| 57 | ).id; | |
| 58 | value(sendReply(db, alice, { id }, T0)); | |
| 59 | return id; | |
| 60 | }; | |
| 61 | const notes = (db: ReturnType<typeof fresh>, id: number) => | |
| 62 | value(getLead(db, alice, { id })).messages.filter((m) => m.day >= AFTER_SHOWING_DAY && m.day < 1000); | |
| 63 | const book = (db: ReturnType<typeof fresh>, id: number, when: string, address = "12 Example St", now = at(0.5)) => | |
| 64 | value(bookShowing(db, alice, { id, when, address }, now)); | |
| 65 | ||
| 66 | test("the note is a draft from 8:00 the morning after, not before, and names the address", () => { | |
| 67 | const db = fresh(); | |
| 68 | const id = answered(db); | |
| 69 | book(db, id, "2026-03-03T10:00"); // Tuesday | |
| 70 | expect(notes(db, id)).toHaveLength(1); | |
| 71 | expect(notes(db, id)[0]!.due_at).toBe("2026-03-04T08:00:00.000Z"); | |
| 72 | turn(db, new Date("2026-03-04T07:59:00Z")); | |
| 73 | expect(notes(db, id)[0]!.status).toBe("scheduled"); | |
| 74 | turn(db, new Date("2026-03-04T08:00:00Z")); | |
| 75 | const [note] = notes(db, id); | |
| 76 | expect(note!.status).toBe("due"); | |
| 77 | expect(note!.body).toContain("how did you like 12 Example St?"); | |
| 78 | expect(note!.body).toMatch(/another home/); | |
| 79 | expect(note!.body).toMatch(/numbers/); | |
| 80 | expect(note!.signature).toContain(currentAgent(db).name); | |
| 81 | expect(dayLabel(note!.day)).toBe("After the showing"); | |
| 82 | // Nothing was sent: it waits for the agent. | |
| 83 | expect(value(getLead(db, alice, { id })).messages.filter((m) => m.status === "sent")).toHaveLength(1); | |
| 84 | }); | |
| 85 | ||
| 86 | test("8:00 is read in the agent's zone", () => { | |
| 87 | expect(morningAfter("2026-03-03T10:00:00.000Z", "UTC")).toBe("2026-03-04T08:00:00.000Z"); | |
| 88 | expect(morningAfter("2026-03-03T10:00:00.000Z", "America/Chicago")).toBe("2026-03-04T14:00:00.000Z"); | |
| 89 | // 20:00Z is already the 4th in Tokyo, so the morning after is the 5th at 8:00 there. | |
| 90 | expect(morningAfter("2026-03-03T20:00:00.000Z", "Asia/Tokyo")).toBe("2026-03-04T23:00:00.000Z"); | |
| 91 | }); | |
| 92 | ||
| 93 | test("the other cadence days keep the hold, and the note is not held by it", () => { | |
| 94 | const db = fresh(); | |
| 95 | const id = answered(db); | |
| 96 | book(db, id, "2026-03-03T10:00"); | |
| 97 | turn(db, new Date("2026-03-04T08:30:00Z")); // the day 3 follow-up is due at 10:00 and held until then | |
| 98 | const status = Object.fromEntries( | |
| 99 | value(getLead(db, alice, { id })).messages.map((m) => [m.day, m.status]), | |
| 100 | ); | |
| 101 | expect(status[AFTER_SHOWING_DAY]).toBe("due"); | |
| 102 | expect(status[3]).toBe("scheduled"); | |
| 103 | }); | |
| 104 | ||
| 105 | test("a cancelled showing gets none, and a moved one only the one note", () => { | |
| 106 | const db = fresh(); | |
| 107 | const id = answered(db); | |
| 108 | book(db, id, "2026-03-03T10:00"); | |
| 109 | book(db, id, "2026-03-05T10:00", "9 Moved Ln"); | |
| 110 | expect(notes(db, id)).toHaveLength(1); | |
| 111 | expect(notes(db, id)[0]!.due_at).toBe("2026-03-06T08:00:00.000Z"); | |
| 112 | expect(notes(db, id)[0]!.body).toContain("9 Moved Ln"); | |
| 113 | value(cancelShowing(db, alice, { id }, at(1))); | |
| 114 | expect(notes(db, id)).toHaveLength(0); | |
| 115 | turn(db, new Date("2026-03-10T09:00:00Z")); | |
| 116 | expect(notes(db, id)).toHaveLength(0); | |
| 117 | }); | |
| 118 | ||
| 119 | test("one note per showing, and it survives a restart", () => { | |
| 120 | const dir = mkdtempSync(join(tmpdir(), "app-after-restart-")); | |
| 121 | const db = openDb(dir); | |
| 122 | storeAgent(db, { ...currentAgent(db), timeZone: "UTC" }, "test", T0.toISOString()); | |
| 123 | const id = answered(db); | |
| 124 | book(db, id, "2026-03-03T10:00"); | |
| 125 | turn(db, new Date("2026-03-04T08:00:00Z")); | |
| 126 | turn(db, new Date("2026-03-04T09:00:00Z")); | |
| 127 | expect(notes(db, id)).toHaveLength(1); | |
| 128 | db.close(); | |
| 129 | ||
| 130 | const again = openDb(dir); | |
| 131 | expect(notes(again, id)).toHaveLength(1); | |
| 132 | // Send the first note; a second showing then gets a second. | |
| 133 | const first = notes(again, id)[0]!; | |
| 134 | again.query("UPDATE message SET status = 'sent', sent_at = ?1 WHERE id = ?2").run("2026-03-04T09:00:00.000Z", first.id); | |
| 135 | value(bookShowing(again, alice, { id, when: "2026-03-10T10:00", address: "7 Second Ave" }, new Date("2026-03-05T10:00:00Z"))); | |
| 136 | const both = notes(again, id); | |
| 137 | expect(both.map((m) => m.status)).toEqual(["sent", "scheduled"]); | |
| 138 | expect(both[1]!.day).not.toBe(both[0]!.day); | |
| 139 | expect(both[1]!.body).toContain("7 Second Ave"); | |
| 140 | turn(again, new Date("2026-03-11T08:00:00Z")); | |
| 141 | expect(notes(again, id).map((m) => m.status)).toEqual(["sent", "due"]); | |
| 142 | }); | |
| 143 | ||
| 144 | test("a note approved and held for quiet hours stays; moving the showing adds no second note", () => { | |
| 145 | const db = fresh(); | |
| 146 | const id = answered(db, true); | |
| 147 | book(db, id, "2026-03-03T10:00"); | |
| 148 | // A text approved at 22:00 is held for 8:00 (the suite turns quiet hours off; here they are on). | |
| 149 | const was = process.env.QUIET_HOURS; | |
| 150 | delete process.env.QUIET_HOURS; | |
| 151 | turn(db, new Date("2026-03-04T08:00:00Z")); | |
| 152 | value(sendReply(db, alice, { id }, new Date("2026-03-04T22:00:00Z"))); | |
| 153 | if (was !== undefined) process.env.QUIET_HOURS = was; | |
| 154 | const [held] = notes(db, id); | |
| 155 | expect(held!.status).toBe("held"); | |
| 156 | value(bookShowing(db, alice, { id, when: "2026-03-09T10:00", address: "Moved Ln" }, new Date("2026-03-04T22:30:00Z"))); | |
| 157 | expect(notes(db, id).map((m) => [m.id, m.status])).toEqual([[held!.id, "held"]]); | |
| 158 | value(cancelShowing(db, alice, { id }, new Date("2026-03-04T23:00:00Z"))); | |
| 159 | expect(notes(db, id).map((m) => m.status)).toEqual(["held"]); | |
| 160 | }); | |
| 161 | ||
| 162 | test("text with written consent, email otherwise, and email again after STOP", () => { | |
| 163 | const db = fresh(); | |
| 164 | const texted = answered(db, true); | |
| 165 | const emailed = answered(db, false, "Eli Park"); | |
| 166 | book(db, texted, "2026-03-03T10:00"); | |
| 167 | book(db, emailed, "2026-03-03T10:00"); | |
| 168 | expect(notes(db, texted)[0]!.channel).toBe("text"); | |
| 169 | expect(notes(db, texted)[0]!.body.length).toBeLessThanOrEqual(MAX_BODY.text); | |
| 170 | expect(notes(db, texted)[0]!.signature).toContain("STOP"); | |
| 171 | expect(notes(db, emailed)[0]!.channel).toBe("email"); | |
| 172 | value(optOutTexts(db, alice, { id: texted }, at(1))); | |
| 173 | expect(notes(db, texted)[0]!.channel).toBe("email"); | |
| 174 | expect(notes(db, texted)[0]!.body).toContain("12 Example St"); | |
| 175 | }); | |
| 176 | ||
| 177 | test("a lead who is qualified or lost, or reachable nowhere, gets none", () => { | |
| 178 | const db = fresh(); | |
| 179 | const id = answered(db); | |
| 180 | book(db, id, "2026-03-03T10:00"); | |
| 181 | value(setState(db, alice, { id, state: "qualified" }, at(1))); | |
| 182 | expect(notes(db, id).map((m) => m.status)).toEqual(["skipped"]); | |
| 183 | turn(db, new Date("2026-03-05T09:00:00Z")); | |
| 184 | expect(notes(db, id).map((m) => m.status)).toEqual(["skipped"]); | |
| 185 | ||
| 186 | const nowhere = answered(db, false, "Quin Vale"); | |
| 187 | db.query("UPDATE lead SET email = '' WHERE id = ?1").run(nowhere); | |
| 188 | book(db, nowhere, "2026-03-03T10:00"); | |
| 189 | expect(notes(db, nowhere)).toHaveLength(0); | |
| 190 | }); | |
| 191 | ||
| 192 | test("the agent can edit the note on sending it, and the plan shows it as 'After the showing'", () => { | |
| 193 | const db = fresh(); | |
| 194 | const id = answered(db); | |
| 195 | book(db, id, "2026-03-03T10:00"); | |
| 196 | turn(db, new Date("2026-03-04T08:00:00Z")); | |
| 197 | const note = notes(db, id)[0]!; | |
| 198 | const edited = sendReply(db, alice, { id, body: "Hi Dana, what did you think of the kitchen?" }, new Date("2026-03-04T08:05:00Z")); | |
| 199 | expect(edited.ok).toBe(true); | |
| 200 | const lead = value(getLead(db, alice, { id })); | |
| 201 | const sent = lead.messages.find((m) => m.id === note.id)!; | |
| 202 | expect(sent.status).toBe("sent"); | |
| 203 | expect(sent.body).toContain("the kitchen"); | |
| 204 | const html = leadPage({ who: alice, agent: currentAgent(db), lead, mayUpdate: true, next: null, now: Date.parse("2026-03-04T08:05:00Z") }); | |
| 205 | expect(html).toContain("After the showing"); | |
| 206 | }); | |
| 207 | ||
| 208 | test("a script-laden address is escaped on the page and the text stays within its limit", () => { | |
| 209 | const db = fresh(); | |
| 210 | const id = answered(db, true); | |
| 211 | const evil = `<script>alert(1)</script>${"x".repeat(160)}`; | |
| 212 | book(db, id, "2026-03-03T10:00", evil); | |
| 213 | const [note] = notes(db, id); | |
| 214 | expect(note!.body.length).toBeLessThanOrEqual(MAX_BODY.text); | |
| 215 | turn(db, new Date("2026-03-04T08:00:00Z")); | |
| 216 | const lead = value(getLead(db, alice, { id })); | |
| 217 | const html = leadPage({ who: alice, agent: currentAgent(db), lead, mayUpdate: true, next: null, now: Date.parse("2026-03-04T08:01:00Z") }); | |
| 218 | expect(html).not.toContain("<script>alert(1)"); | |
| 219 | expect(html).toContain("<script>alert(1)"); | |
| 220 | }); | |
| 221 | ||
| 222 | test("a sample desk has a showing from yesterday and its note ready now", () => { | |
| 223 | for (const now of ["2026-03-04T15:00:00Z", "2026-03-04T03:00:00Z"]) { | |
| 224 | const db = openDb(mkdtempSync(join(tmpdir(), "app-after-sample-"))); | |
| 225 | seed(db, new Date(now)); | |
| 226 | const ready = db | |
| 227 | .query<{ name: string; status: string; body: string }, [number]>( | |
| 228 | "SELECT lead.name, message.status, message.body FROM message JOIN lead ON lead.id = message.lead_id WHERE message.day = ?1", | |
| 229 | ) | |
| 230 | .all(AFTER_SHOWING_DAY); | |
| 231 | expect(ready).toHaveLength(1); | |
| 232 | expect(ready[0]!.status).toBe("due"); | |
| 233 | expect(ready[0]!.body).toContain("418 Juniper Ln"); | |
| 234 | } | |
| 235 | }); |
src/drafts.ts +38
| @@ -43,6 +43,25 @@ export const MAX_BODY: Readonly<Record<Channel, number>> = { | ||
| 43 | 43 | export const ANSWER_DAY = 1000; |
| 44 | 44 | export const isAnswer = (day: number): boolean => day >= ANSWER_DAY; |
| 45 | 45 | |
| 46 | /** | |
| 47 | * The note the morning after a showing is numbered from here (the first | |
| 48 | * showing's is 500, the next 501, …), below `ANSWER_DAY` and above every | |
| 49 | * cadence day, so it meets neither: the `(lead, day)` pair stays unique per showing. | |
| 50 | */ | |
| 51 | export const AFTER_SHOWING_DAY = 500; | |
| 52 | export const isAfterShowing = (day: number): boolean => | |
| 53 | day >= AFTER_SHOWING_DAY && day < ANSWER_DAY; | |
| 54 | ||
| 55 | /** What a message is called, by its day: "First reply", "After the showing", "Day 7 follow-up". */ | |
| 56 | export const dayLabel = (day: number): string => | |
| 57 | day === 0 | |
| 58 | ? "First reply" | |
| 59 | : isAnswer(day) | |
| 60 | ? "Answer to their reply" | |
| 61 | : isAfterShowing(day) | |
| 62 | ? "After the showing" | |
| 63 | : `Day ${day} follow-up`; | |
| 64 | ||
| 46 | 65 | export interface Asker { |
| 47 | 66 | readonly name: string; |
| 48 | 67 | readonly phone: string; |
| @@ -265,6 +284,25 @@ export function draft(lead: Asker, day: number, agent: Writer): string { | ||
| 265 | 284 | return channel === "text" ? touch : `${touch}\n\nBest,`; |
| 266 | 285 | } |
| 267 | 286 | |
| 287 | /** | |
| 288 | * The morning after a showing: how was the home (named when the address is | |
| 289 | * known), and a next step. Short enough to text; an address too long for | |
| 290 | * that is left out of the text rather than cutting the message. | |
| 291 | */ | |
| 292 | export function afterShowingDraft(lead: Asker, address: string): string { | |
| 293 | const first = firstName(lead.name); | |
| 294 | const asked = (home: string): string => | |
| 295 | `how did you like ${home}? If you'd like to see another home, or talk numbers, just say the word.`; | |
| 296 | const named = address === "" ? "the home" : address; | |
| 297 | if (channelFor(lead) === "text") { | |
| 298 | const text = `Hi ${first}, ${asked(named)}`; | |
| 299 | return text.length <= MAX_BODY.text | |
| 300 | ? text | |
| 301 | : `Hi ${first}, ${asked("the home")}`.slice(0, MAX_BODY.text); | |
| 302 | } | |
| 303 | return `Hi ${first},\n\nIt was a pleasure showing you around — ${asked(named)}\n\nBest,`; | |
| 304 | } | |
| 305 | ||
| 268 | 306 | const AGREES = /\b(yes|yeah|yep|sure|sounds good|works for me|that works|perfect|great)\b/i; |
| 269 | 307 | |
| 270 | 308 | /** |
src/leads.ts +78 −8
| @@ -13,11 +13,14 @@ | ||
| 13 | 13 | |
| 14 | 14 | import type { Database } from "bun:sqlite"; |
| 15 | 15 | import { |
| 16 | AFTER_SHOWING_DAY, | |
| 17 | afterShowingDraft, | |
| 16 | 18 | ANSWER_DAY, |
| 17 | 19 | answerDraft, |
| 18 | 20 | CADENCE, |
| 19 | 21 | channelFor, |
| 20 | 22 | draft, |
| 23 | isAfterShowing, | |
| 21 | 24 | isAnswer, |
| 22 | 25 | signature, |
| 23 | 26 | type Channel, |
| @@ -28,7 +31,7 @@ import { GREEN_MS } from "./clock.ts"; | ||
| 28 | 31 | import type { Agent } from "./env.ts"; |
| 29 | 32 | import { isQuiet, nextOpening } from "./quiet.ts"; |
| 30 | 33 | import { type LostReason } from "./lost.ts"; |
| 31 | import { holdUntil, type Showing } from "./showings.ts"; | |
| 34 | import { holdUntil, morningAfter, type Showing } from "./showings.ts"; | |
| 32 | 35 | |
| 33 | 36 | export type State = "new" | "contacted" | "qualified" | "lost"; |
| 34 | 37 | export const CLOSED: readonly State[] = ["qualified", "lost"]; |
| @@ -693,10 +696,10 @@ export function recordCall( | ||
| 693 | 696 | /** Touches not yet sent come due on the cadence's days counted from `at`. */ |
| 694 | 697 | function respace(db: Database, id: number, at: string): void { |
| 695 | 698 | const waiting = db |
| 696 | .query<{ id: number; day: number }, [number]>( | |
| 697 | "SELECT id, day FROM message WHERE lead_id = ?1 AND status = 'scheduled'", | |
| 699 | .query<{ id: number; day: number }, [number, number]>( | |
| 700 | "SELECT id, day FROM message WHERE lead_id = ?1 AND status = 'scheduled' AND day < ?2", | |
| 698 | 701 | ) |
| 699 | .all(id); | |
| 702 | .all(id, AFTER_SHOWING_DAY); | |
| 700 | 703 | for (const m of waiting) |
| 701 | 704 | db.query("UPDATE message SET due_at = ?1 WHERE id = ?2").run( |
| 702 | 705 | later(at, m.day), |
| @@ -767,7 +770,7 @@ export function redraftUnsent(db: Database, agent: Agent): void { | ||
| 767 | 770 | ); |
| 768 | 771 | else |
| 769 | 772 | db.query("UPDATE message SET body = ?1, signature = ?2 WHERE id = ?3").run( |
| 770 | draft(found, m.day, agent), | |
| 773 | wordsFor(db, found, m.day, agent), | |
| 771 | 774 | signature(m.channel, agent), |
| 772 | 775 | m.id, |
| 773 | 776 | ); |
| @@ -775,6 +778,17 @@ export function redraftUnsent(db: Database, agent: Agent): void { | ||
| 775 | 778 | })(); |
| 776 | 779 | } |
| 777 | 780 | |
| 781 | /** The words for an unsent touch or after-showing note: the cadence's, or the note for the lead's showing as it stands. */ | |
| 782 | const wordsFor = (db: Database, found: Lead, day: number, agent: Agent): string => | |
| 783 | isAfterShowing(day) | |
| 784 | ? afterShowingDraft( | |
| 785 | found, | |
| 786 | db | |
| 787 | .query<{ address: string }, [number]>("SELECT address FROM showing WHERE lead_id = ?1") | |
| 788 | .get(found.id)?.address ?? "", | |
| 789 | ) | |
| 790 | : draft(found, day, agent); | |
| 791 | ||
| 778 | 792 | /** The answer to the reply it was drafted for, drafted again for the lead as they are now. */ |
| 779 | 793 | const answerAgain = (db: Database, found: Lead, day: number): string => |
| 780 | 794 | answerDraft( |
| @@ -854,7 +868,7 @@ function redraftFor(db: Database, lead: Lead, agent: Agent, aside: boolean): voi | ||
| 854 | 868 | "UPDATE message SET channel = ?1, body = ?2, signature = ?3 WHERE id = ?4", |
| 855 | 869 | ).run( |
| 856 | 870 | channel, |
| 857 | isAnswer(m.day) ? answerAgain(db, lead, m.day) : draft(lead, m.day, agent), | |
| 871 | isAnswer(m.day) ? answerAgain(db, lead, m.day) : wordsFor(db, lead, m.day, agent), | |
| 858 | 872 | signature(channel, agent), |
| 859 | 873 | m.id, |
| 860 | 874 | ); |
| @@ -910,7 +924,7 @@ export function reopen(db: Database, found: Lead, at: string, agent: Agent): voi | ||
| 910 | 924 | ).run( |
| 911 | 925 | m.day === 0 ? "draft" : "scheduled", |
| 912 | 926 | channel, |
| 913 | draft(found, m.day, agent), | |
| 927 | wordsFor(db, found, m.day, agent), | |
| 914 | 928 | signature(channel, agent), |
| 915 | 929 | m.day === 0 ? found.created_at : m.due_at, |
| 916 | 930 | m.id, |
| @@ -969,7 +983,8 @@ export function advance(db: Database, at: string, only?: number): number[] { | ||
| 969 | 983 | `UPDATE message SET status = 'due' |
| 970 | 984 | WHERE status = 'scheduled' AND due_at <= ?1 AND (?2 = 0 OR id = ?2) |
| 971 | 985 | AND lead_id IN (SELECT id FROM lead WHERE state = 'contacted') |
| 972 | AND lead_id NOT IN (SELECT lead_id FROM showing WHERE hold_until > ?1) | |
| 986 | AND (day >= ${AFTER_SHOWING_DAY} AND day < ${ANSWER_DAY} | |
| 987 | OR lead_id NOT IN (SELECT lead_id FROM showing WHERE hold_until > ?1)) | |
| 973 | 988 | AND lead_id NOT IN (SELECT lead_id FROM message WHERE day >= ${ANSWER_DAY} AND status IN ('draft', 'held')) |
| 974 | 989 | RETURNING lead_id`, |
| 975 | 990 | ) |
| @@ -988,6 +1003,7 @@ export function bookShowing( | ||
| 988 | 1003 | when: string, |
| 989 | 1004 | address: string, |
| 990 | 1005 | at: string, |
| 1006 | agent: Agent, | |
| 991 | 1007 | ): void { |
| 992 | 1008 | db.transaction(() => { |
| 993 | 1009 | db.query( |
| @@ -997,14 +1013,68 @@ export function bookShowing( | ||
| 997 | 1013 | db.query( |
| 998 | 1014 | "UPDATE message SET status = 'scheduled' WHERE lead_id = ?1 AND status = 'due'", |
| 999 | 1015 | ).run(found.id); |
| 1016 | noteAfterShowing(db, found, when, address, agent); | |
| 1000 | 1017 | db.query("UPDATE lead SET updated_at = ?1 WHERE id = ?2").run(at, found.id); |
| 1001 | 1018 | })(); |
| 1002 | 1019 | } |
| 1003 | 1020 | |
| 1021 | /** Whether a message could still be taken back or moved: nothing has gone, and nothing is approved to go (a held one is approved, so it is neither). */ | |
| 1022 | const UNSENT: readonly string[] = ["draft", "scheduled", "due", "skipped"]; | |
| 1023 | const UNSENT_SQL = `(${UNSENT.map((status) => `'${status}'`).join(", ")})`; | |
| 1024 | ||
| 1025 | /** | |
| 1026 | * The note for the morning after the showing at `when`, drafted for the | |
| 1027 | * agent to approve, due at 8:00 in their time zone. A showing booked again | |
| 1028 | * before its note went moves that note; one booked after it went gets a | |
| 1029 | * note of its own. A lead who can be reached nowhere gets none. | |
| 1030 | */ | |
| 1031 | function noteAfterShowing( | |
| 1032 | db: Database, | |
| 1033 | found: Lead, | |
| 1034 | when: string, | |
| 1035 | address: string, | |
| 1036 | agent: Agent, | |
| 1037 | ): void { | |
| 1038 | const last = db | |
| 1039 | .query<{ id: number; day: number; status: string }, [number, number, number]>( | |
| 1040 | "SELECT id, day, status FROM message WHERE lead_id = ?1 AND day >= ?2 AND day < ?3 ORDER BY day DESC LIMIT 1", | |
| 1041 | ) | |
| 1042 | .get(found.id, AFTER_SHOWING_DAY, ANSWER_DAY); | |
| 1043 | // Approved and held for 8:00 (quiet hours): it goes as approved, and a moved showing does not get a second. | |
| 1044 | if (last?.status === "held") return; | |
| 1045 | const unsent = last !== null && UNSENT.includes(last.status); | |
| 1046 | if (!reachable(found)) { | |
| 1047 | if (unsent) db.query("DELETE FROM message WHERE id = ?1").run(last.id); | |
| 1048 | return; | |
| 1049 | } | |
| 1050 | const channel = channelFor(found); | |
| 1051 | const body = afterShowingDraft(found, address); | |
| 1052 | const sig = signature(channel, agent); | |
| 1053 | const due = morningAfter(when, agent.timeZone); | |
| 1054 | if (unsent) { | |
| 1055 | db.query( | |
| 1056 | "UPDATE message SET status = 'scheduled', skipped_by_hand = 0, channel = ?1, body = ?2, signature = ?3, due_at = ?4 WHERE id = ?5", | |
| 1057 | ).run(channel, body, sig, due, last.id); | |
| 1058 | return; | |
| 1059 | } | |
| 1060 | const day = last === null ? AFTER_SHOWING_DAY : last.day + 1; | |
| 1061 | if (day >= ANSWER_DAY) return; | |
| 1062 | db.query( | |
| 1063 | "INSERT INTO message (lead_id, day, channel, body, signature, status, due_at) VALUES (?1, ?2, ?3, ?4, ?5, 'scheduled', ?6)", | |
| 1064 | ).run(found.id, day, channel, body, sig, due); | |
| 1065 | } | |
| 1066 | ||
| 1067 | /** Someone the agent can write to: they can be texted, or there is an email to write to. */ | |
| 1068 | const reachable = (lead: Pick<Lead, "consent" | "phone" | "email">): boolean => | |
| 1069 | channelFor(lead) === "text" || lead.email !== ""; | |
| 1070 | ||
| 1004 | 1071 | /** The showing is off: follow-ups whose day has come are due again at once. */ |
| 1005 | 1072 | export function cancelShowing(db: Database, found: Lead, at: string): void { |
| 1006 | 1073 | db.transaction(() => { |
| 1007 | 1074 | db.query("DELETE FROM showing WHERE lead_id = ?1").run(found.id); |
| 1075 | db.query( | |
| 1076 | `DELETE FROM message WHERE lead_id = ?1 AND day >= ${AFTER_SHOWING_DAY} AND day < ${ANSWER_DAY} AND status IN ${UNSENT_SQL}`, | |
| 1077 | ).run(found.id); | |
| 1008 | 1078 | db.query("UPDATE lead SET updated_at = ?1 WHERE id = ?2").run(at, found.id); |
| 1009 | 1079 | advance(db, at); |
| 1010 | 1080 | })(); |
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 { isAnswer } from "../drafts.ts"; | |
| 10 | import { isAfterShowing, isAnswer } from "../drafts.ts"; | |
| 11 | 11 | import { goesLabel, RECORD_NOTE } from "../quiet.ts"; |
| 12 | 12 | import { showingWhen } from "../showings.ts"; |
| 13 | 13 | import { MAX_EMAIL_TEXT, missingLine } from "../intake.ts"; |
| @@ -166,7 +166,7 @@ function status(lead: DeskLead, now: number, zone: string): string { | ||
| 166 | 166 | if (lead.ready !== null && isAnswer(lead.ready.day)) |
| 167 | 167 | return `<span class="status" data-ready>They wrote back — answer drafted</span>`; |
| 168 | 168 | if (lead.ready !== null) |
| 169 | return `<span class="status" data-ready>Day ${lead.ready.day} follow-up ready</span>`; | |
| 169 | return `<span class="status" data-ready>${isAfterShowing(lead.ready.day) ? "After the showing — note ready" : `Day ${lead.ready.day} follow-up ready`}</span>`; | |
| 170 | 170 | if (lead.next_at !== null) |
| 171 | 171 | return `<span class="status">Next follow-up ${escape(day(held(lead, lead.next_at), zone))}</span>`; |
| 172 | 172 | return `<span class="status">Follow-ups done</span>`; |
src/pages/lead.ts +3 −8
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import { callLine, MAX_NOTE, OUTCOME_LABEL, OUTCOMES } from "../calls.ts"; |
| 8 | import { channelFor, isAnswer, MAX_BODY } from "../drafts.ts"; | |
| 8 | import { channelFor, dayLabel, isAfterShowing, isAnswer, 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"; |
| @@ -27,12 +27,7 @@ type HistoryEntry = | ||
| 27 | 27 | |
| 28 | 28 | const CHANNEL = { text: "text", email: "email" } as const; |
| 29 | 29 | |
| 30 | const titleOf = (m: Message): string => | |
| 31 | m.day === 0 | |
| 32 | ? "First reply" | |
| 33 | : isAnswer(m.day) | |
| 34 | ? "Answer to their reply" | |
| 35 | : `Day ${m.day} follow-up`; | |
| 30 | const titleOf = (m: Message): string => dayLabel(m.day); | |
| 36 | 31 | |
| 37 | 32 | /** A message's words, line breaks kept, and the signature under them. */ |
| 38 | 33 | const words = (m: Pick<Message, "body" | "signature">): string => |
| @@ -175,7 +170,7 @@ export function leadPage(input: { | ||
| 175 | 170 | }<ol class="plan">${cadence |
| 176 | 171 | .map( |
| 177 | 172 | (m) => `<li data-status="${m.status}"> |
| 178 | <span class="plan-day">Day ${m.day}</span> | |
| 173 | <span class="plan-day">${isAfterShowing(m.day) ? "After the showing" : `Day ${m.day}`}</span> | |
| 179 | 174 | <span class="plan-when">${escape(day(m.sent_at ?? m.due_at, agent.timeZone))}</span> |
| 180 | 175 | <span class="plan-status">${waits(m) ? "Waits for the showing" : PLAN[m.status]}</span> |
| 181 | 176 | ${m.status === "scheduled" ? `<details class="peek"><summary>Preview</summary>${words(m)}</details>` : ""} |
src/sample.ts +15 −1
| @@ -9,9 +9,11 @@ | ||
| 9 | 9 | |
| 10 | 10 | import type { Database } from "bun:sqlite"; |
| 11 | 11 | import type { Source } from "./drafts.ts"; |
| 12 | import { morningAfter, wallAt } from "./showings.ts"; | |
| 12 | 13 | import { currentAgent } from "./profile.ts"; |
| 13 | 14 | import { |
| 14 | 15 | advance, |
| 16 | bookShowing, | |
| 15 | 17 | close, |
| 16 | 18 | findLead, |
| 17 | 19 | insertLead, |
| @@ -30,6 +32,8 @@ interface Sample extends NewLead { | ||
| 30 | 32 | /** The cadence days already sent. */ |
| 31 | 33 | readonly touched?: readonly number[]; |
| 32 | 34 | readonly closed?: Closing; |
| 35 | /** A showing already held, this long ago (rounded to the agent's morning after it). */ | |
| 36 | readonly showedAgo?: number; | |
| 33 | 37 | /** The agent's private note on the lead, as of the first reply or the inquiry. */ |
| 34 | 38 | readonly note?: string; |
| 35 | 39 | } |
| @@ -69,7 +73,7 @@ export const SAMPLES: readonly Sample[] = [ | ||
| 69 | 73 | person("Tom Becker", "website", true, "Do you work with first-time buyers? We have no idea where to start.", { ago: 5 * H + 30 * M }), |
| 70 | 74 | 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 }), |
| 71 | 75 | 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." }), |
| 72 | 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] }), | |
| 76 | 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 }), | |
| 73 | 77 | 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] }), |
| 74 | 78 | 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" } }), |
| 75 | 79 | 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] }), |
| @@ -94,6 +98,14 @@ export function seed(db: Database, now: Date): number { | ||
| 94 | 98 | sendReady(db, id, iso(replied + day * D + 55 * M)); |
| 95 | 99 | } |
| 96 | 100 | } |
| 101 | if (sample.showedAgo !== undefined) { | |
| 102 | // Yesterday at 10:00 in the agent's zone (a day earlier still when this morning's 8:00 has not come): its note is on the desk now. | |
| 103 | const agent = currentAgent(db); | |
| 104 | let held = wallAt(t - sample.showedAgo, 0, 10, agent.timeZone) ?? t - sample.showedAgo; | |
| 105 | if (Date.parse(morningAfter(iso(held), agent.timeZone)) > t) | |
| 106 | held = wallAt(held, -1, 10, agent.timeZone) ?? held - D; | |
| 107 | bookShowing(db, findLead(db, id)!, iso(held), "418 Juniper Ln", iso(replied(sample, asked)), agent); | |
| 108 | } | |
| 97 | 109 | if (sample.note !== undefined) |
| 98 | 110 | writeNote(db, findLead(db, id)!, sample.note, iso(asked + 30 * M)); |
| 99 | 111 | if (sample.closed !== undefined) close(db, id, sample.closed, iso(t - 2 * D)); |
| @@ -102,6 +114,8 @@ export function seed(db: Database, now: Date): number { | ||
| 102 | 114 | return SAMPLES.length; |
| 103 | 115 | } |
| 104 | 116 | |
| 117 | const replied = (sample: Sample, asked: number): number => asked + (sample.answeredIn ?? 0); | |
| 118 | ||
| 105 | 119 | /** Send what is ready for a sample lead, as drafted, as the agent. */ |
| 106 | 120 | function sendReady(db: Database, id: number, at: string): void { |
| 107 | 121 | const agent = currentAgent(db); |
src/showings.ts +20
| @@ -108,6 +108,26 @@ export const localInput = (ms: number, zone: string): string => | ||
| 108 | 108 | export const holdUntil = (at: string): string => |
| 109 | 109 | new Date(Date.parse(at) + DAY_MS).toISOString(); |
| 110 | 110 | |
| 111 | /** | |
| 112 | * The instant at `hour`:00 on the wall clock of `zone`, `days` days after | |
| 113 | * the wall date `ms` falls on there; null when the clocks skip that hour. | |
| 114 | */ | |
| 115 | export function wallAt(ms: number, days: number, hour: number, zone: string): number | null { | |
| 116 | const wall = new Date(wallMinutes(ms, zone) * 60_000); | |
| 117 | return instantIn( | |
| 118 | wall.getUTCFullYear(), | |
| 119 | wall.getUTCMonth() + 1, | |
| 120 | wall.getUTCDate() + days, | |
| 121 | hour, | |
| 122 | 0, | |
| 123 | zone, | |
| 124 | ); | |
| 125 | } | |
| 126 | ||
| 127 | /** When the note after a showing at `at` comes due: the next morning at 8:00 in `zone`, the agent's. */ | |
| 128 | export const morningAfter = (at: string, zone: string): string => | |
| 129 | new Date(wallAt(Date.parse(at), 1, 8, zone) ?? Date.parse(at) + DAY_MS).toISOString(); | |
| 130 | ||
| 111 | 131 | /** In `zone`, the agent's: "Sat 10:00", or "Sat Dec 12 10:00" beyond the coming week. */ |
| 112 | 132 | export function showingWhen(at: string, now: number, zone: string): string { |
| 113 | 133 | const d = new Date(at); |
src/terminal.ts +6 −11
| @@ -57,7 +57,7 @@ import { between, elapsed, recordLine, sourceLine, tone } from "./clock.ts"; | ||
| 57 | 57 | import { permit } from "./contract.ts"; |
| 58 | 58 | import { goesLabel, isQuiet, nextOpening, RECORD_NOTE } from "./quiet.ts"; |
| 59 | 59 | import { said as saidOf } from "./sentences.ts"; |
| 60 | import { SOURCE_LABEL, SOURCES, channelFor, isAnswer, type Channel, type Source } from "./drafts.ts"; | |
| 60 | import { SOURCE_LABEL, SOURCES, channelFor, dayLabel, isAfterShowing, isAnswer, type Channel, type Source } from "./drafts.ts"; | |
| 61 | 61 | import { ssh, title, where } from "./env.ts"; |
| 62 | 62 | import { LOST_REASONS, REASON_LABEL, lostSaid } from "./lost.ts"; |
| 63 | 63 | import { answerSince, CLOSED, MAX_QUERY, VIEWS, queryOf, type DeskLead, type LeadDetail, type Message, type View as DeskView } from "./leads.ts"; |
| @@ -165,12 +165,7 @@ const DETAIL_FIELDS = [ | ||
| 165 | 165 | { key: "phone", label: "Phone (empty takes it away)" }, |
| 166 | 166 | ] as const; |
| 167 | 167 | |
| 168 | const titleOf = (m: Message): string => | |
| 169 | m.day === 0 | |
| 170 | ? "First reply" | |
| 171 | : isAnswer(m.day) | |
| 172 | ? "Answer to their reply" | |
| 173 | : `Day ${m.day} follow-up`; | |
| 168 | const titleOf = (m: Message): string => dayLabel(m.day); | |
| 174 | 169 | |
| 175 | 170 | export interface Terminal { |
| 176 | 171 | /** The screen as lines, for the width given. */ |
| @@ -275,7 +270,7 @@ export function terminal( | ||
| 275 | 270 | ? "reply drafted" |
| 276 | 271 | : isAnswer(l.ready.day) |
| 277 | 272 | ? "wrote back · answer drafted" |
| 278 | : `day ${l.ready.day} follow-up ready`; | |
| 273 | : isAfterShowing(l.ready.day) ? "after the showing · note ready" : `day ${l.ready.day} follow-up ready`; | |
| 279 | 274 | const name = clean(l.name).padEnd(18).slice(0, 18); |
| 280 | 275 | const showing = |
| 281 | 276 | l.showing === null |
| @@ -409,7 +404,7 @@ export function terminal( | ||
| 409 | 404 | dim( |
| 410 | 405 | `${sent} sent · plan: ${l.messages |
| 411 | 406 | .filter((m) => m.day > 0 && !isAnswer(m.day)) |
| 412 | .map((m) => `day ${m.day} ${m.status}`) | |
| 407 | .map((m) => `${isAfterShowing(m.day) ? "after the showing" : `day ${m.day}`} ${m.status}`) | |
| 413 | 408 | .join(", ") || "starts with the first reply"}`, |
| 414 | 409 | ), |
| 415 | 410 | "", |
| @@ -784,11 +779,11 @@ export function terminal( | ||
| 784 | 779 | } else if (input === "k" && skippable(l) !== undefined) { |
| 785 | 780 | const m = skippable(l)!; |
| 786 | 781 | const skipped = skipFollowUp(db, me, { id, messageId: m.id }); |
| 787 | said = skipped.ok ? `Day ${m.day} skipped. The rest of the plan is as it was.` : skipped.message; | |
| 782 | 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; | |
| 788 | 783 | } else if (input === "K" && restorable(l) !== undefined) { |
| 789 | 784 | const m = restorable(l)!; |
| 790 | 785 | const back = restoreFollowUp(db, me, { id, messageId: m.id }); |
| 791 | said = back.ok ? `Day ${m.day} is back in the plan.` : back.message; | |
| 786 | said = back.ok ? `${isAfterShowing(m.day) ? "The note after the showing" : `Day ${m.day}`} is back in the plan.` : back.message; | |
| 792 | 787 | } else if (input === "e") { |
| 793 | 788 | const r = ready(l); |
| 794 | 789 | if (r === undefined) said = "Nothing is waiting to go to this lead right now."; |
new above
offline
Duplicate of UNSENT, and it omits held: a held (approved, waiting) note makes this false, so a rebooking inserts a second note for the same showing. Use one list and decide what held does; add a test.