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

A lead who writes in again with the same email or phone lands on their existing lead, not as a second one #53

opened this

Merged 33d 14h ago, live in production

A lead who writes in again with the same email or phone now lands on their existing lead instead of becoming a second row. bun test passes (268 tests). I couldn't run bun x tsc --noEmit: tsc 7.0.2 only printed its help text here, so the type check is unverified.

Notes

opened this

A lead who writes in again with the same email or phone now lands on their existing lead instead of becoming a second row. bun test passes (268 tests). I couldn't run bun x tsc --noEmit: tsc 7.0.2 only printed its help text here, so the type check is unverified. The repo has no scripts/gate.ts, scripts/fast.ts or design-lint script.

What changed

  • Matching: findMatch in src/leads.ts matches on email in any letter case. It uses phone digits only when the arrival has no email, so two family members sharing a phone stay separate if they each give an email. A name alone never merges. When several leads match, an open one wins over a closed one, then the newest.
  • Merging: writeInAgain in src/leads.ts adds the new message to the existing lead's history through recordInbound, which drafts a fresh reply. If the lead was qualified or lost, it reopens it through the existing reopen path.
  • Where it applies: addLead in src/actions.ts handles this, so the desk form, pasted email, webhook, MCP and terminal all merge. The sample-inquiry button goes through the same path.
  • What the desk says: the web redirect says "<name> wrote in again", and the terminal says "<name> wrote in again; an answer is drafted."
  • Audit: a merge is recorded as one merged event. Its detail names the arrival's source and the lead, and the trail reads "<lead> wrote in again (via Zillow)". I added merged to ACTS in src/audit.ts and the wording in src/sentences.ts.
  • Permissions: the check is still permit(who, "lead", "create"). No contract.json cell changed.
  • Tests: new cases in src/leads.test.ts cover a different-case email, a differently typed phone, a no-match arrival that still creates a lead, and a closed lead reopening. I adjusted a few existing tests (src/actions.test.ts, src/audit.test.ts, src/server.test.ts and others) so they still pass.
  • README: "How it works" now has a paragraph on writing in twice.

What I chose not to do

  • I did not merge existing duplicate leads.
  • I did not merge on name.
  • I did not add a way to undo a merge.

To try it Add a lead on the desk, then add another with the same email in different letter case. You land on the first lead with "wrote in again", and the audit log shows one merged row.

I did not edit src/pages/desk.ts, which the issue lists. The "wrote in again" message goes through the existing redirect message, which the desk already shows.

Closes #50

2 earlier comments
commented

A lead who writes in again with the same email or phone now lands on their existing lead instead of becoming a second row. bun test passes (268 tests). I couldn't run bun x tsc --noEmit: tsc 7.0.2 only printed its help text here, so the type check is unverified. The repo has no scripts/gate.ts, scripts/fast.ts or design-lint script.

What changed

  • Matching: findMatch in src/leads.ts matches on email in any letter case. It uses phone digits only when the arrival has no email, so two family members sharing a phone stay separate if they each give an email. A name alone never merges. When several leads match, an open one wins over a closed one, then the newest.
  • Merging: writeInAgain in src/leads.ts adds the new message to the existing lead's history through recordInbound, which drafts a fresh reply. If the lead was qualified or lost, it reopens it through the existing reopen path.
  • Where it applies: addLead in src/actions.ts handles this, so the desk form, pasted email, webhook, MCP and terminal all merge. The sample-inquiry button goes through the same path.
  • What the desk says: the web redirect says "<name> wrote in again", and the terminal says "<name> wrote in again; an answer is drafted."
  • Audit: a merge is recorded as one merged event. Its detail names the arrival's source and the lead, and the trail reads "<lead> wrote in again (via Zillow)". I added merged to ACTS in src/audit.ts and the wording in src/sentences.ts.
  • Permissions: the check is still permit(who, "lead", "create"). No contract.json cell changed.
  • Tests: new cases in src/leads.test.ts cover a different-case email, a differently typed phone, a no-match arrival that still creates a lead, and a closed lead reopening. I adjusted a few existing tests (src/actions.test.ts, src/audit.test.ts, src/server.test.ts and others) so they still pass.
  • README: "How it works" now has a paragraph on writing in twice.

