From 2f85a40fdb9a01f446a9d9e0a09ee97473be4261 Mon Sep 17 00:00:00 2001 From: Eduardo Lopes <155753879+eduardolopesx03@users.noreply.github.com> Date: Mon, 2 Feb 2026 17:10:33 -0300 Subject: [PATCH 1/2] Add geral dashboard insights endpoint --- Controllers/DashboardGeralController.cs | 25 + Dtos/GeralDashboardInsightsDto.cs | 93 ++ Program.cs | 1 + Services/GeralDashboardInsightsService.cs | 917 ++++++++++++++++++ .../GeralDashboardInsightsServiceTests.cs | 116 +++ .../line-gestao-api.Tests.csproj | 24 + 6 files changed, 1176 insertions(+) create mode 100644 Controllers/DashboardGeralController.cs create mode 100644 Dtos/GeralDashboardInsightsDto.cs create mode 100644 Services/GeralDashboardInsightsService.cs create mode 100644 line-gestao-api.Tests/GeralDashboardInsightsServiceTests.cs create mode 100644 line-gestao-api.Tests/line-gestao-api.Tests.csproj diff --git a/Controllers/DashboardGeralController.cs b/Controllers/DashboardGeralController.cs new file mode 100644 index 0000000..6c70287 --- /dev/null +++ b/Controllers/DashboardGeralController.cs @@ -0,0 +1,25 @@ +using line_gestao_api.Dtos; +using line_gestao_api.Services; +using Microsoft.AspNetCore.Mvc; + +namespace line_gestao_api.Controllers +{ + [ApiController] + [Route("api/dashboard/geral")] + public class DashboardGeralController : ControllerBase + { + private readonly GeralDashboardInsightsService _service; + + public DashboardGeralController(GeralDashboardInsightsService service) + { + _service = service; + } + + [HttpGet("insights")] + public async Task> GetInsights() + { + var dto = await _service.GetInsightsAsync(); + return Ok(dto); + } + } +} diff --git a/Dtos/GeralDashboardInsightsDto.cs b/Dtos/GeralDashboardInsightsDto.cs new file mode 100644 index 0000000..f700225 --- /dev/null +++ b/Dtos/GeralDashboardInsightsDto.cs @@ -0,0 +1,93 @@ +using System.Collections.Generic; + +namespace line_gestao_api.Dtos +{ + public class GeralDashboardInsightsDto + { + public GeralDashboardKpisDto Kpis { get; set; } = new(); + public GeralDashboardChartsDto Charts { get; set; } = new(); + public List ClientGroups { get; set; } = new(); + } + + public class GeralDashboardKpisDto + { + public int TotalLinhas { get; set; } + public int TotalAtivas { get; set; } + public GeralDashboardVivoKpiDto Vivo { get; set; } = new(); + public GeralDashboardTravelKpiDto TravelMundo { get; set; } = new(); + public GeralDashboardAdditionalKpiDto Adicionais { get; set; } = new(); + } + + public class GeralDashboardVivoKpiDto + { + public int QtdLinhas { get; set; } + public decimal TotalBaseMensal { get; set; } + public decimal TotalAdicionaisMensal { get; set; } + public decimal TotalGeralMensal { get; set; } + public decimal MediaPorLinha { get; set; } + public decimal? MinPorLinha { get; set; } + public decimal? MaxPorLinha { get; set; } + } + + public class GeralDashboardTravelKpiDto + { + public int ComTravel { get; set; } + public int SemTravel { get; set; } + public decimal TotalValue { get; set; } + } + + public class GeralDashboardAdditionalKpiDto + { + public int TotalLinesWithAnyPaidAdditional { get; set; } + public int TotalLinesWithNoPaidAdditional { get; set; } + public List ServicesPaid { get; set; } = new(); + public List ServicesNotPaid { get; set; } = new(); + } + + public class GeralDashboardServiceKpiDto + { + public string ServiceName { get; set; } = string.Empty; + public int CountLines { get; set; } + public decimal TotalValue { get; set; } + } + + public class GeralDashboardChartsDto + { + public GeralDashboardChartDto LinhasPorFranquia { get; set; } = new(); + public GeralDashboardChartDto AdicionaisPagosPorServico { get; set; } = new(); + public GeralDashboardChartDto TravelMundo { get; set; } = new(); + } + + public class GeralDashboardChartDto + { + public List Labels { get; set; } = new(); + public List Values { get; set; } = new(); + public List? Totals { get; set; } + } + + public class GeralDashboardClientGroupDto + { + public string Cliente { get; set; } = string.Empty; + public int TotalLinhas { get; set; } + public int Ativos { get; set; } + public int Bloqueados { get; set; } + public int LinhasVivo { get; set; } + public List TagsBase { get; set; } = new(); + public List TagsExtras { get; set; } = new(); + public List AdicionaisPorServico { get; set; } = new(); + } + + public class GeralDashboardClientServiceDto + { + public string ServiceName { get; set; } = string.Empty; + public int PaidCount { get; set; } + public int NotPaidCount { get; set; } + public decimal TotalValue { get; set; } + } + + public class GeralDashboardTagDto + { + public string Label { get; set; } = string.Empty; + public string Value { get; set; } = string.Empty; + } +} diff --git a/Program.cs b/Program.cs index 58b50b5..31f68dd 100644 --- a/Program.cs +++ b/Program.cs @@ -36,6 +36,7 @@ builder.Services.AddDbContext(options => builder.Services.AddHttpContextAccessor(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddIdentityCore(options => { diff --git a/Services/GeralDashboardInsightsService.cs b/Services/GeralDashboardInsightsService.cs new file mode 100644 index 0000000..5ccfbe7 --- /dev/null +++ b/Services/GeralDashboardInsightsService.cs @@ -0,0 +1,917 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using line_gestao_api.Data; +using line_gestao_api.Dtos; +using Microsoft.EntityFrameworkCore; + +namespace line_gestao_api.Services +{ + public class GeralDashboardInsightsService + { + private static readonly CultureInfo PtBr = new("pt-BR"); + + private const string ServiceGestaoVozDados = "GESTÃO VOZ E DADOS"; + private const string ServiceSkeelo = "SKEELO"; + private const string ServiceVivoNewsPlus = "VIVO NEWS PLUS"; + private const string ServiceVivoTravelMundo = "VIVO TRAVEL MUNDO"; + private const string ServiceVivoGestaoDispositivo = "VIVO GESTÃO DISPOSITIVO"; + + private readonly AppDbContext _db; + + public GeralDashboardInsightsService(AppDbContext db) + { + _db = db; + } + + public async Task GetInsightsAsync() + { + var qLines = _db.MobileLines.AsNoTracking(); + + var totals = await qLines + .GroupBy(_ => 1) + .Select(g => new TotalsProjection + { + TotalLinhas = g.Count(), + TotalAtivas = g.Count(x => (x.Status ?? "").ToLower().Contains("ativo")), + TotalBloqueados = g.Count(x => + (x.Status ?? "").ToLower().Contains("bloque") || + (x.Status ?? "").ToLower().Contains("perda") || + (x.Status ?? "").ToLower().Contains("roubo")), + VivoLinhas = g.Count(x => + x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null), + VivoBaseTotal = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.ValorPlanoVivo ?? 0m) + : 0m), + VivoAdicionaisTotal = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.GestaoVozDados ?? 0m) + (x.Skeelo ?? 0m) + (x.VivoNewsPlus ?? 0m) + + (x.VivoTravelMundo ?? 0m) + (x.VivoGestaoDispositivo ?? 0m) + : 0m), + VivoMinBase = g.Where(x => + x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + .Select(x => x.ValorPlanoVivo ?? 0m) + .DefaultIfEmpty() + .Min(), + VivoMaxBase = g.Where(x => + x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + .Select(x => x.ValorPlanoVivo ?? 0m) + .DefaultIfEmpty() + .Max(), + TravelCom = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + x.VivoTravelMundo != null), + TravelSem = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + x.VivoTravelMundo == null), + TravelTotal = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.VivoTravelMundo ?? 0m) + : 0m), + PaidGestaoVozDados = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.GestaoVozDados ?? 0m) > 0m), + PaidSkeelo = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.Skeelo ?? 0m) > 0m), + PaidNews = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoNewsPlus ?? 0m) > 0m), + PaidTravel = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoTravelMundo ?? 0m) > 0m), + PaidGestaoDispositivo = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoGestaoDispositivo ?? 0m) > 0m), + NotPaidGestaoVozDados = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.GestaoVozDados ?? 0m) <= 0m), + NotPaidSkeelo = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.Skeelo ?? 0m) <= 0m), + NotPaidNews = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoNewsPlus ?? 0m) <= 0m), + NotPaidTravel = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoTravelMundo ?? 0m) <= 0m), + NotPaidGestaoDispositivo = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoGestaoDispositivo ?? 0m) <= 0m), + TotalLinesWithAnyPaidAdditional = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + ((x.GestaoVozDados ?? 0m) > 0m || + (x.Skeelo ?? 0m) > 0m || + (x.VivoNewsPlus ?? 0m) > 0m || + (x.VivoTravelMundo ?? 0m) > 0m || + (x.VivoGestaoDispositivo ?? 0m) > 0m)), + TotalLinesWithNoPaidAdditional = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.GestaoVozDados ?? 0m) <= 0m && + (x.Skeelo ?? 0m) <= 0m && + (x.VivoNewsPlus ?? 0m) <= 0m && + (x.VivoTravelMundo ?? 0m) <= 0m && + (x.VivoGestaoDispositivo ?? 0m) <= 0m), + TotalGestaoVozDados = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.GestaoVozDados ?? 0m) + : 0m), + TotalSkeelo = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.Skeelo ?? 0m) + : 0m), + TotalNews = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.VivoNewsPlus ?? 0m) + : 0m), + TotalTravel = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.VivoTravelMundo ?? 0m) + : 0m), + TotalGestaoDispositivo = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.VivoGestaoDispositivo ?? 0m) + : 0m) + }) + .FirstOrDefaultAsync(); + + var franquiasRaw = await qLines + .Select(x => new FranquiaProjection { FranquiaVivo = x.FranquiaVivo, PlanoContrato = x.PlanoContrato }) + .ToListAsync(); + + var linhasPorFranquia = BuildFranquiaBuckets(franquiasRaw); + + var clientGroupsRaw = await qLines + .Where(x => x.Cliente != null && x.Cliente != "") + .GroupBy(x => x.Cliente!) + .Select(g => new ClientGroupProjection + { + Cliente = g.Key, + TotalLinhas = g.Count(), + Ativos = g.Count(x => (x.Status ?? "").ToLower().Contains("ativo")), + Bloqueados = g.Count(x => + (x.Status ?? "").ToLower().Contains("bloque") || + (x.Status ?? "").ToLower().Contains("perda") || + (x.Status ?? "").ToLower().Contains("roubo")), + LinhasVivo = g.Count(x => + x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null), + TravelCom = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + x.VivoTravelMundo != null), + TravelSem = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + x.VivoTravelMundo == null), + PaidGestaoVozDados = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.GestaoVozDados ?? 0m) > 0m), + PaidSkeelo = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.Skeelo ?? 0m) > 0m), + PaidNews = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoNewsPlus ?? 0m) > 0m), + PaidTravel = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoTravelMundo ?? 0m) > 0m), + PaidGestaoDispositivo = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoGestaoDispositivo ?? 0m) > 0m), + NotPaidGestaoVozDados = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.GestaoVozDados ?? 0m) <= 0m), + NotPaidSkeelo = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.Skeelo ?? 0m) <= 0m), + NotPaidNews = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoNewsPlus ?? 0m) <= 0m), + NotPaidTravel = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoTravelMundo ?? 0m) <= 0m), + NotPaidGestaoDispositivo = g.Count(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) && + (x.VivoGestaoDispositivo ?? 0m) <= 0m), + TotalGestaoVozDados = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.GestaoVozDados ?? 0m) + : 0m), + TotalSkeelo = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.Skeelo ?? 0m) + : 0m), + TotalNews = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.VivoNewsPlus ?? 0m) + : 0m), + TotalTravel = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.VivoTravelMundo ?? 0m) + : 0m), + TotalGestaoDispositivo = g.Sum(x => + (x.ValorPlanoVivo != null || + x.FranquiaVivo != null || + x.ValorContratoVivo != null || + x.GestaoVozDados != null || + x.Skeelo != null || + x.VivoNewsPlus != null || + x.VivoTravelMundo != null || + x.VivoGestaoDispositivo != null) + ? (x.VivoGestaoDispositivo ?? 0m) + : 0m) + }) + .OrderBy(x => x.Cliente) + .ToListAsync(); + + var dto = new GeralDashboardInsightsDto + { + Kpis = BuildKpis(totals), + Charts = BuildCharts(totals, linhasPorFranquia), + ClientGroups = BuildClientGroups(clientGroupsRaw) + }; + + return dto; + } + + private static GeralDashboardKpisDto BuildKpis(TotalsProjection? totals) + { + if (totals == null) + { + return new GeralDashboardKpisDto + { + Vivo = new GeralDashboardVivoKpiDto(), + TravelMundo = new GeralDashboardTravelKpiDto(), + Adicionais = new GeralDashboardAdditionalKpiDto() + }; + } + + var totalGeralMensal = totals.VivoBaseTotal + totals.VivoAdicionaisTotal; + var media = totals.VivoLinhas > 0 ? totalGeralMensal / totals.VivoLinhas : 0m; + + return new GeralDashboardKpisDto + { + TotalLinhas = totals.TotalLinhas, + TotalAtivas = totals.TotalAtivas, + Vivo = new GeralDashboardVivoKpiDto + { + QtdLinhas = totals.VivoLinhas, + TotalBaseMensal = totals.VivoBaseTotal, + TotalAdicionaisMensal = totals.VivoAdicionaisTotal, + TotalGeralMensal = totalGeralMensal, + MediaPorLinha = media, + MinPorLinha = totals.VivoLinhas > 0 ? totals.VivoMinBase : null, + MaxPorLinha = totals.VivoLinhas > 0 ? totals.VivoMaxBase : null + }, + TravelMundo = new GeralDashboardTravelKpiDto + { + ComTravel = totals.TravelCom, + SemTravel = totals.TravelSem, + TotalValue = totals.TravelTotal + }, + Adicionais = new GeralDashboardAdditionalKpiDto + { + TotalLinesWithAnyPaidAdditional = totals.TotalLinesWithAnyPaidAdditional, + TotalLinesWithNoPaidAdditional = totals.TotalLinesWithNoPaidAdditional, + ServicesPaid = new List + { + new() { ServiceName = ServiceGestaoVozDados, CountLines = totals.PaidGestaoVozDados, TotalValue = totals.TotalGestaoVozDados }, + new() { ServiceName = ServiceSkeelo, CountLines = totals.PaidSkeelo, TotalValue = totals.TotalSkeelo }, + new() { ServiceName = ServiceVivoNewsPlus, CountLines = totals.PaidNews, TotalValue = totals.TotalNews }, + new() { ServiceName = ServiceVivoTravelMundo, CountLines = totals.PaidTravel, TotalValue = totals.TotalTravel }, + new() { ServiceName = ServiceVivoGestaoDispositivo, CountLines = totals.PaidGestaoDispositivo, TotalValue = totals.TotalGestaoDispositivo } + }, + ServicesNotPaid = new List + { + new() { ServiceName = ServiceGestaoVozDados, CountLines = totals.NotPaidGestaoVozDados, TotalValue = 0m }, + new() { ServiceName = ServiceSkeelo, CountLines = totals.NotPaidSkeelo, TotalValue = 0m }, + new() { ServiceName = ServiceVivoNewsPlus, CountLines = totals.NotPaidNews, TotalValue = 0m }, + new() { ServiceName = ServiceVivoTravelMundo, CountLines = totals.NotPaidTravel, TotalValue = 0m }, + new() { ServiceName = ServiceVivoGestaoDispositivo, CountLines = totals.NotPaidGestaoDispositivo, TotalValue = 0m } + } + } + }; + } + + private static GeralDashboardChartsDto BuildCharts(TotalsProjection? totals, FranquiaBuckets franquias) + { + var adicionaisLabels = new List + { + ServiceGestaoVozDados, + ServiceSkeelo, + ServiceVivoNewsPlus, + ServiceVivoTravelMundo, + ServiceVivoGestaoDispositivo + }; + + var adicionaisValues = totals == null + ? new List { 0, 0, 0, 0, 0 } + : new List + { + totals.PaidGestaoVozDados, + totals.PaidSkeelo, + totals.PaidNews, + totals.PaidTravel, + totals.PaidGestaoDispositivo + }; + + var adicionaisTotals = totals == null + ? new List { 0m, 0m, 0m, 0m, 0m } + : new List + { + totals.TotalGestaoVozDados, + totals.TotalSkeelo, + totals.TotalNews, + totals.TotalTravel, + totals.TotalGestaoDispositivo + }; + + return new GeralDashboardChartsDto + { + LinhasPorFranquia = new GeralDashboardChartDto + { + Labels = franquias.Labels, + Values = franquias.Values + }, + AdicionaisPagosPorServico = new GeralDashboardChartDto + { + Labels = adicionaisLabels, + Values = adicionaisValues, + Totals = adicionaisTotals + }, + TravelMundo = new GeralDashboardChartDto + { + Labels = new List { "Com", "Sem" }, + Values = totals == null + ? new List { 0, 0 } + : new List { totals.TravelCom, totals.TravelSem } + } + }; + } + + private static List BuildClientGroups(IEnumerable rows) + { + var list = new List(); + + foreach (var row in rows) + { + var baseTags = new List + { + new() { Label = "LINHAS", Value = row.TotalLinhas.ToString(PtBr) }, + new() { Label = "ATIVAS", Value = row.Ativos.ToString(PtBr) }, + new() { Label = "BLOQUEADAS", Value = row.Bloqueados.ToString(PtBr) }, + new() { Label = "LINHAS VIVO", Value = row.LinhasVivo.ToString(PtBr) } + }; + + var extras = new List + { + new() { Label = "TRAVEL MUNDO", Value = row.TravelCom.ToString(PtBr) }, + new() { Label = "SEM TRAVEL", Value = row.TravelSem.ToString(PtBr) }, + new() { Label = ServiceGestaoVozDados, Value = row.PaidGestaoVozDados.ToString(PtBr) }, + new() { Label = $"R$ {ServiceGestaoVozDados}", Value = FormatCurrency(row.TotalGestaoVozDados) }, + new() { Label = ServiceSkeelo, Value = row.PaidSkeelo.ToString(PtBr) }, + new() { Label = $"R$ {ServiceSkeelo}", Value = FormatCurrency(row.TotalSkeelo) }, + new() { Label = ServiceVivoNewsPlus, Value = row.PaidNews.ToString(PtBr) }, + new() { Label = $"R$ {ServiceVivoNewsPlus}", Value = FormatCurrency(row.TotalNews) }, + new() { Label = ServiceVivoTravelMundo, Value = row.PaidTravel.ToString(PtBr) }, + new() { Label = $"R$ {ServiceVivoTravelMundo}", Value = FormatCurrency(row.TotalTravel) }, + new() { Label = ServiceVivoGestaoDispositivo, Value = row.PaidGestaoDispositivo.ToString(PtBr) }, + new() { Label = $"R$ {ServiceVivoGestaoDispositivo}", Value = FormatCurrency(row.TotalGestaoDispositivo) } + }; + + var serviceDetails = new List + { + new() + { + ServiceName = ServiceGestaoVozDados, + PaidCount = row.PaidGestaoVozDados, + NotPaidCount = row.NotPaidGestaoVozDados, + TotalValue = row.TotalGestaoVozDados + }, + new() + { + ServiceName = ServiceSkeelo, + PaidCount = row.PaidSkeelo, + NotPaidCount = row.NotPaidSkeelo, + TotalValue = row.TotalSkeelo + }, + new() + { + ServiceName = ServiceVivoNewsPlus, + PaidCount = row.PaidNews, + NotPaidCount = row.NotPaidNews, + TotalValue = row.TotalNews + }, + new() + { + ServiceName = ServiceVivoTravelMundo, + PaidCount = row.PaidTravel, + NotPaidCount = row.NotPaidTravel, + TotalValue = row.TotalTravel + }, + new() + { + ServiceName = ServiceVivoGestaoDispositivo, + PaidCount = row.PaidGestaoDispositivo, + NotPaidCount = row.NotPaidGestaoDispositivo, + TotalValue = row.TotalGestaoDispositivo + } + }; + + list.Add(new GeralDashboardClientGroupDto + { + Cliente = row.Cliente, + TotalLinhas = row.TotalLinhas, + Ativos = row.Ativos, + Bloqueados = row.Bloqueados, + LinhasVivo = row.LinhasVivo, + TagsBase = baseTags, + TagsExtras = extras, + AdicionaisPorServico = serviceDetails + }); + } + + return list; + } + + private static FranquiaBuckets BuildFranquiaBuckets(IEnumerable rows) + { + var map = new Dictionary(); + + foreach (var row in rows) + { + var label = NormalizeFranquiaLabel(row.FranquiaVivo, row.PlanoContrato); + var key = label; + if (!map.TryGetValue(key, out var bucket)) + { + bucket = new FranquiaBucket { Label = label, SortKey = BuildFranquiaSortKey(label) }; + map[key] = bucket; + } + + bucket.Count++; + } + + var ordered = map.Values + .OrderBy(x => x.SortKey) + .ThenBy(x => x.Label) + .ToList(); + + return new FranquiaBuckets + { + Labels = ordered.Select(x => x.Label).ToList(), + Values = ordered.Select(x => x.Count).ToList() + }; + } + + private static string NormalizeFranquiaLabel(decimal? value, string? planoContrato) + { + if (value.HasValue && value.Value > 0) + { + return NormalizeNumericFranquia(value.Value); + } + + var parsed = ParseFranquiaFromPlano(planoContrato); + return string.IsNullOrWhiteSpace(parsed) ? "Sem Franquia" : parsed; + } + + private static string? ParseFranquiaFromPlano(string? planoContrato) + { + if (string.IsNullOrWhiteSpace(planoContrato)) return null; + + var match = Regex.Match(planoContrato, @"(?\d+(?:[.,]\d+)?)\s*(?GB|MB)", RegexOptions.IgnoreCase); + if (!match.Success) return null; + + var valueRaw = match.Groups["val"].Value.Replace(",", "."); + if (!decimal.TryParse(valueRaw, NumberStyles.Any, CultureInfo.InvariantCulture, out var value)) return null; + + var unit = match.Groups["unit"].Value.ToUpperInvariant(); + if (unit == "GB") + { + return $"{TrimDecimal(value)}GB"; + } + + return $"{Math.Round(value):0}MB"; + } + + private static string NormalizeNumericFranquia(decimal value) + { + if (value < 1m) + { + var mb = Math.Round(value * 1000m); + return $"{mb:0}MB"; + } + + if (value >= 100m && value < 1024m) + { + return $"{Math.Round(value):0}MB"; + } + + if (value >= 1024m) + { + var gb = value / 1024m; + return $"{TrimDecimal(gb)}GB"; + } + + return $"{TrimDecimal(value)}GB"; + } + + private static decimal BuildFranquiaSortKey(string label) + { + if (label.Equals("Sem Franquia", StringComparison.OrdinalIgnoreCase)) + { + return decimal.MaxValue; + } + + var match = Regex.Match(label, @"(?\d+(?:[.,]\d+)?)\s*(?GB|MB)", RegexOptions.IgnoreCase); + if (!match.Success) return decimal.MaxValue - 1; + + var valueRaw = match.Groups["val"].Value.Replace(",", "."); + if (!decimal.TryParse(valueRaw, NumberStyles.Any, CultureInfo.InvariantCulture, out var value)) + { + return decimal.MaxValue - 1; + } + + var unit = match.Groups["unit"].Value.ToUpperInvariant(); + return unit == "MB" ? value / 1000m : value; + } + + private static string TrimDecimal(decimal value) + { + return value % 1 == 0 ? value.ToString("0", CultureInfo.InvariantCulture) : value.ToString("0.#", CultureInfo.InvariantCulture); + } + + private static string FormatCurrency(decimal value) + { + return value.ToString("C", PtBr); + } + + private sealed class FranquiaBucket + { + public string Label { get; set; } = string.Empty; + public int Count { get; set; } + public decimal SortKey { get; set; } + } + + private sealed class FranquiaBuckets + { + public List Labels { get; set; } = new(); + public List Values { get; set; } = new(); + } + + private sealed class FranquiaProjection + { + public decimal? FranquiaVivo { get; set; } + public string? PlanoContrato { get; set; } + } + + private sealed class TotalsProjection + { + public int TotalLinhas { get; set; } + public int TotalAtivas { get; set; } + public int TotalBloqueados { get; set; } + public int VivoLinhas { get; set; } + public decimal VivoBaseTotal { get; set; } + public decimal VivoAdicionaisTotal { get; set; } + public decimal VivoMinBase { get; set; } + public decimal VivoMaxBase { get; set; } + public int TravelCom { get; set; } + public int TravelSem { get; set; } + public decimal TravelTotal { get; set; } + public int PaidGestaoVozDados { get; set; } + public int PaidSkeelo { get; set; } + public int PaidNews { get; set; } + public int PaidTravel { get; set; } + public int PaidGestaoDispositivo { get; set; } + public int NotPaidGestaoVozDados { get; set; } + public int NotPaidSkeelo { get; set; } + public int NotPaidNews { get; set; } + public int NotPaidTravel { get; set; } + public int NotPaidGestaoDispositivo { get; set; } + public int TotalLinesWithAnyPaidAdditional { get; set; } + public int TotalLinesWithNoPaidAdditional { get; set; } + public decimal TotalGestaoVozDados { get; set; } + public decimal TotalSkeelo { get; set; } + public decimal TotalNews { get; set; } + public decimal TotalTravel { get; set; } + public decimal TotalGestaoDispositivo { get; set; } + } + + private sealed class ClientGroupProjection + { + public string Cliente { get; set; } = string.Empty; + public int TotalLinhas { get; set; } + public int Ativos { get; set; } + public int Bloqueados { get; set; } + public int LinhasVivo { get; set; } + public int TravelCom { get; set; } + public int TravelSem { get; set; } + public int PaidGestaoVozDados { get; set; } + public int PaidSkeelo { get; set; } + public int PaidNews { get; set; } + public int PaidTravel { get; set; } + public int PaidGestaoDispositivo { get; set; } + public int NotPaidGestaoVozDados { get; set; } + public int NotPaidSkeelo { get; set; } + public int NotPaidNews { get; set; } + public int NotPaidTravel { get; set; } + public int NotPaidGestaoDispositivo { get; set; } + public decimal TotalGestaoVozDados { get; set; } + public decimal TotalSkeelo { get; set; } + public decimal TotalNews { get; set; } + public decimal TotalTravel { get; set; } + public decimal TotalGestaoDispositivo { get; set; } + } + } +} diff --git a/line-gestao-api.Tests/GeralDashboardInsightsServiceTests.cs b/line-gestao-api.Tests/GeralDashboardInsightsServiceTests.cs new file mode 100644 index 0000000..ca2b5e6 --- /dev/null +++ b/line-gestao-api.Tests/GeralDashboardInsightsServiceTests.cs @@ -0,0 +1,116 @@ +using line_gestao_api.Data; +using line_gestao_api.Models; +using line_gestao_api.Services; +using Microsoft.EntityFrameworkCore; +using Xunit; + +namespace line_gestao_api.Tests +{ + public class GeralDashboardInsightsServiceTests + { + [Fact] + public async Task GetInsightsAsync_ReturnsZerosWhenNoLines() + { + var tenantId = Guid.NewGuid(); + var provider = new TestTenantProvider(tenantId); + var db = BuildContext(provider); + + var service = new GeralDashboardInsightsService(db); + var result = await service.GetInsightsAsync(); + + Assert.NotNull(result); + Assert.Equal(0, result.Kpis.TotalLinhas); + Assert.Equal(0, result.Kpis.Vivo.QtdLinhas); + Assert.NotNull(result.Charts.LinhasPorFranquia); + Assert.NotNull(result.ClientGroups); + } + + [Fact] + public async Task GetInsightsAsync_RespectsTenantIsolation() + { + var tenantA = Guid.NewGuid(); + var tenantB = Guid.NewGuid(); + var provider = new TestTenantProvider(tenantA); + var db = BuildContext(provider); + + db.MobileLines.AddRange( + new MobileLine + { + TenantId = tenantA, + Cliente = "Cliente A", + Status = "Ativo", + ValorPlanoVivo = 100m, + GestaoVozDados = 10m + }, + new MobileLine + { + TenantId = tenantB, + Cliente = "Cliente B", + Status = "Ativo", + ValorPlanoVivo = 200m, + GestaoVozDados = 20m + }); + + await db.SaveChangesAsync(); + + var service = new GeralDashboardInsightsService(db); + var result = await service.GetInsightsAsync(); + + Assert.Equal(1, result.Kpis.TotalLinhas); + Assert.Equal(1, result.Kpis.Vivo.QtdLinhas); + Assert.Equal("Cliente A", result.ClientGroups.Single().Cliente); + } + + [Fact] + public async Task GetInsightsAsync_ReturnsNonNullCollections() + { + var tenantId = Guid.NewGuid(); + var provider = new TestTenantProvider(tenantId); + var db = BuildContext(provider); + + db.MobileLines.Add(new MobileLine + { + TenantId = tenantId, + Cliente = "Cliente X", + Status = "Ativo", + ValorPlanoVivo = 120m, + Skeelo = 5m, + VivoTravelMundo = 0m + }); + + await db.SaveChangesAsync(); + + var service = new GeralDashboardInsightsService(db); + var result = await service.GetInsightsAsync(); + + Assert.NotNull(result.Kpis.Adicionais.ServicesPaid); + Assert.NotNull(result.Kpis.Adicionais.ServicesNotPaid); + Assert.NotEmpty(result.ClientGroups); + Assert.NotEmpty(result.Charts.AdicionaisPagosPorServico.Labels); + } + + private static AppDbContext BuildContext(TestTenantProvider provider) + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(Guid.NewGuid().ToString()) + .Options; + + return new AppDbContext(options, provider); + } + + private sealed class TestTenantProvider : ITenantProvider + { + public TestTenantProvider(Guid tenantId) + { + TenantId = tenantId; + } + + public Guid? TenantId { get; private set; } + + public void SetTenantId(Guid? tenantId) + { + TenantId = tenantId; + } + } + } +} diff --git a/line-gestao-api.Tests/line-gestao-api.Tests.csproj b/line-gestao-api.Tests/line-gestao-api.Tests.csproj new file mode 100644 index 0000000..c799d69 --- /dev/null +++ b/line-gestao-api.Tests/line-gestao-api.Tests.csproj @@ -0,0 +1,24 @@ + + + + net10.0 + false + enable + enable + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + From dfce34f64d0534e63b7b0eecbf333e3dce147af3 Mon Sep 17 00:00:00 2001 From: Eduardo Lopes <155753879+eduardolopesx03@users.noreply.github.com> Date: Mon, 2 Feb 2026 17:31:25 -0300 Subject: [PATCH 2/2] Exclude test project from API build --- line-gestao-api.csproj | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/line-gestao-api.csproj b/line-gestao-api.csproj index 00b820a..2577bc3 100644 --- a/line-gestao-api.csproj +++ b/line-gestao-api.csproj @@ -16,6 +16,13 @@ + + + + + + +