diff --git a/EnvelopeGenerator.Application/Contracts/IEnvelopeSmsHandler.cs b/EnvelopeGenerator.Application/Contracts/IEnvelopeSmsHandler.cs
new file mode 100644
index 00000000..6397ecef
--- /dev/null
+++ b/EnvelopeGenerator.Application/Contracts/IEnvelopeSmsHandler.cs
@@ -0,0 +1,15 @@
+using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver;
+using EnvelopeGenerator.Application.DTOs.Messaging;
+
+namespace EnvelopeGenerator.Application.Contracts;
+
+public interface IEnvelopeSmsHandler
+{
+ ///
+ /// If expiration is passed then, sends sms and returns smsResponse and up-to-date expiration; otherwise send expiration.
+ ///
+ ///
+ ///
+ ///
+ Task<(SmsResponse? SmsResponse, DateTime Expiration)> SendTotpAsync(EnvelopeReceiverSecretDto er_secret, CancellationToken cToken = default);
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Contracts/IEnvelopeSmsService.cs b/EnvelopeGenerator.Application/Contracts/IEnvelopeSmsService.cs
deleted file mode 100644
index 6efc6abf..00000000
--- a/EnvelopeGenerator.Application/Contracts/IEnvelopeSmsService.cs
+++ /dev/null
@@ -1,5 +0,0 @@
-namespace EnvelopeGenerator.Application.Contracts;
-
-public interface IEnvelopeSmsService
-{
-}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Extensions/DIExtensions.cs b/EnvelopeGenerator.Application/Extensions/DIExtensions.cs
index 7ef576c0..59c56724 100644
--- a/EnvelopeGenerator.Application/Extensions/DIExtensions.cs
+++ b/EnvelopeGenerator.Application/Extensions/DIExtensions.cs
@@ -60,7 +60,7 @@ namespace EnvelopeGenerator.Application.Extensions
services.AddHttpClientService(config.GetSection(nameof(GtxMessagingParams)));
services.TryAddSingleton();
- services.TryAddSingleton();
+ services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton();
diff --git a/EnvelopeGenerator.Application/Services/EnvelopeSmsHandler.cs b/EnvelopeGenerator.Application/Services/EnvelopeSmsHandler.cs
new file mode 100644
index 00000000..e4f71d58
--- /dev/null
+++ b/EnvelopeGenerator.Application/Services/EnvelopeSmsHandler.cs
@@ -0,0 +1,50 @@
+using EnvelopeGenerator.Application.Configurations;
+using EnvelopeGenerator.Application.Contracts;
+using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver;
+using EnvelopeGenerator.Application.DTOs.Messaging;
+using EnvelopeGenerator.Application.Extensions;
+using Microsoft.Extensions.Caching.Distributed;
+using Microsoft.Extensions.Options;
+
+namespace EnvelopeGenerator.Application.Services;
+
+public class EnvelopeSmsHandler : IEnvelopeSmsHandler
+{
+ private readonly ISmsSender _sender;
+
+ private readonly TotpSmsParams _totpSmsParams;
+
+ private readonly IDistributedCache _dCache;
+
+ private readonly ICodeGenerator _codeGenerator;
+
+ public EnvelopeSmsHandler(ISmsSender sender, IOptions totpSmsParamsOptions, IDistributedCache distributedCache, ICodeGenerator codeGenerator)
+ {
+ _sender = sender;
+ _totpSmsParams = totpSmsParamsOptions.Value;
+ _dCache = distributedCache;
+ _codeGenerator = codeGenerator;
+ }
+
+ ///
+ /// If expiration is passed then, sends sms and returns smsResponse and up-to-date expiration; otherwise send expiration.
+ ///
+ ///
+ ///
+ ///
+ public async Task<(SmsResponse? SmsResponse, DateTime Expiration)> SendTotpAsync(EnvelopeReceiverSecretDto er_secret, CancellationToken cToken = default)
+ {
+ var key = string.Format(_totpSmsParams.Expiration.CacheKeyFormat, er_secret.EnvelopeId, er_secret.ReceiverId);
+ var expiration = await _dCache.GetDateTimeAsync(key, cToken);
+
+ if(expiration is DateTime expirationDateTime && expirationDateTime < DateTime.Now)
+ return (null, expirationDateTime);
+ else
+ {
+ var new_expiration = DateTime.Now.AddSeconds(_totpSmsParams.TotpStep);
+ var totp = _codeGenerator.GenerateTotp(er_secret.Receiver!.TotpSecretkey!, _totpSmsParams.TotpStep);
+ var msg = string.Format(_totpSmsParams.Format, totp, new_expiration.ToString(_totpSmsParams.Expiration.Format, _totpSmsParams.Expiration.CultureInfo));
+ return (await _sender.SendSmsAsync(er_secret.PhoneNumber!, msg), new_expiration);
+ }
+ }
+}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Application/Services/EnvelopeSmsService.cs b/EnvelopeGenerator.Application/Services/EnvelopeSmsService.cs
deleted file mode 100644
index 075e0249..00000000
--- a/EnvelopeGenerator.Application/Services/EnvelopeSmsService.cs
+++ /dev/null
@@ -1,18 +0,0 @@
-using EnvelopeGenerator.Application.Configurations;
-using EnvelopeGenerator.Application.Contracts;
-using Microsoft.Extensions.Options;
-
-namespace EnvelopeGenerator.Application.Services;
-
-public class EnvelopeSmsService : IEnvelopeSmsService
-{
- private readonly ISmsSender _sender;
-
- private readonly TotpSmsParams _totpSmsParams;
-
- public EnvelopeSmsService(ISmsSender sender, IOptions totpSmsParamsOptions)
- {
- _sender = sender;
- _totpSmsParams = totpSmsParamsOptions.Value;
- }
-}
\ No newline at end of file
diff --git a/EnvelopeGenerator.Web/Controllers/HomeController.cs b/EnvelopeGenerator.Web/Controllers/HomeController.cs
index abf5afff..8d5db4e8 100644
--- a/EnvelopeGenerator.Web/Controllers/HomeController.cs
+++ b/EnvelopeGenerator.Web/Controllers/HomeController.cs
@@ -21,9 +21,9 @@ using EnvelopeGenerator.Application.DTOs;
using DigitalData.Core.Client;
using EnvelopeGenerator.Application.Extensions;
using Microsoft.Extensions.Caching.Distributed;
-using System.Globalization;
using Microsoft.Extensions.Options;
using EnvelopeGenerator.Application.Configurations;
+using EnvelopeGenerator.Application.DTOs.Messaging;
namespace EnvelopeGenerator.Web.Controllers
{
@@ -44,8 +44,9 @@ namespace EnvelopeGenerator.Web.Controllers
private readonly IReceiverService _rcvService;
private readonly IDistributedCache _dCache;
private readonly TotpSmsParams _totpSmsParams;
+ private readonly IEnvelopeSmsHandler _envSmsHandler;
- public HomeController(EnvelopeOldService envelopeOldService, ILogger logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeHistoryService historyService, IStringLocalizer localizer, IConfiguration configuration, HtmlSanitizer sanitizer, Cultures cultures, IEnvelopeMailService envelopeMailService, IEnvelopeReceiverReadOnlyService readOnlyService, ISmsSender messagingService, ICodeGenerator codeGenerator, IReceiverService receiverService, IDistributedCache distributedCache, IOptions totpSmsParamsOptions)
+ public HomeController(EnvelopeOldService envelopeOldService, ILogger logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeHistoryService historyService, IStringLocalizer localizer, IConfiguration configuration, HtmlSanitizer sanitizer, Cultures cultures, IEnvelopeMailService envelopeMailService, IEnvelopeReceiverReadOnlyService readOnlyService, ISmsSender messagingService, ICodeGenerator codeGenerator, IReceiverService receiverService, IDistributedCache distributedCache, IOptions totpSmsParamsOptions, IEnvelopeSmsHandler envelopeSmsService)
{
this.envelopeOldService = envelopeOldService;
_envRcvService = envelopeReceiverService;
@@ -62,6 +63,7 @@ namespace EnvelopeGenerator.Web.Controllers
_rcvService = receiverService;
_dCache = distributedCache;
_totpSmsParams = totpSmsParamsOptions.Value;
+ _envSmsHandler = envelopeSmsService;
}
[HttpGet("/")]
@@ -174,22 +176,13 @@ namespace EnvelopeGenerator.Web.Controllers
{
if (viaSms)
{
- //TODO: create a service (like EnvelopeSmsService)
- //add date time cache
- var key = string.Format(_totpSmsParams.Expiration.CacheKeyFormat, er_secret.EnvelopeId, er_secret.ReceiverId);
- var expiration = await _dCache.GetDateTimeAsync(key);
- if (expiration is null || expiration <= DateTime.Now)
+ var (smsRes, expiration) = await _envSmsHandler.SendTotpAsync(er_secret);
+
+ if (smsRes is not null && smsRes.Failed)
{
- var new_expiration = DateTime.Now.AddSeconds(_totpSmsParams.TotpStep);
- var totp = _codeGenerator.GenerateTotp(er_secret.Receiver!.TotpSecretkey!, _totpSmsParams.TotpStep);
- var msg = string.Format(_totpSmsParams.Format, totp, new_expiration.ToString(_totpSmsParams.Expiration.Format, _totpSmsParams.Expiration.CultureInfo));
- var smsRes = await _msgService.SendSmsAsync(er_secret.PhoneNumber!, msg);
- if (smsRes.Failed)
- {
- var res_json = JsonConvert.SerializeObject(smsRes);
- _logger.LogEnvelopeError(envelopeReceiverId: envelopeReceiverId, message: $"An unexpected error occurred while sending an SMS code. Response: ${res_json}");
- return this.ViewInnerServiceError();
- }
+ var res_json = JsonConvert.SerializeObject(smsRes);
+ _logger.LogEnvelopeError(envelopeReceiverId: envelopeReceiverId, message: $"An unexpected error occurred while sending an SMS code. Response: ${res_json}");
+ return this.ViewInnerServiceError();
}
return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", expiration);