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.

Update route.ts

Kiran Kfda18f59b1df
diff --git a/apps/web/app/(ee)/api/shopify/integration/webhook/route.ts b/apps/web/app/(ee)/api/shopify/integration/webhook/route.ts
index 027756c1df..a9ad369d10 100644
--- a/apps/web/app/(ee)/api/shopify/integration/webhook/route.ts
+++ b/apps/web/app/(ee)/api/shopify/integration/webhook/route.ts
@@ -1,6 +1,8 @@
+import { captureWebhookLog } from "@/lib/api-logs/capture-webhook-log";
import { isLocalDev } from "@/lib/api/environment";
import { prisma } from "@/lib/prisma";
import { log } from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
import crypto from "crypto";
import { appUninstalled } from "./app-uninstalled";
import { customersDataRequest } from "./customers-data-request";
@@ -20,6 +22,7 @@ const relevantTopics = new Set([
// POST /api/shopify/integration/webhook – Listen to Shopify webhook events
export const POST = async (req: Request) => {
+ const startTime = Date.now();
const data = await req.text();
const headers = req.headers;
const topic = headers.get("x-shopify-topic") || "";
@@ -65,6 +68,14 @@ export const POST = async (req: Request) => {
);
}
+ const requestLog = {
+ workspaceId: workspace.id,
+ method: req.method,
+ path: "/shopify/integration/webhook" as const,
+ requestBody: event,
+ userAgent: req.headers.get("user-agent"),
+ };
+
let response = "OK";
try {
@@ -105,7 +116,32 @@ export const POST = async (req: Request) => {
type: "errors",
});
- return new Response(`[Shopify] Webhook handler failed. View logs`);
+ const response = new Response(
+ `[Shopify] Webhook handler failed. View logs`,
+ );
+
+ waitUntil(
+ captureWebhookLog({
+ ...requestLog,
+ statusCode: 500,
+ duration: Date.now() - startTime,
+ responseBody: response,
+ }),
+ );
+
+ return response;
+ }
+
+ // orders/paid is logged by processShopifyOrderJob after the order is processed
+ if (topic !== "orders/paid") {
+ waitUntil(
+ captureWebhookLog({
+ ...requestLog,
+ statusCode: 200,
+ duration: Date.now() - startTime,
+ responseBody: response,
+ }),
+ );
}
return new Response(response);