feat: add multi-tenant client management for admins
- Add tenants API endpoints (list, get, create) - Add tenant middleware override via X-View-Tenant header - Add TenantSelector dropdown component in header - Add tenant view store with persistence - Add Clientes management page - Update all navigation layouts with Clientes link for admins Admins can now: - View list of all clients - Create new clients with automatic schema setup - Switch between viewing different clients' data - See which client they are currently viewing Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
60
apps/api/src/controllers/tenants.controller.ts
Normal file
60
apps/api/src/controllers/tenants.controller.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import * as tenantsService from '../services/tenants.service.js';
|
||||
import { AppError } from '../utils/errors.js';
|
||||
|
||||
export async function getAllTenants(req: Request, res: Response, next: NextFunction) {
|
||||
try {
|
||||
// Only admin can list all tenants
|
||||
if (req.user!.role !== 'admin') {
|
||||
throw new AppError(403, 'Solo administradores pueden ver todos los clientes');
|
||||
}
|
||||
|
||||
const tenants = await tenantsService.getAllTenants();
|
||||
res.json(tenants);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTenant(req: Request, res: Response, next: NextFunction) {
|
||||
try {
|
||||
if (req.user!.role !== 'admin') {
|
||||
throw new AppError(403, 'Solo administradores pueden ver detalles de clientes');
|
||||
}
|
||||
|
||||
const tenant = await tenantsService.getTenantById(req.params.id);
|
||||
if (!tenant) {
|
||||
throw new AppError(404, 'Cliente no encontrado');
|
||||
}
|
||||
|
||||
res.json(tenant);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function createTenant(req: Request, res: Response, next: NextFunction) {
|
||||
try {
|
||||
if (req.user!.role !== 'admin') {
|
||||
throw new AppError(403, 'Solo administradores pueden crear clientes');
|
||||
}
|
||||
|
||||
const { nombre, rfc, plan, cfdiLimit, usersLimit } = req.body;
|
||||
|
||||
if (!nombre || !rfc) {
|
||||
throw new AppError(400, 'Nombre y RFC son requeridos');
|
||||
}
|
||||
|
||||
const tenant = await tenantsService.createTenant({
|
||||
nombre,
|
||||
rfc,
|
||||
plan,
|
||||
cfdiLimit,
|
||||
usersLimit,
|
||||
});
|
||||
|
||||
res.status(201).json(tenant);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user