Webhook Discord

This commit is contained in:
2026-07-02 18:38:04 +02:00
parent 34fe0b461e
commit 3505bc149d
10 changed files with 171 additions and 1 deletions

View File

@@ -400,6 +400,20 @@ const writeIdempotentValue = async (db, key, value) => {
);
};
// Atomically claims a one-time slot for the given key (e.g. per webhook event
// notification). Returns true only for the first caller; concurrent retries of
// the same event lose the INSERT race and get false.
const claimNotificationOnce = async (db, key) => {
const result = await run(
db,
`INSERT INTO billing_idempotency (id, response_json, created_at)
VALUES ($1, CAST($2 AS jsonb), $3)
ON CONFLICT (id) DO NOTHING`,
[key, JSON.stringify({ claimedAt: nowIso() }), nowIso()],
);
return result.changes > 0;
};
const grantRevenueCatTopupIfNeeded = async (db, account, transactionId, productId) => {
if (!transactionId || !isSupportedTopupProduct(productId)) {
return false;
@@ -790,6 +804,7 @@ const isInsufficientCreditsError = (error) => {
module.exports = {
AVAILABLE_PRODUCTS,
chargeKey,
claimNotificationOnce,
consumeCreditsWithIdempotency,
endpointKey,
ensureBillingSchema,