What I chose not to do

  • I did not merge existing duplicate leads.
  • I did not merge on name.
  • I did not add a way to undo a merge.

To try it Add a lead on the desk, then add another with the same email in different letter case. You land on the first lead with "wrote in again", and the audit log shows one merged row.

I did not edit src/pages/desk.ts, which the issue lists. The "wrote in again" message goes through the existing redirect message, which the desk already shows.

commented

A lead who writes in again, by the same email or the same phone digits, now lands on their existing lead instead of becoming a second one. bun test passes (273). The type check has no tsconfig in the repo and bun x tsc --noEmit only prints help, so I ran tsc with a temporary config outside the clone. It reports 12 errors, the same 12 as before this change, none in the files I touched.

What changed

  • Matching: findMatch in src/leads.ts matches on email (lowercased) or phone digits, whichever fits. An open lead is preferred, then the newest. The phone comparison now happens in SQL using the existing digits-only expression, and the string surgery and the load-every-lead filter are gone. A name alone never matches.
  • Merge: the new message joins the lead's history and a fresh reply is drafted. A closed lead (qualified or lost) goes back through reopen.
  • Audit: one merged event names the arrival's source and the lead. Its detail is built and read by a typed MergeDetail, mergeDetail and readMergeDetail in src/audit.ts. addLead works out whether it merged once.
  • Desk order (my addition): the desk now sorts by the latest of a lead's creation time and its last inbound message, so a returning lead moves to the top of "needs you". This also moves any lead whose reply is logged by hand. I did it because the issue asks for the top of "needs you", and the desk was ordered by creation time only.
  • Sample button: once the sample inquiries are used up, it adds a new person (a variant of the oldest inquiry with its own address and number) instead of merging. The README says so.
  • README: the "writes in twice" paragraph now says email or phone, that an open lead is preferred, and that people sharing a phone merge unless one gives another number.

Tests

  • New tests cover:
    • the phone merge through addLead, with a different email;
    • an unmatched person still creating a lead;
    • an open lead preferred over a closed one;
    • lost and qualified leads reopening as contacted with the cadence rescheduled and a merged row;
    • the lead landing first in "needs you";
    • the web redirect and the lead page both saying "Dana P. wrote in again" (shown through the notice cookie);
    • the terminal message;
    • the sample button adding a lead after the set is used.
  • Matching on phone made many existing tests, which shared one phone number across different people, merge by accident. I gave each of those leads its own number. The assertions are unchanged except the sample-button one, which was wrong now and I rewrote.

What I did not do

  • Family phones (item 1): the sharing risk named in the issue is now real, since a shared phone merges even with a different email. I documented it instead of adding a guard.
  • I did not edit src/pages/desk.ts. The notice already shows on the page, and the test checks the rendered text.
  • The terminal's sample key ('i') still says "is on the desk"; it can no longer merge in practice.
commented

I merged main into the branch and pushed. The only conflict was src/leads.test.ts, and bun test now passes (276 tests, 0 failures).

The conflict

  • Imports: I kept both sides: the findMatch, addLead, listLeads and listAudit imports from this branch, and sourceLine from main.
  • Tests: both sides had appended tests at the end of the file, so I kept all of them. That includes main's new "by source" test.

An intermediate push to ignore My first push, commit bb92528, was broken. I closed the merge test with a misplaced });, which left the file with a syntax error. I should have re-run the tests before pushing. The follow-up commit 3ef9379 fixes the placement and is the one to look at.

I did not run a type check this time.

Sign in to join this conversation.

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

Keyboard shortcuts

Docs