(row: {
subscription: {
plan: (typeof BlackPlans)[number]
} | null
timeSubscriptionCreated: Date | null
fixedUsage: number | null
rollingUsage: number | null
timeFixedUpdated: Date | null
timeRollingUpdated: Date | null
})
| 314 | } |
| 315 | |
| 316 | function getSubscriptionStatus(row: { |
| 317 | subscription: { |
| 318 | plan: (typeof BlackPlans)[number] |
| 319 | } | null |
| 320 | timeSubscriptionCreated: Date | null |
| 321 | fixedUsage: number | null |
| 322 | rollingUsage: number | null |
| 323 | timeFixedUpdated: Date | null |
| 324 | timeRollingUpdated: Date | null |
| 325 | }) { |
| 326 | if (!row.timeSubscriptionCreated || !row.subscription) { |
| 327 | return { weekly: null, rolling: null, rateLimited: null, retryIn: null } |
| 328 | } |
| 329 | |
| 330 | const black = BlackData.getLimits({ plan: row.subscription.plan }) |
| 331 | const now = new Date() |
| 332 | const week = getWeekBounds(now) |
| 333 | |
| 334 | const fixedLimit = black.fixedLimit ? centsToMicroCents(black.fixedLimit * 100) : null |
| 335 | const rollingLimit = black.rollingLimit ? centsToMicroCents(black.rollingLimit * 100) : null |
| 336 | const rollingWindowMs = (black.rollingWindow ?? 5) * 3600 * 1000 |
| 337 | |
| 338 | // Calculate current weekly usage (reset if outside current week) |
| 339 | const currentWeekly = |
| 340 | row.fixedUsage && row.timeFixedUpdated && row.timeFixedUpdated >= week.start ? row.fixedUsage : 0 |
| 341 | |
| 342 | // Calculate current rolling usage |
| 343 | const windowStart = new Date(now.getTime() - rollingWindowMs) |
| 344 | const currentRolling = |
| 345 | row.rollingUsage && row.timeRollingUpdated && row.timeRollingUpdated >= windowStart ? row.rollingUsage : 0 |
| 346 | |
| 347 | // Check rate limiting |
| 348 | const isWeeklyLimited = fixedLimit !== null && currentWeekly >= fixedLimit |
| 349 | const isRollingLimited = rollingLimit !== null && currentRolling >= rollingLimit |
| 350 | |
| 351 | let retryIn: string | null = null |
| 352 | if (isWeeklyLimited) { |
| 353 | const retryAfter = Math.ceil((week.end.getTime() - now.getTime()) / 1000) |
| 354 | retryIn = formatRetryTime(retryAfter) |
| 355 | } else if (isRollingLimited && row.timeRollingUpdated) { |
| 356 | const retryAfter = Math.ceil((row.timeRollingUpdated.getTime() + rollingWindowMs - now.getTime()) / 1000) |
| 357 | retryIn = formatRetryTime(retryAfter) |
| 358 | } |
| 359 | |
| 360 | return { |
| 361 | weekly: fixedLimit !== null ? `${formatMicroCents(currentWeekly)} / $${black.fixedLimit}` : null, |
| 362 | rolling: rollingLimit !== null ? `${formatMicroCents(currentRolling)} / $${black.rollingLimit}` : null, |
| 363 | rateLimited: isWeeklyLimited || isRollingLimited ? "yes" : "no", |
| 364 | retryIn, |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | function printHeader(title: string) { |
| 369 | console.log() |
no test coverage detected