Skip to content

Commit

Permalink
feat: new endpoint to get plan balance
Browse files Browse the repository at this point in the history
  • Loading branch information
blancfabian committed Nov 14, 2023
1 parent a833ab2 commit a29b6b2
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; }

Check warning on line 7 in doppler-beplic/Models/Responses/PlanBalanceResponse.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Models/Responses/PlanBalanceResponse.cs#L7

Added line #L7 was not covered by tests

public int? ConversationsQtyBalance { get; set; }

Check warning on line 9 in doppler-beplic/Models/Responses/PlanBalanceResponse.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Models/Responses/PlanBalanceResponse.cs#L9

Added line #L9 was not covered by tests

public decimal? WhatsAppCreditBalance { get; set; }

Check warning on line 11 in doppler-beplic/Models/Responses/PlanBalanceResponse.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Models/Responses/PlanBalanceResponse.cs#L11

Added line #L11 was not covered by tests

public string? Error { get; set; }

Check warning on line 13 in doppler-beplic/Models/Responses/PlanBalanceResponse.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Models/Responses/PlanBalanceResponse.cs#L13

Added line #L13 was not covered by tests

public string? ErrorStatus { get; set; }

Check warning on line 15 in doppler-beplic/Models/Responses/PlanBalanceResponse.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Models/Responses/PlanBalanceResponse.cs#L15

Added line #L15 was not covered by tests
}
}
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);

Check warning on line 157 in doppler-beplic/Program.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Program.cs#L157

Added line #L157 was not covered by tests

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));
})

Check warning on line 167 in doppler-beplic/Program.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Program.cs#L159-L167

Added lines #L159 - L167 were not covered by tests
.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();

Check warning on line 38 in doppler-beplic/Services/Classes/BeplicSdk.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicSdk.cs#L38

Added line #L38 was not covered by tests

var request = new RestRequest(resource, metod);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer " + AccessToken);
request.Parameters.AddParameters(parameters);

Check warning on line 43 in doppler-beplic/Services/Classes/BeplicSdk.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicSdk.cs#L40-L43

Added lines #L40 - L43 were not covered by tests

return await _client.ExecuteAsync(request);
}

Check warning on line 46 in doppler-beplic/Services/Classes/BeplicSdk.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicSdk.cs#L45-L46

Added lines #L45 - L46 were not covered by tests

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();

Check warning on line 140 in doppler-beplic/Services/Classes/BeplicService.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicService.cs#L140

Added line #L140 was not covered by tests

try
{
var parameters = new Parameter[]
{
Parameter.CreateParameter("idExternal",idExternal,ParameterType.QueryString)
};

Check warning on line 147 in doppler-beplic/Services/Classes/BeplicService.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicService.cs#L144-L147

Added lines #L144 - L147 were not covered by tests

var response = await _sdk.ExecuteResource("/services/beplicpartners/v1/integra/plan/balance", parameters, Method.Get);

Check warning on line 149 in doppler-beplic/Services/Classes/BeplicService.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicService.cs#L149

Added line #L149 was not covered by tests

if (response.IsSuccessStatusCode)
{
var deserealizedResponse = JsonConvert.DeserializeAnonymousType(response.Content ?? "", new
{
success = false,
message = string.Empty,
data = new
{
conversationsQtyBalance = 0,
whatsAppCreditBalance = (decimal?)0.0
}
});

Check warning on line 162 in doppler-beplic/Services/Classes/BeplicService.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicService.cs#L154-L162

Added lines #L154 - L162 were not covered by tests

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;

Check warning on line 174 in doppler-beplic/Services/Classes/BeplicService.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicService.cs#L174

Added line #L174 was not covered by tests
result.ErrorStatus = deserealizedResponse?.Status;
result.Error = deserealizedResponse?.Message;
}
}
catch (Exception ex)

Check warning on line 179 in doppler-beplic/Services/Classes/BeplicService.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicService.cs#L178-L179

Added lines #L178 - L179 were not covered by tests
{
LogErrorException(idExternal, ex);
result.Success = false;
result.Error = ex.Message;
}

Check warning on line 184 in doppler-beplic/Services/Classes/BeplicService.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicService.cs#L181-L184

Added lines #L181 - L184 were not covered by tests

return result;
}

Check warning on line 187 in doppler-beplic/Services/Classes/BeplicService.cs

View check run for this annotation

Codecov / codecov/patch

doppler-beplic/Services/Classes/BeplicService.cs#L186-L187

Added lines #L186 - L187 were not covered by tests

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 a29b6b2

Please sign in to comment.