import type { ActionFunctionArgs } from "react-router"; import { authenticate } from "../shopify.server"; import db from "../db.server"; import { DISCOUNT_TITLE, monthKey } from "../lib/publish.server"; import { recordOrder, type OrderPayload } from "../lib/orders.server"; export const action = async ({ request }: ActionFunctionArgs) => { const { shop, payload } = await authenticate.webhook(request); const row = await db.shop.findUnique({ where: { domain: shop } }); if (!row || row.uninstalledAt) return new Response(); const offers = await db.offer.findMany({ where: { shopId: row.id }, select: { id: true, name: true } }); const result = await recordOrder(row.id, payload as OrderPayload, offers, DISCOUNT_TITLE); if (result.hasBundle && result.isNew) { const key = monthKey(); await db.shop.update({ where: { id: row.id }, data: row.monthKey === key ? { monthOrders: { increment: 1 } } : { monthKey: key, monthOrders: 1 }, }); } return new Response(); };