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
Check · Review · Land · Live done — Landed and 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.
shipped production is serving it deploy aba151a
2 earlier comments · 1 event
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:
findMatchinsrc/leads.tsmatches 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:
writeInAgaininsrc/leads.tsadds the new message to the existing lead's history throughrecordInbound, which drafts a fresh reply. If the lead was qualified or lost, it reopens it through the existingreopenpath. - Where it applies:
addLeadinsrc/actions.tshandles 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
mergedevent. Its detail names the arrival's source and the lead, and the trail reads "<lead> wrote in again (via Zillow)". I addedmergedtoACTSinsrc/audit.tsand the wording insrc/sentences.ts. - Permissions: the check is still
permit(who, "lead", "create"). Nocontract.jsoncell changed. - Tests: new cases in
src/leads.test.tscover 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.tsand 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.
Changes needed (one round, all listed):
- FIRST: box 1 asks that a matching email OR phone merges.
findMatch(src/leads.ts) uses the phone only when the arrival has no email, and every door requires an email (the pull's own test comment says so), so phone matching can never fire in practice. Same phone with a different email is another lead here, against the issue. Match on either: email (lowercased) or phone digits, preferring an open lead, then newest. Update the README sentence and the 'shared phone' test to match. - The phone match loads every lead with a phone and filters in JS, and builds SQL with
rank.replace(" LIMIT 1", ""). Compare digits in SQL, or keep a normalised phone column; drop the string surgery. - Tests missing for behaviours added: (a) the phone merge through
addLead(onlyfindMatchis tested directly); (b) the closed-lead test only assertsstate !== 'lost', so assert it is reopened through thereopenpath (state, rescheduled cadence, a 'reopened' audit row if that path writes one); (c) the lead lands at the top of 'needs you' after merge (box 1); (d) the web redirect says '<name> wrote in again' (routes.ts) and the terminal message (terminal.ts), each with a test that fails without the change; (e) a qualified lead, not only lost. - The desk wording: the issue names the desk saying 'Dana P. wrote in again'. Show that in a test against the rendered desk/redirect, not only in the code path; Mason did not touch src/pages/desk.ts, so prove the message is displayed.
- The sample-inquiry button now stops adding leads once the samples are used up and silently becomes 'wrote in again'. For a sales demo that is a regression; keep the button adding a distinct lead (e.g. vary the email) or say in the README why not.
bun x tsc --noEmitwas not run (builder says it printed help). Type check must be verified before landing.- Clean-up:
addLeadchecksresult.okthree times and re-readsresult.value.idinside a ternary; computemergedonce. Themergedaudit cell should also carry the new arrival's source in a typed detail rather than ad-hoc JSON built inline.
Checked fine: permit(lead, create) unchanged, no contract.json cell loosened, README paragraph added, audit merged event names source and lead.
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:
findMatchinsrc/leads.tsmatches 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
mergedevent names the arrival's source and the lead. Its detail is built and read by a typedMergeDetail,mergeDetailandreadMergeDetailinsrc/audit.ts.addLeadworks 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
contactedwith the cadence rescheduled and amergedrow; - 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.
- the phone merge through
- 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.
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,listLeadsandlistAuditimports from this branch, andsourceLinefrommain. - 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.
Re-read at head. Merge runs inside addLead behind permit(lead, create), so desk form, pasted email, webhook, MCP and terminal all merge; contract.json is untouched. Matching is parameterised SQL (lowercased email, or phone digits via the existing DIGITS expression); no name merge. Closed leads reopen through reopen; one merged audit row carries the source and lead id and is rendered escaped. The terminal says "wrote in again"; the web redirect notice does too, with a test. Tests cover case-insensitive email, phone typed differently, no-match creates, closed reopens, top of needs-you and the audit row. README paragraph present. Non-blocking: the doc comment on addLead says phone matches only when no email came, but findMatch matches either; align the comment. Non-blocking: the desk ordering change also moves hand-logged inbound leads, which is a sensible side effect. Type check was not run by the builder; I could not either.
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 testpasses (268 tests). I couldn't runbun x tsc --noEmit: tsc 7.0.2 only printed its help text here, so the type check is unverified. The repo has noscripts/gate.ts,scripts/fast.tsor design-lint script.What changed
findMatchinsrc/leads.tsmatches 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.writeInAgaininsrc/leads.tsadds the new message to the existing lead's history throughrecordInbound, which drafts a fresh reply. If the lead was qualified or lost, it reopens it through the existingreopenpath.addLeadinsrc/actions.tshandles this, so the desk form, pasted email, webhook, MCP and terminal all merge. The sample-inquiry button goes through the same path.mergedevent. Its detail names the arrival's source and the lead, and the trail reads "<lead> wrote in again (via Zillow)". I addedmergedtoACTSinsrc/audit.tsand the wording insrc/sentences.ts.permit(who, "lead", "create"). Nocontract.jsoncell changed.src/leads.test.tscover 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.tsand others) so they still pass.What I chose not to do
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
mergedrow.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