Skip to content

Commit

Permalink
Config setting to default date range
Browse files Browse the repository at this point in the history
  • Loading branch information
jSylvestre committed Mar 11, 2024
1 parent 3be2cd7 commit c50c922
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
11 changes: 8 additions & 3 deletions Sloth.Web/Controllers/ReportsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@
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
{

[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> dataLimitingOptions) : base(userManager, dbContext)
{
_dataLimitingOptions = dataLimitingOptions.Value;
}

public IActionResult Index()
{
Expand Down Expand Up @@ -78,7 +83,7 @@ public async Task<IActionResult> DownloadableTransactions(TransactionsFilterMode
if (filter == null)
filter = new TransactionsFilterModel();

FilterHelpers.SanitizeTransactionsFilter(filter);
FilterHelpers.SanitizeTransactionsFilter(filter, _dataLimitingOptions.DefaultDateRange);

var model = new TransfersReportViewModel
{
Expand Down
8 changes: 6 additions & 2 deletions Sloth.Web/Controllers/TransactionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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> dataLimitingOptions) : base(userManager, dbContext)
{
_webHookService = webHookService;
_aggieEnterpriseService = aggieEnterpriseService;
_dataLimitingOptions = dataLimitingOptions.Value;
}


Expand All @@ -45,7 +49,7 @@ public async Task<IActionResult> Index(TransactionsFilterModel filter = null)
if (filter == null)
filter = new TransactionsFilterModel();

FilterHelpers.SanitizeTransactionsFilter(filter);
FilterHelpers.SanitizeTransactionsFilter(filter, _dataLimitingOptions.DefaultDateRange);

IQueryable<Transaction> query;

Expand Down
12 changes: 6 additions & 6 deletions Sloth.Web/Helpers/FilterHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Sloth.Web/Models/DataLimitingOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Sloth.Web.Models
{
public class DataLimitingOptions
{
public int DefaultDateRange { get; set; }
}
}
1 change: 1 addition & 0 deletions Sloth.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<SparkpostOptions>(Configuration.GetSection("SparkPost"));
services.Configure<NotificationOptions>(Configuration.GetSection("Notifications"));
services.Configure<KfsOptions>(Configuration.GetSection("Kfs"));
services.Configure<DataLimitingOptions>(Configuration.GetSection("DataLimiting"));


// add infrastructure services
Expand Down
3 changes: 3 additions & 0 deletions Sloth.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
"MaxRetryDelaySeconds": 32,
"MinFetchAgeMinutes": 10
},
"DataLimiting": {
"DefaultDateRange": 30,
},
"RebuildDb": false,
"AutoMigrateDb": false,
"CasBaseUrl": "https://ssodev.ucdavis.edu/cas"
Expand Down

0 comments on commit c50c922

Please sign in to comment.