From ee46a20fb29f2550c3b17e7e2ec20c7a6c6fd162 Mon Sep 17 00:00:00 2001 From: RafidMuhymin Date: Mon, 6 Nov 2023 17:25:46 +0600 Subject: [PATCH] fixed retry attempts duplicating exact orders --- src/utils/context.ts | 12 ++++++++++-- src/workers/dilmahtea-me-dimass-webhook/src/index.ts | 4 +--- .../dilmahtea-me-dimass-webhook/wrangler.toml | 2 +- src/workers/dilmahtea-me-stripe-webhook/src/index.ts | 4 +--- .../dilmahtea-me-stripe-webhook/wrangler.toml | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/utils/context.ts b/src/utils/context.ts index 9add438b..37925dd8 100644 --- a/src/utils/context.ts +++ b/src/utils/context.ts @@ -18,8 +18,16 @@ export default new Proxy( }, ) as Context; -export async function setupContext(id: string) { - key = await hash(id, "SHA-256"); +export async function setupContext(request: Request, id: string | number) { + // remove retry attempt params from URL + const requestURL = new URL(request.url); + + requestURL.searchParams.delete("attempt"); + requestURL.searchParams.delete("requestID"); + + const contextID = requestURL.toString() + id; + + key = await hash(contextID, "SHA-256"); const storedContext = await (env.WORKER_CONTEXTS as KVNamespace).get(key); diff --git a/src/workers/dilmahtea-me-dimass-webhook/src/index.ts b/src/workers/dilmahtea-me-dimass-webhook/src/index.ts index 319c060a..78e69b7f 100644 --- a/src/workers/dilmahtea-me-dimass-webhook/src/index.ts +++ b/src/workers/dilmahtea-me-dimass-webhook/src/index.ts @@ -30,9 +30,7 @@ async function handlePOST(request: Request, env: ENV): Promise { const webhookData = JSON.parse(payload) as WebhookResponseData; - const contextID = request.url + webhookData.id; - - await setupContext(contextID); + await setupContext(request, webhookData.id); const event = request.headers.get("X-SP-Event") as AcceptedShipmentEvents; diff --git a/src/workers/dilmahtea-me-dimass-webhook/wrangler.toml b/src/workers/dilmahtea-me-dimass-webhook/wrangler.toml index 10c05360..dbeb9d2d 100644 --- a/src/workers/dilmahtea-me-dimass-webhook/wrangler.toml +++ b/src/workers/dilmahtea-me-dimass-webhook/wrangler.toml @@ -12,7 +12,7 @@ routes = [ kv_namespaces = [ { binding = "EXACT_TOKENS", id = "0cbeb7b84a6a4e31b9c3360f97c9cef5" }, { binding = "EXACT_LAYOUTS", id = "b75eb1bee9d7488fa33b8be89caab307" }, - { binding = "WORKER_CONTEXTS", id = "3fcd1790b1e644caa0d76d1be2fc27f7" } + { binding = "WORKER_CONTEXTS", id = "bf39422efbc34bd58d3c3e064660474f" } ] services = [{ binding = "RETRY_WORKERS", service = "dilmahtea-me-retry-workers" }] diff --git a/src/workers/dilmahtea-me-stripe-webhook/src/index.ts b/src/workers/dilmahtea-me-stripe-webhook/src/index.ts index b01001d1..a867dbd4 100644 --- a/src/workers/dilmahtea-me-stripe-webhook/src/index.ts +++ b/src/workers/dilmahtea-me-stripe-webhook/src/index.ts @@ -42,9 +42,7 @@ async function handlePOST(request: Request, env: ENV) { // @ts-ignore const { paymentID, payment_type } = event.data.object.metadata as MetaData; - const contextID = request.url + paymentID; - - await setupContext(contextID); + await setupContext(request, paymentID); const PAYMENT_INTENTS = payment_type === "crowdfunding" diff --git a/src/workers/dilmahtea-me-stripe-webhook/wrangler.toml b/src/workers/dilmahtea-me-stripe-webhook/wrangler.toml index 61bc80db..da1a1897 100644 --- a/src/workers/dilmahtea-me-stripe-webhook/wrangler.toml +++ b/src/workers/dilmahtea-me-stripe-webhook/wrangler.toml @@ -14,7 +14,7 @@ kv_namespaces = [ { binding = "EXACT_TOKENS", id = "0cbeb7b84a6a4e31b9c3360f97c9cef5" }, { binding = "CROWDFUNDINGS", id = "c971dd86589647799fb64dde47563fac" }, { binding = "ECOMMERCE_PAYMENTS", id = "5086c43e26a14fa3a177ea876f514a8f" }, - { binding = "WORKER_CONTEXTS", id = "3fcd1790b1e644caa0d76d1be2fc27f7" } + { binding = "WORKER_CONTEXTS", id = "bf39422efbc34bd58d3c3e064660474f" } ] services = [{ binding = "RETRY_WORKERS", service = "dilmahtea-me-retry-workers" }]