import { getCurrentCustomer } from "@/lib/auth"; import { getOwnedSponsorship, type OwnedSponsorship } from "@/lib/sponsorship/access"; import { rateLimit } from "@/lib/rate-limit"; import { getLocale } from "@/i18n/locale"; import type { Locale } from "@/i18n/config"; import type { Customer } from "@/generated/prisma/client"; export interface OwnedContext { customer: Customer; sponsorship: OwnedSponsorship; locale: Locale; } /** * Anmeldung und Besitz in einem Schritt. null heisst fuer jede Route: 404 — * auch ohne Anmeldung, damit eine Route nicht verraet, ob es die Patenschaft * gibt. */ export async function loadOwnedSponsorship(id: string): Promise { const customer = await getCurrentCustomer(); if (!customer) return null; const locale = await getLocale(); const sponsorship = await getOwnedSponsorship(customer.id, id, locale); return sponsorship ? { customer, sponsorship, locale } : null; } // Rendern (Textur, Medaille, ZIP) kostet spuerbar CPU. Zwischengespeicherte // Ergebnisse zaehlen nicht; das Limit trifft nur echte Renderlaeufe. 60 in // 10 Minuten reichen fuer jede normale Nutzung mehrerer Patenschaften. const RENDERS_PER_WINDOW = 60; const RENDER_WINDOW_MS = 10 * 60 * 1000; export function allowRender(customerId: string): boolean { return rateLimit(`sponsorship-render:${customerId}`, RENDERS_PER_WINDOW, RENDER_WINDOW_MS); }