From 941b98b1a4e400e581f725c7be97aa7f6a5116eb Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Wed, 27 Nov 2024 15:13:41 +0100 Subject: [PATCH] =?UTF-8?q?feat(SmsResponse):=20Erstellung=20eines=20Stand?= =?UTF-8?q?ardantwort-DTOs=20f=C3=BCr=20SMS-Anfragen.=20=20-=20GtxMessagin?= =?UTF-8?q?gResponse=20f=C3=BCr=20rohe=20dynamische=20Antwort=20erstellt.?= =?UTF-8?q?=20=20-=20Mapping-Profil=20hinzuf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Contracts/IMessagingService.cs | 8 +++---- .../DTOs/Messaging/SmsResponse.cs | 17 ++++++++++++++ .../MappingExtensions,.cs | 11 +++++++++ .../MappingProfiles/BasicDtoMappingProfile.cs | 9 ++++++++ .../Services/GTXMessagingService.cs | 23 ++++++++++++------- .../HttpResponse/GtxMessagingResponse.cs | 4 ++++ .../Test/TestMessagingController.cs | 2 +- 7 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs create mode 100644 EnvelopeGenerator.Application/MappingExtensions,.cs create mode 100644 EnvelopeGenerator.Domain/HttpResponse/GtxMessagingResponse.cs 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);