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

Fix a lead's name, email or phone from the lead's page, and unsent drafts follow the new details #38

Merged opened this

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

Merged

An open lead's page now has "Edit details" for name, email and phone. bun test passes (174 tests) and design lint is clean. bun x tsc --noEmit isn't usable here: the repo has no tsconfig or dependencies, so bun x fetched a tsc that only printed its help text.

Notes

shipped production is serving it deploy aba151a

opened this

An open lead's page now has "Edit details" for name, email and phone. bun test passes (174 tests) and design lint is clean. bun x tsc --noEmit isn't usable here: the repo has no tsconfig or dependencies, so bun x fetched a tsc that only printed its help text. The repo has no scripts/gate.ts or scripts/fast.ts, so neither ran.

What changed

  • Edit (src/actions.ts): a new editLead action checks the fields the way addLead does, through one shared check in src/actions.ts. A field you leave out stays as it is. An empty email or phone takes it away, but at least one must remain. Only open leads can be edited. It uses the existing lead update permission, so no contract cell changed.
  • Drafts (src/leads.ts): every unsent draft is written again for the new details, by whichever channel the lead now allows.
    • If the phone is removed or the lead opted out, unsent texts become emails (the STOP rule, now shared with the opt-out code).
    • If the email is removed, unsent emails become texts only for a lead who agreed to texts.
    • Otherwise they stay emails. The page says they are waiting for an address, and sending is refused with that reason. There is no stored note, because messages have no column for one.
  • Audit: a new detail column on the audit table holds the changed fields with old and new values as JSON. One edited row is written per change that alters something; saving the same values writes nothing. The Activity page and the terminal show it, escaped, e.g. corrected Dana's details: phone “(206) 555-0199” → nothing.
  • Doors:
    • the page's "Edit details" section, in its own function in src/pages/lead.ts;
    • PATCH /leads/:id (sending a state and details together is a 422), and POST /leads/:id/edit; contract/openapi.json is re-emitted;
    • the MCP edit_lead tool;
    • d on the terminal's lead screen.

    A cross-site POST is refused by the existing door.

  • Docs: the README's "How it works" now covers this.
  • Tests: src/details.test.ts covers the edit and its audit row, the 422 cases, removing the phone, removing the email, and a refused request. It also covers the page form, the Activity wording, the PATCH/POST/MCP/terminal doors, and the migration keeping messages. I updated the MCP tool-list assertion in src/server.test.ts for the new tool.

Things to look at

  • Schema rebuild: the old lead.email column required at least 3 characters, so an email could never be removed. SQLite can't drop that rule in place, so a migration rebuilds the lead table. migrate in src/db.ts now turns foreign keys off around the migrations, runs a foreign-key check, and turns them back on, because dropping the old table would otherwise delete every message. A test checks that messages survive.
  • Changed test: the test in src/db.test.ts that re-runs migrations needed one more line. The new ALTER TABLE audit ADD COLUMN detail can't run twice, so the test drops that column first. The test was wrong for the new migration; its intent is unchanged.
  • Anon gets 401: the issue says an identity without update permission gets 403. The only identity without it is anon, which the app answers with 401 "sign in". I tested that and the audited refusal.
  • Not done: an edit never re-opens a closed lead, and the terminal doesn't validate until the last step.

Try it on the preview: open a lead, tap "Edit details", clear the phone and save, then check that the draft reads as an email and that Activity (as admin) shows the change.

🤖 Generated with Claude Code

Closes #35

2 earlier comments · 1 event
commented

An open lead's page now has "Edit details" for name, email and phone. bun test passes (174 tests) and design lint is clean. bun x tsc --noEmit isn't usable here: the repo has no tsconfig or dependencies, so bun x fetched a tsc that only printed its help text. The repo has no scripts/gate.ts or scripts/fast.ts, so neither ran.

What changed

  • Edit (src/actions.ts): a new editLead action checks the fields the way addLead does, through one shared check in src/actions.ts. A field you leave out stays as it is. An empty email or phone takes it away, but at least one must remain. Only open leads can be edited. It uses the existing lead update permission, so no contract cell changed.
  • Drafts (src/leads.ts): every unsent draft is written again for the new details, by whichever channel the lead now allows.
    • If the phone is removed or the lead opted out, unsent texts become emails (the STOP rule, now shared with the opt-out code).
    • If the email is removed, unsent emails become texts only for a lead who agreed to texts.
    • Otherwise they stay emails. The page says they are waiting for an address, and sending is refused with that reason. There is no stored note, because messages have no column for one.
  • Audit: a new detail column on the audit table holds the changed fields with old and new values as JSON. One edited row is written per change that alters something; saving the same values writes nothing. The Activity page and the terminal show it, escaped, e.g. corrected Dana's details: phone “(206) 555-0199” → nothing.
  • Doors:
    • the page's "Edit details" section, in its own function in src/pages/lead.ts;
    • PATCH /leads/:id (sending a state and details together is a 422), and POST /leads/:id/edit; contract/openapi.json is re-emitted;
    • the MCP edit_lead tool;
    • d on the terminal's lead screen.

    A cross-site POST is refused by the existing door.

  • Docs: the README's "How it works" now covers this.
  • Tests: src/details.test.ts covers the edit and its audit row, the 422 cases, removing the phone, removing the email, and a refused request. It also covers the page form, the Activity wording, the PATCH/POST/MCP/terminal doors, and the migration keeping messages. I updated the MCP tool-list assertion in src/server.test.ts for the new tool.

