Exemplo n.º 1
0
        // GET: Household
        public ActionResult Index()
        {
            var householdId = User.Identity.GetHouseholdId();
            var model       = new HouseholdIndexViewModel();

            if (householdId != null)
            {
                var household = _manager.Get(householdId.Value);
                model.Name    = household.Name;
                model.Members = household.Users.Select(s => s.FullName).ToList();
            }
            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult Index()
        {
            // Use this for details since there will be no reason to view all households
            Household household        = db.Households.Find(Convert.ToInt32(User.Identity.GetHouseholdId()));
            HouseholdIndexViewModel vm = new HouseholdIndexViewModel();

            vm.Id            = household.Id;
            vm.Name          = household.Name;
            vm.Members       = household.Members;
            vm.JoinRequests  = household.JoinRequests;
            vm.ExistingUsers = db.Users.ToList().Where(u => !household.Members.Contains(u)).Select(u => u.FullName).ToArray();

            return(View(vm));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Invite([Bind(Include = "Invitees")] HouseholdIndexViewModel vm)
        {
            Household       household     = db.Households.Find(Convert.ToInt32(User.Identity.GetHouseholdId()));
            string          currentUserId = User.Identity.GetUserId();
            ApplicationUser currentUser   = db.Users.Find(currentUserId);

            if (ModelState.IsValid)
            {
                if (vm.Invitees != null)
                {
                    // Send Invites
                    foreach (string invitee in vm.Invitees)
                    {
                        bool userIsNotAlreadyHouseholdMember = household.Members.ToList().FirstOrDefault(m => m.Email == invitee || m.FullName == invitee) == null; // this will do for now but need to figure out way of solving 2 same names, either make names be unique (not optimal) or use email as value always

                        if (userIsNotAlreadyHouseholdMember)
                        {
                            bool userIsRegistered = db.Users.ToList().FirstOrDefault(u => u.FullName == invitee) != null;

                            if (userIsRegistered)
                            {
                                string     email      = db.Users.ToList().FirstOrDefault(u => u.FullName == invitee && u.Email != currentUser.Email).Email;
                                Invitation invitation = new Invitation();
                                invitation.HouseholdId = household.Id;
                                invitation.UserEmail   = email;
                                db.Invitations.Add(invitation);
                                db.SaveChanges();

                                //email notification
                                var callbackUrl = Url.Action("JoinAccept", "Households", new { householdId = household.Id }, protocol: Request.Url.Scheme);
                                try
                                {
                                    var from    = "ReFund Budgeter<*****@*****.**>";
                                    var to      = email;
                                    var message = new MailMessage(from, to)
                                    {
                                        Subject    = "You've Been Invited!",
                                        Body       = $"You were invited to join household \"<strong>{household.Name}</strong>\" by <strong>{currentUser.FirstName} {currentUser.LastName}</strong>. Click <a href='{callbackUrl}'>here</a> to join.",
                                        IsBodyHtml = true
                                    };
                                    var svc = new PersonalEmail();
                                    await svc.SendAsync(message);

                                    ViewBag.Message = "Email has been sent";
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.Message);
                                    await Task.FromResult(0);
                                }
                            }
                            else
                            {
                                // If use is not registered then they should have provided an email
                                ValidEmailChecker VEC = new ValidEmailChecker();
                                bool validEmail       = VEC.IsValidEmail(invitee);

                                if (validEmail)
                                {
                                    Invitation invitation = new Invitation();
                                    invitation.HouseholdId = household.Id;
                                    invitation.UserEmail   = invitee;
                                    db.Invitations.Add(invitation);
                                    db.SaveChanges();

                                    bool emailBelongsToExistingUser = db.Users.Select(u => u.Email).Contains(invitee);
                                    if (emailBelongsToExistingUser)
                                    {
                                        //email notification
                                        var callbackUrl = Url.Action("JoinAccept", "Households", new { householdId = household.Id }, protocol: Request.Url.Scheme);
                                        try
                                        {
                                            var from    = "ReFund Budgeter<*****@*****.**>";
                                            var to      = invitee;
                                            var message = new MailMessage(from, to)
                                            {
                                                Subject    = "You've Been Invited!",
                                                Body       = $"You were invited to join household \"<strong>{household.Name}</strong>\" by <strong>{currentUser.FirstName} {currentUser.LastName}</strong>. Click <a href='{callbackUrl}'>here</a> to join.",
                                                IsBodyHtml = true
                                            };
                                            var svc = new PersonalEmail();
                                            await svc.SendAsync(message);

                                            ViewBag.Message = "Email has been sent";
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.Message);
                                            await Task.FromResult(0);
                                        }
                                    }
                                    else
                                    {
                                        //email notification
                                        var callbackUrl = Url.Action("Register", "Account", null, protocol: Request.Url.Scheme);
                                        try
                                        {
                                            var from    = "ReFund Budgeter<*****@*****.**>";
                                            var to      = invitee;
                                            var message = new MailMessage(from, to)
                                            {
                                                Subject    = "You've Been Invited!",
                                                Body       = $"You were invited to join household \"<strong>{household.Name}</strong>\" by <strong>{currentUser.FirstName} {currentUser.LastName}</strong>. If you would like to join, you must first register for Refund Budgeter. After that, you can choose the join option to become a part of this household. Click <a href='{callbackUrl}'>here</a> to get started.",
                                                IsBodyHtml = true
                                            };
                                            var svc = new PersonalEmail();
                                            await svc.SendAsync(message);

                                            ViewBag.Message = "Email has been sent";
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.Message);
                                            await Task.FromResult(0);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(RedirectToAction("Index"));
        }