File size: 883 Bytes
1fe7075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 };
}