feat(emails): send one-time welcome email on account creation

Merged
SW
4mo ago
+512-2

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)

PathHookedWhy
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)❌ deliberatethat flow already sends its own verification-code email
lib/accounts/getOrCreateAccountByEmail.ts (org-member invite pre-provisioning)❌ deliberateinvited 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:

  1. Creation-path semantics: the send is only reachable from the create-new branches; a sign-in of an existing account short-circuits earlier.
  2. email_send_log lookup guard: sendWelcomeEmail first calls selectWelcomeEmailLog(accountId), which matches a prior status = "sent" row whose raw_body carries the "type":"welcome_email" marker. Already sent → skip. send_failed rows 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 footer
  • lib/emails/sendWelcomeEmail.ts (+ test): guard → build → Resend send → log
  • lib/supabase/email_send_log/selectWelcomeEmailLog.ts (+ test): idempotency lookup (Supabase access stays in lib/supabase/)
  • lib/const.ts: WELCOME_EMAIL_LOG_TYPE
  • lib/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 on main are all in untouched test files).
  • pnpm lint and pnpm 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/accounts and getOrCreateAccountIdByAuthToken; skips wallet-only signups, agent signups, and invite pre-provisioning.
    • Idempotent via email_send_log marker {"type":"welcome_email"}; failed sends can retry.
    • Sends via Resend from RECOUP_FROM_EMAIL; logs sent or send_failed; never blocks account creation.
    • Updated content: five-step onboarding with /setup/* links on CHAT_APP_URL; durable images on Vercel Blob and i.scdn.co; primary CTA targets /setup.
  • Refactors

    • Reuse generic selectEmailSendLog with a new accountId filter for per-account dedup; removed the bespoke selectWelcomeEmailLog.
    • Extracted imageTag helper for step thumbnails.

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.