@@ -17,8 +17,8 @@ type VercelResponse = {
1717 json : ( data : unknown ) => void ;
1818 send : ( data : unknown ) => void ;
1919} ;
20- import { generateAdaptiveQuestions } from "../src/lib/services/gemini" ;
21- import { getCollection } from "../src/lib/mongodb" ;
20+ import { generateAdaptiveQuestions } from "../src/lib/services/gemini.js " ;
21+ import { getCollection } from "../src/lib/mongodb.js " ;
2222
2323export default async function handler (
2424 req : VercelRequest ,
@@ -29,23 +29,62 @@ export default async function handler(
2929 }
3030
3131 try {
32- interface OnboardingRequestBody {
32+ type OnboardingAction = "generate" | "complete" ;
33+ interface BaseOnboardingBody {
34+ action ?: OnboardingAction ;
3335 firebaseUid ?: string ;
36+ }
37+ interface GenerateBody extends BaseOnboardingBody {
3438 ageRange ?: string ;
3539 experience ?: string ;
3640 monthlyIncome ?: string ;
3741 riskTolerance ?: number ;
3842 learningGoals ?: string [ ] ;
3943 }
40- const body = req . body as OnboardingRequestBody ;
44+ interface CompleteBody extends BaseOnboardingBody {
45+ action : "complete" ;
46+ answers ?: Record < string , unknown > ;
47+ startingLesson ?: number ;
48+ }
49+
50+ const body = req . body as GenerateBody | CompleteBody ;
51+ const action : OnboardingAction = body . action || "generate" ;
52+
53+ if ( action === "complete" ) {
54+ const { firebaseUid, answers, startingLesson } = body as CompleteBody ;
55+
56+ if ( ! firebaseUid || ! answers ) {
57+ return res . status ( 400 ) . json ( { error : "Missing required completion data" } ) ;
58+ }
59+
60+ const usersCollection = await getCollection ( "users" ) ;
61+ const result = await usersCollection . updateOne (
62+ { firebaseUid } ,
63+ {
64+ $set : {
65+ onboardingCompleted : true ,
66+ onboardingAnswers : answers ,
67+ currentLesson : typeof startingLesson === "number" ? startingLesson : 1 ,
68+ updatedAt : new Date ( ) ,
69+ } ,
70+ }
71+ ) ;
72+
73+ if ( result . matchedCount === 0 ) {
74+ return res . status ( 404 ) . json ( { error : "User not found" } ) ;
75+ }
76+
77+ return res . status ( 200 ) . json ( { success : true } ) ;
78+ }
79+
4180 const {
4281 firebaseUid,
4382 ageRange,
4483 experience,
4584 monthlyIncome,
4685 riskTolerance,
4786 learningGoals,
48- } = body ;
87+ } = body as GenerateBody ;
4988
5089 // Validate required fields
5190 if ( ! firebaseUid || ! ageRange || ! experience || ! monthlyIncome || riskTolerance === undefined || ! learningGoals ) {
@@ -61,28 +100,6 @@ export default async function handler(
61100 learningGoals
62101 ) ;
63102
64- // Save to MongoDB
65- const usersCollection = await getCollection ( "users" ) ;
66-
67- await usersCollection . updateOne (
68- { firebaseUid } ,
69- {
70- $set : {
71- onboardingCompleted : true ,
72- onboardingAnswers : {
73- ageRange,
74- investmentExperience : experience ,
75- monthlyIncome,
76- riskTolerance,
77- goals : learningGoals ,
78- } ,
79- currentLesson : geminiResponse . startingLesson ,
80- updatedAt : new Date ( ) ,
81- } ,
82- } ,
83- { upsert : true }
84- ) ;
85-
86103 return res . status ( 200 ) . json ( {
87104 adaptiveQuestions : geminiResponse . adaptiveQuestions ,
88105 startingLesson : geminiResponse . startingLesson ,
@@ -94,4 +111,4 @@ export default async function handler(
94111 message : error instanceof Error ? error . message : "Unknown error"
95112 } ) ;
96113 }
97- }
114+ }
0 commit comments