(params: {
userId: string
subscriptionId: string
logger: Logger
conn?: DbConn
})
| 551 | // --------------------------------------------------------------------------- |
| 552 | |
| 553 | export async function expireActiveBlockGrants(params: { |
| 554 | userId: string |
| 555 | subscriptionId: string |
| 556 | logger: Logger |
| 557 | conn?: DbConn |
| 558 | }): Promise<number> { |
| 559 | const { userId, subscriptionId, logger, conn = db } = params |
| 560 | const now = new Date() |
| 561 | |
| 562 | const expired = await conn |
| 563 | .update(schema.creditLedger) |
| 564 | .set({ expires_at: now }) |
| 565 | .where( |
| 566 | and( |
| 567 | eq(schema.creditLedger.user_id, userId), |
| 568 | eq(schema.creditLedger.type, 'subscription'), |
| 569 | gt(schema.creditLedger.expires_at, now), |
| 570 | ), |
| 571 | ) |
| 572 | .returning({ operation_id: schema.creditLedger.operation_id }) |
| 573 | |
| 574 | if (expired.length > 0) { |
| 575 | logger.info( |
| 576 | { userId, subscriptionId, expiredCount: expired.length }, |
| 577 | 'Expired active block grants', |
| 578 | ) |
| 579 | } |
| 580 | |
| 581 | return expired.length |
| 582 | } |
| 583 | |
| 584 | // --------------------------------------------------------------------------- |
| 585 | // Subscription lookup |
no test coverage detected