Public: anyone can read this; the crew can change it.
When a lead replies STOP, one tap stops all texts to them and every unsent draft becomes an email #30
Check · Review · Land · Live done — Landed and live in production.
mason/speed-to-lead:mason/28-when-a-lead-replies-stop-one-tap-stops-a → main 3 commits fork 3 threads · 3 unresolved
15 files
changed +348
−9
README.md +9
| @@ -112,6 +112,15 @@ relocating, a first purchase, price. Then: | ||
| 112 | 112 | written consent to be texted, **Email only** if they didn't. The app never |
| 113 | 113 | drafts a text to someone without that consent — it writes an email instead. |
| 114 | 114 | |
| 115 | **When someone replies STOP**, open their lead and use **They asked to stop | |
| 116 | texts** (tick the confirm box first), press `t` on their screen in the | |
| 117 | terminal, `POST /leads/{id}/opt-out`, or the `opt_out_texts` MCP tool. Their | |
| 118 | badge reads **Email only (opted out)** with the date, every draft not yet | |
| 119 | sent — the first reply and the remaining follow-ups, edited or not — is | |
| 120 | written again as an email, and sent messages stay in the history as they | |
| 121 | were. It is one-way: the app has no button to turn texting back on (new | |
| 122 | written consent comes with a new inquiry), and asking twice changes nothing. | |
| 123 | ||
| 115 | 124 | **Your signature** goes on every message, and can't be edited off: your |
| 116 | 125 | name, brokerage and license number, as state advertising rules require. |
| 117 | 126 | Texts also say "Reply STOP to opt out." |
contract/openapi.json +65
| @@ -379,6 +379,14 @@ | ||
| 379 | 379 | "type": "boolean", |
| 380 | 380 | "description": "Prior written consent to be texted. Without it, only email." |
| 381 | 381 | }, |
| 382 | "opted_out_at": { | |
| 383 | "type": [ | |
| 384 | "string", | |
| 385 | "null" | |
| 386 | ], | |
| 387 | "format": "date-time", | |
| 388 | "description": "When they asked to stop texts; consent is false from then on." | |
| 389 | }, | |
| 382 | 390 | "state": { |
| 383 | 391 | "enum": [ |
| 384 | 392 | "new", |
| @@ -436,6 +444,7 @@ | ||
| 436 | 444 | "source", |
| 437 | 445 | "message", |
| 438 | 446 | "consent", |
| 447 | "opted_out_at", | |
| 439 | 448 | "state", |
| 440 | 449 | "created_at", |
| 441 | 450 | "replied_at", |
| @@ -520,6 +529,44 @@ | ||
| 520 | 529 | } |
| 521 | 530 | } |
| 522 | 531 | }, |
| 532 | "/leads/{id}/opt-out": { | |
| 533 | "post": { | |
| 534 | "summary": "They replied STOP: no more texts to this lead, for good. Every unsent draft becomes an email; sent messages stay as they were. No body needed; asking twice changes nothing.", | |
| 535 | "operationId": "postLeadsIdOptOut", | |
| 536 | "parameters": [ | |
| 537 | { | |
| 538 | "name": "id", | |
| 539 | "in": "path", | |
| 540 | "required": true, | |
| 541 | "schema": { | |
| 542 | "type": "integer" | |
| 543 | } | |
| 544 | } | |
| 545 | ], | |
| 546 | "responses": { | |
| 547 | "200": { | |
| 548 | "description": "They replied STOP: no more texts to this lead, for good. Every unsent draft becomes an email; sent messages stay as they were. No body needed; asking twice changes nothing.", | |
| 549 | "content": { | |
| 550 | "application/json": { | |
| 551 | "schema": { | |
| 552 | "$ref": "#/components/schemas/Lead" | |
| 553 | } | |
| 554 | } | |
| 555 | } | |
| 556 | }, | |
| 557 | "default": { | |
| 558 | "description": "{ error, message }: why not", | |
| 559 | "content": { | |
| 560 | "application/json": { | |
| 561 | "schema": { | |
| 562 | "$ref": "#/components/schemas/Error" | |
| 563 | } | |
| 564 | } | |
| 565 | } | |
| 566 | } | |
| 567 | } | |
| 568 | } | |
| 569 | }, | |
| 523 | 570 | "/tick": { |
| 524 | 571 | "post": { |
| 525 | 572 | "summary": "The day turns: every follow-up whose day has come is ready to approve. Safe to call as often as you like; the app also does it hourly.", |
| @@ -998,6 +1045,14 @@ | ||
| 998 | 1045 | "type": "boolean", |
| 999 | 1046 | "description": "Prior written consent to be texted. Without it, only email." |
| 1000 | 1047 | }, |
| 1048 | "opted_out_at": { | |
| 1049 | "type": [ | |
| 1050 | "string", | |
| 1051 | "null" | |
| 1052 | ], | |
| 1053 | "format": "date-time", | |
| 1054 | "description": "When they asked to stop texts; consent is false from then on." | |
| 1055 | }, | |
| 1001 | 1056 | "state": { |
| 1002 | 1057 | "enum": [ |
| 1003 | 1058 | "new", |
| @@ -1036,6 +1091,7 @@ | ||
| 1036 | 1091 | "source", |
| 1037 | 1092 | "message", |
| 1038 | 1093 | "consent", |
| 1094 | "opted_out_at", | |
| 1039 | 1095 | "state", |
| 1040 | 1096 | "created_at", |
| 1041 | 1097 | "replied_at", |
| @@ -1171,6 +1227,14 @@ | ||
| 1171 | 1227 | "type": "boolean", |
| 1172 | 1228 | "description": "Prior written consent to be texted. Without it, only email." |
| 1173 | 1229 | }, |
| 1230 | "opted_out_at": { | |
| 1231 | "type": [ | |
| 1232 | "string", | |
| 1233 | "null" | |
| 1234 | ], | |
| 1235 | "format": "date-time", | |
| 1236 | "description": "When they asked to stop texts; consent is false from then on." | |
| 1237 | }, | |
| 1174 | 1238 | "state": { |
| 1175 | 1239 | "enum": [ |
| 1176 | 1240 | "new", |
| @@ -1225,6 +1289,7 @@ | ||
| 1225 | 1289 | "source", |
| 1226 | 1290 | "message", |
| 1227 | 1291 | "consent", |
| 1292 | "opted_out_at", | |
| 1228 | 1293 | "state", |
| 1229 | 1294 | "created_at", |
| 1230 | 1295 | "replied_at", |
src/actions.test.ts +69
| @@ -11,6 +11,7 @@ import { | ||
| 11 | 11 | addLead, |
| 12 | 12 | getLead, |
| 13 | 13 | listLeads, |
| 14 | optOutTexts, | |
| 14 | 15 | readNewLead, |
| 15 | 16 | resetSample, |
| 16 | 17 | sendReply, |
| @@ -301,3 +302,71 @@ test("reopening a lead that was never answered makes it new with its first reply | ||
| 301 | 302 | expect(back.messages[0]!.status).toBe("draft"); |
| 302 | 303 | expect(setState(db, anon, { id: lead.id, state: "open" }).ok).toBe(false); |
| 303 | 304 | }); |
| 305 | ||
| 306 | test("opting out turns every unsent text into an email and leaves what was sent alone", () => { | |
| 307 | const lead = must(addLead(db, alice, { ...maria, name: "Says Stop" }, at(0))); | |
| 308 | const first = must(sendReply(db, alice, { id: lead.id, body: "Hi, edited." }, at(0))); | |
| 309 | const sentBefore = first.messages.find((m) => m.status === "sent")!; | |
| 310 | expect(sentBefore.channel).toBe("text"); | |
| 311 | expect(first.messages.filter((m) => m.day > 0).every((m) => m.channel === "text")).toBe(true); | |
| 312 | must(tick(db, alice, at(1, 1))); | |
| 313 | const out = must(optOutTexts(db, alice, { id: lead.id }, at(2))); | |
| 314 | expect(out.consent).toBe(false); | |
| 315 | expect(out.opted_out_at).toBe(at(2).toISOString()); | |
| 316 | expect(out.state).toBe("contacted"); | |
| 317 | const unsent = out.messages.filter((m) => m.status !== "sent"); | |
| 318 | expect(unsent.length).toBe(5); | |
| 319 | for (const m of unsent) { | |
| 320 | expect(m.channel).toBe("email"); | |
| 321 | expect(m.signature).not.toContain("STOP"); | |
| 322 | expect(m.body).toContain("Best,"); | |
| 323 | } | |
| 324 | expect(out.messages.find((m) => m.id === sentBefore.id)).toEqual(sentBefore); | |
| 325 | // Statuses are untouched: the due one is still due, the rest still scheduled. | |
| 326 | expect(unsent.map((m) => m.status)).toEqual(["due", "scheduled", "scheduled", "scheduled", "scheduled"]); | |
| 327 | }); | |
| 328 | ||
| 329 | test("an opted-out lead is never texted: not by tick, not by putting a closed lead back", () => { | |
| 330 | const lead = must(addLead(db, alice, { ...maria, name: "Quiet Now" }, at(0))); | |
| 331 | must(sendReply(db, alice, { id: lead.id }, at(0))); | |
| 332 | must(optOutTexts(db, alice, { id: lead.id }, at(0, 5))); | |
| 333 | must(tick(db, alice, at(40))); | |
| 334 | expect(must(getLead(db, alice, { id: lead.id })).messages.every((m) => m.channel === (m.status === "sent" ? "text" : "email"))).toBe(true); | |
| 335 | must(setState(db, alice, { id: lead.id, state: "lost" }, at(41))); | |
| 336 | const back = must(setState(db, alice, { id: lead.id, state: "open" }, at(42))); | |
| 337 | expect(back.messages.filter((m) => m.status !== "sent").every((m) => m.channel === "email")).toBe(true); | |
| 338 | // A new inquiry with the same phone and consent is a new lead; the old one stays opted out. | |
| 339 | const again = must(addLead(db, alice, { ...maria, name: "Quiet Now" }, at(43))); | |
| 340 | expect(again.id).not.toBe(lead.id); | |
| 341 | const old = must(getLead(db, alice, { id: lead.id })); | |
| 342 | expect(old.consent).toBe(false); | |
| 343 | expect(old.opted_out_at).not.toBeNull(); | |
| 344 | // The file itself refuses a text for them. | |
| 345 | expect(() => db.query("UPDATE message SET channel = 'text' WHERE lead_id = ?1 AND day = 30").run(lead.id)).toThrow(); | |
| 346 | }); | |
| 347 | ||
| 348 | test("opting out twice is harmless; a lead that never consented has nothing to stop", () => { | |
| 349 | const lead = must(addLead(db, alice, { ...maria, name: "Twice" }, at(0))); | |
| 350 | const one = must(optOutTexts(db, alice, { id: lead.id }, at(1))); | |
| 351 | const two = must(optOutTexts(db, alice, { id: lead.id }, at(2))); | |
| 352 | expect(two).toEqual(one); | |
| 353 | const never = must(addLead(db, alice, { ...maria, name: "Never", consent: false }, at(0))); | |
| 354 | const refused = optOutTexts(db, alice, { id: never.id }); | |
| 355 | expect(refused.ok).toBe(false); | |
| 356 | expect(!refused.ok && refused.error).toBe("invalid"); | |
| 357 | expect(optOutTexts(db, anon, { id: lead.id }).ok).toBe(false); | |
| 358 | expect(optOutTexts(db, alice, { id: 999_999 }).ok).toBe(false); | |
| 359 | }); | |
| 360 | ||
| 361 | test("an opt-out is written to the audit trail, and the sample reset clears it with the rest", () => { | |
| 362 | const lead = must(addLead(db, alice, { ...maria, name: "Audited Stop" }, at(0))); | |
| 363 | must(optOutTexts(db, alice, { id: lead.id }, at(1))); | |
| 364 | const row = db | |
| 365 | .query<{ act: string; who_name: string; outcome: string }, [string]>( | |
| 366 | "SELECT act, who_name, outcome FROM audit WHERE target = ?1 AND act = 'opted_out'", | |
| 367 | ) | |
| 368 | .get(String(lead.id)); | |
| 369 | expect(row).toEqual({ act: "opted_out", who_name: "alice", outcome: "ok" }); | |
| 370 | must(resetSample(db, root)); | |
| 371 | expect(db.query("SELECT count(*) AS n FROM lead WHERE opted_out_at IS NOT NULL").get()).toEqual({ n: 0 }); | |
| 372 | }); |
src/actions.ts +33
| @@ -25,6 +25,7 @@ import { | ||
| 25 | 25 | reopen, |
| 26 | 26 | MAX_QUERY, |
| 27 | 27 | insertLead, |
| 28 | optOut, | |
| 28 | 29 | nextNeeding, |
| 29 | 30 | readyFor, |
| 30 | 31 | redraftUnsent, |
| @@ -380,6 +381,38 @@ function mark( | ||
| 380 | 381 | return ok(detail(db, found.value.id)!); |
| 381 | 382 | } |
| 382 | 383 | |
| 384 | /** | |
| 385 | * They replied STOP. Texting ends for this lead for good (nothing in the app | |
| 386 | * turns it back on) and every unsent message becomes an email. Asking again | |
| 387 | * is harmless; a lead who never agreed to texts has nothing to stop. | |
| 388 | */ | |
| 389 | export const optOutTexts = ( | |
| 390 | db: Database, | |
| 391 | who: Who, | |
| 392 | input: { id: number }, | |
| 393 | at?: Date, | |
| 394 | ): Result<LeadDetail> => | |
| 395 | audited(db, who, "lead", "update", input.id, stopTexts(db, who, input, at), "opted_out"); | |
| 396 | ||
| 397 | function stopTexts( | |
| 398 | db: Database, | |
| 399 | who: Who, | |
| 400 | input: { id: number }, | |
| 401 | at?: Date, | |
| 402 | ): Result<LeadDetail> { | |
| 403 | const found = leadFor(db, who, "update", input.id, { | |
| 404 | signIn: "Sign in to update this lead.", | |
| 405 | no: "You may not update this lead.", | |
| 406 | }); | |
| 407 | if (!found.ok) return found; | |
| 408 | if (found.value.opted_out_at !== null) return found; | |
| 409 | if (!found.value.consent) | |
| 410 | return fail("invalid", "This lead never agreed to be texted, so there is nothing to stop."); | |
| 411 | optOut(db, found.value, stamp(at), currentAgent(db)); | |
| 412 | emit("lead.changed", { id: found.value.id }); | |
| 413 | return ok(detail(db, found.value.id)!); | |
| 414 | } | |
| 415 | ||
| 383 | 416 | /** Remove one lead and everything said to it — a test entry, a spammer. */ |
| 384 | 417 | export const deleteLead = ( |
| 385 | 418 | db: Database, |
src/audit.ts +2 −2
| @@ -32,8 +32,8 @@ export interface AuditRow { | ||
| 32 | 32 | readonly act: string | null; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | /** Messages sent are `reply` and `first_reply`; a close is `qualified` or `lost`; `reopened` puts it back. */ | |
| 36 | export const ACTS = ["reply", "first_reply", "qualified", "lost", "reopened"] as const; | |
| 35 | /** Messages sent are `reply` and `first_reply`; a close is `qualified` or `lost`; `reopened` puts it back; `opted_out` is a lead who asked to stop texts. */ | |
| 36 | export const ACTS = ["reply", "first_reply", "qualified", "lost", "reopened", "opted_out"] as const; | |
| 37 | 37 | export type Act = (typeof ACTS)[number]; |
| 38 | 38 | |
| 39 | 39 | export interface Attempt { |
src/db.ts +4
| @@ -98,6 +98,10 @@ export const migrations: string[] = [ | ||
| 98 | 98 | // the Activity page can say it and filter to messages sent. Older rows have none. |
| 99 | 99 | `ALTER TABLE audit ADD COLUMN act TEXT`, |
| 100 | 100 | `CREATE INDEX audit_act ON audit (act, id)`, |
| 101 | // When a lead asked us to stop texting (replied STOP). Opting out clears | |
| 102 | // `consent` too, so the consent rule and its triggers hold with no change; | |
| 103 | // nothing in the app sets it back. | |
| 104 | `ALTER TABLE lead ADD COLUMN opted_out_at TEXT`, | |
| 101 | 105 | ]; |
| 102 | 106 | |
| 103 | 107 | export function openDb(dir = dataDir): Database { |
src/leads.ts +27
| @@ -35,6 +35,8 @@ export interface Lead { | ||
| 35 | 35 | readonly message: string; |
| 36 | 36 | /** Prior written consent to be texted. */ |
| 37 | 37 | readonly consent: boolean; |
| 38 | /** When they asked to stop texts (consent ended); null if they never did. */ | |
| 39 | readonly opted_out_at: string | null; | |
| 38 | 40 | readonly state: State; |
| 39 | 41 | readonly created_at: string; |
| 40 | 42 | /** When the first reply went out; null while they wait. */ |
| @@ -402,6 +404,31 @@ export function redraftUnsent(db: Database, agent: Agent): void { | ||
| 402 | 404 | })(); |
| 403 | 405 | } |
| 404 | 406 | |
| 407 | /** | |
| 408 | * They replied STOP: consent ends, and every message not yet sent is | |
| 409 | * drafted again as an email (the edit of an unsent text is replaced). | |
| 410 | * Sent messages are history and stay as they were. Doing it twice | |
| 411 | * changes nothing the second time. | |
| 412 | */ | |
| 413 | export function optOut(db: Database, found: Lead, at: string, agent: Agent): void { | |
| 414 | db.transaction(() => { | |
| 415 | db.query( | |
| 416 | "UPDATE lead SET consent = 0, opted_out_at = coalesce(opted_out_at, ?1), updated_at = ?1 WHERE id = ?2", | |
| 417 | ).run(at, found.id); | |
| 418 | const now = findLead(db, found.id)!; | |
| 419 | const channel = channelFor(now); | |
| 420 | const unsent = db | |
| 421 | .query<{ id: number; day: number }, [number]>( | |
| 422 | "SELECT id, day FROM message WHERE lead_id = ?1 AND status <> 'sent'", | |
| 423 | ) | |
| 424 | .all(found.id); | |
| 425 | for (const m of unsent) | |
| 426 | db.query( | |
| 427 | "UPDATE message SET channel = ?1, body = ?2, signature = ?3 WHERE id = ?4", | |
| 428 | ).run(channel, draft(now, m.day, agent), signature(channel, agent), m.id); | |
| 429 | })(); | |
| 430 | } | |
| 431 | ||
| 405 | 432 | /** Qualified or lost: the lead leaves the cadence, and nothing more is drafted to go. */ |
| 406 | 433 | export function close( |
| 407 | 434 | db: Database, |
src/mcp.ts +8
| @@ -14,6 +14,7 @@ import { | ||
| 14 | 14 | getProfile, |
| 15 | 15 | listLeads, |
| 16 | 16 | saveProfile, |
| 17 | optOutTexts, | |
| 17 | 18 | approveReply, |
| 18 | 19 | setState, |
| 19 | 20 | type Result, |
| @@ -124,6 +125,13 @@ export const tools: readonly Tool[] = [ | ||
| 124 | 125 | run: (db, who, args) => |
| 125 | 126 | setState(db, who, { id: Number(args.id), state: args.state }), |
| 126 | 127 | }, |
| 128 | { | |
| 129 | name: "opt_out_texts", | |
| 130 | description: | |
| 131 | "A lead replied STOP: no more texts to them, for good (there is no way to turn texting back on). Every unsent draft becomes an email; sent messages stay as they were.", | |
| 132 | inputSchema: ONE, | |
| 133 | run: (db, who, args) => optOutTexts(db, who, { id: Number(args.id) }), | |
| 134 | }, | |
| 127 | 135 | { |
| 128 | 136 | name: "get_profile", |
| 129 | 137 | description: |
src/pages/lead.ts +12 −1
| @@ -77,7 +77,7 @@ export function leadPage(input: { | ||
| 77 | 77 | } |
| 78 | 78 | ${ |
| 79 | 79 | channelFor(lead) === "email" |
| 80 | ? `<p class="hint">Email only: ${lead.consent ? "no phone number to text." : "they have not given written consent to be texted."}</p>` | |
| 80 | ? `<p class="hint">Email only: ${lead.consent ? "no phone number to text." : lead.opted_out_at !== null ? "they asked to stop texts." : "they have not given written consent to be texted."}</p>` | |
| 81 | 81 | : "" |
| 82 | 82 | } |
| 83 | 83 | </div>`; |
| @@ -120,6 +120,16 @@ export function leadPage(input: { | ||
| 120 | 120 | </section>` |
| 121 | 121 | : ""; |
| 122 | 122 | |
| 123 | const stop = | |
| 124 | mayUpdate && lead.consent | |
| 125 | ? `<section class="states"> | |
| 126 | <form method="post" action="${path}/opt-out"> | |
| 127 | <p class="hint">Only after they replied STOP. Texting to them ends for good, and every unsent draft, edited or not, is replaced by an email.</p> | |
| 128 | <label class="check"><input type="checkbox" required> Yes, they asked to stop texts</label> | |
| 129 | <button class="button button-quiet">They asked to stop texts</button> | |
| 130 | </form> | |
| 131 | </section>` | |
| 132 | : ""; | |
| 123 | 133 | // A page that needs nothing from the agent never leaves them stuck. |
| 124 | 134 | const onward = |
| 125 | 135 | ready !== undefined |
| @@ -155,6 +165,7 @@ export function leadPage(input: { | ||
| 155 | 165 | ${thread} |
| 156 | 166 | ${plan} |
| 157 | 167 | ${states} |
| 168 | ${stop} | |
| 158 | 169 | </div>`, |
| 159 | 170 | }); |
| 160 | 171 | } |
src/pages/parts.ts +3 −1
| @@ -25,7 +25,9 @@ export const source = (lead: Lead): string => | ||
| 25 | 25 | export const consent = (lead: Lead): string => |
| 26 | 26 | channelFor(lead) === "text" |
| 27 | 27 | ? `<span class="badge" data-consent="text" title="They gave written consent to be texted">Text OK</span>` |
| 28 | : `<span class="badge" data-consent="email" title="No written consent to text: email only">Email only</span>`; | |
| 28 | : lead.opted_out_at !== null | |
| 29 | ? `<span class="badge" data-consent="email" title="They asked to stop texts">Email only (opted out ${escape(day(lead.opted_out_at))})</span>` | |
| 30 | : `<span class="badge" data-consent="email" title="No written consent to text: email only">Email only</span>`; | |
| 29 | 31 | |
| 30 | 32 | export const STATE_LABEL: Readonly<Record<Lead["state"], string>> = { |
| 31 | 33 | new: "New", |
src/routes.ts +27
| @@ -25,6 +25,7 @@ import { | ||
| 25 | 25 | listLeads, |
| 26 | 26 | resetSample, |
| 27 | 27 | saveProfile, |
| 28 | optOutTexts, | |
| 28 | 29 | approveReply, |
| 29 | 30 | nextLead, |
| 30 | 31 | setState, |
| @@ -120,6 +121,11 @@ const LEAD_FIELDS = { | ||
| 120 | 121 | type: "boolean", |
| 121 | 122 | description: "Prior written consent to be texted. Without it, only email.", |
| 122 | 123 | }, |
| 124 | opted_out_at: { | |
| 125 | type: ["string", "null"], | |
| 126 | format: "date-time", | |
| 127 | description: "When they asked to stop texts; consent is false from then on.", | |
| 128 | }, | |
| 123 | 129 | state: { enum: ["new", "contacted", "qualified", "lost"] }, |
| 124 | 130 | created_at: STAMP, |
| 125 | 131 | replied_at: { type: ["string", "null"], format: "date-time" }, |
| @@ -300,6 +306,12 @@ async function readFields( | ||
| 300 | 306 | const body = await readJson(request); |
| 301 | 307 | return body === undefined ? undefined : fields(body); |
| 302 | 308 | } |
| 309 | // A POST with no body at all (a bare `curl -X POST`) has no fields; a garbled one is still a fault. | |
| 310 | if ( | |
| 311 | !request.headers.has("content-type") && | |
| 312 | (request.headers.get("content-length") ?? "0") === "0" | |
| 313 | ) | |
| 314 | return {}; | |
| 303 | 315 | const form = await readForm(request); |
| 304 | 316 | return form === undefined |
| 305 | 317 | ? undefined |
| @@ -529,6 +541,21 @@ export const routes: readonly Route[] = [ | ||
| 529 | 541 | answer: LEAD, |
| 530 | 542 | }, |
| 531 | 543 | }, |
| 544 | { | |
| 545 | method: "POST", | |
| 546 | path: "/leads/:id/opt-out", | |
| 547 | handle: (c) => | |
| 548 | posted( | |
| 549 | c, | |
| 550 | () => optOutTexts(c.db, c.who, { id: ID(c) }), | |
| 551 | () => ({ to: `/leads/${ID(c)}`, said: "Texts stopped. Unsent drafts are now emails." }), | |
| 552 | ), | |
| 553 | api: { | |
| 554 | summary: | |
| 555 | "They replied STOP: no more texts to this lead, for good. Every unsent draft becomes an email; sent messages stay as they were. No body needed; asking twice changes nothing.", | |
| 556 | answer: LEAD, | |
| 557 | }, | |
| 558 | }, | |
| 532 | 559 | { |
| 533 | 560 | method: "POST", |
| 534 | 561 | path: "/tick", |
src/sentences.ts +5
| @@ -35,6 +35,11 @@ export function deed( | ||
| 35 | 35 | done: `put ${lead} back in follow-ups`, |
| 36 | 36 | tried: `put ${lead} back in follow-ups`, |
| 37 | 37 | }; |
| 38 | if (row.act === "opted_out") | |
| 39 | return { | |
| 40 | done: `recorded that ${lead} asked to stop texts`, | |
| 41 | tried: `record that ${lead} asked to stop texts`, | |
| 42 | }; | |
| 38 | 43 | if (row.verb === "create") |
| 39 | 44 | return { done: "added a lead", tried: "add a lead" }; |
| 40 | 45 | if (row.verb === "delete") |
src/server.test.ts +45
| @@ -288,6 +288,7 @@ test("the MCP server lists the tools and calls them as the caller", async () => | ||
| 288 | 288 | "new_sample_inquiry", |
| 289 | 289 | "send_reply", |
| 290 | 290 | "mark_lead", |
| 291 | "opt_out_texts", | |
| 291 | 292 | "get_profile", |
| 292 | 293 | "set_profile", |
| 293 | 294 | ]); |
| @@ -657,3 +658,47 @@ test("POST /sample/inquiry: the button and the JSON route make the same lead; an | ||
| 657 | 658 | const refused = await api("/sample/inquiry", { method: "POST", ...jsonBody({}) }, {}); |
| 658 | 659 | expect([401, 403]).toContain(refused.status); |
| 659 | 660 | }); |
| 661 | ||
| 662 | test("a lead is opted out from its page, the API and MCP; same-origin only; no way back", async () => { | |
| 663 | const one = await created({ ...NEW, name: "Page Stop" }); | |
| 664 | const page = await (await api(`/leads/${one.id}`, {}, alice)).text(); | |
| 665 | expect(page).toContain(`action="/leads/${one.id}/opt-out"`); | |
| 666 | expect(page).toContain("They asked to stop texts"); | |
| 667 | const cross = await api(`/leads/${one.id}/opt-out`, { method: "POST", headers: { "sec-fetch-site": "cross-site" }, body: form({}) }, alice); | |
| 668 | expect(cross.status).toBe(403); | |
| 669 | expect((await lead(one.id)).messages[0]!.channel).toBe("text"); | |
| 670 | const done = await api(`/leads/${one.id}/opt-out`, { method: "POST", body: form({}), redirect: "manual" }, alice); | |
| 671 | expect(done.status).toBe(303); | |
| 672 | const after = await (await api(`/leads/${one.id}`, {}, alice)).text(); | |
| 673 | expect(after).toContain("Email only (opted out"); | |
| 674 | expect(after).not.toContain("opt-out"); | |
| 675 | expect((await lead(one.id)).messages[0]!.channel).toBe("email"); | |
| 676 | ||
| 677 | const two = await created({ ...NEW, name: "Api Stop" }); | |
| 678 | expect((await api(`/leads/${two.id}/opt-out`, { method: "POST" }, {})).status).toBeOneOf([401, 403]); | |
| 679 | const res = await api(`/leads/${two.id}/opt-out`, { method: "POST", ...jsonBody({}) }, alice); | |
| 680 | expect(res.status).toBe(200); | |
| 681 | expect(((await res.json()) as { consent: boolean }).consent).toBe(false); | |
| 682 | expect((await api(`/leads/${two.id}/opt-out`, { method: "POST", ...jsonBody({}) }, alice)).status).toBe(200); | |
| 683 | ||
| 684 | const three = await created({ ...NEW, name: "Mcp Stop" }); | |
| 685 | const viaMcp = await rpc({ jsonrpc: "2.0", id: 9, method: "tools/call", params: { name: "opt_out_texts", arguments: { id: three.id } } }, alice); | |
| 686 | expect(viaMcp.result.isError).toBeUndefined(); | |
| 687 | expect((await lead(three.id)).messages[0]!.channel).toBe("email"); | |
| 688 | ||
| 689 | const never = await created({ ...NEW, name: "No Consent", consent: false }); | |
| 690 | expect(await (await api(`/leads/${never.id}`, {}, alice)).text()).not.toContain("opt-out"); | |
| 691 | expect((await api(`/leads/${never.id}/opt-out`, { method: "POST", ...jsonBody({}) }, alice)).status).toBe(422); | |
| 692 | expect(await (await api("/openapi.json", {}, alice)).text()).toContain("/leads/{id}/opt-out"); | |
| 693 | }); | |
| 694 | ||
| 695 | test("a bodyless POST has no fields, but a garbled form body still fails", async () => { | |
| 696 | const one = await created({ ...NEW, name: "Garbled" }); | |
| 697 | expect((await api(`/leads/${one.id}/opt-out`, { method: "POST" }, alice)).status).toBeLessThan(400); | |
| 698 | const garbled = await api( | |
| 699 | "/leads", | |
| 700 | { method: "POST", headers: { "content-type": "multipart/form-data" }, body: "garbage" }, | |
| 701 | alice, | |
| 702 | ).then((r) => r.status, () => 500); | |
| 703 | expect(garbled).toBeGreaterThanOrEqual(400); | |
| 704 | }); |
src/terminal.test.ts +22
| @@ -328,3 +328,25 @@ test("o puts a closed lead back in follow-ups", () => { | ||
| 328 | 328 | expect(read(t)).toContain("Maria Gonzalez: back in follow-ups."); |
| 329 | 329 | expect(read(t)).toContain("q qualified"); |
| 330 | 330 | }); |
| 331 | ||
| 332 | test("t asks, then y records that they asked to stop texts; any other key does not", () => { | |
| 333 | const db = fresh(); | |
| 334 | const t = terminal(db, alice); | |
| 335 | t.key("\r"); | |
| 336 | expect(read(t)).toContain("t they asked to stop texts"); | |
| 337 | const id = (listLeads(db, alice) as { ok: true; value: { leads: { id: number }[] } }).value.leads[0]!.id; | |
| 338 | t.key("t"); | |
| 339 | expect(read(t)).toContain("texting cannot be turned back on"); | |
| 340 | t.key("n"); | |
| 341 | expect((getLead(db, alice, { id }) as { ok: true; value: { consent: boolean } }).value.consent).toBe(true); | |
| 342 | t.key("t"); | |
| 343 | t.key("y"); | |
| 344 | expect(read(t)).toContain("Texts stopped"); | |
| 345 | const after = (getLead(db, alice, { id }) as { ok: true; value: { consent: boolean; messages: { channel: string }[] } }).value; | |
| 346 | expect(after.consent).toBe(false); | |
| 347 | expect(after.messages[0]!.channel).toBe("email"); | |
| 348 | expect(read(t)).toContain("they asked to stop texts."); | |
| 349 | expect(read(t)).not.toContain("t they asked to stop texts"); | |
outdated
LOW: |
||
| 350 | t.key("t"); // no longer offered | |
| 351 | expect(read(t)).not.toContain("texting cannot"); | |
| 352 | }); |
src/terminal.ts +17 −5
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | * Screens: |
| 8 | 8 | * desk j/k move · enter open · f view (needs/open/closed/all) · / find by name, email or phone · n log a lead · i new sample inquiry · p profile · a activity (admin) · R restart samples (admin) · esc quit |
| 9 | 9 | * lead s approve and send · e edit, then send · ] next lead that needs you · |
| 10 | * q qualified · x lost · D remove (admin) · esc back | |
| 10 | * q qualified · x lost · t asked to stop texts · D remove (admin) · esc back | |
| 11 | 11 | * edit type; enter is a new line · ctrl-d approve and send · esc cancel |
| 12 | 12 | * add one field at a time; enter next · esc cancel |
| 13 | 13 | * profile the name, brokerage and license every message carries · |
| @@ -29,6 +29,7 @@ import { | ||
| 29 | 29 | getLead, |
| 30 | 30 | getProfile, |
| 31 | 31 | listLeads, |
| 32 | optOutTexts, | |
| 32 | 33 | resetSample, |
| 33 | 34 | saveProfile, |
| 34 | 35 | approveReply, |
| @@ -112,7 +113,7 @@ type View = | ||
| 112 | 113 | | { name: "lead"; id: number } |
| 113 | 114 | | { name: "edit"; id: number; text: string } |
| 114 | 115 | | { name: "add"; step: number; typed: string; fields: Record<string, string> } |
| 115 | | { name: "confirm"; what: "remove" | "reset"; id?: number; back: View }; | |
| 116 | | { name: "confirm"; what: "remove" | "reset" | "opt-out"; id?: number; back: View }; | |
| 116 | 117 | |
| 117 | 118 | const titleOf = (m: Message): string => |
| 118 | 119 | m.day === 0 ? "First reply" : `Day ${m.day} follow-up`; |
| @@ -257,7 +258,9 @@ export function terminal( | ||
| 257 | 258 | dim( |
| 258 | 259 | l.consent |
| 259 | 260 | ? " Email only: no phone number to text." |
| 260 | : " Email only: no written consent to text.", | |
| 261 | : l.opted_out_at !== null | |
| 262 | ? " Email only: they asked to stop texts." | |
| 263 | : " Email only: no written consent to text.", | |
outdated
LOW: the added ternary branch is indented inconsistently with its sibling |
||
| 261 | 264 | ), |
| 262 | 265 | ); |
| 263 | 266 | const body = editing === undefined ? r.body : `${editing}█`; |
| @@ -284,6 +287,7 @@ export function terminal( | ||
| 284 | 287 | ...(l.state === "qualified" || l.state === "lost" |
| 285 | 288 | ? ["o put back in follow-ups"] |
| 286 | 289 | : ["q qualified", "x lost"]), |
| 290 | ...(l.consent ? ["t they asked to stop texts"] : []), | |
| 287 | 291 | ...(nextLead(db, me, { except: l.id }) === null ? [] : ["] next that needs you"]), |
| 288 | 292 | ...(admin() ? ["D remove"] : []), |
| 289 | 293 | "esc back", |
| @@ -420,7 +424,9 @@ export function terminal( | ||
| 420 | 424 | : [ |
| 421 | 425 | view.what === "reset" |
| 422 | 426 | ? "Restart the sample leads from now? Every lead on the desk is replaced." |
| 423 | : `Remove ${clean(lead(view.id ?? 0)?.name ?? "this lead")} and everything said to them?`, | |
| 427 | : view.what === "opt-out" | |
| 428 | ? `${clean(lead(view.id ?? 0)?.name ?? "This lead")} asked to stop texts? Every unsent draft becomes an email, and texting cannot be turned back on.` | |
| 429 | : `Remove ${clean(lead(view.id ?? 0)?.name ?? "this lead")} and everything said to them?`, | |
| 424 | 430 | "", |
| 425 | 431 | dim("y yes · any other key no"), |
| 426 | 432 | ]; |
| @@ -545,7 +551,9 @@ export function terminal( | ||
| 545 | 551 | } else if (input === "o" && CLOSED.includes(l.state)) { |
| 546 | 552 | const back = setState(db, me, { id, state: "open" }); |
| 547 | 553 | said = back.ok ? `${l.name}: back in follow-ups.` : back.message; |
| 548 | } else if (input === "D") | |
| 554 | } else if (input === "t" && l.consent) | |
| 555 | view = { name: "confirm", what: "opt-out", id, back: view }; | |
| 556 | else if (input === "D") | |
| 549 | 557 | view = { name: "confirm", what: "remove", id, back: view }; |
| 550 | 558 | return; |
| 551 | 559 | } |
| @@ -604,6 +612,10 @@ export function terminal( | ||
| 604 | 612 | said = reset.ok ? "The sample leads start again from now." : reset.message; |
| 605 | 613 | cursor = 0; |
| 606 | 614 | view = { name: "desk" }; |
| 615 | } else if (what === "opt-out") { | |
| 616 | const stopped = optOutTexts(db, me, { id: id ?? 0 }); | |
| 617 | said = stopped.ok ? "Texts stopped. Unsent drafts are now emails." : stopped.message; | |
| 618 | view = back; | |
| 607 | 619 | } else { |
| 608 | 620 | const removed = deleteLead(db, me, { id: id ?? 0 }); |
| 609 | 621 | said = removed.ok ? "Removed." : removed.message; |
new above
offline
MEDIUM:
readForm(request).catch(() => new FormData())changes every form-posting route (add lead, profile, send, mark), not just opt-out. A malformed or garbage body used to fail loudly. Now it silently becomes empty fields, which hides client bugs and invalid input at the boundary. Fix: leave readFields as it was. The opt-out handler never reads fields (postedcalls it for you), so handle the empty-body case only for this route, or have readFields treat an absent body (no content-type or content-length 0) as empty rather than catching all errors. Add a test that a malformed form body on another route still fails.