22import supabase from '../../../utils/supabase/config' ;
33import recommendationService from '../../../utils/supabase/recommendationService' ;
44
5-
6-
75export default async function handler ( req , res ) {
86 if ( req . method !== 'GET' ) {
97 return res . status ( 405 ) . json ( { error : 'Only GET supported' } ) ;
@@ -14,53 +12,59 @@ export default async function handler(req, res) {
1412 return res . status ( 401 ) . json ( { error : 'Missing or invalid Authorization header' } ) ;
1513 }
1614
17- const token = authHeader . split ( 'Bearer ' ) [ 1 ] ;
18- const { data : userData , error : userError } = await supabase . auth . getUser ( token ) ;
15+ try {
16+ const token = authHeader . split ( 'Bearer ' ) [ 1 ] ;
17+ const { data : userData , error : userError } = await supabase . auth . getUser ( token ) ;
1918
20- if ( userError || ! userData ?. user ) {
21- return res . status ( 401 ) . json ( { error : 'Invalid or expired token' } ) ;
22- }
19+ if ( userError || ! userData ?. user ) {
20+ console . error ( 'Auth error:' , userError ?. message || 'No user data' ) ;
21+ return res . status ( 401 ) . json ( { error : 'Invalid or expired token' } ) ;
22+ }
2323
24- const email = userData . user . email ;
24+ const email = userData . user . email ;
2525
26- // Get user ID from 'users' table
27- const { data : userRow , error : userLookupError } = await supabase
28- . from ( 'users' )
29- . select ( 'userid' )
30- . eq ( 'email' , email )
31- . single ( ) ;
26+ const { data : userRow , error : userLookupError } = await supabase
27+ . from ( 'users' )
28+ . select ( 'userid' )
29+ . eq ( 'email' , email )
30+ . single ( ) ;
3231
33- if ( userLookupError || ! userRow ) {
34- return res . status ( 404 ) . json ( { error : 'User not found' } ) ;
35- }
32+ if ( userLookupError || ! userRow ) {
33+ console . error ( 'User lookup failed:' , userLookupError ?. message || 'User not found' ) ;
34+ return res . status ( 404 ) . json ( { error : 'User not found' } ) ;
35+ }
3636
37- // Get sentiment reviews
38- const { data : sentimentData , error : sentimentError } = await supabase
39- . from ( 'sentiment' )
40- . select ( '*' )
41- . eq ( 'userid' , userRow . userid )
42- . order ( 'created_at' , { ascending : false } ) ;
37+ const { data : sentimentData , error : sentimentError } = await supabase
38+ . from ( 'sentiment' )
39+ . select ( '*' )
40+ . eq ( 'userid' , userRow . userid )
41+ . order ( 'created_at' , { ascending : false } ) ;
4342
44- if ( sentimentError ) {
45- return res . status ( 500 ) . json ( { error : 'Failed to fetch sentiment data' } ) ;
46- }
43+ if ( sentimentError ) {
44+ console . error ( 'Sentiment fetch error:' , sentimentError . message ) ;
45+ return res . status ( 500 ) . json ( { error : 'Failed to fetch sentiment data' } ) ;
46+ }
47+
48+ const reviews = sentimentData . map ( item => ( {
49+ productName : item . product_name ,
50+ rating : item . star_rating ,
51+ likedFeatures : item . sentiment_label === 'positive' ? 'style, comfort' : '' ,
52+ dislikedFeatures : item . sentiment_label === 'negative' ? 'fit, quality' : '' ,
53+ comment : item . review_text
54+ } ) ) ;
4755
48- // Transform reviews into Gemini-compatible format
49- const reviews = sentimentData . map ( item => ( {
50- productName : item . product_name ,
51- rating : item . star_rating ,
52- likedFeatures : item . sentiment_label === 'positive' ? 'style, comfort' : '' ,
53- dislikedFeatures : item . sentiment_label === 'negative' ? 'fit, quality' : '' ,
54- comment : item . review_text
55- } ) ) ;
56+ const userPayload = {
57+ reviews : reviews . slice ( 0 , 3 ) ,
58+ stylePreferences : { } ,
59+ purchaseHistory : [ ] ,
60+ budget : { weeklyAmount : 100 , spentAmount : 0 }
61+ } ;
5662
57- const userPayload = {
58- reviews : reviews . slice ( 0 , 3 ) , // ✅ limit to 3 to avoid token limit
59- stylePreferences : { } ,
60- purchaseHistory : [ ] ,
61- budget : { weeklyAmount : 100 , spentAmount : 0 }
62- } ;
63+ const recommendations = await recommendationService . generateRecommendations ( userPayload ) ;
64+ return res . status ( 200 ) . json ( { recommendations } ) ;
6365
64- const recommendations = await recommendationService . generateRecommendations ( userPayload ) ;
65- return res . status ( 200 ) . json ( { recommendations } ) ;
66+ } catch ( err ) {
67+ console . error ( 'Unexpected server error in getReviews:' , err ) ;
68+ return res . status ( 500 ) . json ( { error : 'Internal Server Error' } ) ;
69+ }
6670}
0 commit comments