feat(valuation): lead-capture + Telegram parity across every valuation caller (chat#1885)
What
The 💰 Valuation lead Attio upsert + team Telegram ping was marketing-frontend-only: only the funnel's client-side captureRunLead → POST /api/valuation/lead (a marketing route) fired it. Chat-triggered valuations (chat/lib/valuation/runValuation → POST /api/valuation) and direct-api calls create no Attio lead and no alert.
This lifts lead-capture into runValuationHandler — the handler that owns the POST /api/valuation milestone — so every caller (chat, direct api, marketing funnel) captures a lead + fires one team alert. It mirrors the merged valuation-email pattern (#773): a deferred after() once the catalog is materialized and the band computed, best-effort, never blocking the response.
Closes the "Valuation lead-capture + Telegram parity across entry points" item in recoupable/chat#1885.
How
- Port the Attio primitives (
lib/attio/{request,assertPersonByEmail,createNote,isRecordInList,addRecordToList}) and the lead orchestration (upsertValuationLead,leadAttributes,leadNoteContent) verbatim from marketing (api had Telegram but no Attio). Added a whole-dollarlib/format/usd. - New
captureValuationLeadorchestrator — the server-side equivalent of the marketing/api/valuation/leadroute body, with two deliberate differences:- resolves the owner's email from the account (
selectAccountEmails({ accountIds })), never the request body; - the band arrives in api shape (
{ low, mid, high }) → mapsmidto the lead'scentral; - includes the measured
lifetimeStreams(aggregate.totalStreams) that the funnel's client-side path never had — a richer lead.
- resolves the owner's email from the account (
- Wire it into
runValuationHandleras a secondafter(() => …)beside the valuation-email one.
Attio behavior is unchanged from the funnel: assert Person by email → append a chronology Note every run → add to the Valuation Leads pipeline at New only if absent (re-runs never duplicate the card).
No contract change
POST /api/valuation still returns { catalog, band, songs_measured }. Lead-capture + Telegram are side-effects (exactly like the valuation email in #773), so there is no OpenAPI/docs change — accuracy over symmetry, we don't document a response the API doesn't emit.
De-dupe
The plan is: api is the single source, and marketing drops its now-duplicate client-side ping in a follow-up marketing PR so a funnel valuation fires exactly one lead. Recommended merge order: this PR, then the marketing removal back-to-back (a brief double-fire window for funnel valuations during rollout is cosmetic — one extra Attio note + one extra ping).
⚠️ Deploy prerequisite
The api Vercel project needs ATTIO_API_KEY set (Preview + Production) for the lead to actually write to Attio — upsertValuationLead no-ops with a logged ATTIO_API_KEY not configured otherwise. Telegram creds (TELEGRAM_CHAT_ID + bot token) already exist in api. Verify/set ATTIO_API_KEY on the api project before merging.
Tests
TDD, RED→GREEN per unit. 45 tests across lib/attio, lib/format, lib/valuation green; full suite + tsc --noEmit (my files clean; the 3 lib/trigger errors pre-exist on main) + eslint all clean.
- Attio primitives: URL/method/auth/body + best-effort no-throw on failure.
upsertValuationLead: attributes, chronology note, pipeline dedup, missing-key + assert-error paths.captureValuationLead: email-from-account,mid → centralmapping, Telegram format + Attio deep link, no-email skip, Attio-fail-still-pings, Telegram-fail-never-throws, optional-signal omission.
Live real-send verification against the preview (chat + direct-api valuation each → one Attio lead + one Telegram ping; funnel still one after the marketing PR) to follow in a PR comment once ATTIO_API_KEY is on the preview.
🤖 Generated with Claude Code
Summary by cubic
Moves valuation lead capture into the API runValuationHandler so chat, direct API, and marketing callers all create one Attio lead and one Telegram alert. Runs after valuation completes and never blocks the response, addressing chat#1885.
-
New Features
- Added
captureValuationLead: resolves owner email from the account, mapsvalueBand.mid→central, addslifetimeStreamsand optionalfollowerCount. - Attio: upserts Person, adds a markdown note, and dedupes a “Valuation Leads” pipeline card; Telegram ping includes an Attio deep link.
- Ported Attio helpers (
lib/attio/*) andlib/format/usd; wired viaafter()inrunValuationHandler. NoPOST /api/valuationresponse change.
- Added
-
Migration
- Set
ATTIO_API_KEYfor the API (Preview + Production) before deploy. - Remove the marketing client
/api/valuation/leadcall after rollout to avoid double alerts.
- Set