Webhook Discord
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user