private async Task <UserProfileProperty> UpdateUserProfileProperty(UserProfileProperty userProfileProperty, ProfileProperty profileProperty, bool isAdmin, bool uniqueChecked)
        {
            await ValidateUser(userProfileProperty);

            var userProfilePropertyLogic = new UserProfilePropertyLogic(Cache, Context);

            var existingUserProfileProperty = await userProfilePropertyLogic.GetUserProfileProperty(userProfileProperty.UserId, userProfileProperty.ProfilePropertyId, isAdmin);

            UserProfileProperty updatedUserProfileProperty = null;

            var evaluateUnique = ShouldValidateUniqueness(userProfileProperty, profileProperty, uniqueChecked);

            string lockName = "UpdateUserProfileProperty-ProfilePropertyId-" + profileProperty.ProfilePropertyId;

            if (existingUserProfileProperty != null && existingUserProfileProperty.UserProfilePropertyId > 0)
            {
                updatedUserProfileProperty = await ProcessExistingProfileProperty(existingUserProfileProperty, userProfileProperty, profileProperty, evaluateUnique, lockName);
            }
            else
            {
                updatedUserProfileProperty = await ProcessNewProfileProperty(existingUserProfileProperty, userProfileProperty, profileProperty, evaluateUnique, lockName, userProfilePropertyLogic, isAdmin);
            }

            //await SendUserProfilePropertyUpdatedEvent(updatedUserProfileProperty);

            await new UserProfilePropertyCache(Cache).InvalidateUserProfilePropertiesCache(updatedUserProfileProperty.UserId);

            return(updatedUserProfileProperty);
        }
        public async Task <List <UserProfileProperty> > Get(int?userId)
        {
            CheckIfPassedUserIDAllowed(userId);

            var userProfilePropertyLogic = new UserProfilePropertyLogic(Cache, Context);

            return(await userProfilePropertyLogic.GetUserProfileProperties(userId ?? UserId, IsAdmin));
        }
        private async Task ValidateUniqueness(UserProfileProperty userProfileProperty, ProfileProperty profileProperty)
        {
            UserProfilePropertyLogic userProfilePropertyLogic = new UserProfilePropertyLogic(Cache, Context);

            List <int> users = await userProfilePropertyLogic.FindUsersFromValue(profileProperty.ProfilePropertyId, userProfileProperty.Value);

            if (users != null && users.Count > 0)
            {
                throw new FriendlyException("UserProfileProperty.ValueAlreadyTaken", profileProperty.Name + " is already taken by another user");
            }
        }
        public async Task <UserProfileProperty> Get(int id)
        {
            var userProfilePropertyLogic = new UserProfilePropertyLogic(Cache, Context);

            var userProfileProperty = await userProfilePropertyLogic.GetUserProfileProperty(id, IsAdmin);

            if (userProfileProperty == null)
            {
                return(null);
            }

            CheckIfPassedUserIDAllowed(userProfileProperty.UserId);

            return(userProfileProperty);
        }
        private async Task <UserProfileProperty> ProcessNewProfileProperty(UserProfileProperty existingUserProfileProperty, UserProfileProperty userProfileProperty, ProfileProperty profileProperty, bool evaluateUnique, string lockName, UserProfilePropertyLogic userProfilePropertyLogic, bool isAdmin)
        {
            if (string.IsNullOrEmpty(userProfileProperty.Value))
            {
                // If the update is empty and the user profile property doesn't exist return a blank profile entry.
                return(existingUserProfileProperty);
            }
            if (existingUserProfileProperty.ProfilePropertyName.ToLower() == "email")
            {
                BudgetUser cascadeCall = null;
                cascadeCall = await CascadeEmail(userProfileProperty);

                // cascade call failed, return
                if (cascadeCall == null)
                {
                    return(existingUserProfileProperty);
                }
                await CreateProfileProperty(userProfileProperty, profileProperty, evaluateUnique, lockName);
            }
            else
            {
                await CreateProfileProperty(userProfileProperty, profileProperty, evaluateUnique, lockName);
            }

            // Now that the property exists, grab it with definition info.
            UserProfileProperty updatedUserProfileProperty = await userProfilePropertyLogic.GetUserProfileProperty(userProfileProperty.UserId, userProfileProperty.ProfilePropertyId, isAdmin);

            return(updatedUserProfileProperty);
        }