Esempio n. 1
0
        public async Task PopulateShiftWorkItemOccurences(RecurringWorkItemDto recurringWorkItem, DateTime now, int userId, Models.Patrol patrol, bool populateWorkitemAssignments = true)
        {
            var shifts = await _workItemRepository.GetShiftRecurringWorkItems(recurringWorkItem.Id);

            var workItems = await _workItemRepository.GetWorkItems(recurringWorkItem.Id, now);

            var allScheduledShiftAssignments = new List <ScheduledShiftAssignmentDto>();

            foreach (var shift in shifts)
            {
                var scheduledShiftAssignments = await _shiftRepository.GetScheduledShiftAssignments(recurringWorkItem.PatrolId, from : now, shiftId : shift.ShiftId);

                allScheduledShiftAssignments.AddRange(scheduledShiftAssignments);

                var scheduledShifts = scheduledShiftAssignments.GroupBy(x => x.ScheduledShiftId);

                foreach (var scheduledShift in scheduledShifts)
                {
                    var shiftWorkItems  = workItems.Where(x => x.ScheduledShiftId == scheduledShift.Key);
                    var shiftStartLocal = scheduledShift.First().StartsAt.UtcToPatrolLocal(patrol);

                    if (!shiftWorkItems.Any())
                    {
                        //create the work item
                        var workItem = new WorkItem()
                        {
                            AdminGroupId      = recurringWorkItem.AdminGroupId,
                            CreatedAt         = now,
                            CreatedByUserId   = userId,
                            CompletionMode    = recurringWorkItem.CompletionMode,
                            DescriptionMarkup = recurringWorkItem.DescriptionMarkup,
                            Location          = recurringWorkItem.Location,
                            Name                = recurringWorkItem.Name,
                            PatrolId            = recurringWorkItem.PatrolId,
                            RecurringWorkItemId = recurringWorkItem.Id,
                            ScheduledShiftId    = scheduledShift.Key,
                            ScheduledAt         = new DateTime(shiftStartLocal.Year, shiftStartLocal.Month, shiftStartLocal.Day, shift.ScheduledAtHour, shift.ScheduledAtMinute, 0).UtcFromPatrolLocal(patrol)
                        };
                        await _workItemRepository.InsertWorkItem(workItem);
                    }
                    else
                    {
                        //realistically should only ever be 1
                        foreach (var workItem in shiftWorkItems)
                        {
                            if (!workItem.CompletedAt.HasValue && !workItem.CanceledAt.HasValue)
                            {
                                workItem.CompletionMode    = recurringWorkItem.CompletionMode;
                                workItem.DescriptionMarkup = recurringWorkItem.DescriptionMarkup;
                                workItem.Location          = recurringWorkItem.Location;
                                workItem.Name         = recurringWorkItem.Name;
                                workItem.ScheduledAt  = new DateTime(shiftStartLocal.Year, shiftStartLocal.Month, shiftStartLocal.Day, shift.ScheduledAtHour, shift.ScheduledAtMinute, 0).UtcFromPatrolLocal(patrol);
                                workItem.AdminGroupId = recurringWorkItem.AdminGroupId;
                                await _workItemRepository.UpdateWorkItem(workItem);
                            }
                        }
                    }
                }
            }

            if (populateWorkitemAssignments)
            {
                await RecalculateShiftWorkItemAssignments(allScheduledShiftAssignments);
            }
        }