fix(withdrawal): record one open request and send one receipt #383
Inga granskare
Etiketter
Inga etiketter
area/backups
area/ci
area/control-panel
area/identity
area/infra
area/observability
area/payments
area/security
area/storage
area/web
blocked
needs-info
needs-triage
priority
high
priority
low
priority
medium
ready-for-implementation
type
bug
type
chore
type
docs
type
epic
type
feature
type
task
wontfix
Ingen milstolpe
Inget projekt
Inga tilldelade
1 deltagare
Notiser
Förfallodatum
Inget förfallodatum satt.
Beroenden
Inga beroenden satta
Referens
bitborg/bitborg-web!383
Läser in…
Hänvisa till i nytt ärende
Ingen beskrivning angiven.
Ta bort grenen "fix/withdrawal-once"
Borttagning av en gren är permanent. Även om den borttagna grenen kan fortsätta existera en kort tid innan den faktiskt tas bort, kan det INTE ångras i de flesta fall. Vill du fortsätta?
What
A double submit of the withdrawal form now records one request, calls billing once and sends one receipt.
How
withdrawal_requests_open_sub_idxonsubwherewithdrawn_atandrejectionare null. One open request per account.ON CONFLICT DO NOTHING. The caller that loses re-reads the latest row and returns it, or its rejection if it was closed meanwhile. The winner drives billing and the receipt.UPDATE ... SET receipt_sent_at WHERE receipt_sent_at IS NULL RETURNING. The mail goes out only if a row came back. A failed or throwing send clears the claim, so a later retry sends it.Billing
The billing
withdrawcall is idempotent. A repeat for an already withdrawn subscription returns the stored withdrawal time and refund total, and does not refund again. Billing also serialises on the subscription row lock. On the portal side,processWithdrawaltakes a per-account Postgres advisory lock (pg_advisory_xact_lock) for the whole request, so two parallel withdrawals for one account run one after the other: the second sees the settled row and calls billing only if the first failed. It holds one database connection during the billing call, bounded by the per-account rate limit and the pool size.Tests
src/lib/withdrawal.db.test.tsruns against Postgres (TEST_DATABASE_URL): two parallel calls end with one row, one billing call and one receipt. It also covers no resend on retry, and resend after a failed send.pnpm test,check,lint,build,format:checkanddrizzle-kit checkpass.Notes
0012first closes older duplicate open rows, keeping the newest open row per account, then creates the index. Closed rows get rejectionnone, the most neutral existing value (no "settle by hand" log). A DB test runs the migration SQL over seeded duplicates.Closes #325
ccf742d7acd38c737d62