@@ -14,10 +14,10 @@ import {
1414 eq ,
1515 gt ,
1616 gte ,
17- inArray ,
1817 isNull ,
1918 lt ,
20- or ,
19+ lte ,
20+ ne ,
2121 sql ,
2222} from 'drizzle-orm'
2323
@@ -602,8 +602,9 @@ type DbTransaction = Parameters<typeof db.transaction>[0] extends (
602602 : never
603603
604604/**
605- * Migrates unused free & referral credits into a single grant that expires
606- * at `expiresAt`. The old grants have their balance zeroed.
605+ * Migrates unused credits (any type with a non-null expires_at in the future)
606+ * into a single grant that expires at `expiresAt`. The old grants have their
607+ * balance zeroed.
607608 */
608609async function migrateUnusedCredits ( params : {
609610 tx : DbTransaction
@@ -615,20 +616,17 @@ async function migrateUnusedCredits(params: {
615616 const { tx, userId, subscriptionId, expiresAt, logger } = params
616617 const now = new Date ( )
617618
618- // Find all free/referral grants with remaining balance (excluding org grants)
619619 const unusedGrants = await tx
620620 . select ( )
621621 . from ( schema . creditLedger )
622622 . where (
623623 and (
624624 eq ( schema . creditLedger . user_id , userId ) ,
625625 gt ( schema . creditLedger . balance , 0 ) ,
626- inArray ( schema . creditLedger . type , [ 'free' , 'referral' ] ) ,
626+ ne ( schema . creditLedger . type , 'subscription' ) ,
627627 isNull ( schema . creditLedger . org_id ) ,
628- or (
629- isNull ( schema . creditLedger . expires_at ) ,
630- gt ( schema . creditLedger . expires_at , now ) ,
631- ) ,
628+ gt ( schema . creditLedger . expires_at , now ) ,
629+ lte ( schema . creditLedger . expires_at , expiresAt ) ,
632630 ) ,
633631 )
634632
0 commit comments