diff --git a/StandManager.Domain/Entita/MailQueue.cs b/StandManager.Domain/Entita/MailQueue.cs index 0514765..464dc7d 100644 --- a/StandManager.Domain/Entita/MailQueue.cs +++ b/StandManager.Domain/Entita/MailQueue.cs @@ -16,5 +16,6 @@ public class MailQueue : EntitaBase public enum MailFrom { Invitation = 0, - Confirmation = 1 + Confirmation, + Registrazione } \ No newline at end of file diff --git a/StandManager.MailProcessor/MailProcessor.cs b/StandManager.MailProcessor/MailProcessor.cs index 20da95f..1e0834d 100644 --- a/StandManager.MailProcessor/MailProcessor.cs +++ b/StandManager.MailProcessor/MailProcessor.cs @@ -1,20 +1,18 @@ -using System.Net.Http.Headers; -using System.Net.Http.Json; -using System.Text; -using System.Text.Json; using StandManager.Domain.DTO; -using StandManager.MailProcessor.Mail; using StandManager.Service.Interfaces; +using StandManager.Service.Resolver; public class MailProcessor { private readonly IManagerService _managerService; private readonly EmailConfig _config; + private readonly IMailProcessorResolver _resolver; - public MailProcessor(IManagerService managerService, EmailConfig config) + public MailProcessor(IManagerService managerService, EmailConfig config, IMailProcessorResolver resolver) { _managerService = managerService; _config = config; + _resolver = resolver; } public async Task Process() @@ -23,54 +21,11 @@ public class MailProcessor x.Eliminato == false && x.Sent == false && string.IsNullOrEmpty(x.Error), solaLettura:false); using var httpClient = new HttpClient(); - foreach (var mailQueue in list) + foreach (var mailQueueByFrom in list.GroupBy(x => x.From)) { - var email = new Email() - { - From = _config.From, - Body = string.IsNullOrEmpty(mailQueue.Args) ? mailQueue.Body : string.Format(mailQueue.Body, mailQueue.Args?.Split(_config.MailSplitChar) ?? [""]), - Cc = null, - Subject = mailQueue.Subject, - To = new(){"g.vitari@oaservice.it"} // mailQueue.ToList.Split(";").ToList() - }; - var messageJson = JsonSerializer.Serialize(email); - - // multipart - using var form = new MultipartFormDataContent(); - form.Add( - new StringContent(messageJson, Encoding.UTF8, "application/json"), - "messageJson" - ); - - //Attachments - /*var filePath = @"C:\temp\test.pdf"; - var fileBytes = await File.ReadAllBytesAsync(filePath); + var processor = _resolver.Resolve(mailQueueByFrom.Key); - var fileContent = new ByteArrayContent(fileBytes); - fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf"); - - form.Add( - fileContent, - "attachments", - Path.GetFileName(filePath) - );*/ - try - { - var response = await httpClient.PostAsync( - _config.ServerAddress, - form - ); - response.EnsureSuccessStatusCode(); - mailQueue.Sent = true; - await _managerService.MailQueueService.Salva(mailQueue); - } - catch (Exception e) - { - mailQueue.Sent = false; - mailQueue.Error = e.Message; - await _managerService.MailQueueService.Salva(mailQueue); - } - + await processor.ProcessAsync(mailQueueByFrom.ToList(), _config, httpClient); } var a = list; diff --git a/StandManager.MailProcessor/Program.cs b/StandManager.MailProcessor/Program.cs index 7d26707..deddf56 100644 --- a/StandManager.MailProcessor/Program.cs +++ b/StandManager.MailProcessor/Program.cs @@ -5,10 +5,10 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using StandManager.Domain.DTO; using StandManager.Infrastructure.DAL.Context; -using StandManager.MailProcessor.Mail; using StandManager.Service; using StandManager.Service.Interfaces; using StandManager.Service.Repository; +using StandManager.Service.Resolver; var host = Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => @@ -36,6 +36,9 @@ var host = Host.CreateDefaultBuilder(args) services.AddScoped(); services.AddScoped(); + //MailProcessor + services.AddScoped(); + services.AddScoped(); }) .Build(); diff --git a/StandManager.Service/Interfaces/IMailProcessor.cs b/StandManager.Service/Interfaces/IMailProcessor.cs new file mode 100644 index 0000000..5d49982 --- /dev/null +++ b/StandManager.Service/Interfaces/IMailProcessor.cs @@ -0,0 +1,10 @@ +using StandManager.Domain.DTO; +using StandManager.Domain.Entita; + +namespace StandManager.Service.Interfaces; + +public interface IMailProcessor +{ + MailFrom MailFrom { get; } + Task ProcessAsync(List mailQueueList, EmailConfig config, HttpClient httpClient); +} \ No newline at end of file diff --git a/StandManager.MailProcessor/Mail/Email.cs b/StandManager.Service/Mail/Email.cs similarity index 82% rename from StandManager.MailProcessor/Mail/Email.cs rename to StandManager.Service/Mail/Email.cs index a6cdb25..c55e3a9 100644 --- a/StandManager.MailProcessor/Mail/Email.cs +++ b/StandManager.Service/Mail/Email.cs @@ -1,4 +1,4 @@ -namespace StandManager.MailProcessor.Mail; +namespace StandManager.Service.Mail; public class Email { diff --git a/StandManager.Service/RegistrazioneMailProcessor.cs b/StandManager.Service/RegistrazioneMailProcessor.cs new file mode 100644 index 0000000..e7684be --- /dev/null +++ b/StandManager.Service/RegistrazioneMailProcessor.cs @@ -0,0 +1,72 @@ +using System.Text; +using System.Text.Json; +using StandManager.Domain.DTO; +using StandManager.Domain.Entita; +using StandManager.Service.Interfaces; +using StandManager.Service.Mail; + +namespace StandManager.Service; + +public class RegistrazioneMailProcessor : IMailProcessor +{ + private readonly IManagerService _managerService; + + public RegistrazioneMailProcessor(IManagerService managerService) + { + _managerService = managerService; + } + + public MailFrom MailFrom => MailFrom.Registrazione; + + public async Task ProcessAsync(List mailQueueList, EmailConfig _config, HttpClient httpClient) + { + foreach (var mailQueue in mailQueueList) + { + var email = new Email() + { + From = _config.From, + Body = string.IsNullOrEmpty(mailQueue.Args) ? mailQueue.Body : string.Format(mailQueue.Body, mailQueue.Args?.Split(_config.MailSplitChar) ?? [""]), + Cc = null, + Subject = mailQueue.Subject, + To = new(){"g.vitari@oaservice.it"} // mailQueue.ToList.Split(";").ToList() + }; + var messageJson = JsonSerializer.Serialize(email); + + // multipart + using var form = new MultipartFormDataContent(); + form.Add( + new StringContent(messageJson, Encoding.UTF8, "application/json"), + "messageJson" + ); + + //Attachments + /*var filePath = @"C:\temp\test.pdf"; + var fileBytes = await File.ReadAllBytesAsync(filePath); + + var fileContent = new ByteArrayContent(fileBytes); + fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf"); + + form.Add( + fileContent, + "attachments", + Path.GetFileName(filePath) + );*/ + try + { + var response = await httpClient.PostAsync( + _config.ServerAddress, + form + ); + response.EnsureSuccessStatusCode(); + mailQueue.Sent = true; + await _managerService.MailQueueService.Salva(mailQueue); + } + catch (Exception e) + { + mailQueue.Sent = false; + mailQueue.Error = e.Message; + await _managerService.MailQueueService.Salva(mailQueue); + } + } + } +} \ No newline at end of file diff --git a/StandManager.Service/Resolver/IMailProcessorResolver.cs b/StandManager.Service/Resolver/IMailProcessorResolver.cs new file mode 100644 index 0000000..4fc68e9 --- /dev/null +++ b/StandManager.Service/Resolver/IMailProcessorResolver.cs @@ -0,0 +1,9 @@ +using StandManager.Domain.Entita; +using StandManager.Service.Interfaces; + +namespace StandManager.Service.Resolver; + +public interface IMailProcessorResolver +{ + IMailProcessor Resolve(MailFrom mailFrom); +} \ No newline at end of file diff --git a/StandManager.Service/Resolver/MailProcessorResolver.cs b/StandManager.Service/Resolver/MailProcessorResolver.cs new file mode 100644 index 0000000..56bbc70 --- /dev/null +++ b/StandManager.Service/Resolver/MailProcessorResolver.cs @@ -0,0 +1,22 @@ +using StandManager.Domain.Entita; +using StandManager.Service.Interfaces; + +namespace StandManager.Service.Resolver; + +public class MailProcessorResolver : IMailProcessorResolver +{ + private readonly Dictionary _processors; + + public MailProcessorResolver(IEnumerable processors) + { + _processors = processors.ToDictionary(p => p.MailFrom); + } + + public IMailProcessor Resolve(MailFrom mailFrom) + { + if (!_processors.TryGetValue(mailFrom, out var processor)) + throw new NotSupportedException($"MailFrom {mailFrom} non supportato"); + + return processor; + } +} \ No newline at end of file diff --git a/StandManager/Components/Pages/Home.razor b/StandManager/Components/Pages/Home.razor index b24d17f..69cf9de 100644 --- a/StandManager/Components/Pages/Home.razor +++ b/StandManager/Components/Pages/Home.razor @@ -15,7 +15,7 @@ - + diff --git a/StandManager/Components/Pages/Registrazione.razor b/StandManager/Components/Pages/Registrazione.razor index 2417457..3719ad5 100644 --- a/StandManager/Components/Pages/Registrazione.razor +++ b/StandManager/Components/Pages/Registrazione.razor @@ -88,6 +88,14 @@ Mail = iscrizione.Mail }; await _managerService.IscrizioneEventoPerMailService.Salva(model); + + var queue = new MailQueue() + { + ToList = iscrizione.Mail, + From = MailFrom.Registrazione + }; + await _managerService.MailQueueService.Salva(queue); + _navManager.NavigateTo("/Grazie"); } } \ No newline at end of file diff --git a/StandManager/Components/Pages/RegistrazioneInFiera.razor b/StandManager/Components/Pages/RegistrazioneInFiera.razor index ef3b4a6..ee7b4e9 100644 --- a/StandManager/Components/Pages/RegistrazioneInFiera.razor +++ b/StandManager/Components/Pages/RegistrazioneInFiera.razor @@ -9,7 +9,8 @@ @rendermode InteractiveServer Iscrizione Evento - + +
@if (showCodiceCliente) diff --git a/StandManager/Program.cs b/StandManager/Program.cs index 6f5bc82..3ecce24 100644 --- a/StandManager/Program.cs +++ b/StandManager/Program.cs @@ -10,6 +10,7 @@ using StandManager.Service.Interfaces; using StandManager.Service.Repository; using StandManager.Utils; using System.Reflection; +using StandManager.Service.Resolver; var builder = WebApplication.CreateBuilder(args); @@ -50,6 +51,10 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +//MailProcessor +builder.Services.AddScoped(); +builder.Services.AddScoped(); + builder.Services.AddRadzenComponents(); builder.Services.AddHttpContextAccessor(); diff --git a/script.sql b/script.sql index 0da009c..3634a88 100644 --- a/script.sql +++ b/script.sql @@ -1,27 +1,51 @@ BEGIN TRANSACTION; -ALTER TABLE [Destinazione] ADD [ComuneIstatId] uniqueidentifier NULL; +CREATE TABLE [MailQueue] ( + [Id] uniqueidentifier NOT NULL, + [Subject] nvarchar(max) NOT NULL, + [Body] nvarchar(max) NOT NULL, + [ToList] nvarchar(max) NOT NULL, + [Args] nvarchar(max) NULL, + [From] int NOT NULL, + [Sent] bit NOT NULL, + [Error] nvarchar(max) NULL, + [DataCreazione] datetime2 NOT NULL, + [DataModifica] datetime2 NULL, + [Eliminato] bit NOT NULL, + [IdUtenteCreazione] uniqueidentifier NULL, + [IdUtenteModifica] uniqueidentifier NULL, + CONSTRAINT [PK_MailQueue] PRIMARY KEY ([Id]), + CONSTRAINT [FK_MailQueue_Utente_IdUtenteCreazione] FOREIGN KEY ([IdUtenteCreazione]) REFERENCES [Utente] ([Id]), + CONSTRAINT [FK_MailQueue_Utente_IdUtenteModifica] FOREIGN KEY ([IdUtenteModifica]) REFERENCES [Utente] ([Id]) +); -ALTER TABLE [Destinazione] ADD [ProvinciaIstatId] uniqueidentifier NULL; +CREATE INDEX [IX_MailQueue_IdUtenteCreazione] ON [MailQueue] ([IdUtenteCreazione]); -CREATE INDEX [IX_Destinazione_ComuneIstatId] ON [Destinazione] ([ComuneIstatId]); - -CREATE INDEX [IX_Destinazione_ProvinciaIstatId] ON [Destinazione] ([ProvinciaIstatId]); - -ALTER TABLE [Destinazione] ADD CONSTRAINT [FK_Destinazione_ComuneIstat_ComuneIstatId] FOREIGN KEY ([ComuneIstatId]) REFERENCES [ComuneIstat] ([Id]); - -ALTER TABLE [Destinazione] ADD CONSTRAINT [FK_Destinazione_Province_ProvinciaIstatId] FOREIGN KEY ([ProvinciaIstatId]) REFERENCES [Province] ([Id]); +CREATE INDEX [IX_MailQueue_IdUtenteModifica] ON [MailQueue] ([IdUtenteModifica]); INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) -VALUES (N'20260107093044_CittaEProv', N'9.0.11'); +VALUES (N'20260109133732_MailQueue', N'9.0.11'); -ALTER TABLE [ComuneIstat] ADD [ProvinciaIstatId] uniqueidentifier NULL; +CREATE TABLE [IscrizioneEventoPerMail] ( + [Id] uniqueidentifier NOT NULL, + [Mail] nvarchar(max) NOT NULL, + [Processata] bit NOT NULL, + [Inviata] bit NOT NULL, + [DataCreazione] datetime2 NOT NULL, + [DataModifica] datetime2 NULL, + [Eliminato] bit NOT NULL, + [IdUtenteCreazione] uniqueidentifier NULL, + [IdUtenteModifica] uniqueidentifier NULL, + CONSTRAINT [PK_IscrizioneEventoPerMail] PRIMARY KEY ([Id]), + CONSTRAINT [FK_IscrizioneEventoPerMail_Utente_IdUtenteCreazione] FOREIGN KEY ([IdUtenteCreazione]) REFERENCES [Utente] ([Id]), + CONSTRAINT [FK_IscrizioneEventoPerMail_Utente_IdUtenteModifica] FOREIGN KEY ([IdUtenteModifica]) REFERENCES [Utente] ([Id]) +); -CREATE INDEX [IX_ComuneIstat_ProvinciaIstatId] ON [ComuneIstat] ([ProvinciaIstatId]); +CREATE INDEX [IX_IscrizioneEventoPerMail_IdUtenteCreazione] ON [IscrizioneEventoPerMail] ([IdUtenteCreazione]); -ALTER TABLE [ComuneIstat] ADD CONSTRAINT [FK_ComuneIstat_Province_ProvinciaIstatId] FOREIGN KEY ([ProvinciaIstatId]) REFERENCES [Province] ([Id]); +CREATE INDEX [IX_IscrizioneEventoPerMail_IdUtenteModifica] ON [IscrizioneEventoPerMail] ([IdUtenteModifica]); INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) -VALUES (N'20260107093529_ProvSuComune', N'9.0.11'); +VALUES (N'20260120130758_IscrizioneEventoPerMail', N'9.0.11'); COMMIT; GO