diff --git a/doppler-beplic/Models/Responses/PlanBalanceResponse.cs b/doppler-beplic/Models/Responses/PlanBalanceResponse.cs new file mode 100644 index 0000000..b5ba09d --- /dev/null +++ b/doppler-beplic/Models/Responses/PlanBalanceResponse.cs @@ -0,0 +1,17 @@ +using Newtonsoft.Json; + +namespace DopplerBeplic.Models.Responses +{ + public class PlanBalanceResponse + { + public bool Success { get; set; } + + public int? ConversationsQtyBalance { get; set; } + + public decimal? WhatsAppCreditBalance { get; set; } + + public string? Error { get; set; } + + public string? ErrorStatus { get; set; } + } +} diff --git a/doppler-beplic/Program.cs b/doppler-beplic/Program.cs index 8d6caeb..528c950 100644 --- a/doppler-beplic/Program.cs +++ b/doppler-beplic/Program.cs @@ -151,6 +151,24 @@ .WithOpenApi() .RequireAuthorization(Policies.OnlySuperuser); +app.MapGet("/plan/balance/{idExternal}", async Task, BadRequest>> (string idExternal, + IBeplicService beplicService) => +{ + var response = await beplicService.GetPlanBalance(idExternal); + + return response.Success ? + TypedResults.Ok(response) + : TypedResults.BadRequest( + string.Format( + CultureInfo.InvariantCulture, + "Failed to get user plan balance. ErrorStatus: {0} Error: {1}", + response.ErrorStatus, + response.Error)); +}) +.WithName("PlanBalance") +.WithOpenApi() +.RequireAuthorization(Policies.OnlySuperuser); + app.Run(); // Make the implicit Program class public so test projects can access it diff --git a/doppler-beplic/Services/Classes/BeplicSdk.cs b/doppler-beplic/Services/Classes/BeplicSdk.cs index 372d30e..069ee40 100644 --- a/doppler-beplic/Services/Classes/BeplicSdk.cs +++ b/doppler-beplic/Services/Classes/BeplicSdk.cs @@ -32,6 +32,19 @@ public async Task ExecuteResource(string resource, object body, Me return await _client.ExecuteAsync(request); } + + public async Task ExecuteResource(string resource, Parameter[] parameters, Method metod) + { + await EnsureAuthentication(); + + var request = new RestRequest(resource, metod); + request.AddHeader("Content-Type", "application/json"); + request.AddHeader("Authorization", "Bearer " + AccessToken); + request.Parameters.AddParameters(parameters); + + return await _client.ExecuteAsync(request); + } + private RestClient GetClient() { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13; diff --git a/doppler-beplic/Services/Classes/BeplicService.cs b/doppler-beplic/Services/Classes/BeplicService.cs index c8fa64b..709a3db 100644 --- a/doppler-beplic/Services/Classes/BeplicService.cs +++ b/doppler-beplic/Services/Classes/BeplicService.cs @@ -4,6 +4,7 @@ using DopplerBeplic.Services.Interfaces; using Microsoft.Extensions.Options; using Newtonsoft.Json; +using RestSharp; namespace DopplerBeplic.Services.Classes { @@ -134,6 +135,57 @@ public async Task UpdateCustomer(CustomerUpdateDTO custo return result; } + public async Task GetPlanBalance(string idExternal) + { + var result = new PlanBalanceResponse(); + + try + { + var parameters = new Parameter[] + { + Parameter.CreateParameter("idExternal",idExternal,ParameterType.QueryString) + }; + + var response = await _sdk.ExecuteResource("/services/beplicpartners/v1/integra/plan/balance", parameters, Method.Get); + + if (response.IsSuccessStatusCode) + { + var deserealizedResponse = JsonConvert.DeserializeAnonymousType(response.Content ?? "", new + { + success = false, + message = string.Empty, + data = new + { + conversationsQtyBalance = 0, + whatsAppCreditBalance = (decimal?)0.0 + } + }); + + result.Success = deserealizedResponse?.success ?? false; + result.Error = result.Success ? string.Empty : deserealizedResponse?.message; + result.ConversationsQtyBalance = deserealizedResponse?.data.conversationsQtyBalance; + result.WhatsAppCreditBalance = deserealizedResponse?.data.whatsAppCreditBalance; + } + else + { + LogInfoBadRequest(idExternal.ToString(), response.Content ?? "", response.StatusCode.ToString()); + var deserealizedResponse = JsonConvert.DeserializeAnonymousType(response.Content ?? string.Empty, new ErrorResponse()); + + result.Success = false; + result.ErrorStatus = deserealizedResponse?.Status; + result.Error = deserealizedResponse?.Message; + } + } + catch (Exception ex) + { + LogErrorException(idExternal, ex); + result.Success = false; + result.Error = ex.Message; + } + + return result; + } + private string ParseCustomerStatus(CustomerUpdateDTO customerData) { if (string.IsNullOrEmpty(customerData.Customer.Status)) diff --git a/doppler-beplic/Services/Interfaces/IBeplicService.cs b/doppler-beplic/Services/Interfaces/IBeplicService.cs index b028283..766b7ad 100644 --- a/doppler-beplic/Services/Interfaces/IBeplicService.cs +++ b/doppler-beplic/Services/Interfaces/IBeplicService.cs @@ -10,5 +10,7 @@ public interface IBeplicService Task UpdateCustomer(CustomerUpdateDTO customerData); Task UpdateUserAdmin(UserAdminUpdateDTO userAdminData); + + Task GetPlanBalance(string idExternal); } }