diff --git a/src/app/api/onboarding/route.ts b/src/app/api/onboarding/route.ts new file mode 100644 index 0000000..06a8623 --- /dev/null +++ b/src/app/api/onboarding/route.ts @@ -0,0 +1,63 @@ +import { NextResponse } from "next/server"; +import { db } from "@/db"; +import { auth } from "@/auth"; +import { families, familyMembers, children } from "@/db/schema/family"; +import { users } from "@/db/schema/auth"; +import { eq } from "drizzle-orm"; + +export async function POST(request: Request) { + const session = await auth(); + + if (!session?.user?.email) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const body = await request.json(); + const { familyName, memberName, childName, birthDate, sex } = body; + + // Get or create user + let user = await db.query.users.findFirst({ + where: eq(users.email, session.user.email), + }); + + if (!user) { + return NextResponse.json({ error: "User not found" }, { status: 404 }); + } + + // Create family + const [family] = await db + .insert(families) + .values({ + name: familyName || "The Family", + }) + .returning(); + + // Add member + await db.insert(familyMembers).values({ + familyId: family.id, + userId: user.id, + role: "admin", + displayName: memberName, + }); + + // Calculate stage + const birth = new Date(birthDate); + const months = Math.floor((Date.now() - birth.getTime()) / (1000 * 60 * 60 * 24 * 30)); + let stage: "newborn" | "infant" | "solids_start" | "toddler_early" | "toddler_late" | "preschool" = "newborn"; + if (months >= 36) stage = "preschool"; + else if (months >= 24) stage = "toddler_late"; + else if (months >= 12) stage = "toddler_early"; + else if (months >= 6) stage = "solids_start"; + else if (months >= 3) stage = "infant"; + + // Create child + await db.insert(children).values({ + familyId: family.id, + name: childName, + birthDate: birth, + sex, + currentStage: stage, + }); + + return NextResponse.json({ success: true }); +} \ No newline at end of file diff --git a/src/app/onboarding/page.tsx b/src/app/onboarding/page.tsx new file mode 100644 index 0000000..8d2d912 --- /dev/null +++ b/src/app/onboarding/page.tsx @@ -0,0 +1,139 @@ +"use client"; + +import { useState } from "react"; +import { useRouter } from "next/navigation"; + +export default function OnboardingPage() { + const router = useRouter(); + const [step, setStep] = useState(1); + const [loading, setLoading] = useState(false); + const [form, setForm] = useState({ + familyName: "", + memberName: "", + childName: "", + birthDate: "", + sex: "" as "male" | "female" | "other", + }); + + const handleSubmit = async () => { + setLoading(true); + try { + const res = await fetch("/api/onboarding", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(form), + }); + if (res.ok) { + router.push("/"); + } + } catch (e) { + console.error(e); + } + setLoading(false); + }; + + return ( +
Let's set up your family
+