Example #1
0
        public ActionResult <UserDto> EditAccounts([FromRoute] int userID, [FromBody] UserEditAcountsDto userEditAccountsDto)
        {
            var userDto = EFModels.Entities.User.GetByUserID(_dbContext, userID);

            if (userDto == null)
            {
                return(NotFound($"Could not find an User with the ID {userID}."));
            }

            if (!Account.ValidateAllExist(_dbContext, userEditAccountsDto.AccountIDs))
            {
                return(NotFound("One or more of the Account IDs was invalid."));
            }

            var addedAccountIDs = EFModels.Entities.User.SetAssociatedAccounts(_dbContext, userID, userEditAccountsDto.AccountIDs);
            var addedAccounts   = Account.GetByAccountID(_dbContext, addedAccountIDs);

            if (addedAccounts != null && addedAccounts.Count > 0)
            {
                SendEmailToLandownerAndAdmins(userDto, addedAccounts);
            }

            return(Ok(userDto));
        }
Example #2
0
        public ActionResult <UserDto> AddAccountsForCurrentUserUsingAccountVerificationKeys([FromBody] UserEditAcountsDto userEditAccountsDto)
        {
            var userFromContextDto = UserContext.GetUserFromHttpContext(_dbContext, HttpContext);

            if (userFromContextDto == null)
            {
                return(NotFound($"Could not find Current User"));
            }

            if (!Account.ValidateAllExist(_dbContext, userEditAccountsDto.AccountIDs))
            {
                return(NotFound("One or more of the Account IDs was invalid."));
            }

            var currentAccountIDsForUser = Account.ListByUserID(_dbContext, userFromContextDto.UserID).Select(x => x.AccountID).ToList();
            var allAccountIDsForUser     = userEditAccountsDto.AccountIDs.Union(currentAccountIDsForUser).ToList();

            EFModels.Entities.User.SetAssociatedAccounts(_dbContext, userFromContextDto.UserID, allAccountIDsForUser);

            if (userFromContextDto.Role.RoleID == (int)RoleEnum.Unassigned)
            {
                var userUpsertDto = new UserUpsertDto
                {
                    Email       = userFromContextDto.Email,
                    FirstName   = userFromContextDto.FirstName,
                    LastName    = userFromContextDto.LastName,
                    RoleID      = (int)RoleEnum.LandOwner,
                    PhoneNumber = userFromContextDto.Phone
                };

                userFromContextDto = EFModels.Entities.User.UpdateUserEntity(_dbContext, userFromContextDto.UserID, userUpsertDto);
            }

            Account.UpdateAccountVerificationKeyLastUsedDateForAccountIDs(_dbContext, userEditAccountsDto.AccountIDs);

            return(Ok(userFromContextDto));
        }