Api-PWA/DocuMed.Infrastructure/Services/SmsService.cs

71 lines
2.4 KiB
C#

using Microsoft.Extensions.Hosting;
using System;
namespace DocuMed.Infrastructure.Services;
public class SmsService(
IRestApiWrapper restApiWrapper,
IOptionsSnapshot<SiteSettings> optionsSnapshot,
ILogger<SmsService> logger,
IHostEnvironment environment) : ISmsService
{
private readonly ILogger<SmsService> _logger = logger;
private readonly SiteSettings _siteSettings = optionsSnapshot.Value;
public async Task SendForgerPasswordAsync(string phoneNumber, string newPassword)
{
var rest = await restApiWrapper.KaveNegarRestApi.SendLookUp(_siteSettings.KaveNegarApiKey, phoneNumber, "forgetPassword", newPassword,
null, null , null, null);
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, "login-documed", verifyCode);
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);
}
}
public async Task SendLookUpAsync(string phoneNumber, string template, string token, string? token2 = null, string? token3 = null, string? token10 = null, string? token20 = null)
{
try
{
var rest = await restApiWrapper.KaveNegarRestApi.SendLookUp(_siteSettings.KaveNegarApiKey, phoneNumber, template, token, token2, token3, token10, token20);
if (rest.Return.status != 200)
throw new BaseApiException(ApiResultStatusCode.SendSmsError, rest.Return.message);
}
catch (ApiException apiException)
{
logger.LogError(apiException.Message);
}
catch (Exception apiException)
{
logger.LogError(apiException.Message);
}
}
}