feat: voucher crud and list

This commit is contained in:
opencode committed 2026-10-18 10:30:55 +02:00
1 parent 73295653ee
commit 660d6cff51
7 files changed
+358

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
import { NextRequest, NextResponse } from "next/server";
import { getDb } from "@/lib/db";
export async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const db = getDb();
const v = await db.voucher.findUnique({ where: { id: Number(id) } });
if (!v) return NextResponse.json({ error: "not found" }, { status: 404 });
return NextResponse.json(v);
}
export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const body = await req.json().catch(() => ({}));
if (body.action !== "redeem") return NextResponse.json({ error: "unknown action" }, { status: 400 });
const db = getDb();
const v = await db.voucher.findUnique({ where: { id: Number(id) } });
if (!v) return NextResponse.json({ error: "not found" }, { status: 404 });
const updated = await db.voucher.update({ where: { id: v.id }, data: { status: "eingeloest", restCents: 0 } });
return NextResponse.json(updated);
}
+57
View File
@@ -0,0 +1,57 @@
import { NextRequest, NextResponse } from "next/server";
import { getDb } from "@/lib/db";
import { getVoucherStatus } from "@/lib/voucher-status";
import { validateVoucher } from "@/lib/vouchers";
export async function GET(req: NextRequest) {
const filter = req.nextUrl.searchParams.get("filter");
const db = getDb();
const all = await db.voucher.findMany({ orderBy: { createdAt: "desc" } });
const now = Date.now();
const in14d = now + 14 * 24 * 3600 * 1000;
const filtered = all.filter((v) => {
const s = getVoucherStatus(v);
if (filter === "eingeloest") return s === "eingeloest";
if (filter === "abgelaufen") return s === "abgelaufen";
if (filter === "bald")
return (
s === "aktiv" &&
v.ablaufdatum &&
new Date(v.ablaufdatum).getTime() >= now &&
new Date(v.ablaufdatum).getTime() <= in14d
);
if (filter === "aktiv") return s === "aktiv";
return true;
});
return NextResponse.json(filtered);
}
export async function POST(req: NextRequest) {
const body = await req.json();
const input = {
name: String(body.name ?? ""),
anbieter: String(body.anbieter ?? ""),
wertCents: Number(body.wertCents),
restCents: body.restCents === undefined || body.restCents === null || body.restCents === "" ? Number(body.wertCents) : Number(body.restCents),
ablaufdatum: body.ablaufdatum ? String(body.ablaufdatum) : null,
codeText: body.codeText ? String(body.codeText) : null,
kategorie: body.kategorie ? String(body.kategorie) : null,
notiz: body.notiz ? String(body.notiz) : null,
};
const errs = validateVoucher(input);
if (errs.length) return NextResponse.json({ errors: errs }, { status: 400 });
const db = getDb();
const created = await db.voucher.create({
data: {
name: input.name.trim(),
anbieter: input.anbieter.trim(),
wertCents: input.wertCents,
restCents: input.restCents,
ablaufdatum: input.ablaufdatum ? new Date(input.ablaufdatum) : null,
codeText: input.codeText,
kategorie: input.kategorie,
notiz: input.notiz,
},
});
return NextResponse.json(created, { status: 201 });
}