92 lines
4.1 KiB
TypeScript
92 lines
4.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getDb } from "@/lib/db";
|
|
import { getVoucherStatus } from "@/lib/voucher-status";
|
|
import { validateVoucher, isExpiringSoon } from "@/lib/vouchers";
|
|
import { parseReminderDays } from "@/lib/reminders";
|
|
import { normalizeDomain, withLogoUrl } from "@/lib/logo";
|
|
|
|
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 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" && isExpiringSoon(v.ablaufdatum);
|
|
if (filter === "aktiv") return s === "aktiv";
|
|
return true;
|
|
});
|
|
return NextResponse.json(await Promise.all(filtered.map(withLogoUrl)));
|
|
}
|
|
|
|
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 contactIds: number[] = Array.isArray(body.contactIds)
|
|
? body.contactIds.map(Number).filter((n: number) => Number.isInteger(n))
|
|
: [];
|
|
if (contactIds.length) {
|
|
const found = await db.contact.findMany({ where: { id: { in: contactIds } }, select: { id: true } });
|
|
if (found.length !== new Set(contactIds).size) {
|
|
return NextResponse.json({ errors: ["contactIds"] }, { status: 400 });
|
|
}
|
|
}
|
|
let createdBy: string | null = null;
|
|
if (body.memberId !== undefined && body.memberId !== null && String(body.memberId).trim() !== "") {
|
|
const memberId = Number(body.memberId);
|
|
if (!Number.isInteger(memberId)) return NextResponse.json({ errors: ["memberId"] }, { status: 400 });
|
|
const member = await db.householdMember.findUnique({ where: { id: memberId } });
|
|
if (!member) return NextResponse.json({ errors: ["memberId"] }, { status: 400 });
|
|
createdBy = member.name;
|
|
}
|
|
const providerDomain = normalizeDomain(
|
|
body.providerDomain === undefined || body.providerDomain === null ? null : String(body.providerDomain)
|
|
);
|
|
if (body.providerDomain !== undefined && body.providerDomain !== null && String(body.providerDomain).trim() !== "" && !providerDomain) {
|
|
return NextResponse.json({ errors: ["providerDomain"] }, { status: 400 });
|
|
}
|
|
const reminderRaw =
|
|
typeof body.reminderDays === "string"
|
|
? body.reminderDays
|
|
: Array.isArray(body.reminderDays)
|
|
? body.reminderDays
|
|
: undefined;
|
|
const explicitNone = Array.isArray(body.reminderDays) && body.reminderDays.length === 0;
|
|
const reminderDays = parseReminderDays(reminderRaw);
|
|
if (!explicitNone && reminderRaw !== undefined && (typeof reminderRaw !== "string" || reminderRaw.trim() !== "") && reminderDays.length === 0) {
|
|
return NextResponse.json({ errors: ["reminderDays"] }, { status: 400 });
|
|
}
|
|
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,
|
|
providerDomain,
|
|
createdBy,
|
|
contacts: contactIds.length ? { create: contactIds.map((contactId) => ({ contactId })) } : undefined,
|
|
rules: {
|
|
create: reminderDays.map((tageVorher) => ({ tageVorher })),
|
|
},
|
|
},
|
|
});
|
|
return NextResponse.json(created, { status: 201 });
|
|
}
|