From c50c922c6a158ce023039f212dce44f371e21d8b Mon Sep 17 00:00:00 2001 From: Jason Sylvestre Date: Mon, 11 Mar 2024 13:17:36 -0700 Subject: [PATCH] Config setting to default date range --- Sloth.Web/Controllers/ReportsController.cs | 11 ++++++++--- Sloth.Web/Controllers/TransactionsController.cs | 8 ++++++-- Sloth.Web/Helpers/FilterHelpers.cs | 12 ++++++------ Sloth.Web/Models/DataLimitingOptions.cs | 7 +++++++ Sloth.Web/Startup.cs | 1 + Sloth.Web/appsettings.json | 3 +++ 6 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 Sloth.Web/Models/DataLimitingOptions.cs diff --git a/Sloth.Web/Controllers/ReportsController.cs b/Sloth.Web/Controllers/ReportsController.cs index ddfd0f6d..5e913abe 100644 --- a/Sloth.Web/Controllers/ReportsController.cs +++ b/Sloth.Web/Controllers/ReportsController.cs @@ -21,6 +21,8 @@ using Sloth.Web.Models.TransactionViewModels; using Sloth.Web.Resources; using Sloth.Web.Helpers; +using Sloth.Web.Models; +using Microsoft.Extensions.Options; namespace Sloth.Web.Controllers { @@ -28,8 +30,11 @@ namespace Sloth.Web.Controllers [Authorize(Policy = PolicyCodes.TeamAnyRole)] public class ReportsController : SuperController { - public ReportsController(ApplicationUserManager userManager, SlothDbContext dbContext) : base(userManager, dbContext) - { } + private readonly DataLimitingOptions _dataLimitingOptions; + public ReportsController(ApplicationUserManager userManager, SlothDbContext dbContext, IOptions dataLimitingOptions) : base(userManager, dbContext) + { + _dataLimitingOptions = dataLimitingOptions.Value; + } public IActionResult Index() { @@ -78,7 +83,7 @@ public async Task DownloadableTransactions(TransactionsFilterMode if (filter == null) filter = new TransactionsFilterModel(); - FilterHelpers.SanitizeTransactionsFilter(filter); + FilterHelpers.SanitizeTransactionsFilter(filter, _dataLimitingOptions.DefaultDateRange); var model = new TransfersReportViewModel { diff --git a/Sloth.Web/Controllers/TransactionsController.cs b/Sloth.Web/Controllers/TransactionsController.cs index 992ef075..c08921ad 100644 --- a/Sloth.Web/Controllers/TransactionsController.cs +++ b/Sloth.Web/Controllers/TransactionsController.cs @@ -21,6 +21,8 @@ using Sloth.Web.Models.TransactionViewModels; using Sloth.Web.Resources; using Sloth.Web.Helpers; +using Microsoft.Extensions.Options; +using Sloth.Web.Models; namespace Sloth.Web.Controllers { @@ -29,12 +31,14 @@ public class TransactionsController : SuperController { private readonly IWebHookService _webHookService; private readonly IAggieEnterpriseService _aggieEnterpriseService; + private readonly DataLimitingOptions _dataLimitingOptions; public TransactionsController(ApplicationUserManager userManager, SlothDbContext dbContext, IWebHookService webHookService, - IAggieEnterpriseService aggieEnterpriseService) : base(userManager, dbContext) + IAggieEnterpriseService aggieEnterpriseService, IOptions dataLimitingOptions) : base(userManager, dbContext) { _webHookService = webHookService; _aggieEnterpriseService = aggieEnterpriseService; + _dataLimitingOptions = dataLimitingOptions.Value; } @@ -45,7 +49,7 @@ public async Task Index(TransactionsFilterModel filter = null) if (filter == null) filter = new TransactionsFilterModel(); - FilterHelpers.SanitizeTransactionsFilter(filter); + FilterHelpers.SanitizeTransactionsFilter(filter, _dataLimitingOptions.DefaultDateRange); IQueryable query; diff --git a/Sloth.Web/Helpers/FilterHelpers.cs b/Sloth.Web/Helpers/FilterHelpers.cs index b4ca4f67..bd002bb4 100644 --- a/Sloth.Web/Helpers/FilterHelpers.cs +++ b/Sloth.Web/Helpers/FilterHelpers.cs @@ -5,15 +5,15 @@ namespace Sloth.Web.Helpers { public class FilterHelpers { - public static void SanitizeTransactionsFilter(TransactionsFilterModel model) + public static void SanitizeTransactionsFilter(TransactionsFilterModel model, int defaultDays) { - var fromUtc = (model.From ?? DateTime.Now.AddMonths(-1)).ToUniversalTime().Date; + var fromUtc = (model.From ?? DateTime.Now.AddDays(defaultDays * -1)).ToUniversalTime().Date; var throughUtc = (model.To ?? DateTime.Now).ToUniversalTime().AddDays(1).Date; if (fromUtc > DateTime.UtcNow || fromUtc < DateTime.UtcNow.AddYears(-100)) { - // invalid, so default to filtering from one month ago - var from = DateTime.Now.AddMonths((-1)).Date; + // invalid, so default to filtering from one month ago -- Use parameter defaultDays instead + var from = DateTime.Now.AddDays(defaultDays * -1).Date; model.From = from; fromUtc = from.ToUniversalTime(); } @@ -24,8 +24,8 @@ public static void SanitizeTransactionsFilter(TransactionsFilterModel model) if (fromUtc >= throughUtc) { - // invalid, so default to filtering through one month after fromUtc - throughUtc = fromUtc.AddMonths(1).AddDays(1).Date; + // invalid, so default to filtering through one month after fromUtc -- Use parameter defaultDays instead + throughUtc = fromUtc.AddDays(defaultDays * -1).Date; model.To = throughUtc.AddDays(-1).ToLocalTime(); } else diff --git a/Sloth.Web/Models/DataLimitingOptions.cs b/Sloth.Web/Models/DataLimitingOptions.cs new file mode 100644 index 00000000..dde8ea4d --- /dev/null +++ b/Sloth.Web/Models/DataLimitingOptions.cs @@ -0,0 +1,7 @@ +namespace Sloth.Web.Models +{ + public class DataLimitingOptions + { + public int DefaultDateRange { get; set; } + } +} diff --git a/Sloth.Web/Startup.cs b/Sloth.Web/Startup.cs index 1d577b19..40b49d0f 100644 --- a/Sloth.Web/Startup.cs +++ b/Sloth.Web/Startup.cs @@ -60,6 +60,7 @@ public void ConfigureServices(IServiceCollection services) services.Configure(Configuration.GetSection("SparkPost")); services.Configure(Configuration.GetSection("Notifications")); services.Configure(Configuration.GetSection("Kfs")); + services.Configure(Configuration.GetSection("DataLimiting")); // add infrastructure services diff --git a/Sloth.Web/appsettings.json b/Sloth.Web/appsettings.json index c9f3aaf8..ab5de268 100644 --- a/Sloth.Web/appsettings.json +++ b/Sloth.Web/appsettings.json @@ -58,6 +58,9 @@ "MaxRetryDelaySeconds": 32, "MinFetchAgeMinutes": 10 }, + "DataLimiting": { + "DefaultDateRange": 30, + }, "RebuildDb": false, "AutoMigrateDb": false, "CasBaseUrl": "https://ssodev.ucdavis.edu/cas"