import { Gift } from "lucide-react"; import { getTranslations } from "next-intl/server"; import { createVoucher, deleteVoucher } from "@/actions/admin-vouchers"; import { StatusCard } from "@/components/admin/dashboard"; import { prisma } from "@/lib/prisma"; import { redirect } from "next/navigation"; import { canAccess, getAdminContext, PERMS } from "@/lib/permissions"; export const dynamic = "force-dynamic"; type VoucherRow = { id: bigint; code: string; amount: number; maxUses: number; useCount: number; expiresAt: Date | null; createdAt: Date | null; }; function fmtDate(d: Date | null): string { if (!d) return "—"; return d.toISOString().slice(0, 16).replace("T", " "); } export default async function AdminVouchers() { const { session, permissions } = await getAdminContext(); if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) { redirect("/admin"); } const t = await getTranslations("pages.admin.vouchers"); const now = Date.now(); let vouchers: VoucherRow[] = []; try { vouchers = await prisma.websiteShopVouchers.findMany({ select: { id: true, code: true, amount: true, maxUses: true, useCount: true, expiresAt: true, createdAt: true, }, orderBy: { id: "desc" }, take: 200, }); } catch { vouchers = []; } const active = vouchers.filter((v) => { const expired = v.expiresAt ? v.expiresAt.getTime() <= now : false; const usedUp = v.maxUses > 0 && v.useCount >= v.maxUses; return !expired && !usedUp; }).length; const expiredCount = vouchers.filter( (v) => v.expiresAt && v.expiresAt.getTime() <= now, ).length; const usedUpCount = vouchers.filter( (v) => v.maxUses > 0 && v.useCount >= v.maxUses, ).length; return (

{t("title")}

{t("subtitle")}

0 ? "ok" : "neutral"} icon="✅" /> 0 ? "warn" : "neutral"} icon="📉" /> 0 ? "danger" : "neutral"} icon="⏰" />

{t("newVoucher")}

{t("title")} ({vouchers.length})

{vouchers.map((v) => { const expired = v.expiresAt ? v.expiresAt.getTime() <= now : false; const usedUp = v.maxUses > 0 && v.useCount >= v.maxUses; return ( ); })}
{t("colCode")} {t("colAmount")} {t("colUses")} {t("colStatus")} {t("colExpires")} {t("colCreated")}
{v.code} {v.amount} {v.useCount} / {v.maxUses} {expired ? ( {t("statusExpired")} ) : usedUp ? ( {t("statusUsedUp")} ) : ( {t("statusActive")} )} {fmtDate(v.expiresAt)} {fmtDate(v.createdAt)}
{vouchers.length === 0 ? (
{t("noVouchers")}
) : null}
); }