Welcome email has no delivery record and cannot be retried

未关闭
#29 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
40/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
postgresql, typescript
领域
backend, databases

调研方向

Start with the Stripe checkout.session.completed handler and the welcome send in lib/email/send.ts, then read the sweep pattern in bot/index.ts and claimStuckTicketForRetry in lib/db/queries/tickets.ts. Check the PUBLIC_PATHS prefix rules in auth.ts before adding any route. Done means the orgs columns exist, a failed send is retryable up to a capped number of attempts, a successful send is never repeated, and tests fail if the claim stops being atomic.

由索引模型根据 Issue 内容生成。

描述

backend enhancement

Copied from DeadantCo/answerLoops#19 · originally opened by sethr07 on 2026-11-20

Copied from https://github.com/answerLoops/answerLoops/issues/45 — originally opened by @NathanTarbert on 2026-08-21

Summary

The welcome email is sent once, from the Stripe checkout.session.completed handler, and there is no record that it was attempted. If the mail provider is unavailable at that moment the send is logged and dropped. Nothing retries it and nothing surfaces it, so the org simply never gets welcomed.

The send is deliberately non-fatal — returning a non-2xx would make Stripe retry an event whose subscription is already written — so "swallow and log" is the right immediate behaviour. What's missing is the durable record that would let it be retried.

Why it can't retry today

Whether an org has already been welcomed is inferred rather than recorded: the handler treats "this org had no subscription before this event" as "this org has not been welcomed." That works for the happy path but makes a failed send unrecoverable, because after the upsert the org does have a subscription — so a retry would read the same signal and conclude the welcome was already handled.

Proposed fix

1. Record the outcome. Add welcome_email_sent_at and welcome_email_attempts to orgs. This replaces the inference with a fact and makes a failed send visibly incomplete rather than indistinguishable from a completed one.

2. Stamp it on success. The checkout handler sets welcome_email_sent_at after a successful send and continues to return 200 either way.

3. Sweep the gaps. A periodic job finds orgs with an active subscription, welcome_email_sent_at IS NULL, subscribed more than a few minutes ago, bounded to a recent window so a permanently undeliverable address is not retried indefinitely. bot/index.ts already runs two sweeps of this shape (org purge, stuck tickets) and this should follow the same structure.

4. Claim atomically before sending. Same pattern as claimStuckTicketForRetry in lib/db/queries/tickets.ts:

UPDATE orgs
   SET welcome_email_attempts = welcome_email_attempts + 1
 WHERE id = $1
   AND welcome_email_sent_at IS NULL
   AND welcome_email_attempts < $2
RETURNING id

No rows returned means another worker holds it or the attempt cap is reached. The cap is what prevents an unbounded retry loop against an address that will never accept mail.

The sweep should call a BOT_SECRET-authenticated route, mirroring /api/ingest/retry-stuck.

One thing to get right when adding it: PUBLIC_PATHS in auth.ts matches by prefix, and the route must be covered by it or the session middleware rejects the request before BOT_SECRET is ever checked. /api/ingest/retry-stuck is already covered by the existing /api/ingest entry and needed none of its own — there is a test asserting exactly that. A route placed elsewhere, for example under /api/email/, is not covered by the more specific /api/email/ingest entry and would need its own.

Follow-on worth considering

Transactional sends from lib/email/send.ts write no row at all, while outbound ticket replies in lib/email/reply.ts do — which is why the existing provider webhook can mark those delivered or bounced and cannot do the same here. Recording the welcome send the same way would close the second gap: sent successfully, bounced afterwards, and nothing reflects it.

Acceptance criteria

  • A send failure leaves the org in a state a later run can detect and retry
  • A retried send happens at most once per org, under concurrency
  • Retries are capped and time-bounded
  • A successful send is recorded and a second one never fires
  • Migration, sweep, and route are covered by tests that fail if the claim stops being atomic

Start here: new to the project? Read CONTRIBUTING.md first. It covers the ground rules, running answerLoops locally, and how to open a pull request.

主要语言
TypeScript
星标
0
派生
0
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

DeadantCo/answerLoopsdev 的其他 Issue

查看 DeadantCo/answerLoopsdev 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。