示例#1
0
        public async Task <IActionResult> Details(DateTime date)
        {
            List <EventModel>        events     = null;
            List <GroupModel>        groups     = null;
            List <VolunteerModel>    volunteers = null;
            List <VolunteerModel>    absences   = null;
            List <VolunteerJobModel> jobs       = null;
            AttendanceModel          attendance;
            DateTime            dateTime      = date.Date;
            CalendarRepository  calendarRepo  = new CalendarRepository(configModel.ConnectionString);
            VolunteerRepository volunteerRepo = new VolunteerRepository(configModel.ConnectionString);
            bool staff;
            bool busDriver;
            bool volunteer;
            bool scheduled  = false;
            bool isSaturday = (dateTime.DayOfWeek == DayOfWeek.Saturday);
            var  user       = await userManager.GetUserAsync(User);

            // Check if the user is in the volunteer role, as all other roles are considered to be present unless specified otherwise
            volunteer = (user != null && User.IsInRole(UserHelpers.UserRoles.Volunteer.ToString()));

            // Check if the user is in the staff or bus driver roles, as those must be treated differently
            staff     = (user != null && User.IsInRole(UserHelpers.UserRoles.Staff.ToString()));
            busDriver = (user != null && User.IsInRole(UserHelpers.UserRoles.Staff.ToString()));

            // All roles except volunteers are considered scheduled by default
            if (user != null && !volunteer)
            {
                scheduled = true;
            }

            // Get the events occurring on this date, if any.  We only want to get the list of attendees if the user is a staff user
            events = calendarRepo.GetEvents(dateTime, staff);
            // If it isn't a saturday, events are all we need to check.
            if (!isSaturday)
            {
                return(new JsonResult(new
                {
                    Error = "",
                    Events = events,
                    Groups = groups,
                    People = volunteers,
                    Scheduled = false,
                    Jobs = jobs
                }));
            }

            // Get any groups volunteering on this date.  Again, we only get details if the user is staff.
            groups = calendarRepo.GetGroups(dateTime, staff);

            //  If the user is not logged in, we have all of the information already and can simply return.
            if (user == null)
            {
                return(new JsonResult(new
                {
                    Error = "",
                    Events = events,
                    Groups = groups,
                    People = volunteers,
                    Scheduled = scheduled,
                    Jobs = jobs
                }));
            }

            if (volunteerRepo.AreVolunteerJobsEnabled())
            {
                try
                {
                    jobs = volunteerRepo.GetVolunteerJobs(dateTime, staff);
                }
                catch (Exception e)
                {
                    return(Utilities.ErrorJson(e.Message));
                }
            }
            else
            {
                jobs = null;
            }

            // If the user is a volunteer, check if they are scheduled on this day.  Bus drivers, volunteer captains, and staff are considered scheduled by default
            if (volunteer)
            {
                attendance = calendarRepo.GetSingleAttendance(user.VolunteerId, dateTime);
                if (attendance != null && attendance.Scheduled)
                {
                    scheduled = true;
                }
                else
                {
                    scheduled = false;
                }
            }
            else
            {
                attendance = calendarRepo.GetSingleAttendance(user.VolunteerId, dateTime);
                if (attendance != null && !attendance.Scheduled)
                {
                    scheduled = false;
                }
                else
                {
                    scheduled = true;
                }
            }

            // If the user is not staff, we now want to return
            if (!staff)
            {
                return(new JsonResult(new
                {
                    Error = "",
                    Events = events,
                    Groups = groups,
                    People = volunteers,
                    Scheduled = scheduled,
                    Jobs = jobs
                }));
            }

            // If the user is staff, get the list of volunteers who will be attending and people who have said they will not be attending, and the jobs that have people signed up for them
            try
            {
                volunteers = volunteerRepo.GetScheduledVolunteers(dateTime);
                absences   = volunteerRepo.GetAbsences(dateTime);
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(e.Message));
            }

            return(new JsonResult(new
            {
                Error = "",
                Events = events,
                Groups = groups,
                People = volunteers,
                Absences = absences,
                Scheduled = scheduled,
                Jobs = jobs
            }));
        }