Ejemplo n.º 1
0
        public async Task <ActionResult> UserInfo()
        {
            VolunteerRepository repo         = new VolunteerRepository(configModel.ConnectionString);
            CalendarRepository  calendarRepo = new CalendarRepository(configModel.ConnectionString);
            ClassRepository     classRepo    = new ClassRepository(configModel.ConnectionString);
            BusRepository       busRepo      = new BusRepository(configModel.ConnectionString);
            AttendanceModel     attendance;
            VolunteerModel      profile;
            var user = await userManager.GetUserAsync(User);

            bool checkedIn            = false;
            List <ClassModel> classes = new List <ClassModel>();
            List <BusModel>   buses   = new List <BusModel>();

            // Get the current user's profile
            try
            {
                profile = repo.GetVolunteer(user.VolunteerId);
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(e.Message));
            }

            if (profile == null)
            {
                return(Utilities.ErrorJson("Could not find user profile"));
            }

            if (configModel.DebugMode || DateTime.Now.DayOfWeek == DayOfWeek.Saturday)
            {
                try
                {
                    attendance = calendarRepo.GetSingleAttendance(profile.Id, DateTime.Now.Date);

                    // determine if the current user has been checked in today
                    if (attendance != null && attendance.Attended == true)
                    {
                        checkedIn = true;
                    }

                    // Get the classes the user teaches and buses the user drives
                    classes = classRepo.GetClasses(true).Where(c => c.TeacherId == profile.Id).ToList();
                    buses   = busRepo.GetBusList(true).Where(b => b.DriverId == profile.Id).ToList();
                }
                catch (Exception e)
                {
                    return(Utilities.ErrorJson(e.Message));
                }
            }

            return(new JsonResult(new
            {
                Error = "",
                Profile = profile,
                CheckedIn = checkedIn,
                Classes = classes,
                Buses = buses
            }));
        }
        public async Task <IActionResult> VolunteerInfo(int id)
        {
            var user = await userManager.GetUserAsync(User);

            VolunteerRepository repo         = new VolunteerRepository(configModel.ConnectionString);
            CalendarRepository  calendarRepo = new CalendarRepository(configModel.ConnectionString);
            VolunteerModel      volunteer;
            AttendanceModel     attendance;
            bool checkedIn = false;

            // Ensure that ONLY staff accounts have access to this API endpoint
            if (user == null || !await userManager.IsInRoleAsync(user, UserHelpers.UserRoles.Staff.ToString()))
            {
                return(Utilities.ErrorJson("Not authorized"));
            }

            if (id == 0)
            {
                return(Utilities.ErrorJson("Must include an id"));
            }

            try
            {
                volunteer = repo.GetVolunteer(id);
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(configModel.DebugMode ? e.Message : "An error occurred while accessing the database."));
            }

            if (volunteer == null)
            {
                return(Utilities.ErrorJson("Invalid id"));
            }

            try
            {
                attendance = calendarRepo.GetSingleAttendance(volunteer.Id, DateTime.Now.Date);

                // determine if the current user has been checked in today
                if (attendance != null && attendance.Attended == true)
                {
                    checkedIn = true;
                }
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(e.Message));
            }

            return(new JsonResult(new
            {
                Error = "",
                Volunteer = volunteer,
                CheckedIn = checkedIn
            }));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> SignupSingle(DateModel date)
        {
            var user = await userManager.GetUserAsync(User);

            CalendarRepository repo       = new CalendarRepository(configModel.ConnectionString);
            AttendanceModel    attendance = repo.GetSingleAttendance(user.VolunteerId, date.Date);

            if (date.Date.DayOfWeek != DayOfWeek.Saturday)
            {
                return(Utilities.ErrorJson("Provided date was not a Saturday"));
            }

            if (attendance != null && attendance.Scheduled)
            {
                return(Utilities.ErrorJson("You are already scheduled on this date"));
            }

            if (attendance == null)
            {
                try
                {
                    repo.InsertAttendance(new AttendanceModel
                    {
                        VolunteerId = user.VolunteerId,
                        DayAttended = date.Date,
                        Scheduled   = true,
                        Attended    = false,
                    });
                }
                catch (Exception e)
                {
                    return(Utilities.ErrorJson(e.Message));
                }
            }
            else
            {
                try
                {
                    repo.UpdateScheduled(attendance.Id, true);
                }
                catch (Exception e)
                {
                    return(Utilities.ErrorJson(e.Message));
                }
            }

            return(new JsonResult(new
            {
                Error = ""
            }));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> CancelSingle(DateModel date)
        {
            var user = await userManager.GetUserAsync(User);

            CalendarRepository  repo          = new CalendarRepository(configModel.ConnectionString);
            VolunteerRepository volunteerRepo = new VolunteerRepository(configModel.ConnectionString);
            AttendanceModel     attendance    = repo.GetSingleAttendance(user.VolunteerId, date.Date);
            VolunteerModel      profile;

            if (date.Date.DayOfWeek != DayOfWeek.Saturday)
            {
                return(Utilities.ErrorJson("Provided date was not a Saturday"));
            }

            if (User.IsInRole(UserHelpers.UserRoles.BusDriver.ToString()))
            {
                try
                {
                    profile = volunteerRepo.GetVolunteer(user.VolunteerId);
                    await EmailHelpers.SendEmail("*****@*****.**", "Bus Driver Cancellation",
                                                 $"A bus driver ({profile.FirstName} {profile.LastName}) has been marked as not attending on {date.Date.ToString("dd/MM/yyyy")}.  Please update bus assignments accordingly.",
                                                 configModel.EmailOptions);
                }
                catch (Exception e)
                {
                    return(Utilities.ErrorJson(e.Message));
                }
            }
            // If the user is a volunteer, has not previously scheduled themselves for this day, and is not a class teacher, no need to hit the DB,
            // Since volunteers are assumed to not be present until they indicate otherwise
            else if (User.IsInRole(UserHelpers.UserRoles.Volunteer.ToString()) && (attendance == null || !attendance.Scheduled) && !volunteerRepo.VolunteerIsClassTeacher(user.VolunteerId))
            {
                return(Utilities.ErrorJson("You are not currently scheduled on this date"));
            }

            try
            {
                if (attendance != null)
                {
                    repo.UpdateScheduled(attendance.Id, false);
                }
                else
                {
                    repo.InsertAttendance(new AttendanceModel
                    {
                        VolunteerId = user.VolunteerId,
                        DayAttended = date.Date,
                        Scheduled   = false,
                        Attended    = false,
                    });
                }
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(e.Message));
            }

            return(new JsonResult(new
            {
                Error = ""
            }));
        }
Ejemplo n.º 5
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
            }));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> CancelBusDriver(DriverCheckInModel model)
        {
            var user = await userManager.GetUserAsync(User);

            CheckInRepository   repo    = new CheckInRepository(configModel.ConnectionString);
            CalendarRepository  calRepo = new CalendarRepository(configModel.ConnectionString);
            AttendanceModel     attendance;
            VolunteerRepository volRepo = new VolunteerRepository(configModel.ConnectionString);
            VolunteerModel      profile;

            if (user == null ||
                !(await userManager.IsInRoleAsync(user, UserHelpers.UserRoles.Staff.ToString())))
            {
                return(Utilities.ErrorJson("Not authorized."));
            }

            if (model == null || model.Id == 0)
            {
                return(Utilities.GenerateMissingInputMessage("bus driver id"));
            }

            if (model.Date == DateTime.MinValue)
            {
                return(Utilities.GenerateMissingInputMessage("date"));
            }

            if (model.Date.DayOfWeek != DayOfWeek.Saturday)
            {
                return(Utilities.ErrorJson("Drivers can only be checked in for Saturdays"));
            }

            if (DateTime.Today.Date > model.Date)
            {
                return(Utilities.ErrorJson("Can not cancel check in in the past"));
            }

            try
            {
                profile = volRepo.GetVolunteer(model.Id);

                if (profile == null || profile.Role != UserHelpers.UserRoles.BusDriver.ToString())
                {
                    return(Utilities.ErrorJson("Not a valid bus driver"));
                }

                attendance = calRepo.GetSingleAttendance(model.Id, model.Date);

                if (attendance == null || !attendance.Attended)
                {
                    return(Utilities.ErrorJson("Not currently checked in"));
                }

                repo.CancelBusDriver(model.Id, model.Date);
            }
            catch (Exception e)
            {
                return(Utilities.ErrorJson(e.Message));
            }

            return(Utilities.NoErrorJson());
        }