diff --git a/src/Server/Controllers/ScheduleController.cs b/src/Server/Controllers/ScheduleController.cs index dfd4a713..3106a50f 100644 --- a/src/Server/Controllers/ScheduleController.cs +++ b/src/Server/Controllers/ScheduleController.cs @@ -359,7 +359,7 @@ public async Task> GetSchedul var userId = new Guid(User.FindFirstValue("http://schemas.microsoft.com/identity/claims/objectidentifier") ?? throw new DrogeCodeNullException("No object identifier found")); var customerId = new Guid(User.FindFirstValue("http://schemas.microsoft.com/identity/claims/tenantid") ?? throw new DrogeCodeNullException("customerId not found")); var fromDate = _dateTimeService.Today().ToUniversalTime(); - var result = await _scheduleService.GetScheduledTrainingsForUser(userId, customerId, fromDate, take, skip, clt); + var result = await _scheduleService.GetScheduledTrainingsForUser(userId, customerId, fromDate, take, skip, OrderAscDesc.Asc, clt); if (callHub) { _logger.LogTrace("Calling hub futureTrainings"); @@ -388,7 +388,7 @@ public async Task> AllTrainin try { var customerId = new Guid(User.FindFirstValue("http://schemas.microsoft.com/identity/claims/tenantid") ?? throw new DrogeCodeNullException("customerId not found")); - var result = await _scheduleService.GetScheduledTrainingsForUser(id, customerId, null, take, skip, clt); + var result = await _scheduleService.GetScheduledTrainingsForUser(id, customerId, null, take, skip, OrderAscDesc.Desc, clt); return result; } catch (OperationCanceledException) diff --git a/src/Server/Services/Interfaces/IScheduleService.cs b/src/Server/Services/Interfaces/IScheduleService.cs index 28da5d38..5161d508 100644 --- a/src/Server/Services/Interfaces/IScheduleService.cs +++ b/src/Server/Services/Interfaces/IScheduleService.cs @@ -13,7 +13,7 @@ public interface IScheduleService Task PatchAssignedUserAsync(Guid userId, Guid customerId, PatchAssignedUserRequest body, CancellationToken clt); Task PatchTraining(Guid customerId, PlannedTraining training, bool inRoleEditPast, CancellationToken clt); Task AddTrainingAsync(Guid customerId, PlannedTraining training, Guid trainingId, CancellationToken clt); - Task GetScheduledTrainingsForUser(Guid userId, Guid customerId, DateTime? fromDate, int take, int skip, CancellationToken clt); + Task GetScheduledTrainingsForUser(Guid userId, Guid customerId, DateTime? fromDate, int take, int skip, OrderAscDesc order, CancellationToken clt); Task GetPinnedTrainingsForUser(Guid userId, Guid customerId, DateTime fromDate, CancellationToken clt); Task PutAssignedUserAsync(Guid userId, Guid customerId, OtherScheduleUserRequest body, CancellationToken clt); Task PatchEventIdForUserAvailible(Guid userId, Guid customerId, Guid? availableId, string? calendarEventId, CancellationToken clt); diff --git a/src/Server/Services/ScheduleService.cs b/src/Server/Services/ScheduleService.cs index b0e45e37..5af71abd 100644 --- a/src/Server/Services/ScheduleService.cs +++ b/src/Server/Services/ScheduleService.cs @@ -897,7 +897,7 @@ public async Task PutAssignedUserAsync(Guid userId, Gui return result; } - public async Task GetScheduledTrainingsForUser(Guid userId, Guid customerId, DateTime? fromDate, int take, int skip, CancellationToken clt) + public async Task GetScheduledTrainingsForUser(Guid userId, Guid customerId, DateTime? fromDate, int take, int skip, OrderAscDesc order, CancellationToken clt) { var sw = Stopwatch.StartNew(); var result = new GetScheduledTrainingsForUserResponse(); @@ -912,30 +912,35 @@ public async Task GetScheduledTrainingsFor .ThenInclude(i => i.LinkVehicleTrainings); var users = _database.Users.Where(x => x.CustomerId == customerId && x.DeletedOn == null); result.TotalCount = scheduled.Count(); - var scheduls = await scheduled.OrderByDescending(x => x.Date).Skip(skip).Take(take).ToListAsync(clt); - foreach (var schedul in scheduls) + var schedules = order switch { - if (schedul.Training == null) + OrderAscDesc.Asc => scheduled.OrderBy(x => x.Date), + OrderAscDesc.Desc => scheduled.OrderByDescending(x => x.Date), + _ => throw new UnreachableException($"OrderAscDesc has unknown value {order}") + }; + foreach (var schedule in await schedules.Skip(skip).Take(take).ToListAsync(clt)) + { + if (schedule?.Training == null) { - _logger.LogWarning("No training found for schedule '{ScheduleId}'", schedul.Id); + _logger.LogWarning("No training found for schedule '{ScheduleId}'", schedule?.Id); continue; } - var defaultVehicle = await GetDefaultVehicleForTraining(customerId, schedul.Training, clt); + var defaultVehicle = await GetDefaultVehicleForTraining(customerId, schedule.Training, clt); var plan = new PlannedTraining { - TrainingId = schedul.TrainingId, - DefaultId = schedul.Training.RoosterDefaultId, - Name = schedul.Training.Name, - DateStart = schedul.Training.DateStart, - DateEnd = schedul.Training.DateEnd, - RoosterTrainingTypeId = schedul.Training.RoosterTrainingTypeId, - PlannedFunctionId = schedul.UserFunctionId ?? users?.FirstOrDefault(x => x.Id == userId)?.UserFunctionId, - IsPinned = schedul.Training.IsPinned, - CountToTrainingTarget = schedul.Training.CountToTrainingTarget, + TrainingId = schedule.TrainingId, + DefaultId = schedule.Training.RoosterDefaultId, + Name = schedule.Training.Name, + DateStart = schedule.Training.DateStart, + DateEnd = schedule.Training.DateEnd, + RoosterTrainingTypeId = schedule.Training.RoosterTrainingTypeId, + PlannedFunctionId = schedule.UserFunctionId ?? users?.FirstOrDefault(x => x.Id == userId)?.UserFunctionId, + IsPinned = schedule.Training.IsPinned, + CountToTrainingTarget = schedule.Training.CountToTrainingTarget, IsCreated = true, - ShowTime = schedul.Training.ShowTime ?? true, - PlanUsers = schedul.Training.RoosterAvailables!.Select(a => + ShowTime = schedule.Training.ShowTime ?? true, + PlanUsers = schedule.Training.RoosterAvailables!.Select(a => new PlanUser { UserId = a.UserId, @@ -956,27 +961,27 @@ public async Task GetScheduledTrainingsFor GetAvailability(defaultAveUser, userHolidays, plan.DateStart, plan.DateEnd, plan.DefaultId, null, ref availabilty, ref setBy); user.Availability = availabilty; user.SetBy = setBy ?? user.SetBy; - if (user.VehicleId is null || (user.VehicleId != defaultVehicle && !(schedul.Training.LinkVehicleTrainings?.Any(x => x.VehicleId == user.VehicleId) ?? false))) + if (user.VehicleId is null || (user.VehicleId != defaultVehicle && !(schedule.Training.LinkVehicleTrainings?.Any(x => x.VehicleId == user.VehicleId) ?? false))) user.VehicleId = defaultVehicle; } result.Trainings.Add(plan); - var userMonthInfo = result.UserMonthInfos.FirstOrDefault(x => x.Year == schedul.Training.DateStart.Year && x.Month == schedul.Training.DateStart.Month); + var userMonthInfo = result.UserMonthInfos.FirstOrDefault(x => x.Year == schedule.Training.DateStart.Year && x.Month == schedule.Training.DateStart.Month); if (userMonthInfo is null) { userMonthInfo = new UserMonthInfo { - Year = schedul.Training.DateStart.Year, - Month = schedul.Training.DateStart.Month, + Year = schedule.Training.DateStart.Year, + Month = schedule.Training.DateStart.Month, All = 1, - Valid = schedul.Training.CountToTrainingTarget ? 1 : 0, + Valid = schedule.Training.CountToTrainingTarget ? 1 : 0, }; result.UserMonthInfos.Add(userMonthInfo); } else { userMonthInfo.All += 1; - if (schedul.Training.CountToTrainingTarget) + if (schedule.Training.CountToTrainingTarget) userMonthInfo.Valid += 1; } } diff --git a/src/Shared/Enums/OrderAscDesc.cs b/src/Shared/Enums/OrderAscDesc.cs new file mode 100644 index 00000000..3e32a61b --- /dev/null +++ b/src/Shared/Enums/OrderAscDesc.cs @@ -0,0 +1,7 @@ +namespace Drogecode.Knrm.Oefenrooster.Shared.Enums; + +public enum OrderAscDesc +{ + Asc = 0, + Desc = 1, +} \ No newline at end of file