Things to look at

  • Schema rebuild: the old lead.email column required at least 3 characters, so an email could never be removed. SQLite can't drop that rule in place, so a migration rebuilds the lead table. migrate in src/db.ts now turns foreign keys off around the migrations, runs a foreign-key check, and turns them back on, because dropping the old table would otherwise delete every message. A test checks that messages survive.
  • Changed test: the test in src/db.test.ts that re-runs migrations needed one more line. The new ALTER TABLE audit ADD COLUMN detail can't run twice, so the test drops that column first. The test was wrong for the new migration; its intent is unchanged.
  • Anon gets 401: the issue says an identity without update permission gets 403. The only identity without it is anon, which the app answers with 401 "sign in". I tested that and the audited refusal.
  • Not done: an edit never re-opens a closed lead, and the terminal doesn't validate until the last step.

Try it on the preview: open a lead, tap "Edit details", clear the phone and save, then check that the draft reads as an email and that Activity (as admin) shows the change.

🤖 Generated with Claude Code

commented

I merged main into the branch, resolved the four conflicts, and pushed. bun test passes (185 tests) and design lint is clean.

  • src/db.ts: I put the lead-table rebuild and the audit detail column after main's new showing table. Migrations run by position, so a database that already has the showing migration still runs mine. The rebuild keeps the showing table's foreign key working, and the migrate change that turns foreign keys off around the list is untouched.
  • src/audit.ts: the list of recorded acts now has both sets: showing, showing_off and edited.
  • src/sentences.ts: the Activity wording for edits and for showings are both kept.
  • src/pages/lead.ts: the "Book a showing" section and the "Edit details" section are both kept, each in its own block.

I didn't run bun x tsc --noEmit again. The repo has no tsconfig, so it prints only help text, as before.

Changes requested — before the latest push

The feature works and is well tested, and no contract cell is loosened. Three things to fix in one round:

  1. src/contract.test.ts has no row for the new doors. Add one, like the showing block, saying that POST /leads/:id/edit, PATCH /leads/:id with details and the MCP edit_lead succeed exactly where the lead update cell allows (anon refused, user allowed).
  2. src/routes.ts: the doc comment "One POST, answered for whoever sent it" now sits above the new CONTACT/EDIT_FIELDS block instead of above posted. Move it back.
  3. src/leads.ts redraftFor builds SQL by joining status names into the string. The values are constants, but use a fixed query with bound parameters. Also check that the opt-out path (previously status <> sent, now draft/scheduled/due/skipped) misses no unsent status.

Also confirm that d on the terminal lead screen does not shadow an existing key.

commented

I've made all three changes and answered your terminal-key question, and pushed them to the branch. bun test passes (185 tests).

  1. Contract test: src/contract.test.ts now has an editLead row next to the showing one. It asserts that editing a lead's details succeeds exactly where the lead update cell allows: refused for anon, allowed for a user. It calls the action directly, as the showing rows do. The page, PATCH /leads/:id and the MCP edit_lead all go through that action, and details.test.ts already covers those doors.
  2. Doc comment: "One POST, answered for whoever sent it" is back above posted in src/routes.ts.
  3. Redraft query: redraftFor in src/leads.ts no longer builds SQL from a joined list. It takes a flag and picks one of two fixed queries.
    • Opt-out: it uses status <> 'sent', exactly the old condition. The message table allows only draft, scheduled, due, sent and skipped, so my earlier four-name list happened to cover the same rows, but the fixed query makes that true by construction.
    • Edit: it uses status IN ('draft','scheduled','due'). Skipped messages are deliberately left alone, since they were set aside by a call or a close.

On the terminal, d does not shadow anything. The lead-screen keys in use are s, ], e, q, x, c, w, o, t, D, b, u, esc and backspace, and d was free before this change. D (remove) is a different key, since terminal input is case-sensitive.

Approved

All three earlier asks are met. (1) src/contract.test.ts now has an editLead row beside the showing rows, so the lead-update cell is asserted for the new door. (2) The doc comment above posted is restored. (3) redraftFor picks one of two fixed queries, so no SQL is built from a list, and the opt-out path keeps its old status <> sent condition. I could not diff against 53161b129168 because the clone does not have that commit, so I judged the change as posted. It does what the issue asks. Every door (page, PATCH and POST, MCP edit_lead, terminal d) goes through editLead and permit. Input is validated through the shared contactProblem check, output is escaped, no contract cell is loosened, and the tests cover the behaviour. Non-blocking: the terminal only validates at the last step; a short message there would help.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs