diff --git a/EnvelopeGenerator.Application/Contracts/IMessagingService.cs b/EnvelopeGenerator.Application/Contracts/IMessagingService.cs index 09048206..42ef85fd 100644 --- a/EnvelopeGenerator.Application/Contracts/IMessagingService.cs +++ b/EnvelopeGenerator.Application/Contracts/IMessagingService.cs @@ -1,9 +1,9 @@ -namespace EnvelopeGenerator.Application.Contracts +using EnvelopeGenerator.Application.DTOs.Messaging; + +namespace EnvelopeGenerator.Application.Contracts { public interface IMessagingService { - public Task SendSmsAsync(string recipient, string message); - - public Task SendSmsAsync(string recipient, string message); + public Task SendSmsAsync(string recipient, string message); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs b/EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs new file mode 100644 index 00000000..57528863 --- /dev/null +++ b/EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs @@ -0,0 +1,17 @@ +namespace EnvelopeGenerator.Application.DTOs.Messaging +{ + public record SmsResponse + { + public required bool Ok { get; init; } + + public DateTime? AllowedAt { get; set; } + + public TimeSpan AllowedAfter => Allowed ? TimeSpan.Zero : AllowedAt!.Value - DateTime.Now; + + public bool Allowed => AllowedAt is null || DateTime.Now >= AllowedAt; + + public bool Error => !Ok && Allowed; + + public dynamic? Errors { get; init; } + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/MappingExtensions,.cs b/EnvelopeGenerator.Application/MappingExtensions,.cs new file mode 100644 index 00000000..e9d6c06f --- /dev/null +++ b/EnvelopeGenerator.Application/MappingExtensions,.cs @@ -0,0 +1,11 @@ +using EnvelopeGenerator.Domain.HttpResponse; + +namespace EnvelopeGenerator.Application +{ + public static class MappingExtensions + { + public static bool Ok(this GtxMessagingResponse gtxMessagingResponse) + => gtxMessagingResponse.TryGetValue("message-status", out var status) + && status?.ToString()?.ToLower() == "ok"; + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/MappingProfiles/BasicDtoMappingProfile.cs b/EnvelopeGenerator.Application/MappingProfiles/BasicDtoMappingProfile.cs index 18462893..aad668cb 100644 --- a/EnvelopeGenerator.Application/MappingProfiles/BasicDtoMappingProfile.cs +++ b/EnvelopeGenerator.Application/MappingProfiles/BasicDtoMappingProfile.cs @@ -3,8 +3,10 @@ using EnvelopeGenerator.Application.DTOs; using EnvelopeGenerator.Application.DTOs.EnvelopeHistory; using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver; using EnvelopeGenerator.Application.DTOs.EnvelopeReceiverReadOnly; +using EnvelopeGenerator.Application.DTOs.Messaging; using EnvelopeGenerator.Application.DTOs.Receiver; using EnvelopeGenerator.Domain.Entities; +using EnvelopeGenerator.Domain.HttpResponse; namespace EnvelopeGenerator.Application.MappingProfiles { @@ -50,6 +52,13 @@ namespace EnvelopeGenerator.Application.MappingProfiles CreateMap(); CreateMap(); CreateMap(); + + // Messaging mappings + // for GTX messaging + CreateMap() + .ConstructUsing(gtxRes => gtxRes.Ok() + ? new SmsResponse() { Ok = true } + : new SmsResponse() { Ok = false, Errors = gtxRes }); } } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Services/GTXMessagingService.cs b/EnvelopeGenerator.Application/Services/GTXMessagingService.cs index 407d5908..21cbb003 100644 --- a/EnvelopeGenerator.Application/Services/GTXMessagingService.cs +++ b/EnvelopeGenerator.Application/Services/GTXMessagingService.cs @@ -1,7 +1,10 @@ -using DigitalData.Core.Abstractions.Client; +using AutoMapper; +using DigitalData.Core.Abstractions.Client; using DigitalData.Core.Client; using EnvelopeGenerator.Application.Configurations.GtxMessaging; using EnvelopeGenerator.Application.Contracts; +using EnvelopeGenerator.Application.DTOs.Messaging; +using EnvelopeGenerator.Domain.HttpResponse; using Microsoft.Extensions.Options; namespace EnvelopeGenerator.Application.Services @@ -11,21 +14,25 @@ namespace EnvelopeGenerator.Application.Services private readonly IHttpClientService _smsClient; private readonly SmsParams _smsParams; - - public GtxMessagingService(IHttpClientService smsClient, IOptions smsParamsOptions) + + private readonly IMapper _mapper; + + public GtxMessagingService(IHttpClientService smsClient, IOptions smsParamsOptions, IMapper mapper) { _smsClient = smsClient; _smsParams = smsParamsOptions.Value; + _mapper = mapper; } - public Task SendSmsAsync(string recipient, string message) => SendSmsAsync(recipient: recipient, message: message); - - public async Task SendSmsAsync(string recipient, string message) + public async Task SendSmsAsync(string recipient, string message) { - return await _smsClient.FetchAsync(queryParams: new Dictionary() { + return await _smsClient.FetchAsync(queryParams: new Dictionary() + { { _smsParams.RecipientQueryParamName, recipient }, { _smsParams.MessageQueryParamName, message } - }).ThenAsync(res => res.Json()); + }) + .ThenAsync(res => res.Json()) + .ThenAsync(_mapper.Map); } } } \ No newline at end of file diff --git a/EnvelopeGenerator.Domain/HttpResponse/GtxMessagingResponse.cs b/EnvelopeGenerator.Domain/HttpResponse/GtxMessagingResponse.cs new file mode 100644 index 00000000..cc948e52 --- /dev/null +++ b/EnvelopeGenerator.Domain/HttpResponse/GtxMessagingResponse.cs @@ -0,0 +1,4 @@ +namespace EnvelopeGenerator.Domain.HttpResponse +{ + public class GtxMessagingResponse : Dictionary { } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Web/Controllers/Test/TestMessagingController.cs b/EnvelopeGenerator.Web/Controllers/Test/TestMessagingController.cs index d7a6ecdd..b0d6ee7a 100644 --- a/EnvelopeGenerator.Web/Controllers/Test/TestMessagingController.cs +++ b/EnvelopeGenerator.Web/Controllers/Test/TestMessagingController.cs @@ -15,7 +15,7 @@ namespace EnvelopeGenerator.Web.Controllers.Test } [HttpPost] - public async Task SendAsync(string recipient, string message) + public async Task SendAsync(string recipient, string message, bool staticResponse = true) { var res = await _service.SendSmsAsync(recipient: recipient, message: message); return res is null? StatusCode(StatusCodes.Status500InternalServerError) : Ok(res);