From b5c410b9cfc5faa4084b3bdb3d065bd9dc3a2432 Mon Sep 17 00:00:00 2001 From: Eduardo Lopes <155753879+eduardolopesx03@users.noreply.github.com> Date: Mon, 2 Feb 2026 09:28:53 -0300 Subject: [PATCH] Fix notification export data and selection support --- Controllers/NotificationsController.cs | 134 ++++++++++++++----------- 1 file changed, 78 insertions(+), 56 deletions(-) diff --git a/Controllers/NotificationsController.cs b/Controllers/NotificationsController.cs index baa4f1c..ccdff7d 100644 --- a/Controllers/NotificationsController.cs +++ b/Controllers/NotificationsController.cs @@ -72,10 +72,12 @@ public class NotificationsController : ControllerBase [HttpPatch("read-all")] [HttpPatch("/notifications/read-all")] - public async Task MarkAllAsRead([FromQuery] string? filter) + public async Task MarkAllAsRead( + [FromQuery] string? filter, + [FromBody] NotificationSelectionRequest? request) { var utcNow = DateTime.UtcNow; - var query = ApplyFilter(_db.Notifications, filter) + var query = ApplySelectionAndFilter(_db.Notifications, filter, request?.NotificationIds) .Where(n => !n.Lida); await query.ExecuteUpdateAsync(updates => updates @@ -89,41 +91,53 @@ public class NotificationsController : ControllerBase [HttpGet("/notifications/export")] public async Task ExportNotifications([FromQuery] string? filter) { - var query = ApplyFilter(_db.Notifications.AsNoTracking(), filter); + var query = ApplySelectionAndFilter(_db.Notifications.AsNoTracking(), filter, null); + return await ExportNotificationsAsync(query, filter); + } + [HttpPost("export")] + [HttpPost("/notifications/export")] + public async Task ExportNotifications( + [FromQuery] string? filter, + [FromBody] NotificationSelectionRequest? request) + { + var query = ApplySelectionAndFilter(_db.Notifications.AsNoTracking(), filter, request?.NotificationIds); + return await ExportNotificationsAsync(query, filter); + } + + private async Task ExportNotificationsAsync(IQueryable query, string? filter) + { var rows = await ( - from notification in query - join vigencia in _db.VigenciaLines.AsNoTracking() - on notification.VigenciaLineId equals vigencia.Id into vigencias - from vigencia in vigencias.DefaultIfEmpty() - orderby notification.ReferenciaData descending, notification.Data descending - select new NotificationExportRow( - vigencia == null ? null : vigencia.Item, - vigencia == null ? null : vigencia.Conta, - vigencia == null ? null : vigencia.Linha, - vigencia == null ? null : vigencia.Cliente, - vigencia == null ? null : vigencia.Usuario, - vigencia == null ? null : vigencia.PlanoContrato, - vigencia == null ? null : vigencia.DtEfetivacaoServico, - vigencia == null ? null : vigencia.DtTerminoFidelizacao, - vigencia == null ? null : vigencia.Total, - notification.Tipo)) + from notification in query + join vigencia in _db.VigenciaLines.AsNoTracking() + on notification.VigenciaLineId equals vigencia.Id into vigencias + from vigencia in vigencias.DefaultIfEmpty() + orderby notification.ReferenciaData descending, notification.Data descending + select new NotificationExportRow( + notification.Linha ?? vigencia.Linha, + notification.Cliente ?? vigencia.Cliente, + notification.Usuario ?? vigencia.Usuario, + notification.ReferenciaData ?? vigencia.DtTerminoFidelizacao, + notification.Tipo)) .ToListAsync(); using var workbook = new XLWorkbook(); var worksheet = workbook.Worksheets.Add("Notificacoes"); + var normalizedFilter = NormalizeFilter(filter); + var dateHeader = normalizedFilter switch + { + "vencidas" or "vencido" => "Data da Expiração", + "a-vencer" or "avencer" => "Data a Vencer", + _ => "Data de Referência" + }; + var headers = new[] { - "Item (ID)", - "Conta", - "Linha", + "Número da Linha", "Cliente", "Usuário", - "Plano Contrato", - "DT. DE EFETIVAÇÃO DO SERVIÇO", - "DT. DE TÉRMINO DA FIDELIZAÇÃO", - "TOTAL", + dateHeader, "Status" }; @@ -141,32 +155,20 @@ public class NotificationsController : ControllerBase { var row = rows[i]; var rowIndex = i + 2; - worksheet.Cell(rowIndex, 1).Value = row.Item; - worksheet.Cell(rowIndex, 2).Value = row.Conta ?? string.Empty; - worksheet.Cell(rowIndex, 3).Value = row.Linha ?? string.Empty; - worksheet.Cell(rowIndex, 4).Value = row.Cliente ?? string.Empty; - worksheet.Cell(rowIndex, 5).Value = row.Usuario ?? string.Empty; - worksheet.Cell(rowIndex, 6).Value = row.PlanoContrato ?? string.Empty; - worksheet.Cell(rowIndex, 7).Value = row.DtEfetivacaoServico; - worksheet.Cell(rowIndex, 8).Value = row.DtTerminoFidelizacao; - worksheet.Cell(rowIndex, 9).Value = row.Total; - worksheet.Cell(rowIndex, 10).Value = row.Tipo; + worksheet.Cell(rowIndex, 1).Value = row.Linha ?? string.Empty; + worksheet.Cell(rowIndex, 2).Value = row.Cliente ?? string.Empty; + worksheet.Cell(rowIndex, 3).Value = row.Usuario ?? string.Empty; + worksheet.Cell(rowIndex, 4).Value = row.DataReferencia; + worksheet.Cell(rowIndex, 5).Value = row.Tipo; } - worksheet.Column(1).Width = 12; - worksheet.Column(2).Width = 18; - worksheet.Column(3).Width = 18; - worksheet.Column(4).Width = 22; - worksheet.Column(5).Width = 22; - worksheet.Column(6).Width = 20; - worksheet.Column(7).Width = 22; - worksheet.Column(8).Width = 24; - worksheet.Column(9).Width = 14; - worksheet.Column(10).Width = 14; + worksheet.Column(1).Width = 18; + worksheet.Column(2).Width = 26; + worksheet.Column(3).Width = 24; + worksheet.Column(4).Width = 20; + worksheet.Column(5).Width = 14; - worksheet.Column(7).Style.DateFormat.Format = "dd/MM/yyyy"; - worksheet.Column(8).Style.DateFormat.Format = "dd/MM/yyyy"; - worksheet.Column(9).Style.NumberFormat.Format = "#,##0.00"; + worksheet.Column(4).Style.DateFormat.Format = "dd/MM/yyyy"; worksheet.Columns().AdjustToContents(); @@ -181,9 +183,24 @@ public class NotificationsController : ControllerBase fileName); } + private static IQueryable ApplySelectionAndFilter( + IQueryable query, + string? filter, + IReadOnlyCollection? notificationIds) + { + query = ApplyFilter(query, filter); + + if (notificationIds is { Count: > 0 }) + { + query = query.Where(n => notificationIds.Contains(n.Id)); + } + + return query; + } + private static IQueryable ApplyFilter(IQueryable query, string? filter) { - var normalized = filter?.Trim().ToLowerInvariant(); + var normalized = NormalizeFilter(filter); return normalized switch { "a-vencer" or "avencer" => query.Where(n => n.Tipo == "AVencer"), @@ -192,16 +209,21 @@ public class NotificationsController : ControllerBase }; } + private static string? NormalizeFilter(string? filter) + { + return filter?.Trim().ToLowerInvariant(); + } + private sealed record NotificationExportRow( - int? Item, - string? Conta, string? Linha, string? Cliente, string? Usuario, - string? PlanoContrato, - DateTime? DtEfetivacaoServico, - DateTime? DtTerminoFidelizacao, - decimal? Total, + DateTime? DataReferencia, string Tipo); + public sealed class NotificationSelectionRequest + { + public List? NotificationIds { get; set; } + } + }