diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeExecutor.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeExecutor.cs index f5b639c8..25c5d29c 100644 --- a/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeExecutor.cs +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeExecutor.cs @@ -17,5 +17,5 @@ public interface IEnvelopeExecutor : ISQLExecutor /// /// /// - Task CreateEnvelopeAsync(int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default); + Task CreateEnvelopeAsync(int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default); } diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeReceiverExecutor.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeReceiverExecutor.cs index 10bcbe28..71cd8faa 100644 --- a/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeReceiverExecutor.cs +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/IEnvelopeReceiverExecutor.cs @@ -16,5 +16,5 @@ public interface IEnvelopeReceiverExecutor /// /// /// - Task AddEnvelopeReceiverAsync(string envelope_uuid, string emailAddress, string salutation, string? phone = null, CancellationToken cancellation = default); + Task AddEnvelopeReceiverAsync(string envelope_uuid, string emailAddress, string? salutation = null, string? phone = null, CancellationToken cancellation = default); } diff --git a/EnvelopeGenerator.Application/EnvelopeReceivers/Commands/Create/CreateEnvelopeReceiverCommandHandler.cs b/EnvelopeGenerator.Application/EnvelopeReceivers/Commands/Create/CreateEnvelopeReceiverCommandHandler.cs index 57d69b4e..04a8f598 100644 --- a/EnvelopeGenerator.Application/EnvelopeReceivers/Commands/Create/CreateEnvelopeReceiverCommandHandler.cs +++ b/EnvelopeGenerator.Application/EnvelopeReceivers/Commands/Create/CreateEnvelopeReceiverCommandHandler.cs @@ -1,4 +1,5 @@ using EnvelopeGenerator.Application.Contracts.SQLExecutor; +using EnvelopeGenerator.Domain.Entities; using MediatR; namespace EnvelopeGenerator.Application.EnvelopeReceivers.Commands.Create; @@ -31,10 +32,25 @@ public class CreateEnvelopeReceiverCommandHandler : IRequestHandler /// The command containing all necessary information to create an envelope. - /// Token to observe while waiting for the task to complete. + /// Token to observe while waiting for the task to complete. /// A task representing the asynchronous operation. - public Task Handle(CreateEnvelopeReceiverCommand request, CancellationToken cancellationToken) + public async Task Handle(CreateEnvelopeReceiverCommand request, CancellationToken cancel) { - throw new NotImplementedException(); + int userId = request.UserId ?? throw new InvalidOperationException("UserId cannot be null when creating an envelope."); + + var envelope = await _envelopeExecutor.CreateEnvelopeAsync(userId, request.Title, request.Message, request.TFAEnabled, cancel); + + List sentRecipients = new(); + List unsentRecipients = new(); + + foreach (var receiver in request.Receivers) + { + var envelopeReceiver = await _erExecutor.AddEnvelopeReceiverAsync(envelope.Uuid, receiver.EmailAddress, receiver.Salution, receiver.PhoneNumber, cancel); + + if (envelopeReceiver is null) + unsentRecipients.Add(receiver); + else + sentRecipients.Add(envelopeReceiver); + } } } diff --git a/EnvelopeGenerator.Application/SQL/EnvelopeReceiverAddReadSQL.cs b/EnvelopeGenerator.Application/SQL/EnvelopeReceiverAddReadSQL.cs index c94aecbc..0e3195c8 100644 --- a/EnvelopeGenerator.Application/SQL/EnvelopeReceiverAddReadSQL.cs +++ b/EnvelopeGenerator.Application/SQL/EnvelopeReceiverAddReadSQL.cs @@ -40,7 +40,7 @@ public class EnvelopeReceiverAddReadSQL : ISQL /// /// /// - public static DynamicParameters CreateParameters(string envelope_uuid, string emailAddress, string salutation, string? phone = null) + public static DynamicParameters CreateParameters(string envelope_uuid, string emailAddress, string? salutation = null, string? phone = null) { var parameters = new DynamicParameters(); parameters.Add("@ENV_UID", envelope_uuid); diff --git a/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs b/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs index b1bf792d..b44bb28a 100644 --- a/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs +++ b/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs @@ -20,18 +20,19 @@ public class EnvelopeExecutor : SQLExecutor, IEnvelopeExecutor _envelopeRepository = envelopeRepository; } - public async Task CreateEnvelopeAsync(int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default) + public async Task CreateEnvelopeAsync(int userId, string title = "", string message = "", bool tfaEnabled = false, CancellationToken cancellation = default) { using var connection = new SqlConnection(Params.ConnectionString); var sql = Provider.GetRequiredService(); await connection.OpenAsync(cancellation); var parameters = EnvelopeCreateReadSQL.CreateParmas(userId, title, message, tfaEnabled); var envelopes = await connection.QueryAsync(sql.Raw, parameters); - var envelope = envelopes.FirstOrDefault(); - - if (envelope is null) - return null; - - return await _envelopeRepository.ReadByUuidAsync(envelope.Uuid, withAll: true); + var envelope = envelopes.FirstOrDefault() + ?? throw new InvalidOperationException($"Envelope creation failed. Parameters:" + + $"userId={userId}, title='{title}', message='{message}', tfaEnabled={tfaEnabled}."); ; + + return await _envelopeRepository.ReadByUuidAsync(envelope.Uuid, withAll: true) + ?? throw new InvalidOperationException($"Envelope creation succeeded but retrieval failed. Parameters:" + + $"userId={userId}, title='{title}', message='{message}', tfaEnabled={tfaEnabled}."); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Infrastructure/Executor/EnvelopeReceiverExecutor.cs b/EnvelopeGenerator.Infrastructure/Executor/EnvelopeReceiverExecutor.cs index f62a3b96..219b0716 100644 --- a/EnvelopeGenerator.Infrastructure/Executor/EnvelopeReceiverExecutor.cs +++ b/EnvelopeGenerator.Infrastructure/Executor/EnvelopeReceiverExecutor.cs @@ -19,7 +19,7 @@ public class EnvelopeReceiverExecutor: SQLExecutor, IEnvelopeReceiverExecutor _erRepository = erRepository; } - public async Task AddEnvelopeReceiverAsync(string envelope_uuid, string emailAddress, string salutation, string? phone = null, CancellationToken cancellation = default) + public async Task AddEnvelopeReceiverAsync(string envelope_uuid, string emailAddress, string? salutation, string? phone = null, CancellationToken cancellation = default) { using var connection = new SqlConnection(Params.ConnectionString); var sql = Provider.GetRequiredService();