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

opened this

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.

Notes

opened this

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 new src/lost.ts.
  • Validation: setState in src/actions.ts checks the reason, so every door gets the same rule. A missing or unknown reason on state: lost is 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: x now asks "Why was <lead> lost?" and 1–5 picks the reason. esc or any other key changes nothing.
  • API and MCP: POST /leads/:id/state, PATCH /leads/:id and the MCP mark_lead tool take reason. The lead JSON gains lostReason and lostAt, and contract/openapi.json is 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 on activity(), so it should merge cleanly with #32.
  • CSV: a final lost_reason column. 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_reason and lost_at columns, with lost_at backfilled from updated_at for 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.ts replays 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.

Closes #51

4 earlier comments
commented

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 new src/lost.ts.
  • Validation: setState in src/actions.ts checks the reason, so every door gets the same rule. A missing or unknown reason on state: lost is 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: x now asks "Why was <lead> lost?" and 1–5 picks the reason. esc or any other key changes nothing.
  • API and MCP: POST /leads/:id/state, PATCH /leads/:id and the MCP mark_lead tool take reason. The lead JSON gains lostReason and lostAt, and contract/openapi.json is 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 on activity(), so it should merge cleanly with #32.
  • CSV: a final lost_reason column. 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_reason and lost_at columns, with lost_at backfilled from updated_at for 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.ts replays 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.

commented

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 new closed: { state: "lost", reason } field on the sample entries.
  • Sample test: A new test in src/sample.test.ts checks that the sample desk's Activity counts show at least three real reasons and no "no reason given".
  • close(): It now takes a Closing type in src/leads.ts, either { state: "qualified" } or { state: "lost", reason }. The default of null is gone, so a lost lead can no longer be stored without a reason. setState already 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_reason column holds the stored key (bought_elsewhere), not the label, which suits a spreadsheet filter.
commented

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.com lead rebuild migration now comes first, and this pull's lost_reason / lost_at migrations come after it. New migrations are appended, so databases that already ran main's keep their order.
  • src/db.test.ts: I kept main's findIndex-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 a realtor_com lead, as on main. Grace Kim keeps this branch's closed: { state: "qualified" } form, since close() 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.

commented

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.

commented

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.

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs