diff --git a/src/Client/Pages/Dashboard/Components/StatisticsActionsAll.razor.cs b/src/Client/Pages/Dashboard/Components/StatisticsActionsAll.razor.cs index 98fd788b..c8a34a79 100644 --- a/src/Client/Pages/Dashboard/Components/StatisticsActionsAll.razor.cs +++ b/src/Client/Pages/Dashboard/Components/StatisticsActionsAll.razor.cs @@ -55,7 +55,7 @@ public async Task UpdateAnalyzeYearChartAll() _renderChart = false; await Task.Delay(1); // Will not update without this delay StateHasChanged(); - var analyzeData = await ReportActionRepository.AnalyzeYearChartsAll(SelectedUsers, _selectedPrio, _cls.Token); + var analyzeData = await ReportActionRepository.AnalyzeYearChartsAll(SelectedUsers, _selectedPrio, AllYears ? null : 5, _cls.Token); if (analyzeData is null) return; _elapsedMilliseconds = analyzeData.ElapsedMilliseconds; _data = StatisticsTab.DrawLineChartAll(analyzeData, AllYears); diff --git a/src/Client/Pages/Dashboard/Components/StatisticsTab.razor.cs b/src/Client/Pages/Dashboard/Components/StatisticsTab.razor.cs index 5c2ce7b4..2088b989 100644 --- a/src/Client/Pages/Dashboard/Components/StatisticsTab.razor.cs +++ b/src/Client/Pages/Dashboard/Components/StatisticsTab.razor.cs @@ -65,8 +65,8 @@ public List DrawLineChartAll(AnalyzeYearChartAllResponse? analyzeData var yearCount = 0; foreach (var year in analyzeData.Years.OrderByDescending(x => x.Year)) { - if (!allYears && yearCount >= 5) - break; + /*if (!allYears && yearCount >= 5) + break;*/ if (year.Year <= 2021) { _showHistoricalIncorrectWarning = true; diff --git a/src/Client/Repositories/ReportActionRepository.cs b/src/Client/Repositories/ReportActionRepository.cs index 8e03eb30..e1455ac8 100644 --- a/src/Client/Repositories/ReportActionRepository.cs +++ b/src/Client/Repositories/ReportActionRepository.cs @@ -33,14 +33,15 @@ public async Task GetLastActions(IEnumerable AnalyzeYearChartsAll(IEnumerable users, IEnumerable prio, CancellationToken clt) + public async Task AnalyzeYearChartsAll(IEnumerable users, IEnumerable? prio, int? years, CancellationToken clt) { try { var request = new AnalyzeActionRequest() { Users = users.Select(x => (Guid?)x.Id).ToList(), - Prio = prio + Prio = prio, + Years = years }; var result = await _reportActionClient.AnalyzeYearChartsAllAsync(request, clt); return result; diff --git a/src/Server/Controllers/ReportTrainingController.cs b/src/Server/Controllers/ReportTrainingController.cs index 4d81956d..8fa34117 100644 --- a/src/Server/Controllers/ReportTrainingController.cs +++ b/src/Server/Controllers/ReportTrainingController.cs @@ -93,7 +93,7 @@ public async Task> AnalyzeYearChartsAl if (request.Users.Count > 5) return new AnalyzeYearChartAllResponse() { Message = "To many users" }; - var result = await _reportTrainingService.AnalyzeYearChartsAll(request.Users, customerId, timeZone, clt); + var result = await _reportTrainingService.AnalyzeYearChartsAll(request, customerId, timeZone, clt); return result; } catch (Exception ex) diff --git a/src/Server/Database/DataContext.cs b/src/Server/Database/DataContext.cs index f139d86f..3e075b9b 100644 --- a/src/Server/Database/DataContext.cs +++ b/src/Server/Database/DataContext.cs @@ -6,7 +6,7 @@ namespace Drogecode.Knrm.Oefenrooster.Server.Database { - public class DataContext : DbContext, IDataProtectionKeyContext + public class DataContext(DbContextOptions context) : DbContext(context), IDataProtectionKeyContext { // To persist key's public DbSet DataProtectionKeys { get; set; } = null!; @@ -43,10 +43,6 @@ public class DataContext : DbContext, IDataProtectionKeyContext public DbSet LinkExchanges{ get; set; } - public DataContext(DbContextOptions context) : base(context) - { - } - protected override void OnModelCreating(ModelBuilder modelBuilder) { // Audit diff --git a/src/Server/Program.cs b/src/Server/Program.cs index b19df0ba..751c30b2 100644 --- a/src/Server/Program.cs +++ b/src/Server/Program.cs @@ -86,7 +86,10 @@ new[] { "application/octet-stream" }); }); -builder.Services.AddDbContextPool(options => options.UseNpgsql(dbConnectionString)); +builder.Services.AddDbContextPool(options => +{ + options.UseNpgsql(dbConnectionString); +}); builder.Services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions { ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"], diff --git a/src/Server/Services/Interfaces/IReportTrainingService.cs b/src/Server/Services/Interfaces/IReportTrainingService.cs index bf161a67..9e46f6af 100644 --- a/src/Server/Services/Interfaces/IReportTrainingService.cs +++ b/src/Server/Services/Interfaces/IReportTrainingService.cs @@ -6,5 +6,5 @@ namespace Drogecode.Knrm.Oefenrooster.Server.Services.Interfaces; public interface IReportTrainingService { Task GetListTrainingUser(List users, Guid userId, int count, int skip, Guid customerId, CancellationToken clt); - Task AnalyzeYearChartsAll(List users, Guid customerId, string timeZone, CancellationToken clt); + Task AnalyzeYearChartsAll(AnalyzeTrainingRequest trainingRequest, Guid customerId, string timeZone, CancellationToken clt); } \ No newline at end of file diff --git a/src/Server/Services/ReportActionService.cs b/src/Server/Services/ReportActionService.cs index bc760f03..190cabf7 100644 --- a/src/Server/Services/ReportActionService.cs +++ b/src/Server/Services/ReportActionService.cs @@ -40,19 +40,32 @@ public async Task AnalyzeYearChartsAll(AnalyzeActio && (actionRequest.Prio == null || !actionRequest.Prio.Any() || actionRequest.Prio.Contains(x.Prio)) && x.Users!.Count(y => actionRequest.Users!.Contains(y.DrogeCodeId)) == actionRequest.Users!.Count) .Select(x => new { x.Start, x.Number }) - .OrderBy(x => x.Start) + .OrderByDescending(x => x.Start) .ToListAsync(clt); var result = new AnalyzeYearChartAllResponse { TotalCount = allReports.Count() }; var skipped = 0; + List years = new(); foreach (var report in allReports) { - if (report.Number is not null && (report.Number % 1) != 0 && allReports.Select(x => x.Start.Year == report.Start.Year && x.Number?.FloatingEquals((int)report.Number, 0.1) == true).Any()) + if (report.Number is not null && (report.Number % 1) != 0 && allReports.Count(x => x.Start.Year == report.Start.Year && x.Number?.FloatingEquals((int)report.Number, 0.1) == true) >= 2) { skipped++; continue; } + var zone = TimeZoneInfo.FindSystemTimeZoneById(timeZone); var start = TimeZoneInfo.ConvertTimeFromUtc(report.Start, zone); + + if (!years.Contains(start.Year)) // Could not find a way to check this in the db request. + { + if (years.Count >= actionRequest.Years) + { + break; + } + + years.Add(start.Year); + } + if (result.Years.All(x => x.Year != start.Year)) { result.Years.Add(new AnalyzeYearDetails() { Year = start.Year }); diff --git a/src/Server/Services/ReportTrainingService.cs b/src/Server/Services/ReportTrainingService.cs index 38dc186d..7f891745 100644 --- a/src/Server/Services/ReportTrainingService.cs +++ b/src/Server/Services/ReportTrainingService.cs @@ -31,16 +31,27 @@ public async Task GetListTrainingUser(List AnalyzeYearChartsAll(List users, Guid customerId, string timeZone, CancellationToken clt) + public async Task AnalyzeYearChartsAll(AnalyzeTrainingRequest trainingRequest, Guid customerId, string timeZone, CancellationToken clt) { var sw = Stopwatch.StartNew(); var allReports = _database.ReportTrainings - .Where(x => x.CustomerId == customerId && x.Users.Count(y => users.Contains(y.DrogeCodeId)) == users.Count) + .Where(x => x.CustomerId == customerId && x.Users!.Count(y => trainingRequest.Users!.Contains(y.DrogeCodeId)) == trainingRequest.Users!.Count) .Select(x => new { x.Start }); var result = new AnalyzeYearChartAllResponse { TotalCount = allReports.Count() }; + List years = new(); foreach (var report in allReports) { var start = report.Start.ToDateTimeTimeZone(timeZone).ToDateTime(); + + if (!years.Contains(start.Year)) // Could not find a way to check this in the db request. + { + if (years.Count >= trainingRequest.Years) + { + break; + } + + years.Add(start.Year); + } if (result.Years.All(x => x.Year != start.Year)) { result.Years.Add(new AnalyzeYearDetails() { Year = start.Year }); diff --git a/src/Server/Services/ScheduleService.cs b/src/Server/Services/ScheduleService.cs index b2fe86c8..4cd2be44 100644 --- a/src/Server/Services/ScheduleService.cs +++ b/src/Server/Services/ScheduleService.cs @@ -560,7 +560,6 @@ public async Task ScheduleForAllAsync(Guid userId, Guid else if (vehicle.IsDefault) { vehiclePrev = vehicle; - //break; } } diff --git a/src/Shared/Models/ReportAction/AnalyzeActionRequest.cs b/src/Shared/Models/ReportAction/AnalyzeActionRequest.cs index 0e41fea1..c0154d65 100644 --- a/src/Shared/Models/ReportAction/AnalyzeActionRequest.cs +++ b/src/Shared/Models/ReportAction/AnalyzeActionRequest.cs @@ -4,4 +4,5 @@ public class AnalyzeActionRequest { public List? Users { get; set; } public IEnumerable? Prio { get; set; } + public int? Years { get; set; } } \ No newline at end of file diff --git a/src/Shared/Models/ReportTraining/AnalyzeTrainingRequest.cs b/src/Shared/Models/ReportTraining/AnalyzeTrainingRequest.cs index ded78f7a..39458f67 100644 --- a/src/Shared/Models/ReportTraining/AnalyzeTrainingRequest.cs +++ b/src/Shared/Models/ReportTraining/AnalyzeTrainingRequest.cs @@ -3,4 +3,5 @@ public class AnalyzeTrainingRequest { public List? Users { get; set; } + public int? Years { get; set; } } \ No newline at end of file diff --git a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Seeds/SeedCustomer.cs b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Seeds/SeedCustomer.cs new file mode 100644 index 00000000..e744f0e0 --- /dev/null +++ b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Seeds/SeedCustomer.cs @@ -0,0 +1,18 @@ +using Drogecode.Knrm.Oefenrooster.Server.Database.Models; + +namespace Drogecode.Knrm.Oefenrooster.TestServer.Seeds; + +public class SeedCustomer +{ + public static void Seed(DataContext dataContext, Guid defaultCustomerId) + { + dataContext.Customers.Add(new DbCustomers + { + Id = defaultCustomerId, + Name = "xUnit customer", + TimeZone = "Europe/Amsterdam", + Created = DateTime.UtcNow + }); + dataContext.SaveChanges(); + } +} \ No newline at end of file diff --git a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Seeds/SeedReportAction.cs b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Seeds/SeedReportAction.cs new file mode 100644 index 00000000..4a211b38 --- /dev/null +++ b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Seeds/SeedReportAction.cs @@ -0,0 +1,65 @@ +using Drogecode.Knrm.Oefenrooster.Server.Database.Models; +using Drogecode.Knrm.Oefenrooster.Shared.Helpers; + +namespace Drogecode.Knrm.Oefenrooster.TestServer.Seeds; + +public static class SeedReportAction +{ + public static void Seed(DataContext dataContext, Guid defaultCustomerId) + { + SeedForYear(dataContext, defaultCustomerId, 2022); + SeedForYear(dataContext, defaultCustomerId, 2020); + SeedForYear(dataContext, defaultCustomerId, 2019); + SeedForYear(dataContext, defaultCustomerId, 2018); + SeedForYear(dataContext, defaultCustomerId, 2016); + SeedForYear(dataContext, defaultCustomerId, 2012); + SeedForYear(dataContext, defaultCustomerId, 2004); + dataContext.SaveChanges(); + } + + private static void SeedForYear(DataContext dataContext, Guid defaultCustomerId, int year) + { + var start = new DateTime(year, 3, 8, 8, 5, 41); + dataContext.ReportActions.Add(new DbReportAction + { + Id = Guid.NewGuid(), + CustomerId = defaultCustomerId, + Description = "xUnit Description A", + Start = start, + Commencement = start.AddMinutes(5), + Departure = start.AddMinutes(15), + End = start.AddMinutes(121), + Boat = "xUnit boat", + Prio = "Prio 69", + Users = new List{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}}, + }); + start = start.AddDays(1); + dataContext.ReportActions.Add(new DbReportAction + { + Id = Guid.NewGuid(), + CustomerId = defaultCustomerId, + Description = "xUnit Description B", + Start = start, + Commencement = start.AddMinutes(5), + Departure = start.AddMinutes(15), + End = start.AddMinutes(121), + Boat = "xUnit boat", + Prio = "Prio 1", + Users = new List{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}}, + }); + start = start.AddMonths(1); + dataContext.ReportActions.Add(new DbReportAction + { + Id = Guid.NewGuid(), + CustomerId = defaultCustomerId, + Description = "xUnit Description C", + Start = start, + Commencement = start.AddMinutes(5), + Departure = start.AddMinutes(15), + End = start.AddMinutes(121), + Boat = "xUnit boat", + Prio = "Prio 1", + Users = new List{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}}, + }); + } +} \ No newline at end of file diff --git a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Seeds/SeedReportTraining.cs b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Seeds/SeedReportTraining.cs new file mode 100644 index 00000000..dbc05e30 --- /dev/null +++ b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Seeds/SeedReportTraining.cs @@ -0,0 +1,59 @@ +using Drogecode.Knrm.Oefenrooster.Server.Database.Models; +using Drogecode.Knrm.Oefenrooster.Shared.Helpers; + +namespace Drogecode.Knrm.Oefenrooster.TestServer.Seeds; + +public static class SeedReportTraining +{ + public static void Seed(DataContext dataContext, Guid defaultCustomerId) + { + SeedForYear(dataContext, defaultCustomerId, 2022); + SeedForYear(dataContext, defaultCustomerId, 2020); + SeedForYear(dataContext, defaultCustomerId, 2019); + SeedForYear(dataContext, defaultCustomerId, 2018); + SeedForYear(dataContext, defaultCustomerId, 2016); + SeedForYear(dataContext, defaultCustomerId, 2012); + SeedForYear(dataContext, defaultCustomerId, 2004); + dataContext.SaveChanges(); + } + + private static void SeedForYear(DataContext dataContext, Guid defaultCustomerId, int year) + { + var start = new DateTime(year, 3, 8, 8, 5, 41); + dataContext.ReportTrainings.Add(new DbReportTraining + { + Id = Guid.NewGuid(), + CustomerId = defaultCustomerId, + Description = "xUnit Description A", + Start = start, + Commencement = start.AddMinutes(5), + End = start.AddMinutes(121), + Boat = "xUnit boat", + Users = new List { new DbReportUser { DrogeCodeId = DefaultSettingsHelper.IdTaco } }, + }); + start = start.AddDays(1); + dataContext.ReportTrainings.Add(new DbReportTraining + { + Id = Guid.NewGuid(), + CustomerId = defaultCustomerId, + Description = "xUnit Description B", + Start = start, + Commencement = start.AddMinutes(5), + End = start.AddMinutes(121), + Boat = "xUnit boat", + Users = new List { new DbReportUser { DrogeCodeId = DefaultSettingsHelper.IdTaco } }, + }); + start = start.AddMonths(1); + dataContext.ReportTrainings.Add(new DbReportTraining + { + Id = Guid.NewGuid(), + CustomerId = defaultCustomerId, + Description = "xUnit Description C", + Start = start, + Commencement = start.AddMinutes(5), + End = start.AddMinutes(121), + Boat = "xUnit boat", + Users = new List { new DbReportUser { DrogeCodeId = DefaultSettingsHelper.IdTaco } }, + }); + } +} \ No newline at end of file diff --git a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/BaseTest.cs b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/BaseTest.cs index 08bacec5..006f56c0 100644 --- a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/BaseTest.cs +++ b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/BaseTest.cs @@ -21,6 +21,7 @@ using System.Security.Claims; using Drogecode.Knrm.Oefenrooster.Server.Database.Migrations; using Drogecode.Knrm.Oefenrooster.Shared.Models.UserRole; +using Drogecode.Knrm.Oefenrooster.TestServer.Seeds; namespace Drogecode.Knrm.Oefenrooster.TestServer.Tests; @@ -100,9 +101,7 @@ public BaseTest( UserRoleController = userRoleController; DefaultCustomerId = Guid.NewGuid(); - SeedCustomer(dataContext); - SeedReportAction(dataContext); - SeedReportTraining(dataContext); + SeedCustomer.Seed(dataContext, DefaultCustomerId); var defaultRoles = new List { @@ -138,106 +137,6 @@ public async Task InitializeAsync() DefaultDefaultSchedule = await AddDefaultSchedule(); } - private void SeedCustomer(DataContext dataContext) - { - dataContext.Customers.Add(new DbCustomers - { - Id = DefaultCustomerId, - Name = "xUnit customer", - TimeZone = "Europe/Amsterdam", - Created = DateTime.UtcNow - }); - dataContext.SaveChanges(); - } - - public void SeedReportAction(DataContext dataContext) - { - var start = new DateTime(2022, 3, 8, 8, 5, 41); - dataContext.ReportActions.Add(new DbReportAction - { - Id = Guid.NewGuid(), - CustomerId = DefaultCustomerId, - Description = "xUnit Description A", - Start = start, - Commencement = start.AddMinutes(5), - Departure = start.AddMinutes(15), - End = start.AddMinutes(121), - Boat = "xUnit boat", - Prio = "Prio 69", - Users = new List{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}}, - }); - start = start.AddDays(1); - dataContext.ReportActions.Add(new DbReportAction - { - Id = Guid.NewGuid(), - CustomerId = DefaultCustomerId, - Description = "xUnit Description B", - Start = start, - Commencement = start.AddMinutes(5), - Departure = start.AddMinutes(15), - End = start.AddMinutes(121), - Boat = "xUnit boat", - Prio = "Prio 1", - Users = new List{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}}, - }); - start = start.AddMonths(1); - dataContext.ReportActions.Add(new DbReportAction - { - Id = Guid.NewGuid(), - CustomerId = DefaultCustomerId, - Description = "xUnit Description C", - Start = start, - Commencement = start.AddMinutes(5), - Departure = start.AddMinutes(15), - End = start.AddMinutes(121), - Boat = "xUnit boat", - Prio = "Prio 1", - Users = new List{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}}, - }); - dataContext.SaveChanges(); - } - - public void SeedReportTraining(DataContext dataContext) - { - var start = new DateTime(2022, 3, 8, 8, 5, 41); - dataContext.ReportTrainings.Add(new DbReportTraining - { - Id = Guid.NewGuid(), - CustomerId = DefaultCustomerId, - Description = "xUnit Description A", - Start = start, - Commencement = start.AddMinutes(5), - End = start.AddMinutes(121), - Boat = "xUnit boat", - Users = new List{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}}, - }); - start = start.AddDays(1); - dataContext.ReportTrainings.Add(new DbReportTraining - { - Id = Guid.NewGuid(), - CustomerId = DefaultCustomerId, - Description = "xUnit Description B", - Start = start, - Commencement = start.AddMinutes(5), - End = start.AddMinutes(121), - Boat = "xUnit boat", - Users = new List{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}}, - }); - start = start.AddMonths(1); - dataContext.ReportTrainings.Add(new DbReportTraining - { - Id = Guid.NewGuid(), - CustomerId = DefaultCustomerId, - Description = "xUnit Description C", - Start = start, - Commencement = start.AddMinutes(5), - End = start.AddMinutes(121), - Boat = "xUnit boat", - Users = new List{new DbReportUser{DrogeCodeId = DefaultSettingsHelper.IdTaco}}, - }); - dataContext.SaveChanges(); - } - protected async Task AddUser(string name) { var result = await UserController.AddUser(new DrogeUser @@ -262,6 +161,7 @@ protected async Task AddFunction(string name, bool isDefault) Assert.NotNull(result?.Value?.NewFunction); return result.Value.NewFunction.Id; } + protected async Task AddUserRole(string name) { var result = await UserRoleController.NewUserRole(new DrogeUserRole @@ -388,6 +288,7 @@ protected async Task AddVehicle(string name, string code = "xUnit", bool i Code = code, IsDefault = isDefault, IsActive = isActive, + Order = 4, }; var result = await VehicleController.PutVehicle(vehicle); Assert.NotNull(result?.Value); diff --git a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ReportActionControllerTests.cs b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ReportActionControllerTests.cs index 75898c94..197aed31 100644 --- a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ReportActionControllerTests.cs +++ b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ReportActionControllerTests.cs @@ -6,6 +6,7 @@ using Drogecode.Knrm.Oefenrooster.Shared.Helpers; using Drogecode.Knrm.Oefenrooster.Shared.Models.ReportAction; using Drogecode.Knrm.Oefenrooster.Shared.Services.Interfaces; +using Drogecode.Knrm.Oefenrooster.TestServer.Seeds; namespace Drogecode.Knrm.Oefenrooster.TestServer.Tests.ControllerTests; @@ -30,6 +31,7 @@ public ReportActionControllerTests( base(dataContext, dateTimeServiceMock, scheduleController, userController, functionController, holidayController, trainingTypesController, dayItemController, monthItemController, preComController, vehicleController, defaultScheduleController, reportActionController, reportTrainingController, userRoleController) { + SeedReportAction.Seed(dataContext, DefaultCustomerId); } [Fact] @@ -79,14 +81,33 @@ public async Task AnalyzeYearChartsAllForAllUsersTest() var getResult = await ReportActionController.AnalyzeYearChartsAll(request); Assert.NotNull(getResult.Value?.Years); Assert.True(getResult.Value.Success); - getResult.Value.Years.Should().HaveCount(1); - getResult.Value.TotalCount.Should().Be(3); + getResult.Value.Years.Should().HaveCount(7); + getResult.Value.TotalCount.Should().Be(7*3); var y2022 = getResult.Value.Years.FirstOrDefault(x => x.Year == 2022); Assert.NotNull(y2022); y2022.Months.Should().Contain(x => x.Month == 3 && x.Count == 2); y2022.Months.Should().Contain(x => x.Month == 4 && x.Count == 1); } + [Fact] + public async Task AnalyzeYearChartsAllForAllUsers3YearsTest() + { + var request = new AnalyzeActionRequest() + { + Users = new List { }, + Years = 3 + }; + var getResult = await ReportActionController.AnalyzeYearChartsAll(request); + Assert.NotNull(getResult.Value?.Years); + Assert.True(getResult.Value.Success); + getResult.Value.Years.Should().HaveCount(3); + getResult.Value.TotalCount.Should().Be(7*3); + getResult.Value.Years.Should().Contain(x => x.Year == 2022); + getResult.Value.Years.Should().Contain(x => x.Year == 2020); + getResult.Value.Years.Should().Contain(x => x.Year == 2019); + getResult.Value.Years.Should().NotContain(x => x.Year == 2018); + } + [Fact] public async Task AnalyzeYearChartsAllForTacoTest() { @@ -97,8 +118,8 @@ public async Task AnalyzeYearChartsAllForTacoTest() var getResult = await ReportActionController.AnalyzeYearChartsAll(request); Assert.NotNull(getResult.Value?.Years); Assert.True(getResult.Value.Success); - getResult.Value.Years.Should().HaveCount(1); - getResult.Value.TotalCount.Should().Be(3); + getResult.Value.Years.Should().HaveCount(7); + getResult.Value.TotalCount.Should().Be(7*3); var y2022 = getResult.Value.Years.FirstOrDefault(x => x.Year == 2022); Assert.NotNull(y2022); y2022.Months.Should().Contain(x => x.Month == 3 && x.Count == 2); diff --git a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ReportTrainingControllerTests.cs b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ReportTrainingControllerTests.cs index 88c9d447..dc46c9f9 100644 --- a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ReportTrainingControllerTests.cs +++ b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ReportTrainingControllerTests.cs @@ -1,7 +1,9 @@ using System.Text.Json; using Drogecode.Knrm.Oefenrooster.Server.Controllers; using Drogecode.Knrm.Oefenrooster.Server.Database; +using Drogecode.Knrm.Oefenrooster.Shared.Models.ReportTraining; using Drogecode.Knrm.Oefenrooster.Shared.Services.Interfaces; +using Drogecode.Knrm.Oefenrooster.TestServer.Seeds; namespace Drogecode.Knrm.Oefenrooster.TestServer.Tests.ControllerTests; @@ -26,6 +28,7 @@ public ReportTrainingControllerTests( base(dataContext, dateTimeServiceMock, scheduleController, userController, functionController, holidayController, trainingTypesController, dayItemController, monthItemController, preComController, vehicleController, defaultScheduleController, reportActionController, reportTrainingController, userRoleController) { + SeedReportTraining.Seed(dataContext, DefaultCustomerId); } [Fact] @@ -55,6 +58,25 @@ public async Task GetReportActionsAllUsersTest() getResult.Value.Trainings.Count.Should().BeGreaterOrEqualTo(2); } + [Fact] + public async Task AnalyzeYearChartsAllForAllUsers3YearsTest() + { + var request = new AnalyzeTrainingRequest() + { + Users = new List { }, + Years = 3 + }; + var getResult = await ReportTrainingController.AnalyzeYearChartsAll(request); + Assert.NotNull(getResult.Value?.Years); + Assert.True(getResult.Value.Success); + getResult.Value.Years.Should().HaveCount(3); + getResult.Value.TotalCount.Should().Be(7*3); + getResult.Value.Years.Should().Contain(x => x.Year == 2022); + getResult.Value.Years.Should().Contain(x => x.Year == 2020); + getResult.Value.Years.Should().Contain(x => x.Year == 2019); + getResult.Value.Years.Should().NotContain(x => x.Year == 2018); + } + [Fact] public async Task GetReportActionsUnknownUserTest() { diff --git a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ScheduleControllerTests.cs b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ScheduleControllerTests.cs index 31d6b41f..6eb032db 100644 --- a/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ScheduleControllerTests.cs +++ b/tests/Drogecode.Knrm.Oefenrooster.TestServer/Tests/ControllerTests/ScheduleControllerTests.cs @@ -572,6 +572,38 @@ public async Task GetPlannedTrainingByIdDefaultVehicleNotSelectedTest() MockAuthenticatedUser(VehicleController, DefaultSettingsHelper.IdTaco, DefaultCustomerId); } + [Fact] + public async Task GetPlannedTrainingsForAllDefaultVehicleNotInDbLinkdButLinkedToUserTest() + { + var dateStart = DateTime.Today.AddDays(1).AddHours(21); + var dateEnd = DateTime.Today.AddDays(1).AddHours(15); + var trainingId = await PrepareAssignedTraining(dateStart, dateEnd, true); + var vehicle = await VehicleController.PutVehicle(new DrogeVehicle() + { IsDefault = true, IsActive = true, Name = "GetPlannedTrainingByIdDefaultVehicleNotSelectedButLinkedToUserTest", Code = "xUnit2", Order = 1 }); + var body = new PatchAssignedUserRequest + { + User = new PlanUser() + { + UserId = DefaultUserId, + VehicleId = vehicle.Value!.Value, + Assigned = true, + }, + TrainingId = trainingId + }; + var patchAssignedUserResult = await ScheduleController.PatchAssignedUser(body); + Assert.True(patchAssignedUserResult?.Value?.Success); + var trainingForAll = await ScheduleController.ForAll(dateStart.Month, dateStart.Year, dateStart.Month, dateStart.Day, dateEnd.Year, dateEnd.Month, dateEnd.Day, false); + Assert.NotNull(trainingForAll.Value?.Planners); + Assert.True(trainingForAll.Value.Success); + trainingForAll.Value.Planners.Where(x => x.TrainingId == trainingId).Should().HaveCount(1); + var training = trainingForAll.Value.Planners.FirstOrDefault(x => x.TrainingId == trainingId); + Assert.NotNull(training?.PlanUsers); + training.PlanUsers.Should().NotBeEmpty(); + training.PlanUsers.Should().Contain(x => x.VehicleId == DefaultVehicle); + MockAuthenticatedUser(ScheduleController, DefaultSettingsHelper.IdTaco, DefaultCustomerId); + MockAuthenticatedUser(VehicleController, DefaultSettingsHelper.IdTaco, DefaultCustomerId); + } + [Fact] public async Task GetPlannedTrainingsForAllDefaultVehicleNotSelectedButLinkedToUserTest() {