(params: {
userId: string
logger: Logger
conn?: DbConn
tier?: number | null
})
| 150 | * Checks `limit_override` first, then falls back to the default tier constants. |
| 151 | */ |
| 152 | export async function getSubscriptionLimits(params: { |
| 153 | userId: string |
| 154 | logger: Logger |
| 155 | conn?: DbConn |
| 156 | tier?: number | null |
| 157 | }): Promise<SubscriptionLimits> { |
| 158 | const { userId, logger, conn = db, tier } = params |
| 159 | |
| 160 | const overrides = await conn |
| 161 | .select() |
| 162 | .from(schema.limitOverride) |
| 163 | .where(eq(schema.limitOverride.user_id, userId)) |
| 164 | .limit(1) |
| 165 | |
| 166 | if (overrides.length > 0) { |
| 167 | const o = overrides[0] |
| 168 | logger.debug( |
| 169 | { userId, creditsPerBlock: o.credits_per_block }, |
| 170 | 'Using limit override for user', |
| 171 | ) |
| 172 | return { |
| 173 | creditsPerBlock: o.credits_per_block, |
| 174 | blockDurationHours: o.block_duration_hours, |
| 175 | weeklyCreditsLimit: o.weekly_credit_limit, |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | const tierConfig = |
| 180 | tier != null && tier in SUBSCRIPTION_TIERS |
| 181 | ? SUBSCRIPTION_TIERS[tier as SubscriptionTierPrice] |
| 182 | : DEFAULT_TIER |
| 183 | |
| 184 | return { |
| 185 | creditsPerBlock: tierConfig.creditsPerBlock, |
| 186 | blockDurationHours: tierConfig.blockDurationHours, |
| 187 | weeklyCreditsLimit: tierConfig.weeklyCreditsLimit, |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // --------------------------------------------------------------------------- |
| 192 | // Weekly usage tracking |
no test coverage detected