@@ -14,7 +14,6 @@ import {
1414 eq ,
1515 gt ,
1616 gte ,
17- inArray ,
1817 isNull ,
1918 lt ,
2019 or ,
@@ -175,14 +174,14 @@ export async function getSubscriptionLimits(params: {
175174 * billing-aligned week.
176175 */
177176export async function getWeeklyUsage ( params : {
178- stripeSubscriptionId : string
177+ userId : string
179178 billingPeriodStart : Date
180179 weeklyCreditsLimit : number
181180 logger : Logger
182181 conn ?: DbConn
183182} ) : Promise < WeeklyUsage > {
184183 const {
185- stripeSubscriptionId ,
184+ userId ,
186185 billingPeriodStart,
187186 weeklyCreditsLimit,
188187 conn = db ,
@@ -199,10 +198,7 @@ export async function getWeeklyUsage(params: {
199198 . from ( schema . creditLedger )
200199 . where (
201200 and (
202- eq (
203- schema . creditLedger . stripe_subscription_id ,
204- stripeSubscriptionId ,
205- ) ,
201+ eq ( schema . creditLedger . user_id , userId ) ,
206202 eq ( schema . creditLedger . type , 'subscription' ) ,
207203 gte ( schema . creditLedger . created_at , weekStart ) ,
208204 lt ( schema . creditLedger . created_at , weekEnd ) ,
@@ -255,7 +251,6 @@ export async function ensureActiveBlockGrant(params: {
255251 and (
256252 eq ( schema . creditLedger . user_id , userId ) ,
257253 eq ( schema . creditLedger . type , 'subscription' ) ,
258- eq ( schema . creditLedger . stripe_subscription_id , subscriptionId ) ,
259254 gt ( schema . creditLedger . expires_at , now ) ,
260255 gt ( schema . creditLedger . balance , 0 ) ,
261256 ) ,
@@ -282,7 +277,7 @@ export async function ensureActiveBlockGrant(params: {
282277
283278 // 3. Check weekly limit before creating a new block
284279 const weekly = await getWeeklyUsage ( {
285- stripeSubscriptionId : subscriptionId ,
280+ userId ,
286281 billingPeriodStart : subscription . billing_period_start ,
287282 weeklyCreditsLimit : limits . weeklyCreditsLimit ,
288283 logger,
@@ -392,7 +387,6 @@ export async function checkRateLimit(params: {
392387 logger : Logger
393388} ) : Promise < RateLimitStatus > {
394389 const { userId, subscription, logger } = params
395- const subscriptionId = subscription . stripe_subscription_id
396390 const now = new Date ( )
397391
398392 const limits = await getSubscriptionLimits ( {
@@ -401,7 +395,7 @@ export async function checkRateLimit(params: {
401395 } )
402396
403397 const weekly = await getWeeklyUsage ( {
404- stripeSubscriptionId : subscriptionId ,
398+ userId ,
405399 billingPeriodStart : subscription . billing_period_start ,
406400 weeklyCreditsLimit : limits . weeklyCreditsLimit ,
407401 logger,
@@ -420,15 +414,14 @@ export async function checkRateLimit(params: {
420414 }
421415 }
422416
423- // Find most recent block grant for this subscription
417+ // Find most recent subscription block grant for this user
424418 const blocks = await db
425419 . select ( )
426420 . from ( schema . creditLedger )
427421 . where (
428422 and (
429423 eq ( schema . creditLedger . user_id , userId ) ,
430424 eq ( schema . creditLedger . type , 'subscription' ) ,
431- eq ( schema . creditLedger . stripe_subscription_id , subscriptionId ) ,
432425 ) ,
433426 )
434427 . orderBy ( desc ( schema . creditLedger . created_at ) )
@@ -616,7 +609,6 @@ async function migrateUnusedCredits(params: {
616609 . where (
617610 and (
618611 eq ( schema . creditLedger . user_id , userId ) ,
619- inArray ( schema . creditLedger . type , [ 'free' , 'referral' ] ) ,
620612 gt ( schema . creditLedger . balance , 0 ) ,
621613 or (
622614 isNull ( schema . creditLedger . expires_at ) ,
0 commit comments