From 64568d9ebedd65f8e340a4374419b27ee1d760a7 Mon Sep 17 00:00:00 2001 From: Jason Sylvestre Date: Wed, 8 Jan 2025 10:17:03 -0800 Subject: [PATCH 1/2] Process to update missing existing ones The route would have to be added to the startup file --- Hippo.Core/Services/SlothService.cs | 48 +++++++++++++++++++++++++ Hippo.Web/Controllers/TestController.cs | 11 +++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Hippo.Core/Services/SlothService.cs b/Hippo.Core/Services/SlothService.cs index a3701a1c..0d9a2017 100644 --- a/Hippo.Core/Services/SlothService.cs +++ b/Hippo.Core/Services/SlothService.cs @@ -28,6 +28,7 @@ public interface ISlothService Task TestApiKey(int clusterId); Task ProcessPayments(); //All clusters? Task UpdatePayments(); //All clusters? + Task UpdateMissingPaymentDetails(); //All clusters } public class SlothService : ISlothService { @@ -381,6 +382,53 @@ public async Task UpdatePayments() return !wereThereErrors; } + public async Task 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(content, _serializerOptions); + if (slothResponse.Status == SlothStatuses.Completed) + { + try + { + payment.Details = Serialize(slothResponse.Transfers); //This should fit, but to be safe, need to make it bigger. + 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 }); diff --git a/Hippo.Web/Controllers/TestController.cs b/Hippo.Web/Controllers/TestController.cs index b3350dd6..b2a90f6f 100644 --- a/Hippo.Web/Controllers/TestController.cs +++ b/Hippo.Web/Controllers/TestController.cs @@ -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) + ISecretsService secretsService, IMjmlRenderer mjmlRenderer, IAggieEnterpriseService aggieEnterpriseService, IOptions emailSettings, ISlothService slothService) { _emailService = emailService; _sshService = sshService; @@ -41,6 +43,13 @@ public TestController(IEmailService emailService, ISshService sshService, INotif _mjmlRenderer = mjmlRenderer; _aggieEnterpriseService = aggieEnterpriseService; _emailSettings = emailSettings.Value; + _slothService = slothService; + } + + public async Task UpdateMissingPaymentDetails() + { + var model = await _slothService.UpdateMissingPaymentDetails(); + return Content(model.ToString()); } public async Task TestEmail() From 2fa0501db64bcb9b4602059a064c22420c3d3682 Mon Sep 17 00:00:00 2001 From: Jason Sylvestre Date: Wed, 8 Jan 2025 10:55:38 -0800 Subject: [PATCH 2/2] It is now bigger --- Hippo.Core/Services/SlothService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Hippo.Core/Services/SlothService.cs b/Hippo.Core/Services/SlothService.cs index 0d9a2017..3cdbfa3c 100644 --- a/Hippo.Core/Services/SlothService.cs +++ b/Hippo.Core/Services/SlothService.cs @@ -289,7 +289,7 @@ public async Task 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) { @@ -412,7 +412,7 @@ public async Task UpdateMissingPaymentDetails() { try { - payment.Details = Serialize(slothResponse.Transfers); //This should fit, but to be safe, need to make it bigger. + payment.Details = Serialize(slothResponse.Transfers); await _dbContext.SaveChangesAsync(); } catch (Exception ex)