import type { ActionFunctionArgs } from "react-router"; import prisma from "../db.server"; import { authenticate } from "../shopify.server"; import { firstDelivery, handleOrderCreate, type OrderPayload } from "../services/orders.server"; export const action = async ({ request }: ActionFunctionArgs) => { const { shop, topic, payload, admin, webhookId } = await authenticate.webhook(request); const eventId = request.headers.get("x-shopify-event-id") ?? webhookId; if (!(await firstDelivery(eventId, topic, shop))) return new Response(); try { await handleOrderCreate(shop, payload as OrderPayload, admin); } catch (e) { console.error("orders/create failed", shop, e); // Let Shopify retry; the idempotency row must not block the retry. await prisma.processedWebhook.delete({ where: { id: eventId } }).catch(() => {}); return new Response("retry", { status: 500 }); } return new Response(); };