Skip to content

Commit

Permalink
Merge pull request #53 from FromDoppler/doi-1067-conversations-plan-b…
Browse files Browse the repository at this point in the history
…alance

[DOI-1067] New endpoint to get plan balance
  • Loading branch information
blancfabian authored Nov 27, 2023
2 parents d24161d + 774e469 commit 9232e9d
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doppler-beplic/Models/Responses/PlanBalanceResponse.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
18 changes: 18 additions & 0 deletions doppler-beplic/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@
.WithOpenApi()
.RequireAuthorization(Policies.OnlySuperuser);

app.MapGet("/plan/balance/{idExternal}", async Task<Results<Ok<PlanBalanceResponse>, BadRequest<string>>> (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
Expand Down
13 changes: 13 additions & 0 deletions doppler-beplic/Services/Classes/BeplicSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public async Task<RestResponse> ExecuteResource(string resource, object body, Me

return await _client.ExecuteAsync(request);
}

public async Task<RestResponse> 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;
Expand Down
52 changes: 52 additions & 0 deletions doppler-beplic/Services/Classes/BeplicService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using DopplerBeplic.Services.Interfaces;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using RestSharp;

namespace DopplerBeplic.Services.Classes
{
Expand Down Expand Up @@ -134,6 +135,57 @@ public async Task<CustomerUpdateResponse> UpdateCustomer(CustomerUpdateDTO custo
return result;
}

public async Task<PlanBalanceResponse> 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))
Expand Down
2 changes: 2 additions & 0 deletions doppler-beplic/Services/Interfaces/IBeplicService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public interface IBeplicService
Task<CustomerUpdateResponse> UpdateCustomer(CustomerUpdateDTO customerData);

Task<UserAdminUpdateResponse> UpdateUserAdmin(UserAdminUpdateDTO userAdminData);

Task<PlanBalanceResponse> GetPlanBalance(string idExternal);
}
}

0 comments on commit 9232e9d

Please sign in to comment.