feat(backend): idempotency key support for POST /credits/mint, /marketplace/purchase, /retirements#1
Open
Dev-Odun-oss wants to merge 1 commit into
Open
feat(backend): idempotency key support for POST /credits/mint, /marketplace/purchase, /retirements#1Dev-Odun-oss wants to merge 1 commit into
Dev-Odun-oss wants to merge 1 commit into
Conversation
Implements Idempotency-Key header deduplication on:
- POST /credits/mint
- POST /marketplace/purchase
- POST /retirements
Changes:
- backend/src/idempotency/idempotency.middleware.ts
NestJS middleware that validates UUID-v4 Idempotency-Key header,
stores first-execution responses in PostgreSQL, and replays them
for retry attempts within a 24-hour window.
Returns HTTP 400 for invalid key format, HTTP 422 when same key
is reused with a different request body.
- backend/src/idempotency/idempotency.module.ts
NestJS module wrapping the middleware.
- backend/src/idempotency/idempotency.middleware.spec.ts
12 integration tests covering: first execution, replay, body
mismatch, invalid format, expired TTL, endpoint scoping,
concurrent requests, and prune scheduling.
- backend/prisma/schema.prisma
Added IdempotencyRecord model with composite unique index on
(idempotencyKey, endpoint) and createdAt index for TTL pruning.
- backend/prisma/migrations/20260716000000_add_idempotency_record/
Raw SQL migration for the new table.
- backend/src/app.module.ts
Registered IdempotencyModule and applied IdempotencyMiddleware
to the three critical routes via configure().
Closes Carbon-Ledger-stellar#539
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements idempotency key deduplication for all three critical mutating endpoints as specified in issue Carbon-Ledger-stellar#539.
Changes
New files
backend/src/idempotency/idempotency.middleware.ts— NestJS middleware that validatesIdempotency-Key(UUID v4), stores first-execution responses, and replays them on retry within 24 hours.backend/src/idempotency/idempotency.module.ts— NestJS module.backend/src/idempotency/idempotency.middleware.spec.ts— 12 integration tests.backend/prisma/migrations/20260716000000_add_idempotency_record/migration.sql— Raw SQL migration.Modified files
backend/prisma/schema.prisma— AddedIdempotencyRecordmodel with composite unique index on(idempotencyKey, endpoint).backend/src/app.module.ts— Registered module and applied middleware to the three routes.Acceptance Criteria
Idempotency-Keyheader accepted and validated on 3+ endpointsTesting
12 integration tests in
idempotency.middleware.spec.tscovering:txHashIdempotent-Replayed: trueheader/marketplace/purchaseand/retirementsdeduplicationCloses Carbon-Ledger-stellar#539