Vollstaendige Next.js/Prisma-E-Commerce-Anwendung fuer eine Imkerei: Shop, Warenkorb, Stripe-Checkout, Kundenkonto mit 2FA, Admin-Bereich (Produkte, Ernten, Chargen, Bestellungen, Gutscheine, admin-editierbare Website-Inhalte), i18n (DE/EN), Rate-Limiting/CSP/Audit-Log-Haertung, PostgreSQL- und MySQL-Unterstuetzung ueber austauschbare Prisma- Treiber-Adapter, Unit-/Komponenten-/E2E-Tests.
45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import { prisma } from "@/lib/prisma";
|
|
import type { BulkDiscountTierLike } from "@/lib/bulk-discount-calc";
|
|
|
|
// Startwert, falls noch kein Mengenrabatt-Code existiert — danach frei im
|
|
// Admin-Bereich (Gutscheine) editierbar, siehe ensureBulkDiscountCode().
|
|
const BULK_DISCOUNT_CODE = "MENGE10";
|
|
const BULK_DISCOUNT_PERCENT = 10;
|
|
const BULK_DISCOUNT_MIN_QUANTITY = 3;
|
|
|
|
/**
|
|
* Idempotent — legt einen Mengenrabatt-Code als Startwert an, falls noch
|
|
* keiner existiert. Danach ist er im Admin-Bereich (Gutscheine) frei in Hoehe
|
|
* (value) und Menge (minQuantity) editierbar — update:{} ueberschreibt
|
|
* bestehende Admin-Anpassungen nie.
|
|
*/
|
|
export async function ensureBulkDiscountCode(): Promise<void> {
|
|
await prisma.discountCode.upsert({
|
|
where: { code: BULK_DISCOUNT_CODE },
|
|
create: {
|
|
code: BULK_DISCOUNT_CODE,
|
|
type: "PERCENT",
|
|
value: BULK_DISCOUNT_PERCENT,
|
|
minOrderCents: 0,
|
|
minQuantity: BULK_DISCOUNT_MIN_QUANTITY,
|
|
},
|
|
update: {},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Alle aktuell gueltigen Mengenrabatt-Codes (minQuantity gesetzt) als leichte
|
|
* Vorschau-Daten fuer Warenkorb/Checkout — die eigentliche Anwendung im
|
|
* Checkout laeuft weiterhin ueber die volle DiscountCode-Zeile (fuer
|
|
* usedCount/Order-Verknuepfung), s. api/checkout/route.ts.
|
|
*/
|
|
export async function getBulkDiscountTiers(): Promise<BulkDiscountTierLike[]> {
|
|
const now = new Date();
|
|
const codes = await prisma.discountCode.findMany({
|
|
where: { minQuantity: { not: null }, type: "PERCENT", isActive: true },
|
|
});
|
|
return codes
|
|
.filter((code) => (!code.validFrom || code.validFrom <= now) && (!code.validUntil || code.validUntil >= now))
|
|
.map((code) => ({ code: code.code, minQuantity: code.minQuantity as number, percent: code.value }));
|
|
}
|