Пример #1
0
        public async Task <List <Shift> > UpdateShifts(DutyRosterService dutyRosterService, List <Shift> shifts)
        {
            var overlaps = await GetShiftConflicts(shifts);

            if (overlaps.Any())
            {
                throw new BusinessLayerException(overlaps.SelectMany(ol => ol.ConflictMessages).ToStringWithPipes());
            }

            var shiftIds    = shifts.SelectToList(s => s.Id);
            var savedShifts = Db.Shift.In(shiftIds, s => s.Id);

            if (shifts.Any(s => s.StartDate >= s.EndDate))
            {
                throw new BusinessLayerException($"{nameof(Shift)} Start date cannot come after end date.");
            }

            if (shifts.Any(s => s.Timezone.GetTimezone() == null))
            {
                throw new BusinessLayerException($"A valid {nameof(Shift.Timezone)} needs to be included in the {nameof(Shift)}.");
            }

            shifts = SplitLongShifts(shifts);

            foreach (var shift in shifts)
            {
                //Need to add shifts, because some of the shifts were split.
                if (shift.Id == 0)
                {
                    await AddShift(shift);

                    continue;
                }

                var savedShift = savedShifts.FirstOrDefault(s => s.Id == shift.Id);
                savedShift.ThrowBusinessExceptionIfNull($"{nameof(Shift)} with the id: {shift.Id} could not be found.");
                Db.Entry(savedShift !).CurrentValues.SetValues(shift);
                Db.Entry(savedShift).Property(x => x.LocationId).IsModified = false;
                Db.Entry(savedShift).Property(x => x.ExpiryDate).IsModified = false;

                savedShift.Sheriff = await Db.Sheriff.FindAsync(shift.SheriffId);

                savedShift.AnticipatedAssignment = await Db.Assignment.FindAsync(shift.AnticipatedAssignmentId);
            }
            await Db.SaveChangesAsync();

            await CalculateOvertimeHoursForShifts(shifts);

            await dutyRosterService.AdjustDutySlots(shifts);

            return(await savedShifts.ToListAsync());
        }
Пример #2
0
        public async Task HandleShiftOverrides <T>(T data, DutyRosterService dutyRosterService, List <Shift> shiftConflicts) where T : SheriffEvent
        {
            var adjustShifts   = new List <Shift>();
            var expireShiftIds = new List <int>();

            foreach (var shift in shiftConflicts)
            {
                if (data.StartDate <= shift.StartDate && data.EndDate >= shift.EndDate)
                {
                    expireShiftIds.Add(shift.Id);
                }
                else
                {
                    if (shift.StartDate < data.StartDate && shift.EndDate > data.EndDate)
                    {
                        var newShift = shift.Adapt <Shift>();
                        newShift.Id        = 0;
                        newShift.StartDate = data.EndDate.ConvertToTimezone(data.Timezone);
                        shift.EndDate      = data.StartDate.ConvertToTimezone(data.Timezone);
                        adjustShifts.Add(newShift);
                        adjustShifts.Add(shift);
                    }
                    else if (shift.EndDate > data.EndDate)
                    {
                        shift.StartDate = data.EndDate.ConvertToTimezone(data.Timezone);
                        adjustShifts.Add(shift);
                    }
                    else if (shift.StartDate < data.StartDate)
                    {
                        shift.EndDate = data.StartDate.ConvertToTimezone(data.Timezone);
                        adjustShifts.Add(shift);
                    }
                }
            }

            if (expireShiftIds.Count > 0)
            {
                await ExpireShiftsAndDutySlots(expireShiftIds);
            }
            if (adjustShifts.Count > 0)
            {
                await UpdateShifts(dutyRosterService, adjustShifts);
            }

            await Db.SaveChangesAsync();
        }