feat(emails): send one-time welcome email on account creation
Implements the "Welcome email on signup" item of recoupable/chat#1867.
Problem
email_send_log has zero non-task rows ever: the full 2026-07-13→20 Resend log (158 sends) is 100% scheduled-task reports. A new signup who closes the tab is unreachable. (Prod walkthrough evidence in the issue.)
What this does
When an account is first created with an email address, the api sends a short branded welcome email with exactly one next step (see your catalog valuation, linking to the chat app) and records the attempt in email_send_log.
Creation paths hooked (first-creation only, never sign-in)
| Path | Hooked | Why |
|---|---|---|
lib/accounts/createAccountHandler.ts new-account branch (POST /api/accounts, the Privy web signup) | ✅ | primary signup path; email signups only (wallet-only signups have no address) |
lib/privy/getOrCreateAccountIdByAuthToken.ts create branch (fresh Privy bearer-token users, GET /api/accounts/id etc.) | ✅ | a brand-new Privy user's first request can provision here without ever hitting POST /api/accounts |
lib/agents/handleNormalSignup / handleAgentPrefixSignup (agent signup) | ❌ deliberate | that flow already sends its own verification-code email |
lib/accounts/getOrCreateAccountByEmail.ts (org-member invite pre-provisioning) | ❌ deliberate | invited members haven't signed up yet; welcoming them at invite time is the invite email's job |
Existing-account lookups return before the send in both hooked paths.
Idempotency (as required)
Two layers, both stated per the issue:
- Creation-path semantics: the send is only reachable from the create-new branches; a sign-in of an existing account short-circuits earlier.
email_send_loglookup guard:sendWelcomeEmailfirst callsselectWelcomeEmailLog(accountId), which matches a priorstatus = "sent"row whoseraw_bodycarries the"type":"welcome_email"marker. Already sent → skip.send_failedrows don't match, so a failed welcome can retry on a later provisioning race.
Logging
Same logEmailAttempt → email_send_log path as POST /api/emails: account_id, status (sent / send_failed), resend_id, and raw_body = {"type":"welcome_email","to":…,"subject":…}.
From address (note, per issue)
The repo has exactly one outbound from-address convention: RECOUP_FROM_EMAIL = Agent by Recoup <agent@recoupable.dev> (lib/const.ts), reused here. agent@ is not a dead drop — replies route through the existing inbound-email handling (lib/emails/inbound/), and the standard footer ("you can reply directly to this email") is appended. If we want a dedicated monitored address (e.g. welcome@/hello@), that's a Resend-domain + const change to make on top of this PR; the issue's "not bare agent@" ask is flagged rather than silently resolved.
Safety
sendWelcomeEmail is best-effort and never throws: a Resend outage or DB error can never fail account creation or a first authenticated request. Recipient is always the account's own just-linked email, consistent with the /api/emails self-send policy.
Files
lib/emails/buildWelcomeEmail.ts(+ test): subject/HTML builder, mirrors the existing plain inline-HTML template style,getFrontendBaseUrl()CTA, standard footerlib/emails/sendWelcomeEmail.ts(+ test): guard → build → Resend send → loglib/supabase/email_send_log/selectWelcomeEmailLog.ts(+ test): idempotency lookup (Supabase access stays inlib/supabase/)lib/const.ts:WELCOME_EMAIL_LOG_TYPElib/accounts/createAccountHandler.ts,lib/privy/getOrCreateAccountIdByAuthToken.ts: hooks (+ updated tests)
Verification
Local (worktree at origin/main + this change):
- TDD red-first: all three new units' tests written and confirmed failing before implementation; hook assertions added to the two existing suites and confirmed failing.
- Full vitest suite: 756 files / 4134 tests passed.
pnpm exec tsc --noEmit: 0 errors in any touched file (the ~200 pre-existing errors onmainare all in untouched test files).pnpm lintandpnpm format:check: clean.
Preview verification: pending. Note previews share the prod DB (different key salt) and this send is real. Suggested preview test (clone-to-own-account pattern): on the preview URL, POST /api/accounts with a fresh own-alias email (e.g. sweetmantech+welcome-test@gmail.com), then (1) confirm the email arrives within a minute, (2) confirm one email_send_log row with the new account_id, status='sent', and the welcome_email marker in raw_body, (3) repeat the same POST and confirm no second row/send, (4) POST /api/accounts with an existing email and confirm no send.
How to test
pnpm exec vitest run lib/emails lib/supabase/email_send_log lib/accounts lib/privy
pnpm exec tsc --noEmit
Part of recoupable/chat#1867.
🤖 Generated with Claude Code
Summary by cubic
Send a one-time welcome email when an account is first created with an email address. The email walks through five onboarding steps with /setup deep links built on CHAT_APP_URL, guiding users to their valuation. Part of recoupable/chat#1867.
-
New Features
- Hooks into first creation only:
POST /api/accountsandgetOrCreateAccountIdByAuthToken; skips wallet-only signups, agent signups, and invite pre-provisioning. - Idempotent via
email_send_logmarker{"type":"welcome_email"}; failed sends can retry. - Sends via
ResendfromRECOUP_FROM_EMAIL; logssentorsend_failed; never blocks account creation. - Updated content: five-step onboarding with
/setup/*links onCHAT_APP_URL; durable images on Vercel Blob andi.scdn.co; primary CTA targets/setup.
- Hooks into first creation only:
-
Refactors
- Reuse generic
selectEmailSendLogwith a newaccountIdfilter for per-account dedup; removed the bespokeselectWelcomeEmailLog. - Extracted
imageTaghelper for step thumbnails.
- Reuse generic
Summary by CodeRabbit
- New Features
- New accounts now receive a one-time welcome email when an email address is available.
- The welcome email includes a link to view the account valuation.
- Welcome emails are also sent when accounts are created during authentication.
- Bug Fixes
- Prevented duplicate welcome emails from being sent.
- Email delivery failures no longer interrupt account creation.