feat(offers): generate Angebot/Offer PDFs for draft orders

This commit is contained in:
Gerhard Scheikl
2026-05-09 19:26:33 +02:00
parent 1ec4faaac5
commit 6224597497
8 changed files with 465 additions and 41 deletions
+12 -6
View File
@@ -15,15 +15,17 @@ import { sendInvoiceEmail } from "../services/invoice/email.server";
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const { session, cors } = await authenticate.admin(request);
const orderId = requireOrderId(params);
const url = new URL(request.url);
const kind = (url.searchParams.get("kind") === "offer" ? "offer" : "invoice") as "invoice" | "offer";
const orderGid = orderId.startsWith("gid://")
? orderId
: `gid://shopify/Order/${orderId}`;
: `gid://shopify/${kind === "offer" ? "DraftOrder" : "Order"}/${orderId}`;
const invoices = await db.invoice.findMany({
where: { shopDomain: session.shop, orderId: orderGid },
orderBy: [{ issuedAt: "desc" }],
});
const latest = invoices.find((i) => i.kind === "invoice" && !i.cancelledAt);
const latest = invoices.find((i) => i.kind === kind && !i.cancelledAt);
return cors(
Response.json({
@@ -41,15 +43,18 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
const orderId = requireOrderId(params);
const url = new URL(request.url);
let op = url.searchParams.get("action");
if (!op) {
// Also accept the action from the form body (used by the in-app fetcher).
let kindParam = url.searchParams.get("kind");
if (!op || !kindParam) {
// Also accept the action / kind from the form body (used by the in-app fetcher).
const ct = request.headers.get("content-type") || "";
if (ct.includes("application/x-www-form-urlencoded") || ct.includes("multipart/form-data")) {
const form = await request.formData();
op = (form.get("action") as string | null) ?? null;
op = op ?? ((form.get("action") as string | null) ?? null);
kindParam = kindParam ?? ((form.get("kind") as string | null) ?? null);
}
}
op = op ?? "generate";
const kind: "invoice" | "offer" = kindParam === "offer" ? "offer" : "invoice";
try {
if (op === "cancel_reissue") {
@@ -109,8 +114,9 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
shopDomain: session.shop,
admin,
orderId,
kind,
});
return cors(Response.json({ ok: true, op: "generate", ...result }));
return cors(Response.json({ ok: true, op: kind === "offer" ? "generate_offer" : "generate", ...result }));
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.error("invoice action failed:", err);