From 76364a278c8013bfd7e7306a1a1cc773b72e8dc3 Mon Sep 17 00:00:00 2001 From: Javi111003 <128992433+Javi111003@users.noreply.github.com> Date: Sun, 2 Feb 2025 02:13:56 -0500 Subject: [PATCH] feat_api(be):Added Exporter2Pdf and first query exporting warnings list by company --- .../ExportWarningsByCompanyCommandHandler.cs | 39 +++++++++++++++++-- .../Concrete/TemplateService.cs | 2 +- .../ElectroManage.Infraestructure.csproj | 1 + .../Plugins/Exporters/PdfExporter.cs | 15 ++++++- .../ElectroManage.WebAPI/ServiceExtensions.cs | 4 +- 5 files changed, 54 insertions(+), 7 deletions(-) diff --git a/backend/ElectroManage.Application/Features/Warning/Query/Export/List/ExportWarningsByCompanyCommandHandler.cs b/backend/ElectroManage.Application/Features/Warning/Query/Export/List/ExportWarningsByCompanyCommandHandler.cs index f1672e9e..b9b88d9b 100644 --- a/backend/ElectroManage.Application/Features/Warning/Query/Export/List/ExportWarningsByCompanyCommandHandler.cs +++ b/backend/ElectroManage.Application/Features/Warning/Query/Export/List/ExportWarningsByCompanyCommandHandler.cs @@ -1,25 +1,58 @@ using ElectroManage.Domain.DataAccess.Abstractions; +using ElectroManage.Infraestructure.Plugins; using ElectroManage.Infraestructure.Services; using Microsoft.Extensions.Logging; +using System.Linq.Expressions; namespace ElectroManage.Application.Features.Warning.Query.Export.List; public class ExportWarningsByCompanyCommandHandler : CoreQueryHandler { readonly ILogger _logger; readonly ITemplateService _templateService; + readonly PluginLoader _pluginLoader; public ExportWarningsByCompanyCommandHandler( ILogger logger, ITemplateService templateService, - IExporter exporter, + PluginLoader pluginLoader, IUnitOfWork unitOfWork) : base(unitOfWork) { + _pluginLoader = pluginLoader; _logger = logger; _templateService = templateService; } - public override Task ExecuteAsync(ExportWarningsByCompanyCommand command, CancellationToken ct = default) + public override async Task ExecuteAsync(ExportWarningsByCompanyCommand command, CancellationToken ct = default) { _logger.LogInformation($"{nameof(ExecuteAsync)} | Execution started"); + var userRepo = UnitOfWork!.DbRepository(); + var userInclude = new List>> + { + x => x.Company + }; + var companyRepo = UnitOfWork!.DbRepository(); + var companyInclude = new List>> + { + x => x.Warnings + }; + var user = await userRepo.FirstAsync(filters: u => u.Id == command.UserId, includes: userInclude); + if(user == null) + { + _logger.LogError($"{nameof(ExecuteAsync)} | User with id : {command.UserId} not found"); + ThrowError($"User with id : {command.UserId} not found", 404); + } + var company = await companyRepo.FirstAsync(filters: c => c.Id == command.CompanyId, includes: companyInclude); + if(company == null) + { + _logger.LogError($"{nameof(ExecuteAsync)} | Company with id : {command.CompanyId} not found"); + ThrowError($" Company with id : {command.CompanyId} not found", 404); + } + var html = _templateService.GetAlertsTemplate(user, company); + if(!_pluginLoader.TryGetExporter(command.Format, out var exporter)) + { + _logger.LogError($"{nameof(ExecuteAsync)} | Exporter for format: {command.Format} is not implemented"); + ThrowError($" Exporter for format: {command.Format} is not implemented", 404); + } + var pdf = exporter!.Export(html); _logger.LogInformation($"{nameof(ExecuteAsync)} | Execution completed"); - throw new NotImplementedException(); + return pdf; } } \ No newline at end of file diff --git a/backend/ElectroManage.Infraestructure/Concrete/TemplateService.cs b/backend/ElectroManage.Infraestructure/Concrete/TemplateService.cs index 0e6ab437..e8dda77e 100644 --- a/backend/ElectroManage.Infraestructure/Concrete/TemplateService.cs +++ b/backend/ElectroManage.Infraestructure/Concrete/TemplateService.cs @@ -69,7 +69,7 @@ public string GetAlertsTemplate(User user, Domain.Entites.Sucursal.Company compa private string GetHeaderTemplate(User user) { var html = File.ReadAllText(HeaderTemplateUrl); - html = html.Replace("{{user_name}}", user.UserName) + html = html.Replace("{{username}}", user.UserName) .Replace("{{email}}", user.Email) .Replace("{{companyName}}", user.Company.Name) .Replace("{{logo}}", LogoUrl); diff --git a/backend/ElectroManage.Infraestructure/ElectroManage.Infraestructure.csproj b/backend/ElectroManage.Infraestructure/ElectroManage.Infraestructure.csproj index 19f4018f..bd5391bf 100644 --- a/backend/ElectroManage.Infraestructure/ElectroManage.Infraestructure.csproj +++ b/backend/ElectroManage.Infraestructure/ElectroManage.Infraestructure.csproj @@ -10,6 +10,7 @@ + diff --git a/backend/ElectroManage.Infraestructure/Plugins/Exporters/PdfExporter.cs b/backend/ElectroManage.Infraestructure/Plugins/Exporters/PdfExporter.cs index 4749bdd3..e5709213 100644 --- a/backend/ElectroManage.Infraestructure/Plugins/Exporters/PdfExporter.cs +++ b/backend/ElectroManage.Infraestructure/Plugins/Exporters/PdfExporter.cs @@ -1,4 +1,5 @@ using ElectroManage.Infraestructure.Services; +using SelectPdf; namespace ElectroManage.Infraestructure.Plugins.Exporters; public class PdfExporter : IExporter @@ -6,6 +7,18 @@ public class PdfExporter : IExporter public string Format => "pdf"; public byte[] Export(string data) { - throw new NotImplementedException(); + // Crear una instancia del convertidor HTML a PDF + HtmlToPdf converter = new HtmlToPdf(); + + // Convertir el HTML a un documento PDF + PdfDocument doc = converter.ConvertHtmlString(data); + + // Guardar el documento PDF en un array de bytes + byte[] pdf = doc.Save(); + + // Cerrar el documento para liberar recursos + doc.Close(); + + return pdf; } } \ No newline at end of file diff --git a/backend/ElectroManage.WebAPI/ServiceExtensions.cs b/backend/ElectroManage.WebAPI/ServiceExtensions.cs index 3a692ab4..2ad7fc80 100644 --- a/backend/ElectroManage.WebAPI/ServiceExtensions.cs +++ b/backend/ElectroManage.WebAPI/ServiceExtensions.cs @@ -35,9 +35,9 @@ public static void AddCustomServicesExtension(this IServiceCollection services) services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); + //services.AddScoped(); services.AddScoped(); - //services.AddSingleton(); + services.AddSingleton(); services.AddSingleton, IdentityEmailSender>(); } public static void AddGenericRepositoryExtension(this IServiceCollection services)