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

Merged opened this

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

Merged

A lead who replied STOP can now be opted out in one tap. That ends texts to them and turns every unsent draft into an email. bun test passes (131 tests, 0 failures).

Notes

shipped production is serving it deploy aba151a

opened this

A lead who replied STOP can now be opted out in one tap. That ends texts to them and turns every unsent draft into an email. bun test passes (131 tests, 0 failures). I could not run bun scripts/gate.ts or scripts/fast.ts because the clone doesn't have them. bun x tsc --noEmit only printed the compiler help, since the repo has no tsconfig, so there was no type check. bun run lint:design printed no findings.

What changed

  • Storage: a new append-only migration adds lead.opted_out_at. Opting out sets consent = 0 and stamps that column. channelFor and the existing consent triggers in src/db.ts therefore hold unchanged, and POST /tick and putting a closed lead back in follow-ups can't draft a text for them.
  • Redraft: optOut in src/leads.ts redrafts every message that isn't sent (draft, scheduled, due, and skipped) as an email, using the same drafting as a no-consent lead. A draft you had edited is replaced by a fresh email draft, and the lead page says so above the button. Sent messages are untouched.
  • Action: optOutTexts in src/actions.ts goes through leadFor("update"), so access is update lead through permit. contract.json is unchanged. It returns through audited with a new act, opted_out, which shows on the Activity page as "recorded that X asked to stop texts".
  • Repeat and no-consent cases:
    • Opting out twice returns the lead as it is, with no second audit row.
    • A lead that never consented gets a 422 and no button.
  • No way back: nothing in the app turns texting back on. A new POST /leads with the same email or phone makes a new lead and leaves the old one opted out.
  • Doors:
    • Page: a "They asked to stop texts" form with a confirm checkbox. Afterwards the badge reads "Email only (opted out <date>)".
    • API: POST /leads/:id/opt-out, same-origin checked, in /openapi.json. contract/openapi.json is regenerated, and opted_out_at is added to the lead schema.
    • MCP: an opt_out_texts tool.
    • Terminal: key t on a lead's screen, with a y/n confirm. It is listed in the help line and only offered while the lead can still be texted.
  • README: the consent paragraph now says what to do when someone replies STOP.
  • Sample reset: it clears opt-outs with the rest, because they live on the lead rows.
  • Tests: opt-out turns every unsent text into an email and leaves sent ones alone, including after POST /tick and after reopening a closed lead. Also covered: the audit row and sample reset, doing it twice, a lead that never consented, and the page, API, MCP and terminal doors.

Change beyond the issue: a bodyless POST with no content-type used to return a 500 in readFields (src/routes.ts). It now counts as having no fields, so POST /leads/:id/opt-out works with "no body needed".

Not done: I did not stop a new inquiry with an opted-out phone or email from being texted, because the issue only asks that the existing lead isn't re-enabled.

To try: open a sample lead with "Text OK", tick the box and press "They asked to stop texts". Or press t then y in the terminal, or run curl -X POST …/leads/<id>/opt-out.

🤖 Generated with Claude Code

Closes #28

1 earlier comment · 1 event
commented

I merged main into this branch and pushed it. The full bun test run passes (137 tests). I couldn't run bun x tsc --noEmit, and the gate, design-lint and fast scripts don't exist in this clone.

Conflicts. All five files (src/actions.ts, src/mcp.ts, src/routes.ts, src/pages/lead.ts, src/terminal.ts) conflicted in imports and in the lead page and terminal UI. In each, I kept both sides:

  • Opt-out: the "They asked to stop texts" form on the lead page, t in the terminal, and optOutTexts through the routes and the MCP.
  • From main: the "Next that needs you" link on the lead page, ] in the terminal, and approveReply and nextLead.
  • Terminal help: the header line and the lead key hints now list t and ] together.

Contract. contract/openapi.json was stale after the merge, so one test failed. I re-emitted it with bun run contract:emit.

Type check. In this sandbox bun x tsc --noEmit pulls in a newer TypeScript that only prints its help text, so it checked nothing. Please rely on the forge's check for types.

Scope. The opt-out behaviour is unchanged, and no contract cell was loosened.

Changes requested — before the latest push

The opt-out itself is sound. It asks permit through the existing lead/update cell, so no cell is loosened. It audits, is one-way and idempotent, and reaches the page, the API, the MCP and the terminal, with tests and README. One change to make: readFields in src/routes.ts now swallows every form-parse failure for all form routes. Please scope it to this route or drop it. Also fix the fragile terminal assertion noted inline.

commented

I addressed all three points from the review and pushed to the same branch. src/server.test.ts and src/terminal.test.ts pass; I didn't run the full suite or tsc this time.

  • readFields (src/routes.ts): it no longer catches every form-parse error. It now returns empty fields only for a POST that has no content-type header and a content-length of 0 or none. A garbled form body fails again on every route, as it did before this branch.
  • New test: src/server.test.ts checks that a bodyless opt-out succeeds and that a garbled multipart POST to /leads still fails. The failure is a thrown parse error that the server logs, so the test accepts a rejection or a status of 400 or more.
  • Terminal assertion (src/terminal.test.ts): it now asserts the hint no longer contains t they asked to stop texts, in place of the wrap-dependent \n check.
  • Ternary indent (src/terminal.ts): the opt-out branch is now indented like its sibling : " Email only: no written consent to text." line.
Approved

All earlier asks are met. readFields now treats a request as bodyless only when it has no content-type and a content-length of 0 or none, so a garbled form still fails, and a test covers both cases. The terminal assertion no longer depends on line wrapping, and the ternary indent matches its sibling. The opt-out goes through permit (lead update), has page, API, MCP and terminal doors, is audited, and has tests. No contract cell is loosened. The preview answers 200 on /. I did not exercise the opt-out on the preview itself. Non-blocking: the README could say the terminal key asks for a y to confirm.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs