Files
gutscheintracker/app/api/vouchers/[id]/route.ts
T

27 lines
1.4 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { getDb } from "@/lib/db";
import { withLogoUrl } from "@/lib/logo";
export async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const numId = Number(id);
if (!Number.isInteger(numId)) return NextResponse.json({ error: "not found" }, { status: 404 });
const db = getDb();
const v = await db.voucher.findUnique({ where: { id: numId }, include: { contacts: { include: { contact: true } } } });
if (!v) return NextResponse.json({ error: "not found" }, { status: 404 });
return NextResponse.json(await withLogoUrl(v));
}
export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const numId = Number(id);
if (!Number.isInteger(numId)) return NextResponse.json({ error: "not found" }, { status: 404 });
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: numId } });
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);
}