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 { public class GtxMessagingService : IMessagingService { private readonly IHttpClientService _smsClient; private readonly SmsParams _smsParams; private readonly IMapper _mapper; private readonly ICodeGenerator _codeGen; public string ServiceProvider { get; } public GtxMessagingService(IHttpClientService smsClient, IOptions smsParamsOptions, IMapper mapper, ICodeGenerator codeGenerator) { _smsClient = smsClient; _smsParams = smsParamsOptions.Value; _mapper = mapper; ServiceProvider = GetType().Name.Replace("Service", string.Empty); _codeGen = codeGenerator; } public async Task SendSmsAsync(string recipient, string message) { return await _smsClient.FetchAsync(queryParams: new Dictionary() { { _smsParams.RecipientQueryParamName, recipient }, { _smsParams.MessageQueryParamName, message } }) .ThenAsync(res => res.Json()) .ThenAsync(_mapper.Map); } public async Task SendSmsCodeAsync(string recipient) { var code = _codeGen.GenerateCode(_smsParams.CodeLength); return await SendSmsAsync(recipient: recipient, message: code); } } }