import type { AppConfig } from '../config/app-config.interface.js'; import { UserRole, type Subscription } from '../generated/prisma/client.js'; import type { Allowance } from './interfaces/allowance.interface.js'; /** Admins: unlimited. Subscribers: the plan's monthly allowance. Everyone else: the lifetime free tries. */ export function allowanceFor(role: UserRole, subscription: Subscription | null, config: AppConfig): Allowance { if (role === UserRole.admin) return { plan: 'unlimited', limit: null, countFrom: null, renewsAt: null }; if (subscription) { return { plan: subscription.plan, limit: config.stripe.plans[subscription.plan].monthlyGenerations, countFrom: subscription.currentPeriodStart, renewsAt: subscription.currentPeriodEnd, }; } return { plan: 'free', limit: config.freeGenerations, countFrom: null, renewsAt: null }; }