fix(withdrawal): record one open request and send one receipt #383

Sammanfogat
supernaut sammanfogade 3 incheckningar från fix/withdrawal-once in i main 2026-10-13 08:25:46 +00:00
Ägare

What

A double submit of the withdrawal form now records one request, calls billing once and sends one receipt.

How

  • Partial unique index withdrawal_requests_open_sub_idx on sub where withdrawn_at and rejection are null. One open request per account.
  • The insert uses 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.
  • The receipt is claimed with 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.
  • A retry click on an open request whose receipt already went out does not send it again.

Billing

The billing withdraw call 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, processWithdrawal takes 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.ts runs 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.
  • Unit tests cover the claim, the skipped receipt and the cleared claim.
  • pnpm test, check, lint, build, format:check and drizzle-kit check pass.

Notes

  • Migration 0012 first closes older duplicate open rows, keeping the newest open row per account, then creates the index. Closed rows get rejection none, the most neutral existing value (no "settle by hand" log). A DB test runs the migration SQL over seeded duplicates.
  • Patch bump to 1.38.1.

Closes #325

## What A double submit of the withdrawal form now records one request, calls billing once and sends one receipt. ## How - Partial unique index `withdrawal_requests_open_sub_idx` on `sub` where `withdrawn_at` and `rejection` are null. One open request per account. - The insert uses `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. - The receipt is claimed with `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. - A retry click on an open request whose receipt already went out does not send it again. ## Billing The billing `withdraw` call 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, `processWithdrawal` takes 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.ts` runs 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. - Unit tests cover the claim, the skipped receipt and the cleared claim. - `pnpm test`, `check`, `lint`, `build`, `format:check` and `drizzle-kit check` pass. ## Notes - Migration `0012` first closes older duplicate open rows, keeping the newest open row per account, then creates the index. Closed rows get rejection `none`, the most neutral existing value (no "settle by hand" log). A DB test runs the migration SQL over seeded duplicates. - Patch bump to 1.38.1. Closes #325
supernaut lade till 2 incheckningar 2026-10-13 06:31:33 +00:00
A partial unique index allows one open request per account. The
migration first closes older duplicate open rows. The insert loses
quietly to a concurrent one, and the receipt is claimed atomically
before it is sent.
chore(release): 1.38.1
En del kontroller misslyckades
ci / ci (pull_request) Failing after 2m20s
7a2836c470
supernaut lade till 1 incheckning 2026-10-13 06:46:11 +00:00
fix(withdrawal): serialise calls per account so billing is called once
Alla kontroller lyckades
ci / ci (pull_request) Successful in 2m3s
ccf742d7ac
A second call that arrived after the first insert saw an open row and
treated it as a retry. A per-account advisory lock makes it wait and see
the settled row.
supernaut tvångsskickade fix/withdrawal-once från ccf742d7ac
Alla kontroller lyckades
ci / ci (pull_request) Successful in 2m3s
till d38c737d62
Alla kontroller lyckades
ci / ci (pull_request) Successful in 7m5s
2026-10-13 08:18:10 +00:00
Jämför
supernaut schemalade den här ändringsförfrågan för automatisk sammanfogning när alla kontroller lyckas 2026-10-13 08:18:34 +00:00
supernaut sammanfogade incheckning 2fb81dd27b till main 2026-10-13 08:25:46 +00:00
supernaut tog bort grenen fix/withdrawal-once 2026-10-13 08:25:46 +00:00
Logga in för att delta i denna konversation.
Inga granskare
Ingen milstolpe
Inget projekt
Inga tilldelade
1 deltagare
Notiser
Förfallodatum
Förfallodatumet är ogiltigt eller utanför gränserna. Använd formatet "åååå-mm-dd".

Inget förfallodatum satt.

Beroenden

Inga beroenden satta

Referens
bitborg/bitborg-web!383
Ingen beskrivning angiven.