Beispiel #1
0
        public async Task <bool> UpdateVolunteerAvailability(IFormCollection formData, VolunteerProfile vol, FoodBankContext _context)
        {
            await _context.Entry(vol).Collection(p => p.Availabilities).LoadAsync();

            List <Availability> oldAvailabilities = await _context.Availabilities.Where(a => a.Volunteer.Id == vol.Id).ToListAsync();

            _context.Availabilities.RemoveRange(oldAvailabilities);

            await _context.SaveChangesAsync();

            List <Availability> newAvailabilites = GetAvailabilitiesFromFormData(formData, vol);

            if (newAvailabilites != null)
            {
                await _context.Availabilities.AddRangeAsync(newAvailabilites);

                await _context.SaveChangesAsync();

                return(true);
            }
            else
            {
                await _context.Availabilities.AddRangeAsync(oldAvailabilities);

                await _context.SaveChangesAsync();

                return(false);
            }
        }
Beispiel #2
0
        public async Task <IActionResult> PutFoodItem([FromRoute] int id, [FromBody] FoodItem foodItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != foodItem.Id)
            {
                return(BadRequest());
            }

            _context.Entry(foodItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FoodItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task SendNotifications()
        {
            // if there are no shifts in db, redirect immediately
            if (!_context.Shifts.Any())
            {
                //return RedirectToPage();
            }

            // get all volunteers
            List <AppUser> volunteersUserAccounts = await _context.Users.Where(u => u.VolunteerProfile != null).Include(x => x.VolunteerProfile).ThenInclude(x => x.Availabilities).ToListAsync();

            // for each volunteer, prepare a list of shifts that agrees with their availability
            foreach (var volunteer in volunteersUserAccounts)
            {
                // if this volunteer has no availabilities, skip them
                if (!volunteer.VolunteerProfile.Availabilities.Any())
                {
                    continue;
                }

                await _context.Entry(volunteer.VolunteerProfile).Collection(p => p.Availabilities).LoadAsync();

                List <Shift> workableShifts   = GetWorkablShiftsForVolunteer(volunteer.VolunteerProfile);
                bool         noWorkableShifts = !workableShifts.Any();

                // skip the volunteer if they can't work any shifts
                if (noWorkableShifts)
                {
                    continue;
                }

                string workableShiftsStr = "";

                foreach (var shift in workableShifts)
                {
                    workableShiftsStr += ">   " + shift.Position.Name + ", " + shift.StartTime.ToString("dddd, dd MMMM yyyy") + ", " + shift.StartTime.TimeOfDay + " until " + shift.EndTime.TimeOfDay + "<br/>";
                }

                await _emailSender.SendEmailAsync(volunteer.Email, "Available open shifts - MHFB", $"Hello { volunteer.VolunteerProfile.FirstName} { volunteer.VolunteerProfile.LastName},<br/><br/>"
                                                  + $"According to your availability, you can volunteer for some currently open shifts:<br/><br/>" +
                                                  workableShiftsStr + "</br></br>If you can attend any of these shifts, sign up for them on your <a href='https://volunteer.mhfoodbank.com'>online account</a> at or <a href='mailto:[email protected]'>email</a> us at [email protected].<br/><br/>" +
                                                  "Thanks again for volunteering at the Medicine Hat Food Bank.");
            }
        }
        public async Task <IActionResult> OnPostAsync(IFormCollection formData)
        {
            Positions = _context.Positions.ToList();
            // if we want to add external logins later on
            // ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            if (!ModelState.IsValid)
            {
                return(OnGet());
            }

            Volunteer.WorkExperiences = WorkExperiences;
            Volunteer.References      = References;
            Volunteer.ApprovalStatus  = ApprovalStatus.Pending;

            var domainVolunteerProfile = _mapper.Map <VolunteerProfile>(Volunteer);

            AppUser user = new AppUser
            {
                UserName         = Volunteer.Email,
                Email            = Volunteer.Email,
                VolunteerProfile = domainVolunteerProfile
            };
            var availHandler = new AvailabilityHandler();

            user.VolunteerProfile.Availabilities = availHandler.GetAvailabilitiesFromFormData(formData, user.VolunteerProfile);
            user.VolunteerProfile.Alerts         = new List <Alert>()
            {
                new ApplicationAlert {
                    Date = DateTime.Now, Volunteer = user.VolunteerProfile, Read = false
                }
            };
            user.VolunteerProfile.Positions = AssignPreferredPositions(user.VolunteerProfile);

            IdentityResult accountCreationResult = null;
            IdentityResult addToRoleResult       = null;

            if (user.VolunteerProfile.Availabilities != null)
            {
                accountCreationResult = await _userManager.CreateAsync(user, Volunteer.Password);

                addToRoleResult = await _userManager.AddToRoleAsync(user, "Volunteer");

                if (accountCreationResult.Succeeded && addToRoleResult.Succeeded)
                {
                    await _context.Entry(user.VolunteerProfile).Collection(p => p.Alerts).LoadAsync();

                    _logger.LogInformation("User created a new account with password.");
                    // we should also figure out if we even need account confirmation

                    try
                    {
                        await TrySendConfirmationEmail(user);

                        return(RedirectToPage("/Account/Login", new { registeredUserId = user.Id, statusMessage = "Thank you for applying to the Medicine Hat Food Bank! You will receive a confirmation email to confirm your account. Check your spam/junk folder if the email is not in your inbox." }));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError(String.Empty, "Email failed to send.");
                        _logger.LogError(ex.Message);
                    }
                }
                else
                {
                    if (accountCreationResult != null && !accountCreationResult.Succeeded)
                    {
                        if (_context.Users.Any(e => e.Email == Volunteer.Email))
                        {
                            ModelState.AddModelError("EmailError", "This email is already in use.");
                        }

                        AddIdentityErrors(accountCreationResult);
                    }
                    if (addToRoleResult != null && !addToRoleResult.Succeeded)
                    {
                        AddIdentityErrors(addToRoleResult);
                    }
                }
            }
            else
            {
                if (user.VolunteerProfile.Availabilities == null)
                {
                    ModelState.AddModelError("AvailabilityError", "One or more of the availability ranges are invalid.");
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }