expria-backend/src/lib/access.ts

74 lines
1.7 KiB
TypeScript

export type Plan = 'free' | 'standard' | 'premium'
export type Feature =
| 'oral_t2_live'
| 'detailed_report'
| 'tips'
| 'dashboard'
| 'exam_mode'
| 'pattern_analysis'
| 'preparation_index'
| 'basic_report'
export const PLANS = {
free: {
simulations_lifetime: 5,
oral_t2_live: false,
detailed_report: false,
tips: false,
dashboard: false,
exam_mode: false,
pattern_analysis: false,
preparation_index: false,
},
standard: {
simulations_lifetime: null,
oral_t2_live: false,
detailed_report: true,
tips: true,
dashboard: true,
exam_mode: false,
pattern_analysis: false,
preparation_index: false,
},
premium: {
simulations_lifetime: null,
oral_t2_live: true,
detailed_report: true,
tips: true,
dashboard: true,
exam_mode: true,
pattern_analysis: true,
preparation_index: true,
},
}
export function getPlanPermissions(plan: Plan) {
const perms = PLANS[plan]
if (!perms) {
throw new Error(`Plan inconnu : ${plan}`)
}
return perms
}
export function canUserSimulate(user: { plan: string; simulations_used: number }): {
allowed: boolean
reason?: string
} {
if (!(user.plan in PLANS)) {
return { allowed: false, reason: 'invalid_plan' }
}
const plan = user.plan as Plan
const perms = PLANS[plan]
if (perms.simulations_lifetime !== null && user.simulations_used >= perms.simulations_lifetime) {
return { allowed: false, reason: 'quota_reached' }
}
return { allowed: true }
}
export function checkFeatureAccess(plan: Plan, feature: Feature): boolean {
if (feature === 'basic_report') return true
const perms = PLANS[plan]
if (!perms) return false
return perms[feature as keyof typeof perms] === true
}