Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add `invoiceId` check to `/track/sale` as well

Steven Teybf3c2f458af8
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index 3937e36e36..0f28b42911 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -5,6 +5,7 @@ import { calculateSaleEarnings } from "@/lib/api/sales/calculate-earnings";
import { createId, parseRequestBody } from "@/lib/api/utils";
import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
+import { redis } from "@/lib/upstash";
import { sendWorkspaceWebhookOnEdge } from "@/lib/webhook/publish-edge";
import { transformSaleEventData } from "@/lib/webhook/transform";
import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";
@@ -33,6 +34,22 @@ export const POST = withWorkspaceEdge(
eventName,
} = trackSaleRequestSchema.parse(await parseRequestBody(req));
+ if (invoiceId) {
+ // Skip if invoice id is already processed
+ const ok = await redis.set(`dub_sale_events:invoiceId:${invoiceId}`, 1, {
+ ex: 60 * 60 * 24 * 7,
+ nx: true,
+ });
+
+ if (!ok) {
+ console.info(
+ "[Stripe Webhook] Skipping already processed invoice.",
+ invoiceId,
+ );
+ return `Invoice with ID ${invoiceId} already processed, skipping...`;
+ }
+ }
+
const customerExternalId = customerId || externalId;
if (!customerExternalId) {