public async Task <ActionResult> CreatePassword(CreatePassword model)
        {
            if (ModelState.IsValid)
            {
                //Create new User and Account
                var user = new CommonUser();
                user.Email      = model.InvitedEmail;
                user.FirstName  = model.FirstName;
                user.LastName   = model.LastName;
                user.Permission = model.SystemPermission;
                user.SetPassword(model.Password);


                //Check Account
                var commonAccount = await CommonContext.CommonAccounts.Where(m => m.Id == model.AccountId).SingleOrDefaultAsync();

                var vendorDetail = await _accountCtx.Vendors.Where(x => x.Id == model.VendorId).SingleOrDefaultAsync();


                if (commonAccount == null)
                {
                    ViewBag.SucessMessage = "Account not found";
                    return(View(model));
                }

                CommonUserAccount userAccount = new CommonUserAccount();
                userAccount.CreateUserId = commonAccount.OwnerUserId.Value;
                userAccount.AccountId    = commonAccount.Id;
                userAccount.UpdateUserId = commonAccount.OwnerUserId.Value;
                userAccount.UserId       = user.Id;
                userAccount.Permissions  = (AccountPermissions)model.Permission;

                if (vendorDetail != null)
                {
                    user.Permission = SystemPermissions.Vendor;
                }

                using (var tx = CommonContext.Database.BeginTransaction())
                {
                    CommonContext.Users.Add(user);
                    await CommonContext.SaveChangesAsync();

                    CommonContext.UserAccounts.Add(userAccount);
                    await CommonContext.SaveChangesAsync();

                    tx.Commit();
                }

                //Create AccountUser
                var accountUser = new AccountUser
                {
                    Id        = userAccount.UserId.Value,
                    FirstName = user.FirstName,
                    LastName  = user.LastName,
                    Email     = user.Email
                };

                _accountCtx.AccountUsers.Add(accountUser);

                if (vendorDetail != null)
                {
                    AccountUserVendor accountUserVendor = new AccountUserVendor();
                    accountUserVendor.VendorId      = vendorDetail.Id;
                    accountUserVendor.AccountUserId = accountUser.Id;

                    _accountCtx.AccountUserVendors.Add(accountUserVendor);
                }

                await _accountCtx.SaveChangesAsync();

                await SignInUser(user, false);

                return(RedirectToAction("Index", "Accounts"));
            }
            return(View(model));
        }
        public async Task <ActionResult> AcceptInvite(AcceptInvite model)
        {
            //Check to see if this user exists in Users
            var existingUser = await CommonContext.Users.Where(m => m.Email == model.InvitedEmail).AnyAsync();

            if (existingUser)
            {
                //Check to see if this user already belongs to the account
                //Users can belong to multiple accounts, we need to filter by accountID and InvitedEmail
                var existingAccountUser = await CommonContext.UserAccounts.Include(x => x.User).Where(m => m.User.Email == model.InvitedEmail && m.AccountId == model.AccountId).AnyAsync();

                if (!existingAccountUser)
                {
                    //Get exists User Detail
                    var user = CommonContext.Users.Where(m => m.Email == model.InvitedEmail).SingleOrDefault();

                    //Get Account detail using accountId
                    var commonAccount = await CommonContext.CommonAccounts.Where(m => m.Id == model.AccountId).SingleOrDefaultAsync();

                    var vendorDetail = await _accountCtx.Vendors.Where(x => x.Id == model.VendorId).SingleOrDefaultAsync();

                    if (commonAccount == null)
                    {
                        ViewBag.SucessMessage = "Account not found";
                        return(View(model));
                    }

                    CommonUserAccount userAccount = new CommonUserAccount();
                    userAccount.CreateUserId = commonAccount.OwnerUserId.Value;
                    userAccount.AccountId    = commonAccount.Id;
                    userAccount.UpdateUserId = commonAccount.OwnerUserId.Value;
                    userAccount.UserId       = user.Id;
                    userAccount.Permissions  = (AccountPermissions)model.Permission;

                    using (var tx = CommonContext.Database.BeginTransaction())
                    {
                        CommonContext.UserAccounts.Add(userAccount);
                        await CommonContext.SaveChangesAsync();

                        tx.Commit();
                    }

                    var accountUser = await _accountCtx.AccountUsers.Where(m => m.Id == user.Id).SingleOrDefaultAsync();

                    if (accountUser == null)
                    {
                        //Create AccountUser
                        accountUser = new AccountUser()
                        {
                            Id        = user.Id,
                            FirstName = user.FirstName,
                            LastName  = user.LastName,
                            Email     = user.Email,
                        };
                        _accountCtx.AccountUsers.Add(accountUser);
                        await _accountCtx.SaveChangesAsync();
                    }

                    if (vendorDetail != null)
                    {
                        AccountUserVendor accountUserVendor = new AccountUserVendor();
                        accountUserVendor.VendorId      = vendorDetail.Id;
                        accountUserVendor.AccountUserId = accountUser.Id;

                        user.Permission = SystemPermissions.Vendor;
                        CommonContext.SaveChanges();

                        using (var tx = _accountCtx.Database.BeginTransaction())
                        {
                            _accountCtx.AccountUserVendors.Add(accountUserVendor);
                            await _accountCtx.SaveChangesAsync();

                            tx.Commit();
                        }
                    }

                    return(RedirectToAction("Index", "Accounts"));
                }
                else
                {
                    ViewBag.SucessMessage = " Already Account user.";
                    return(View(model));
                }
            }
            else
            {
                CreatePassword pwdmodel = new Models.User.CreatePassword();
                pwdmodel.AccountId    = model.AccountId;
                pwdmodel.AccountName  = model.AccountName;
                pwdmodel.InvitedEmail = model.InvitedEmail;
                string[] name = model.Name.Split(' ');
                pwdmodel.FirstName        = name[0];
                pwdmodel.LastName         = name[1];
                pwdmodel.Permission       = model.Permission;
                pwdmodel.VendorId         = model.VendorId;
                pwdmodel.SystemPermission = model.SystemPermission;
                return(View("CreatePassword", pwdmodel));
            }
        }