import { ForbiddenException, Global, Injectable, Module } from '@nestjs/common'; import { Plan } from '@prisma/client'; import { PrismaService } from '../prisma/prisma.service'; import { PLANS } from './plans'; /** * Droits d'un magasin selon sa formule. Un magasin sans abonnement actif * (brouillon, essai expiré, impayé) a les droits de la formule Essentiel. * Les modules complémentaires sont gérés par ModulesService. */ @Injectable() export class EntitlementsService { constructor(private prisma: PrismaService) {} async planOf(storeId: string): Promise { const sub = await this.prisma.subscription.findUnique({ where: { storeId }, select: { plan: true, status: true } }); return sub && sub.status === 'ACTIVE' ? sub.plan : 'ESSENTIEL'; } /** Nombre d'employés actifs autorisé ; `adding` = 1 pour une création ou une réactivation. */ async requireStaffSlot(storeId: string) { const plan = await this.planOf(storeId); const max = PLANS[plan].limits.maxStaff; if (max === null) return; const active = await this.prisma.staffUser.count({ where: { storeId, active: true } }); if (active >= max) { throw new ForbiddenException({ statusCode: 403, code: 'PLAN_REQUIRED', requiredPlan: 'PRO', message: `La formule ${PLANS[plan].name} est limitée à ${max} employés actifs. Désactivez un compte ou passez à la formule Pro.`, }); } } } @Global() @Module({ providers: [EntitlementsService], exports: [EntitlementsService] }) export class EntitlementsModule {}