2025-11-26 08:02:48 +01:00

72 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// app/api/user-groups/route.ts
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
export async function POST(req: Request) {
try {
const body = await req.json();
// 🔹 BULK: { names: string[] }
if (Array.isArray(body?.names)) {
const rawNames = body.names as unknown[];
const trimmedNames = rawNames
.filter((n): n is string => typeof n === 'string')
.map((n) => n.trim())
.filter((n) => n.length > 0);
const uniqueNames = Array.from(new Set(trimmedNames));
const groups = [];
for (const name of uniqueNames) {
const group = await prisma.userGroup.upsert({
where: { name },
update: {},
create: { name },
});
groups.push({ id: group.id, name: group.name });
}
return NextResponse.json(
{ groups },
{ status: 200 },
);
}
// 🔹 SINGLE: { name: string } wie bisher
const { name } = body;
if (!name || typeof name !== 'string') {
return NextResponse.json(
{ error: 'Gruppenname ist erforderlich.' },
{ status: 400 },
);
}
const trimmed = name.trim();
if (!trimmed) {
return NextResponse.json(
{ error: 'Gruppenname darf nicht leer sein.' },
{ status: 400 },
);
}
const group = await prisma.userGroup.upsert({
where: { name: trimmed },
update: {},
create: { name: trimmed },
});
return NextResponse.json(
{ id: group.id, name: group.name },
{ status: 200 },
);
} catch (err) {
console.error('[POST /api/user-groups]', err);
return NextResponse.json(
{ error: 'Interner Serverfehler beim Anlegen der User-Gruppe.' },
{ status: 500 },
);
}
}