@@ -220,7 +220,11 @@ export async function exportSubscriptions(): Promise<Record<string, { customer:
220220}
221221
222222export async function getActivePlan ( customerId : string ) : Promise < null | SubscriptionStatus > {
223- const subscriptions = await stripe . subscriptions . list ( { customer : customerId , status : "all" , limit : 10 } ) ;
223+ const [ subscriptions , invoices ] = await Promise . all ( [
224+ stripe . subscriptions . list ( { customer : customerId , status : "all" , limit : 10 } ) ,
225+ stripe . invoices . list ( { customer : customerId , limit : 10 } ) ,
226+ ] ) ;
227+ const hasPastDueInvoices = invoices . data . some ( inv => ! [ "paid" , "void" , "draft" ] . includes ( inv . status ?? "" ) ) ;
224228 const sub2product = new Map < string , Stripe . Product > ( ) ;
225229 for ( const sub of subscriptions . data ) {
226230 const productId = sub . items . data [ 0 ] . price . product ;
@@ -264,7 +268,7 @@ export async function getActivePlan(customerId: string): Promise<null | Subscrip
264268 . toISOString ( ) ,
265269 } ,
266270 renewAfterExpiration : ! subscription . cancel_at_period_end ,
267- pastDue : pastDueSubscription && ! activeSubscription ,
271+ pastDue : hasPastDueInvoices ,
268272 planKind : planData . planKind ,
269273
270274 //omit token field that might be considered as sensitive
@@ -402,7 +406,6 @@ export async function listAllInvoices() {
402406 do {
403407 const result = await stripe . invoices . list ( {
404408 limit : 100 ,
405- status : "paid" ,
406409 starting_after : starting_after ,
407410 created : {
408411 //invoices for past 90 days
@@ -411,7 +414,7 @@ export async function listAllInvoices() {
411414 } ) ;
412415 starting_after = result ?. data [ result . data . length - 1 ] ?. id ;
413416 if ( result ?. data ) {
414- allInvoices . push ( ...result ?. data ) ;
417+ allInvoices . push ( ...result ?. data ?. filter ( i => i . status === "paid" || i . status === "open" ) ) ;
415418 }
416419 } while ( starting_after ) ;
417420 getLog ( )
0 commit comments