Update
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
using BackOffice.BFF.Application.Common.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BackOffice.BFF.Infrastructure.Services;
|
||||
public class AfrinoIdpService : IAfrinoIdpService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<AfrinoIdpService> _logger;
|
||||
|
||||
public AfrinoIdpService(HttpClient httpClient, ILogger<AfrinoIdpService> logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
|
||||
_httpClient.BaseAddress = new Uri("https://ids.afrino.co");
|
||||
}
|
||||
|
||||
public async Task<bool> SendOtp(string mobile)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mobile))
|
||||
{
|
||||
_logger.LogError(message: "شماره موبایل نمیتواند خالی باشد!!");
|
||||
return false;
|
||||
}
|
||||
|
||||
var response = await _httpClient.PostAsJsonAsync<SendOtpRequestDto>("api/Auth/send-otp", new() { PhoneNumber = mobile });
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorMessage = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"خطا در ارسال OTP: {errorMessage}");
|
||||
return false;
|
||||
}
|
||||
|
||||
_logger.LogInformation("OTP با موفقیت ارسال شد.");
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<string> VerifyOtp(string mobile, string otpCode)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mobile))
|
||||
{
|
||||
_logger.LogError("شماره موبایل نمیتواند خالی باشد!!");
|
||||
throw new ArgumentException("شماره موبایل نمیتواند خالی باشد!!");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(otpCode))
|
||||
{
|
||||
_logger.LogError("رمز پویا نمیتواند خالی باشد!!");
|
||||
throw new ArgumentException("رمز پویا نمیتواند خالی باشد!!");
|
||||
}
|
||||
|
||||
var response = await _httpClient.PostAsJsonAsync<VerifyOtpRequestDto>("api/Auth/verify-otp", new() { PhoneNumber = mobile, OtpCode = otpCode });
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorMessage = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"خطا در فعال سازی OTP: {errorMessage}");
|
||||
throw new HttpRequestException($"خطا در فعال سازی OTP: {errorMessage}");
|
||||
}
|
||||
|
||||
var result = await response.Content.ReadFromJsonAsync<VerifyOtpResponseDto>();
|
||||
if (result == null || string.IsNullOrEmpty(result.Token))
|
||||
{
|
||||
_logger.LogError("توکن دریافتی از سرور خالی است.");
|
||||
throw new Exception("توکن دریافتی از سرور خالی است.");
|
||||
}
|
||||
|
||||
return result.Token;
|
||||
}
|
||||
|
||||
public async Task<string> CreateUser(string mobile)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mobile))
|
||||
{
|
||||
_logger.LogError(message: "شماره موبایل نمیتواند خالی باشد!!");
|
||||
throw new ArgumentException("شماره موبایل نمیتواند خالی باشد!!");
|
||||
}
|
||||
|
||||
var response = await _httpClient.PostAsJsonAsync<CreateUserRequestDto>("api/Auth/create-user", new() { PhoneNumber = mobile });
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorMessage = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"خطا : {errorMessage}");
|
||||
throw new HttpRequestException($"خطا : {errorMessage}");
|
||||
}
|
||||
|
||||
var result = await response.Content.ReadFromJsonAsync<CreateUserResponseDto>();
|
||||
if (result == null || string.IsNullOrEmpty(result.UserId))
|
||||
{
|
||||
_logger.LogError("شناسه کاربر دریافتی از سرور خالی است.");
|
||||
throw new Exception("شناسه کاربر دریافتی از سرور خالی است.");
|
||||
}
|
||||
|
||||
return result.UserId;
|
||||
}
|
||||
|
||||
public async Task<bool> AddRole(string mobile, string role)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(mobile))
|
||||
{
|
||||
_logger.LogError(message: "شماره موبایل نمیتواند خالی باشد!!");
|
||||
return false;
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(role))
|
||||
{
|
||||
_logger.LogError(message: "نقش نمیتواند خالی باشد!!");
|
||||
return false;
|
||||
}
|
||||
var response = await _httpClient.PostAsJsonAsync<AddRoleRequestDto>("api/Auth/add-role", new() { PhoneNumber = mobile, Role = role });
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorMessage = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"خطا : {errorMessage}");
|
||||
return false;
|
||||
}
|
||||
|
||||
_logger.LogInformation("نقش با موفقیت اضافه شد.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class SendOtpRequestDto
|
||||
{
|
||||
public string PhoneNumber { get; set; }
|
||||
}
|
||||
|
||||
public class VerifyOtpRequestDto
|
||||
{
|
||||
public string PhoneNumber { get; set; }
|
||||
public string OtpCode { get; set; }
|
||||
}
|
||||
public class VerifyOtpResponseDto
|
||||
{
|
||||
public string Token { get; set; }
|
||||
}
|
||||
public class CreateUserRequestDto
|
||||
{
|
||||
public string PhoneNumber { get; set; }
|
||||
}
|
||||
|
||||
public class CreateUserResponseDto
|
||||
{
|
||||
public string UserId { get; set; }
|
||||
}
|
||||
|
||||
public class AddRoleRequestDto
|
||||
{
|
||||
public string PhoneNumber { get; set; }
|
||||
public string Role { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user