feat(SmsResponse): Erstellung eines Standardantwort-DTOs für SMS-Anfragen.
- GtxMessagingResponse für rohe dynamische Antwort erstellt. - Mapping-Profil hinzufügen
This commit is contained in:
parent
168c33bfea
commit
941b98b1a4
@ -1,9 +1,9 @@
|
||||
namespace EnvelopeGenerator.Application.Contracts
|
||||
using EnvelopeGenerator.Application.DTOs.Messaging;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Contracts
|
||||
{
|
||||
public interface IMessagingService
|
||||
{
|
||||
public Task<dynamic?> SendSmsAsync(string recipient, string message);
|
||||
|
||||
public Task<TResponse?> SendSmsAsync<TResponse>(string recipient, string message);
|
||||
public Task<SmsResponse> SendSmsAsync(string recipient, string message);
|
||||
}
|
||||
}
|
||||
17
EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs
Normal file
17
EnvelopeGenerator.Application/DTOs/Messaging/SmsResponse.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
||||
11
EnvelopeGenerator.Application/MappingExtensions,.cs
Normal file
11
EnvelopeGenerator.Application/MappingExtensions,.cs
Normal file
@ -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";
|
||||
}
|
||||
}
|
||||
@ -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<EnvelopeReceiverBase, EnvelopeReceiverBasicDto>();
|
||||
CreateMap<EnvelopeReceiverReadOnlyCreateDto, EnvelopeReceiverReadOnly>();
|
||||
CreateMap<EnvelopeReceiverReadOnlyUpdateDto, EnvelopeReceiverReadOnly>();
|
||||
|
||||
// Messaging mappings
|
||||
// for GTX messaging
|
||||
CreateMap<GtxMessagingResponse, SmsResponse>()
|
||||
.ConstructUsing(gtxRes => gtxRes.Ok()
|
||||
? new SmsResponse() { Ok = true }
|
||||
: new SmsResponse() { Ok = false, Errors = gtxRes });
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<SmsParams> _smsClient;
|
||||
|
||||
private readonly SmsParams _smsParams;
|
||||
|
||||
public GtxMessagingService(IHttpClientService<SmsParams> smsClient, IOptions<SmsParams> smsParamsOptions)
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GtxMessagingService(IHttpClientService<SmsParams> smsClient, IOptions<SmsParams> smsParamsOptions, IMapper mapper)
|
||||
{
|
||||
_smsClient = smsClient;
|
||||
_smsParams = smsParamsOptions.Value;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public Task<dynamic?> SendSmsAsync(string recipient, string message) => SendSmsAsync<dynamic>(recipient: recipient, message: message);
|
||||
|
||||
public async Task<TResponse?> SendSmsAsync<TResponse>(string recipient, string message)
|
||||
public async Task<SmsResponse> SendSmsAsync(string recipient, string message)
|
||||
{
|
||||
return await _smsClient.FetchAsync(queryParams: new Dictionary<string, object?>() {
|
||||
return await _smsClient.FetchAsync(queryParams: new Dictionary<string, object?>()
|
||||
{
|
||||
{ _smsParams.RecipientQueryParamName, recipient },
|
||||
{ _smsParams.MessageQueryParamName, message }
|
||||
}).ThenAsync(res => res.Json<TResponse>());
|
||||
})
|
||||
.ThenAsync(res => res.Json<GtxMessagingResponse>())
|
||||
.ThenAsync(_mapper.Map<SmsResponse>);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
namespace EnvelopeGenerator.Domain.HttpResponse
|
||||
{
|
||||
public class GtxMessagingResponse : Dictionary<string, object?> { }
|
||||
}
|
||||
@ -15,7 +15,7 @@ namespace EnvelopeGenerator.Web.Controllers.Test
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> SendAsync(string recipient, string message)
|
||||
public async Task<IActionResult> 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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user