Skip to content

Commit

Permalink
Merge pull request #511 from ucdavis/JCS/RecordPaymentDetails
Browse files Browse the repository at this point in the history
Process to update missing existing ones
  • Loading branch information
jSylvestre authored Jan 8, 2025
2 parents 4b64e04 + 2fa0501 commit 8ed9480
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
50 changes: 49 additions & 1 deletion Hippo.Core/Services/SlothService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public interface ISlothService
Task<bool> TestApiKey(int clusterId);
Task<bool> ProcessPayments(); //All clusters?
Task<bool> UpdatePayments(); //All clusters?
Task<bool> UpdateMissingPaymentDetails(); //All clusters
}
public class SlothService : ISlothService
{
Expand Down Expand Up @@ -288,7 +289,7 @@ public async Task<bool> UpdatePayments()

try
{
payment.Details = Serialize(slothResponse.Transfers); //This should fit, but to be safe, need to make it bigger.
payment.Details = Serialize(slothResponse.Transfers);
}
catch(Exception ex)
{
Expand Down Expand Up @@ -381,6 +382,53 @@ public async Task<bool> UpdatePayments()
return !wereThereErrors;
}

public async Task<bool> UpdateMissingPaymentDetails()
{
var wereThereErrors = false;
var allPayments = await _dbContext.Payments.Include(a => a.Order).ThenInclude(a => a.Cluster).Where(a => a.Status == Payment.Statuses.Completed && string.IsNullOrWhiteSpace(a.Details)).ToListAsync();
var paymentGroups = allPayments.GroupBy(a => a.Order.ClusterId);
foreach (var group in paymentGroups)
{
var clusterId = group.Key;
var financialDetail = await _dbContext.FinancialDetails.SingleAsync(a => a.ClusterId == clusterId);
var apiKey = await _secretsService.GetSecret(financialDetail.SecretAccessKey);
using var client = _clientFactory.CreateClient();
client.BaseAddress = new Uri($"{_slothSettings.ApiUrl}Transactions/");
client.DefaultRequestHeaders.Add("X-Auth-Token", apiKey);
foreach (var payment in group)
{
if (string.IsNullOrWhiteSpace(payment.FinancialSystemId))
{
wereThereErrors = true;
Log.Error($"Error processing payment: {payment.Id} for Order: {payment.OrderId} Name: {payment.Order.Name} Error: Missing FinancialSystemId");
continue;
}
var response = await client.GetAsync(payment.FinancialSystemId);
if (response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NoContent)
{
var content = await response.Content.ReadAsStringAsync();
var slothResponse = JsonSerializer.Deserialize<SlothResponseModel>(content, _serializerOptions);
if (slothResponse.Status == SlothStatuses.Completed)
{
try
{
payment.Details = Serialize(slothResponse.Transfers);
await _dbContext.SaveChangesAsync();
}
catch (Exception ex)
{
Log.Error(ex, "Error tring to searialize sloth transfers");
wereThereErrors = true;
}
}
}
}
}

return !wereThereErrors;
}


private static string Serialize(object obj)
{
return JsonSerializer.Serialize(obj, new JsonSerializerOptions { WriteIndented = true });
Expand Down
11 changes: 10 additions & 1 deletion Hippo.Web/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public class TestController : SuperController
private readonly IMjmlRenderer _mjmlRenderer;
private readonly EmailSettings _emailSettings;

public ISlothService _slothService { get; }


public IAggieEnterpriseService _aggieEnterpriseService { get; }

public TestController(IEmailService emailService, ISshService sshService, INotificationService notificationService, AppDbContext dbContext,
ISecretsService secretsService, IMjmlRenderer mjmlRenderer, IAggieEnterpriseService aggieEnterpriseService, IOptions<EmailSettings> emailSettings)
ISecretsService secretsService, IMjmlRenderer mjmlRenderer, IAggieEnterpriseService aggieEnterpriseService, IOptions<EmailSettings> emailSettings, ISlothService slothService)
{
_emailService = emailService;
_sshService = sshService;
Expand All @@ -41,6 +43,13 @@ public TestController(IEmailService emailService, ISshService sshService, INotif
_mjmlRenderer = mjmlRenderer;
_aggieEnterpriseService = aggieEnterpriseService;
_emailSettings = emailSettings.Value;
_slothService = slothService;
}

public async Task<IActionResult> UpdateMissingPaymentDetails()
{
var model = await _slothService.UpdateMissingPaymentDetails();
return Content(model.ToString());
}

public async Task<IActionResult> TestEmail()
Expand Down

0 comments on commit 8ed9480

Please sign in to comment.