using line_gestao_api.Data; using line_gestao_api.Dtos; using line_gestao_api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace line_gestao_api.Controllers; [ApiController] [Route("api/system/tenants")] [Authorize(Policy = "SystemAdmin")] public class SystemTenantsController : ControllerBase { private readonly AppDbContext _db; private readonly ISystemAuditService _systemAuditService; public SystemTenantsController(AppDbContext db, ISystemAuditService systemAuditService) { _db = db; _systemAuditService = systemAuditService; } [HttpGet] public async Task>> GetTenants( [FromQuery] string source = SystemTenantConstants.MobileLinesClienteSourceType, [FromQuery] bool active = true) { var query = _db.Tenants .AsNoTracking() .Where(t => !t.IsSystem); if (!string.IsNullOrWhiteSpace(source)) { query = query.Where(t => t.SourceType == source); } query = query.Where(t => t.Ativo == active); var tenants = await query .OrderBy(t => t.NomeOficial) .Select(t => new SystemTenantListItemDto { TenantId = t.Id, NomeOficial = t.NomeOficial }) .ToListAsync(); await _systemAuditService.LogAsync( action: SystemAuditActions.ListTenants, targetTenantId: SystemTenantConstants.SystemTenantId, metadata: new { source, active, returnedCount = tenants.Count }); return Ok(tenants); } }