Api/Netina.Infrastructure/Services/SmsService.cs

61 lines
2.1 KiB
C#

using Netina.Common.Models.Api;
using Netina.Common.Models.Exception;
using Netina.Core.Abstracts;
using Netina.Domain.Models.Settings;
using Netina.Infrastructure.RestServices;
namespace Netina.Infrastructure.Services;
public class SmsService : ISmsService
{
private readonly IRestApiWrapper _restApiWrapper;
private readonly ILogger<SmsService> _logger;
private readonly IHostEnvironment _environment;
private readonly SiteSettings _siteSettings;
public SmsService(
IRestApiWrapper restApiWrapper,
IOptionsSnapshot<SiteSettings> optionsSnapshot,
ILogger<SmsService> logger,
IHostEnvironment environment)
{
_restApiWrapper = restApiWrapper;
_logger = logger;
_environment = environment;
_siteSettings = optionsSnapshot.Value;
}
public async Task SendForgerPasswordAsync(string phoneNumber, string newPassword)
{
var rest = await _restApiWrapper.KaveNegarRestApi.SendLookUp(_siteSettings.KaveNegarApiKey, phoneNumber, newPassword, null, null, null, "forgetPassword");
if (rest.Return.status != 200)
throw new BaseApiException(ApiResultStatusCode.SendSmsError, rest.Return.message);
}
public async Task SendVerifyCodeAsync(string phoneNumber, string verifyCode)
{
try
{
var rest = await _restApiWrapper.KaveNegarRestApi.SendLookUp(_siteSettings.KaveNegarApiKey, phoneNumber,
verifyCode, null, null, null, _siteSettings.LoginOtpTemplate);
if (rest.Return.status != 200 && _environment.IsProduction())
throw new BaseApiException(ApiResultStatusCode.SendSmsError, rest.Return.message);
}
catch (ApiException apiException)
{
if (_environment.IsProduction())
throw ;
else
_logger.LogError(apiException.Message);
}
catch (Exception apiException)
{
if (_environment.IsProduction())
throw;
else
_logger.LogError(apiException.Message);
}
}
}