Skip to content

Commit

Permalink
🐛 Order trainings on dashboard wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
TDroogers committed Sep 16, 2024
1 parent 7bc92d4 commit b71230e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/Server/Controllers/ScheduleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public async Task<ActionResult<GetScheduledTrainingsForUserResponse>> 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");
Expand Down Expand Up @@ -388,7 +388,7 @@ public async Task<ActionResult<GetScheduledTrainingsForUserResponse>> 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)
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Services/Interfaces/IScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IScheduleService
Task<PatchAssignedUserResponse> PatchAssignedUserAsync(Guid userId, Guid customerId, PatchAssignedUserRequest body, CancellationToken clt);
Task<PatchTrainingResponse> PatchTraining(Guid customerId, PlannedTraining training, bool inRoleEditPast, CancellationToken clt);
Task<AddTrainingResponse> AddTrainingAsync(Guid customerId, PlannedTraining training, Guid trainingId, CancellationToken clt);
Task<GetScheduledTrainingsForUserResponse> GetScheduledTrainingsForUser(Guid userId, Guid customerId, DateTime? fromDate, int take, int skip, CancellationToken clt);
Task<GetScheduledTrainingsForUserResponse> GetScheduledTrainingsForUser(Guid userId, Guid customerId, DateTime? fromDate, int take, int skip, OrderAscDesc order, CancellationToken clt);
Task<GetPinnedTrainingsForUserResponse> GetPinnedTrainingsForUser(Guid userId, Guid customerId, DateTime fromDate, CancellationToken clt);
Task<PutAssignedUserResponse> PutAssignedUserAsync(Guid userId, Guid customerId, OtherScheduleUserRequest body, CancellationToken clt);
Task<bool> PatchEventIdForUserAvailible(Guid userId, Guid customerId, Guid? availableId, string? calendarEventId, CancellationToken clt);
Expand Down
51 changes: 28 additions & 23 deletions src/Server/Services/ScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ public async Task<PutAssignedUserResponse> PutAssignedUserAsync(Guid userId, Gui
return result;
}

public async Task<GetScheduledTrainingsForUserResponse> GetScheduledTrainingsForUser(Guid userId, Guid customerId, DateTime? fromDate, int take, int skip, CancellationToken clt)
public async Task<GetScheduledTrainingsForUserResponse> GetScheduledTrainingsForUser(Guid userId, Guid customerId, DateTime? fromDate, int take, int skip, OrderAscDesc order, CancellationToken clt)
{
var sw = Stopwatch.StartNew();
var result = new GetScheduledTrainingsForUserResponse();
Expand All @@ -912,30 +912,35 @@ public async Task<GetScheduledTrainingsForUserResponse> 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,
Expand All @@ -956,27 +961,27 @@ public async Task<GetScheduledTrainingsForUserResponse> 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;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Shared/Enums/OrderAscDesc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Drogecode.Knrm.Oefenrooster.Shared.Enums;

public enum OrderAscDesc
{
Asc = 0,
Desc = 1,
}

0 comments on commit b71230e

Please sign in to comment.