import type { BookingStatus, PaymentStatus, Prisma } from "@/generated/prisma/client"; const BOOKING_STATUSES: BookingStatus[] = ["PENDING", "CONFIRMED", "CANCELED", "EXPIRED", "NEEDS_REVIEW"]; const PAYMENT_STATUSES: PaymentStatus[] = ["UNPAID", "PROCESSING", "PARTIALLY_PAID", "PAID", "FAILED", "PARTIALLY_REFUNDED", "REFUNDED"]; export type BookingFilter = { status?: BookingStatus; zahlung?: PaymentStatus; modus: "test" | "live"; q?: string }; /** Liest Filter aus der URL; Unbekanntes wird ignoriert statt uebernommen. */ export function parseBookingFilter(params: Record, defaultMode: "test" | "live"): BookingFilter { const get = (k: string) => (typeof params[k] === "string" ? (params[k] as string) : undefined); const status = get("status"); const zahlung = get("zahlung"); const modus = get("modus"); const q = get("q")?.trim().slice(0, 100); return { status: BOOKING_STATUSES.includes(status as BookingStatus) ? (status as BookingStatus) : undefined, zahlung: PAYMENT_STATUSES.includes(zahlung as PaymentStatus) ? (zahlung as PaymentStatus) : undefined, modus: modus === "live" || modus === "test" ? modus : defaultMode, q: q || undefined, }; } export function bookingWhere(filter: BookingFilter): Prisma.BookingWhereInput { return { livemode: filter.modus === "live", ...(filter.status ? { status: filter.status } : {}), ...(filter.zahlung ? { paymentStatus: filter.zahlung } : {}), ...(filter.q ? { OR: [ { reference: { contains: filter.q, mode: "insensitive" } }, { email: { contains: filter.q, mode: "insensitive" } }, { lastName: { contains: filter.q, mode: "insensitive" } }, { firstName: { contains: filter.q, mode: "insensitive" } }, ], } : {}), }; } export function filterQuery(filter: BookingFilter): string { const params = new URLSearchParams(); params.set("modus", filter.modus); if (filter.status) params.set("status", filter.status); if (filter.zahlung) params.set("zahlung", filter.zahlung); if (filter.q) params.set("q", filter.q); return params.toString(); } /** Schutz vor Formel-Injektion: Zellen, die mit = + - @ beginnen, werden entschaerft. */ export function csvCell(value: string | number | null | undefined): string { let s = value === null || value === undefined ? "" : String(value); if (/^[=+\-@\t\r]/.test(s)) s = `'${s}`; return `"${s.replace(/"/g, '""')}"`; } export { BOOKING_STATUSES, PAYMENT_STATUSES };