1- import { NextRequest , NextResponse } from 'next/server'
1+ import { NextResponse } from 'next/server'
22import { createServerClient } from '@/lib/supabase-server'
3- import { getTeamUsageLimits , getTeamSubscription } from '@/lib/subscription '
3+ import { stripe } from '@/lib/stripe '
44
5- export const dynamic = 'force-dynamic'
5+ export async function GET ( ) {
6+ const supabase = createServerClient ( )
7+ const {
8+ data : { user }
9+ } = await supabase . auth . getUser ( )
610
7- export async function GET ( request : NextRequest ) {
8- try {
9- const supabase = createServerClient ( )
10- const { data : { user } , error : authError } = await supabase . auth . getUser ( )
11+ if ( ! user ) {
12+ return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
13+ }
1114
12- if ( authError || ! user ?. id ) {
13- return NextResponse . json ( { error : 'Unauthorized ' } , { status : 401 } )
14- }
15+ if ( ! stripe ) {
16+ return NextResponse . json ( { error : 'Stripe not configured ' } , { status : 500 } )
17+ }
1518
16- // Get user's default team
17- const { data : userTeam , error : teamError } = await supabase
18- . from ( 'users_teams' )
19- . select ( 'teams (id)' )
20- . eq ( 'user_id' , user . id )
21- . eq ( 'is_default' , true )
22- . single ( )
19+ const { data : teamData } = await supabase
20+ . from ( 'users_teams' )
21+ . select ( 'teams (stripe_customer_id)' )
22+ . eq ( 'user_id' , user . id )
23+ . eq ( 'is_default' , true )
24+ . single ( )
2325
24- if ( teamError || ! userTeam ?. teams ) {
25- console . error ( 'Team lookup failed for user:' , user . id , teamError )
26- // Return default free tier limits instead of error
27- return NextResponse . json ( {
28- subscription : {
29- id : 'default' ,
30- name : 'Personal' ,
31- tier : 'free' ,
32- subscription_status : 'active' ,
33- cancel_at_period_end : false
34- } ,
35- usage_limits : [
36- {
37- usage_type : 'api_calls' ,
38- limit_value : 100 ,
39- current_usage : 0 ,
40- period_start : new Date ( ) . toISOString ( ) ,
41- period_end : new Date ( Date . now ( ) + 30 * 24 * 60 * 60 * 1000 ) . toISOString ( )
42- }
43- ]
44- } )
45- }
26+ const customerId = ( teamData ?. teams as any ) ?. stripe_customer_id
4627
47- const team = userTeam . teams as any
48-
49- const [ subscription , usageLimits ] = await Promise . all ( [
50- getTeamSubscription ( team . id ) ,
51- getTeamUsageLimits ( team . id )
52- ] )
28+ if ( ! customerId ) {
29+ return NextResponse . json ( { usage : [ ] } )
30+ }
5331
54- return NextResponse . json ( {
55- subscription,
56- usage_limits : usageLimits
32+ try {
33+ const subscriptions = await stripe . subscriptions . list ( {
34+ customer : customerId ,
35+ status : 'active' ,
36+ expand : [ 'data.items' ] ,
5737 } )
38+
39+ if ( ! subscriptions . data . length ) {
40+ return NextResponse . json ( { usage : [ ] } )
41+ }
42+
43+ const subscription = subscriptions . data [ 0 ]
44+ const usage = subscription . items . data . map ( ( item ) => ( {
45+ id : item . id ,
46+ quantity : item . quantity ,
47+ price : {
48+ id : ( item . price as any ) . id ,
49+ unit_amount : ( item . price as any ) . unit_amount ,
50+ currency : ( item . price as any ) . currency ,
51+ product : ( item . price as any ) . product ,
52+ } ,
53+ } ) )
54+
55+ return NextResponse . json ( { usage } )
5856 } catch ( error ) {
59- console . error ( 'Usage API error:' , error )
60- return NextResponse . json (
61- { error : 'Failed to fetch usage data' } ,
62- { status : 500 }
63- )
57+ console . error ( 'Error fetching Stripe subscription usage:' , error )
58+ return NextResponse . json ( { error : 'Failed to fetch subscription usage' } , { status : 500 } )
6459 }
65- }
60+ }
0 commit comments