예제 #1
0
        public async Task <ActionResult <List <ShiftDayResult> > > GetTodaysShifts()
        {
            var result = new List <ShiftDayResult>();
            var days   = await _shiftsService.GetShiftDaysForDayAsync(DateTime.UtcNow, DepartmentId);

            foreach (var shiftDay in days)
            {
                shiftDay.Shift = await _shiftsService.GetShiftByIdAsync(shiftDay.ShiftId);

                var dayResult = new ShiftDayResult();

                dayResult.ShiftDayId = shiftDay.ShiftDayId;
                dayResult.ShiftId    = shiftDay.ShiftId;
                dayResult.ShiftName  = shiftDay.Shift.Name;
                dayResult.ShitDay    = shiftDay.Day;
                dayResult.Start      = shiftDay.Start;
                dayResult.End        = shiftDay.End;
                dayResult.ShiftType  = shiftDay.Shift.AssignmentType;

                var needs = await _shiftsService.GetShiftDayNeedsAsync(shiftDay.ShiftDayId);

                var personnelRoles = await _personnelRolesService.GetAllRolesForUsersInDepartmentAsync(DepartmentId);

                var signups = await _shiftsService.GetShiftSignpsForShiftDayAsync(shiftDay.ShiftDayId);

                dayResult.SignedUp = await _shiftsService.IsUserSignedUpForShiftDayAsync(shiftDay, UserId);

                dayResult.Signups = new List <ShiftDaySignupResult>();
                foreach (var signup in signups)
                {
                    if (!signup.Denied)
                    {
                        var signupResult = new ShiftDaySignupResult();
                        signupResult.UserId = signup.UserId;
                        signupResult.Roles  = personnelRoles[signup.UserId].Select(x => x.PersonnelRoleId).ToList();

                        dayResult.Signups.Add(signupResult);
                    }
                }

                if (needs != null && needs.Any())
                {
                    dayResult.Needs = new List <ShiftDayGroupNeedsResult>();
                    foreach (var need in needs.Keys)
                    {
                        var dayNeed = new ShiftDayGroupNeedsResult();
                        dayNeed.GroupId = need;

                        dayNeed.GroupNeeds = new List <ShiftDayGroupRoleNeedsResult>();
                        foreach (var dNeed in needs[need])
                        {
                            var groupNeed = new ShiftDayGroupRoleNeedsResult();
                            groupNeed.RoleId = dNeed.Key;
                            groupNeed.Needed = dNeed.Value;

                            dayNeed.GroupNeeds.Add(groupNeed);
                        }

                        dayResult.Needs.Add(dayNeed);
                    }
                }

                result.Add(dayResult);
            }

            return(Ok(result));
        }