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
Merged 32d 23h ago, 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.
-
Reviews
Changes requested — before the latest pushSecurity, 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.
Approved — before the latest pushEarlier 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.
ApprovedApproved. Since my last review the change is the migration rewind (
rebuildStartin src/db.ts, with its test). It targets the lastDROP TABLE IF EXISTS lead_new, skips whenlead_newexists, 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 aClosing, README) are met. No contract cell is loosened. Non-blocking: the audit sentence still omits the reason. -
Checks
shipped production is serving it deploy 231a7fb
-
History
- Find the Realtor.com rebuild by its last step, not the first lead rebuild, when rewinding a stranded database 55eedf1
- Rewind a database stranded mid-rebuild by the earlier migration order, so the preview boots 91f838c
- Merge main into mason/51-mark-a-lead-lost-with-a-reason-bought-wi 8c53c85
- An approved message opens ready to send in the agent's own Mail or Messages app, from the lead's page (#62) 7b27035
- Realtor.com is its own lead source: its leads get their own opener, their own row in the response record, and a sample lead (#57) 77ce7d2
- Sample lost leads carry reasons; close() requires a reason for lost f00f19d
- Mark a lead lost with one reason from a fixed list; count reasons on Activity; lost_reason in the CSV 37d394b
- Skip one upcoming follow-up on a lead, or bring it back, from the lead's page (#54) 6e5036e
- A lead who writes in again with the same email or phone lands on their existing lead, not as a second one (#53) 8a18d73
- The response record says how fast each lead source was answered (#52) 8f8afee
- Keep a private note on a lead (budget, timing, what they want) that shows at the top of their page and never goes to the lead (#49) a4075e8
- The agent rewrites how their first reply opens for each lead source, and sees a new draft in their words before saving (#45) 82f6c8b
- Paste a lead notification email (Zillow, Realtor.com, a website form) and the lead's name, email, phone, source and question are filled in to check and add (#48) 0c396da
- A text approved late at night waits until 8 a.m. and the desk says when it will go (#44) 4648e3e
- Follow-ups speak to what each lead asked about (#41) da7594d
- Download every lead and its status as a spreadsheet the agent can open in Excel or import into their CRM (#40) c9ea383
- Fix a lead's name, email or phone from the lead's page, and unsent drafts follow the new details (#38) d335462
- Book a showing on a lead: it shows on the desk and follow-ups wait until the day after (#37) a59448b
- Log a phone call on a lead so a reply by phone stops the clock and shows in the history (#34) 40a5759
- When a lead writes back, the agent pastes it in: the lead's follow-ups pause and a drafted answer waits on the desk (#31) f9fd252
4 earlier comments
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.
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.
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.
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