public async Task <List <ShiftAvailability> > GetShiftAvailability(int locationId, DateTimeOffset start, DateTimeOffset end) { var sheriffs = await SheriffService.GetSheriffsForShiftAvailability(locationId, start, end); var shiftsForSheriffs = await GetShiftsForSheriffs(sheriffs.Select(s => s.Id), start, end); var sheriffEventConflicts = new List <ShiftAvailabilityConflict>(); sheriffs.ForEach(sheriff => { sheriffEventConflicts.AddRange(sheriff.AwayLocation.Select(s => new ShiftAvailabilityConflict { Conflict = ShiftConflictType.AwayLocation, SheriffId = sheriff.Id, Start = s.StartDate, End = s.EndDate, LocationId = s.LocationId, Location = s.Location })); sheriffEventConflicts.AddRange(sheriff.Leave.Select(s => new ShiftAvailabilityConflict { Conflict = ShiftConflictType.Leave, SheriffId = sheriff.Id, Start = s.StartDate, End = s.EndDate })); sheriffEventConflicts.AddRange(sheriff.Training.Select(s => new ShiftAvailabilityConflict { Conflict = ShiftConflictType.Training, SheriffId = sheriff.Id, Start = s.StartDate, End = s.EndDate })); }); var existingShiftConflicts = shiftsForSheriffs.Select(s => new ShiftAvailabilityConflict { Conflict = ShiftConflictType.Scheduled, SheriffId = s.SheriffId, Location = s.Location, LocationId = s.LocationId, Start = s.StartDate, End = s.EndDate, ShiftId = s.Id }); var allShiftConflicts = sheriffEventConflicts.Concat(existingShiftConflicts).ToList(); return(sheriffs.SelectToList(s => new ShiftAvailability { Start = start, End = end, Sheriff = s, SheriffId = s.Id, Conflicts = allShiftConflicts.WhereToList(asc => asc.SheriffId == s.Id) }) .OrderBy(s => s.Sheriff.LastName) .ThenBy(s => s.Sheriff.FirstName) .ToList()); }
private async Task <List <ShiftConflict> > CheckSheriffEventsOverlap(List <Shift> shifts) { var sheriffEventConflicts = new List <ShiftConflict>(); foreach (var shift in shifts) { var locationId = shift.LocationId; var sheriffs = await SheriffService.GetSheriffsForShiftAvailability(locationId, shift.StartDate, shift.EndDate, shift.SheriffId); var sheriff = sheriffs.FirstOrDefault(); sheriff.ThrowBusinessExceptionIfNull($"Couldn't find active {nameof(Sheriff)}:{shift.SheriffId}, they might not be active in location for the shift."); var validationErrors = new List <string>(); validationErrors.AddRange(sheriff !.AwayLocation.Where(aw => aw.LocationId != shift.LocationId).Select(aw => PrintSheriffEventConflict <SheriffAwayLocation>(aw.Sheriff, aw.StartDate, aw.EndDate, aw.Timezone))); validationErrors.AddRange(sheriff.Leave.Select(aw => PrintSheriffEventConflict <SheriffLeave>(aw.Sheriff, aw.StartDate, aw.EndDate, aw.Timezone))); validationErrors.AddRange(sheriff.Training.Select(aw => PrintSheriffEventConflict <SheriffTraining>(aw.Sheriff, aw.StartDate, aw.EndDate, aw.Timezone))); if (validationErrors.Any()) { sheriffEventConflicts.Add(new ShiftConflict { Shift = shift, ConflictMessages = validationErrors }); } } return(sheriffEventConflicts); }
public async Task <ImportedShifts> ImportWeeklyShifts(int locationId, DateTimeOffset start) { var location = Db.Location.FirstOrDefault(l => l.Id == locationId); location.ThrowBusinessExceptionIfNull($"Couldn't find {nameof(Location)} with id: {locationId}."); var timezone = location?.Timezone; timezone.GetTimezone().ThrowBusinessExceptionIfNull("Timezone was invalid."); //We need to adjust to their start of the week, because it can differ depending on the TZ! var targetStartDate = start.ConvertToTimezone(timezone); var targetEndDate = targetStartDate.TranslateDateIfDaylightSavings(timezone, 7); var sheriffsAvailableAtLocation = await SheriffService.GetSheriffsForShiftAvailability(locationId, targetStartDate, targetEndDate); var sheriffIds = sheriffsAvailableAtLocation.SelectDistinctToList(s => s.Id); var shiftsToImport = Db.Shift .Include(s => s.Location) .Include(s => s.Sheriff) .AsNoTracking() .Where(s => s.LocationId == locationId && s.ExpiryDate == null && s.StartDate < targetEndDate && targetStartDate < s.EndDate && s.SheriffId != null && sheriffIds.Contains(s.SheriffId.Value)); var importedShifts = await shiftsToImport.Select(shift => Db.DetachedClone(shift)).ToListAsync(); foreach (var shift in importedShifts) { shift.SheriffId = shift.SheriffId; shift.StartDate = shift.StartDate.TranslateDateIfDaylightSavings(timezone, 7); shift.EndDate = shift.EndDate.TranslateDateIfDaylightSavings(timezone, 7); } var overlaps = await GetShiftConflicts(importedShifts); var filteredImportedShifts = importedShifts.WhereToList(s => overlaps.All(o => o.Shift.Id != s.Id) && !overlaps.Any(ts => s.Id != ts.Shift.Id && ts.Shift.StartDate < s.EndDate && s.StartDate < ts.Shift.EndDate && ts.Shift.SheriffId == s.SheriffId)); filteredImportedShifts.ForEach(s => s.Id = 0); await Db.Shift.AddRangeAsync(filteredImportedShifts); await Db.SaveChangesAsync(); return(new ImportedShifts { ConflictMessages = overlaps.SelectMany(o => o.ConflictMessages).ToList(), Shifts = filteredImportedShifts }); }