Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SvuGvHpbAQ4syfuLuRvutL
93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
import "server-only";
|
|
import { cookies } from "next/headers";
|
|
import { db } from "./db";
|
|
|
|
export const CART_COOKIE = "sm_cart";
|
|
export type CartEntry = { p: string; v: string | null; q: number };
|
|
|
|
export async function readCartCookie(): Promise<CartEntry[]> {
|
|
const raw = (await cookies()).get(CART_COOKIE)?.value;
|
|
if (!raw) return [];
|
|
try {
|
|
const parsed = JSON.parse(raw);
|
|
if (!Array.isArray(parsed)) return [];
|
|
return parsed
|
|
.filter((e) => e && typeof e.p === "string" && Number.isInteger(e.q) && e.q > 0)
|
|
.map((e) => ({ p: e.p, v: typeof e.v === "string" ? e.v : null, q: Math.min(e.q, 99) }));
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function writeCartCookie(entries: CartEntry[]) {
|
|
(await cookies()).set(CART_COOKIE, JSON.stringify(entries), {
|
|
httpOnly: true,
|
|
sameSite: "lax",
|
|
path: "/",
|
|
maxAge: 60 * 60 * 24 * 30,
|
|
});
|
|
}
|
|
|
|
export async function clearCart() {
|
|
(await cookies()).delete(CART_COOKIE);
|
|
}
|
|
|
|
export type CartLine = {
|
|
key: string;
|
|
productId: string;
|
|
variantId: string | null;
|
|
name: string;
|
|
slug: string;
|
|
variantName: string;
|
|
subtitle: string;
|
|
sku: string;
|
|
image: string;
|
|
unitPrice: number;
|
|
quantity: number;
|
|
maxQuantity: number;
|
|
taxRate: number;
|
|
total: number;
|
|
};
|
|
|
|
/** Löst die Cookie-Einträge gegen die Datenbank auf; Preise kommen immer aus der DB. */
|
|
export async function getCart(): Promise<CartLine[]> {
|
|
const entries = await readCartCookie();
|
|
if (entries.length === 0) return [];
|
|
const products = await db.product.findMany({
|
|
where: { id: { in: entries.map((e) => e.p) }, active: true },
|
|
include: { variants: true },
|
|
});
|
|
const lines: CartLine[] = [];
|
|
for (const e of entries) {
|
|
const product = products.find((p) => p.id === e.p);
|
|
if (!product) continue;
|
|
const variant = e.v ? product.variants.find((v) => v.id === e.v) : null;
|
|
if (e.v && !variant) continue;
|
|
const unitPrice = variant?.price ?? product.price;
|
|
const available = product.trackStock ? (variant ? variant.stock : product.stock) : 99;
|
|
const quantity = Math.min(e.q, Math.max(available, 0));
|
|
if (quantity <= 0) continue;
|
|
lines.push({
|
|
key: `${product.id}:${variant?.id ?? ""}`,
|
|
productId: product.id,
|
|
variantId: variant?.id ?? null,
|
|
name: product.name,
|
|
slug: product.slug,
|
|
variantName: variant?.name ?? "",
|
|
subtitle: product.subtitle,
|
|
sku: variant?.sku || product.sku,
|
|
image: product.images[0] ?? "",
|
|
unitPrice,
|
|
quantity,
|
|
maxQuantity: Math.min(available, 99),
|
|
taxRate: product.taxRate,
|
|
total: unitPrice * quantity,
|
|
});
|
|
}
|
|
return lines;
|
|
}
|
|
|
|
export async function getCartCount(): Promise<number> {
|
|
const entries = await readCartCookie();
|
|
return entries.reduce((n, e) => n + e.q, 0);
|
|
}
|