The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
log shopify webhook
Steven Tey581ad3c3dca0
diff --git a/apps/web/app/api/shopify/integration/webhook/route.ts b/apps/web/app/api/shopify/integration/webhook/route.tsindex 7d1c5a32f4..517269a2ce 100644--- a/apps/web/app/api/shopify/integration/webhook/route.ts+++ b/apps/web/app/api/shopify/integration/webhook/route.ts@@ -1,6 +1,7 @@import { prisma } from "@dub/prisma";import { log } from "@dub/utils";import crypto from "crypto";+import { withAxiom } from "next-axiom";import { appUninstalled } from "./app-uninstalled";import { customersDataRequest } from "./customers-data-request";import { customersRedact } from "./customers-redact";@@ -18,90 +19,95 @@ const relevantTopics = new Set([]);// POST /api/shopify/integration/webhook – Listen to Shopify webhook events-export const POST = async (req: Request) => {- const data = await req.text();- const headers = req.headers;- const topic = headers.get("x-shopify-topic") || "";- const signature = headers.get("x-shopify-hmac-sha256") || "";+export const POST = withAxiom(+ async (req: Request) => {+ const data = await req.text();+ const headers = req.headers;+ const topic = headers.get("x-shopify-topic") || "";+ const signature = headers.get("x-shopify-hmac-sha256") || "";- // Verify signature- const generatedSignature = crypto- .createHmac("sha256", `${process.env.SHOPIFY_WEBHOOK_SECRET}`)- .update(data, "utf8")- .digest("base64");+ // Verify signature+ const generatedSignature = crypto+ .createHmac("sha256", `${process.env.SHOPIFY_WEBHOOK_SECRET}`)+ .update(data, "utf8")+ .digest("base64");- if (generatedSignature !== signature) {- return new Response(`[Shopify] Invalid webhook signature. Skipping...`, {- status: 401,- });- }+ if (generatedSignature !== signature) {+ return new Response(`[Shopify] Invalid webhook signature. Skipping...`, {+ status: 401,+ });+ }++ // Check if topic is relevant+ if (!relevantTopics.has(topic)) {+ return new Response(`[Shopify] Unsupported topic: ${topic}. Skipping...`);+ }- // Check if topic is relevant- if (!relevantTopics.has(topic)) {- return new Response(`[Shopify] Unsupported topic: ${topic}. Skipping...`);- }+ const event = JSON.parse(data);+ const shopDomain = headers.get("x-shopify-shop-domain") || "";- const event = JSON.parse(data);- const shopDomain = headers.get("x-shopify-shop-domain") || "";+ // Find workspace+ const workspace = await prisma.project.findUnique({+ where: {+ shopifyStoreId: shopDomain,+ },+ select: {+ id: true,+ },+ });- // Find workspace- const workspace = await prisma.project.findUnique({- where: {- shopifyStoreId: shopDomain,- },- select: {- id: true,- },- });+ if (!workspace) {+ return new Response(+ `[Shopify] Workspace not found for shop: ${shopDomain}. Skipping...`,+ );+ }- if (!workspace) {- return new Response(- `[Shopify] Workspace not found for shop: ${shopDomain}. Skipping...`,- );- }+ let response = "OK";- let response = "OK";+ try {+ switch (topic) {+ case "orders/paid":+ response = await ordersPaid({+ event,+ workspaceId: workspace.id,+ });+ break;+ case "customers/data_request":+ response = await customersDataRequest({+ event,+ workspaceId: workspace.id,+ });+ break;+ case "customers/redact":+ response = await customersRedact({+ event,+ workspaceId: workspace.id,+ });+ break;+ case "shop/redact":+ response = await shopRedact({+ event,+ workspaceId: workspace.id,+ });+ break;+ case "app/uninstalled":+ response = await appUninstalled({+ shopDomain,+ });+ break;+ }+ } catch (error) {+ await log({+ message: `Shopify webhook failed. Error: ${error.message}`,+ type: "errors",+ });- try {- switch (topic) {- case "orders/paid":- response = await ordersPaid({- event,- workspaceId: workspace.id,- });- break;- case "customers/data_request":- response = await customersDataRequest({- event,- workspaceId: workspace.id,- });- break;- case "customers/redact":- response = await customersRedact({- event,- workspaceId: workspace.id,- });- break;- case "shop/redact":- response = await shopRedact({- event,- workspaceId: workspace.id,- });- break;- case "app/uninstalled":- response = await appUninstalled({- shopDomain,- });- break;+ return new Response(`[Shopify] Webhook handler failed. View logs`);}- } catch (error) {- await log({- message: `Shopify webhook failed. Error: ${error.message}`,- type: "errors",- });-- return new Response(`[Shopify] Webhook handler failed. View logs`);- }- return new Response(response);-};+ return new Response(response);+ },+ {+ logRequestDetails: ["body", "nextUrl"],+ },+);