This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { getHouseholdOrNull } from "@/lib/queries";
|
||||
|
||||
const createPlanSchema = z.object({
|
||||
name: z.string().min(1).max(120),
|
||||
});
|
||||
|
||||
export async function GET() {
|
||||
const household = await getHouseholdOrNull();
|
||||
if (!household) {
|
||||
return NextResponse.json({ plans: [] });
|
||||
}
|
||||
const plans = await prisma.plan.findMany({
|
||||
where: { householdId: household.id },
|
||||
orderBy: { createdAt: "asc" },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
parentPlanId: true,
|
||||
branchFromPhaseId: true,
|
||||
createdAt: true,
|
||||
phases: {
|
||||
select: { id: true, name: true, sequenceNumber: true },
|
||||
orderBy: { sequenceNumber: "asc" },
|
||||
},
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ plans });
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const household = await getHouseholdOrNull();
|
||||
if (!household) {
|
||||
return NextResponse.json(
|
||||
{ error: "Bitte zuerst das Grundprofil (Onboarding) anlegen." },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const parsed = createPlanSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
||||
}
|
||||
|
||||
const plan = await prisma.plan.create({
|
||||
data: { householdId: household.id, name: parsed.data.name },
|
||||
});
|
||||
|
||||
return NextResponse.json({ plan }, { status: 201 });
|
||||
}
|
||||
Reference in New Issue
Block a user