import type { ActionFunctionArgs } from "@remix-run/node"; import { authenticate } from "../shopify.server"; import db from "../db.server"; import { writeCheckoutSnapshot } from "../services/checkout-snapshot.server"; // Keeps Booking.status roughly in step with the order's fulfillment/cancel // state, and refreshes the checkout capacity snapshot the Validation // Function reads. See IMPLEMENTATION_PLAN.md ยง5.2. export const action = async ({ request }: ActionFunctionArgs) => { const { shop, topic, payload, admin } = await authenticate.webhook(request); console.log(`Received ${topic} webhook for ${shop}`); const order = payload as unknown as { admin_graphql_api_id: string; cancelled_at?: string | null; fulfillment_status?: string | null; }; const status = order.cancelled_at ? "cancelled" : order.fulfillment_status === "fulfilled" ? "fulfilled" : null; if (status) { await db.booking.updateMany({ where: { shopDomain: shop, orderId: order.admin_graphql_api_id, status: { notIn: ["cancelled"] } }, data: { status }, }); } if (admin) { await writeCheckoutSnapshot(admin, shop); } return new Response(); };