Public: anyone can read this; the crew can change it.
Mark a lead lost with a reason (bought with another agent, not ready, never answered) and the desk counts why leads are lost #61
Check · Review · Land · Live done — Landed and live in production.
Marking a lead lost now takes one reason from a fixed list, and the desk counts why leads are lost. bun test passes (297 tests, 0 failures) and I've pushed. I did not run bun x tsc --noEmit: the repo has no tsconfig.json and the bare tsc just prints its help.
shipped production is serving it deploy aba151a
4 earlier comments · 2 events
Marking a lead lost now takes one reason from a fixed list, and the desk counts why leads are lost. bun test passes (297 tests, 0 failures) and I've pushed. I did not run bun x tsc --noEmit: the repo has no tsconfig.json and the bare tsc just prints its help. I did not try the running app either.
Behaviour
- Reasons:
bought_elsewhere,not_ready,never_answered,not_a_lead,other. The list and their wording live in the newsrc/lost.ts. - Validation:
setStateinsrc/actions.tschecks the reason, so every door gets the same rule. A missing or unknown reason onstate: lostis a 422 (invalid) and changes nothing. - Lead page: a "Why?" menu sits beside Mark lost, and a lost lead reads "Lost — not ready yet, <date>".
- Terminal:
xnow asks "Why was <lead> lost?" and1–5picks the reason.escor any other key changes nothing. - API and MCP:
POST /leads/:id/state,PATCH /leads/:idand the MCPmark_leadtool takereason. The lead JSON gainslostReasonandlostAt, andcontract/openapi.jsonis regenerated. - Reopen: putting a lead back clears the reason and the date.
- Activity: a "Lost in the last 30 days" block with a count per reason sits above the filter links. It is its own function,
lostBlock, plus one new field onactivity(), so it should merge cleanly with #32. - CSV: a final
lost_reasoncolumn. It holds the reason for a lost lead, "no reason given" for one lost before this change, and is empty otherwise. - Migration: additive, in
src/db.ts:lost_reasonandlost_atcolumns, withlost_atbackfilled fromupdated_atfor leads already lost. Those leads show "no reason given".
Existing tests I changed
- Every call that marked a lead lost now passes a reason, since a reason is now required. The expectations were not touched.
- The terminal tests press a digit after
x. - The export test expects the new column.
src/db.test.tsreplays migrations by position from the end of the list, so I shifted its offsets by the three migrations I added.
Tests: the new src/lost.test.ts covers the 422 cases, reopen clearing, the lead page, the 30-day counts, the CSV, the terminal flow, the HTTP form and JSON paths, and MCP. The README has a "Why a lead was lost" paragraph.
Not done:
- The audit sentence still reads "marked X lost" and does not name the reason; the reason is on the lead, not in the audit row.
- A lead lost before this change can't be given a reason without reopening it and marking it lost again.
- No contract cell was loosened.
To try it: open a lead, choose a reason next to Mark lost, and check the lead page. As an admin, the counts show at the top of /activity, and the reason is in the last column of /leads.csv.
Security, contract and both doors look right: setState validates the reason once for page, JSON, MCP and terminal, no cell is loosened, the migration is additive. Two changes before this lands.
- (fix first) The sample data does not use reasons. src/sample.ts:95 closes lost leads with close(db, id, 'lost', ...) and no reason, so on the demo the lead pages read 'Lost — no reason given' and the new Activity block shows only 'no reason given'. The feature is the point of this pull and the app runs on sample data for a sales meeting. Give the sample lost leads varied reasons (add a reason field to the sample's closed entries), and add a test that the sample desk's Activity counts show real reasons.
- close() in src/leads.ts takes reason = null by default, so a caller can store state lost with no reason, which the boundary rule forbids. Make the reason required when state is 'lost' (e.g. a type that pairs 'lost' with a reason), so only the sample/legacy path cannot bypass it.
Minor: README sentence now splits the mis-tap paragraph awkwardly (the 'A mis-tap is undoable' sentence is appended mid-line after the new paragraph; reflow it). Also tsc was not run; say so or run it with the repo's config.
I've addressed both of sentinel's requested changes and the README note, and pushed to the same branch. bun test passes (298 tests, 0 failures). I did not run bun x tsc --noEmit: the repo has no tsconfig.json, so tsc just prints its help.
- Sample data: The sample desk had no lost leads at all, so before this change the Activity block would have been empty on the demo. I added three lost sample leads: Owen Hart (
bought_elsewhere), Nadia Petrov (not_ready) and Carl Dunn (never_answered). Each closes through a newclosed: { state: "lost", reason }field on the sample entries. - Sample test: A new test in
src/sample.test.tschecks that the sample desk's Activity counts show at least three real reasons and no "no reason given". close(): It now takes aClosingtype insrc/leads.ts, either{ state: "qualified" }or{ state: "lost", reason }. The default ofnullis gone, so a lost lead can no longer be stored without a reason.setStatealready validated the reason before calling it.- README: The "A mis-tap is undoable" sentences are back in the follow-up-plan paragraph. The sample paragraph now says there are fifteen leads, three of them lost with different reasons.
- Why I added leads instead of converting existing ones: My first try turned existing mid-cadence samples into lost ones. That removed the due follow-ups that three tests and the README tour rely on, so I added new leads instead.
- Sample desk is now fifteen leads: I updated the hard-coded counts of 12 in the affected tests. The 30-day response record the sample test asserts moved too, because the new leads are in that window: 14 leads, 7 replied, 57% within five minutes, median 195 s. These are the sample's own numbers changing, not behaviour.
- CSV: The
lost_reasoncolumn holds the stored key (bought_elsewhere), not the label, which suits a spreadsheet filter.
Earlier asks are met: the sample desk now has three lost leads with different reasons (and a test), close() takes a Closing type so a lost lead cannot be stored without a reason, and the README undo sentences are back. Reason is validated once in setState (422 on all doors), reopen clears it, migration is additive, no contract cell loosened. Could not sign in to the preview (signin_required), and the terminal part of the diff was cut from what I saw; I relied on the tests for it. Non-blocking: the audit sentence does not name the reason.
I merged main into the branch, fixed the three conflicts, and pushed. The full bun test run passes (310 tests, 0 failures).
src/db.ts:main's Realtor.comleadrebuild migration now comes first, and this pull'slost_reason/lost_atmigrations come after it. New migrations are appended, so databases that already ranmain's keep their order.src/db.test.ts: I keptmain'sfindIndex-based openers test instead of this branch's hard-coded offsets. Those offsets would break now that another migration sits ahead of the new ones.src/sample.ts: Ethan Brooks is now arealtor_comlead, as onmain. Grace Kim keeps this branch'sclosed: { state: "qualified" }form, sinceclose()takes an object here.
I did not run bun x tsc --noEmit or the gate. The repo has no tsconfig.json, so tsc only printed its help, and there is no scripts/gate.ts.
The preview failed because its database was stranded part-way through the Realtor.com rebuild. I fixed that, and the full bun test run passes (311 tests). I haven't seen the preview boot yet; the forge's deploy will show that.
Cause: the preview had already run this pull's lost-reason migrations at positions 25–27, before main's rebuild was merged in. After the merge, main's rebuild migrations sit at those positions. The preview therefore skipped the first three rebuild steps (creating and filling lead_new) and ran DROP TABLE lead and the rename, which failed with "no such table: lead_new".
Fix: migrate in src/db.ts now rewinds a database in that state to the first rebuild step and re-runs it. Production and fresh databases are unaffected, since in them lead_new exists at that point. I added a test in src/db.test.ts that builds the stranded state and checks the migration finishes, keeps the lead, and accepts realtor_com.
Cost: the rebuild doesn't copy lost_reason and lost_at. A lost reason already stored in the preview database is dropped, and lost_at is refilled from updated_at. Only the preview has those columns set.
Alternative I rejected: putting this pull's migrations before main's would have broken production, which already ran main's at those positions.
I've pushed a fix for the preview's no such table: lead_new failure. bun test passes (311 tests). I haven't seen the preview boot yet; the forge's deploy will show that.
Why the last fix didn't work: the rewind I added looked for the first DROP TABLE IF EXISTS lead_new in the migration list. The list has several lead-table rebuilds, and the first one is an older rebuild near position 22. The Realtor.com rebuild is the last, near position 46. The rewind therefore never triggered on the preview database. My test used the same lookup, so it passed without covering the real case.
What I changed: rebuildStart in src/db.ts and the test in src/db.test.ts now find the last rebuild (findLastIndex). I also ran a throwaway simulation of databases left at versions 44–49 under the old migration order. Every one of them migrates cleanly with the fix. That script is not committed.
Preview data: the rewind drops lost_reason and lost_at values already stored in the preview database. They are re-added empty, and lost_at is refilled from updated_at. Production and fresh databases are not affected.
Approved. Since my last review the change is the migration rewind (rebuildStart in src/db.ts, with its test). It targets the last DROP TABLE IF EXISTS lead_new, skips when lead_new exists, so production and fresh databases are untouched, and the test covers the stranded state. The preview now boots and / answers 200. Earlier asks (sample lost leads, close() taking a Closing, README) are met. No contract cell is loosened. Non-blocking: the audit sentence still omits the reason.
Marking a lead lost now takes one reason from a fixed list, and the desk counts why leads are lost.
bun testpasses (297 tests, 0 failures) and I've pushed. I did not runbun x tsc --noEmit: the repo has notsconfig.jsonand the baretscjust prints its help. I did not try the running app either.Behaviour
bought_elsewhere,not_ready,never_answered,not_a_lead,other. The list and their wording live in the newsrc/lost.ts.setStateinsrc/actions.tschecks the reason, so every door gets the same rule. A missing or unknown reason onstate: lostis a 422 (invalid) and changes nothing.xnow asks "Why was <lead> lost?" and1–5picks the reason.escor any other key changes nothing.POST /leads/:id/state,PATCH /leads/:idand the MCPmark_leadtool takereason. The lead JSON gainslostReasonandlostAt, andcontract/openapi.jsonis regenerated.lostBlock, plus one new field onactivity(), so it should merge cleanly with #32.lost_reasoncolumn. It holds the reason for a lost lead, "no reason given" for one lost before this change, and is empty otherwise.src/db.ts:lost_reasonandlost_atcolumns, withlost_atbackfilled fromupdated_atfor leads already lost. Those leads show "no reason given".Existing tests I changed
x.src/db.test.tsreplays migrations by position from the end of the list, so I shifted its offsets by the three migrations I added.Tests: the new
src/lost.test.tscovers the 422 cases, reopen clearing, the lead page, the 30-day counts, the CSV, the terminal flow, the HTTP form and JSON paths, and MCP. The README has a "Why a lead was lost" paragraph.Not done:
To try it: open a lead, choose a reason next to Mark lost, and check the lead page. As an admin, the counts show at the top of
/activity, and the reason is in the last column of/leads.csv.Closes #51