feat: contacts and voucher assignment
This commit is contained in:
1 parent
c86ec8bd36
commit
f2b1d0293a
8 files changed
+154
-4
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getDb } from "@/lib/db";
|
||||
import { validateContact } from "@/lib/contacts";
|
||||
|
||||
export async function GET() {
|
||||
const db = getDb();
|
||||
const all = await db.contact.findMany({ orderBy: { name: "asc" } });
|
||||
return NextResponse.json(all);
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const input = {
|
||||
name: String(body.name ?? ""),
|
||||
email: body.email ? String(body.email) : null,
|
||||
telegramChatId: body.telegramChatId ? String(body.telegramChatId) : null,
|
||||
};
|
||||
const errs = validateContact(input);
|
||||
if (errs.length) return NextResponse.json({ errors: errs }, { status: 400 });
|
||||
const db = getDb();
|
||||
const created = await db.contact.create({
|
||||
data: { name: input.name.trim(), email: input.email?.trim() || null, telegramChatId: input.telegramChatId?.trim() || null },
|
||||
});
|
||||
return NextResponse.json(created, { status: 201 });
|
||||
}
|
||||
@@ -6,7 +6,7 @@ export async function GET(_req: NextRequest, { params }: { params: Promise<{ id:
|
||||
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 } });
|
||||
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(v);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,9 @@ export async function POST(req: NextRequest) {
|
||||
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))
|
||||
: [];
|
||||
const created = await db.voucher.create({
|
||||
data: {
|
||||
name: input.name.trim(),
|
||||
@@ -43,6 +46,7 @@ export async function POST(req: NextRequest) {
|
||||
codeText: input.codeText,
|
||||
kategorie: input.kategorie,
|
||||
notiz: input.notiz,
|
||||
contacts: contactIds.length ? { create: contactIds.map((contactId) => ({ contactId })) } : undefined,
|
||||
},
|
||||
});
|
||||
return NextResponse.json(created, { status: 201 });
|
||||
|
||||
Reference in new issue
Block a user