diff --git a/src/app/api/onboarding/route.ts b/src/app/api/onboarding/route.ts index faf5055..b553802 100644 --- a/src/app/api/onboarding/route.ts +++ b/src/app/api/onboarding/route.ts @@ -24,34 +24,39 @@ export async function POST(request: Request) { const body = await request.json(); const { familyName, memberName, childName, birthDate, sex } = body; - // Create family - const familyId = crypto.randomUUID(); - await sql` - INSERT INTO families (id, name, tier, max_children, max_members, created_at, updated_at) - VALUES (${familyId}, ${familyName || "The Family"}, 'free', 3, 4, NOW(), NOW()) - `; + try { + // Create family + const familyId = crypto.randomUUID(); + await sql.unsafe( + `INSERT INTO families (id, name, tier, created_at, updated_at) VALUES ($1, $2, 'free', NOW(), NOW())`, + [familyId, familyName || "The Family"] + ); - // Add member - await sql` - INSERT INTO family_members (id, family_id, user_id, role, display_name, created_at) - VALUES (${crypto.randomUUID()}, ${familyId}, ${userId}, 'admin', ${memberName}, NOW()) - `; + // Add member + await sql.unsafe( + `INSERT INTO family_members (id, family_id, user_id, role, display_name, created_at) VALUES ($1, $2, $3, 'admin', $4, NOW())`, + [crypto.randomUUID(), familyId, userId, memberName] + ); - // Create child - const childId = crypto.randomUUID(); - const birth = new Date(birthDate); - const months = Math.floor((Date.now() - birth.getTime()) / (1000 * 60 * 60 * 24 * 30)); - let stage = "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 + const childId = crypto.randomUUID(); + const birth = new Date(birthDate); + const months = Math.floor((Date.now() - birth.getTime()) / (1000 * 60 * 60 * 24 * 30)); + let stage = "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"; - await sql` - INSERT INTO children (id, family_id, name, birth_date, sex, current_stage, created_at) - VALUES (${childId}, ${familyId}, ${childName}, ${birth.toISOString()}, ${sex}, ${stage}, NOW()) - `; + await sql.unsafe( + `INSERT INTO children (id, family_id, name, birth_date, sex, current_stage, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, NOW(), NOW())`, + [childId, familyId, childName, birth.toISOString(), sex, stage] + ); - return NextResponse.json({ success: true, childId, familyId }); + return NextResponse.json({ success: true, childId, familyId }); + } catch (error) { + console.error("Onboarding error:", error); + return NextResponse.json({ error: String(error) }, { status: 500 }); + } } \ No newline at end of file