feat(notify): purchase and refund emails through SMTP and the job queue (#34, email slice) #101

Merged
amiryahaya merged 5 commits from feat/34-email into main 2026-10-26 15:12:23 +01:00
Owner

Email slice of #34 (does not close it: FCM push remains).

What

  • internal/notify: provider-agnostic Sender, embedded text and HTML templates, notify.PlainAddress (ASCII, no header injection). notify/smtpsender: SMTP adapter (implicit TLS on 465, STARTTLS elsewhere, plain only to localhost, TLS required before AUTH, 10 s dial and 30 s send bounds).
  • The job queue is the outbox. Completer.Complete (on the real transition to Completed) and Service.Refund enqueue notify.purchase / notify.refund with EnqueueTx and a dedupe key in their own transaction, so a rollback queues nothing and the worker sends after commit. One purchase job each for waive, wallet pay, M-Pesa callback and free checkout; none for a late payment.
  • Config KWS_SMTP_HOST/PORT/USERNAME/PASSWORD/FROM: all or none, unset = email disabled (nothing queued). The worker refuses to start on bad SMTP config. ADR 0016, plan docs/superpowers/plans/2026-10-26-email-notifications.md, CLAUDE.md, docs/02.

Rulings and known limits (ADR 0016)

  • Recipient is the sale user's email; guest sales get none (follow-up). Purchase email BCCs notification_bcc_email (an invalid one is dropped with a warning; a relay-refused BCC does not fail the send).
  • 5xx to RCPT/DATA and not-found ids are permanent; 4xx, network errors and 5xx to AUTH/MAIL retry. Server reply text is never kept.
  • No push (FCM), top-up or rate-reminder email, opt-out, PDF/QR (#35), bounce handling; English only; a duplicate is possible if an acknowledgement is lost.
  • Known nit: a remote relay that does not offer AUTH after TLS is not reported as such (MAIL fails with a 5xx and retries).
  • POS sales, when built, must not store the clerk as the sale's user, or the receipt goes to the clerk.

Review

Dual pass done. Pass 1 (ponytail): no Must fix. Pass 2 found a data race on the port-465 timeout path, fixed in 6ffb896 together with five smaller items; Pass 1 re-check: Ship.

Checks

make cover: 89.2% (minimum 80%). go test -race ./..., make lint, make contract green (implementer run). The real-relay smoke test (KWS_SMTP_SANDBOX=1) was not run.

Deploy

No migration. Set the five KWS_SMTP_* variables on both the API and the worker and keep the worker running; email stays off until they are set (API-only config dead-letters the jobs).

🤖 Generated with Claude Code

Email slice of #34 (does **not** close it: FCM push remains). ## What - `internal/notify`: provider-agnostic `Sender`, embedded text and HTML templates, `notify.PlainAddress` (ASCII, no header injection). `notify/smtpsender`: SMTP adapter (implicit TLS on 465, STARTTLS elsewhere, plain only to localhost, TLS required before AUTH, 10 s dial and 30 s send bounds). - **The job queue is the outbox.** `Completer.Complete` (on the real transition to `Completed`) and `Service.Refund` enqueue `notify.purchase` / `notify.refund` with `EnqueueTx` and a dedupe key in their own transaction, so a rollback queues nothing and the worker sends after commit. One purchase job each for waive, wallet pay, M-Pesa callback and free checkout; none for a late payment. - Config `KWS_SMTP_HOST/PORT/USERNAME/PASSWORD/FROM`: all or none, unset = email disabled (nothing queued). The worker refuses to start on bad SMTP config. ADR 0016, plan `docs/superpowers/plans/2026-10-26-email-notifications.md`, CLAUDE.md, docs/02. ## Rulings and known limits (ADR 0016) - Recipient is the sale user's email; guest sales get none (follow-up). Purchase email BCCs `notification_bcc_email` (an invalid one is dropped with a warning; a relay-refused BCC does not fail the send). - 5xx to RCPT/DATA and not-found ids are permanent; 4xx, network errors and 5xx to AUTH/MAIL retry. Server reply text is never kept. - No push (FCM), top-up or rate-reminder email, opt-out, PDF/QR (#35), bounce handling; English only; a duplicate is possible if an acknowledgement is lost. - Known nit: a remote relay that does not offer AUTH after TLS is not reported as such (MAIL fails with a 5xx and retries). - POS sales, when built, must not store the clerk as the sale's user, or the receipt goes to the clerk. ## Review Dual pass done. Pass 1 (ponytail): no Must fix. Pass 2 found a data race on the port-465 timeout path, fixed in 6ffb896 together with five smaller items; Pass 1 re-check: Ship. ## Checks `make cover`: **89.2%** (minimum 80%). `go test -race ./...`, `make lint`, `make contract` green (implementer run). The real-relay smoke test (`KWS_SMTP_SANDBOX=1`) was not run. ## Deploy No migration. Set the five `KWS_SMTP_*` variables on **both the API and the worker** and keep the worker running; email stays off until they are set (API-only config dead-letters the jobs). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
notify.Message/Sender/ErrPermanent, embedded purchase and refund templates
(text and HTML), an SMTP sender (implicit TLS on 465, STARTTLS otherwise,
plain only to localhost; header-injection safe; 5xx to RCPT/DATA is
permanent; replies reduced to their code) and the all-or-none KWS_SMTP_*
config.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
sales.Completer and payments.Service take an optional Notifier, called in
the completing/refunding transaction on the transition only; notify.Enqueuer
queues notify.purchase / notify.refund with dedupe keys purchase:<sale> and
refund:<payment>. The worker handlers read the sale or payment (notify/pgstore),
render and send; no address or a sale that is not completed sends nothing.
Wired in API and worker when KWS_SMTP_* is set; one info line otherwise.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ADR 0016 (rulings and known limits), spec 4.7 status, a CLAUDE.md
Notifications section, KWS_SMTP_* deploy notes and the opt-in SMTP
smoke test (KWS_SMTP_SANDBOX=1).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
fix(notify): review fixes for the SMTP sender and worker wiring (#34)
All checks were successful
ci / changes (pull_request) Successful in 10s
ci / clients (pull_request) Has been skipped
ci / api (pull_request) Successful in 5m36s
ci / ci-gate (pull_request) Successful in 2s
6ffb8964d3
- Close the raw connection on context end, not the variable reassigned to
  the TLS client (data race on port 465).
- Fail worker startup when the SMTP sender cannot be built, before the
  completer gets the notifier.
- One bare-address check, notify.PlainAddress (ASCII only), used by
  config, the purchase handler and the sender.
- AUTH only when the server advertises it; explicit EHLO so a failed
  EHLO is reported as itself, not as missing STARTTLS.
- ADR 0016 and CLAUDE.md wording.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/kws!101
No description provided.