Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config setting to default date range #412

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sloth.Test/Web/TransactionControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
using System.Data.Common;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Runtime.CompilerServices;
using Sloth.Web.Models;
using Microsoft.Extensions.Options;

namespace Sloth.Test.Web
{
Expand All @@ -42,6 +44,8 @@ public class TransactionControllerTests
public Mock<IAggieEnterpriseService> MockAggieEnterpriseService { get; set; }
public Mock<IExecutionStrategy> MockExecutionStrategy { get; set; }

public Mock<IOptions<DataLimitingOptions>> MockOptions { get; set; }

public TransactionsController Controller { get; set; }


Expand All @@ -64,6 +68,7 @@ public TransactionControllerTests()
MockDbTransaction = new Mock<DbTransaction>();
MockAggieEnterpriseService = new Mock<IAggieEnterpriseService>();
MockExecutionStrategy = new Mock<IExecutionStrategy>();
MockOptions = new Mock<IOptions<DataLimitingOptions>>();

//Default Data
UserData = new List<User>();
Expand Down Expand Up @@ -117,7 +122,7 @@ public TransactionControllerTests()

var routeData = new RouteData();
routeData.Values.Add("team", "testSlug");
Controller = new TransactionsController(MockUserManager.Object, MockDbContext.Object, MockWebhookService.Object, MockAggieEnterpriseService.Object)
Controller = new TransactionsController(MockUserManager.Object, MockDbContext.Object, MockWebhookService.Object, MockAggieEnterpriseService.Object, MockOptions.Object)
{
ControllerContext = new ControllerContext
{
Expand Down
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
Loading