Ejemplo n.º 1
0
        /// <summary>
        /// Checks to see if the PersonAlias entity should be added to the FollowingSuggestion table. If the entity is not a PersonAlias then addSuggestion will be true.
        /// If the Entity is a PersonAlias then this method will check if the associated Person has another PersonAlias that is being followed.
        /// If there is another PersonAlias being followed then the Following and FollowingSuggestion are updated with this EntityId (PersonAliasId)
        /// which is the PrimaryPersonAliasId.
        /// </summary>
        /// <param name="followerPersonId">The follower person identifier.</param>
        /// <param name="suggestionTypeComponent">The suggestion type component.</param>
        /// <param name="entityIdToBeSavedAsSuggestions">The entity ids.</param>
        /// <param name="entityTypeId">The entity type identifier.</param>
        /// <param name="suggestionContext">The suggestion context.</param>
        /// <param name="followedEntityId">The followed entity identifier.</param>
        /// <param name="addSuggestion">if set to <c>true</c> then the suggestion should be inserted.</param>
        /// <returns>
        /// True if the PersonAlias following suggestion should be inserted or if the EntityType is not a PersonAlias.
        /// </returns>
        private void ProcessFollowingSuggestionAndPersonAliasEntity(int followerPersonId, SuggestionTypeComponent suggestionTypeComponent, List <int> entityIdToBeSavedAsSuggestions, int entityTypeId, RockContext suggestionContext, int followedEntityId, out bool addSuggestion)
        {
            addSuggestion = true;

            // If the Entity is not a PersonAlias no other checks are needed just return with addSuggestion = true
            if (!IsEntityTypePersonAlias(entityTypeId))
            {
                return;
            }

            // since this is a person alias see if a different alias is being used to follow
            var followedPersonId      = new PersonAliasService(suggestionContext).Get(followedEntityId).PersonId;
            var followedPersonAliases = new PersonAliasService(suggestionContext)
                                        .Queryable()
                                        .Where(a => a.PersonId == followedPersonId && a.Id != followedEntityId)
                                        .Select(a => a.Id)
                                        .ToList();

            if (!followedPersonAliases.Any())
            {
                // There are no alternate person alias' there is nothing else to check so just return with addSuggestion = true
                return;
            }

            int existingFollowingPersonAliasId = suggestionTypeComponent.ExistingFollowings
                                                 .GetValueOrDefault(followerPersonId, new List <int>())
                                                 .Where(f => followedPersonAliases.Contains(f))
                                                 .FirstOrDefault();

            // Update the existing following record to use the primary PersonAlias ID and remove it from the list.
            if (existingFollowingPersonAliasId != 0)
            {
                var personAliasEntityTypeId = EntityTypeCache.GetId(Rock.SystemGuid.EntityType.PERSON_ALIAS) ?? 0;
                addSuggestion = false;
                entityIdToBeSavedAsSuggestions.Remove(followedEntityId);

                using (var followingContext = new RockContext())
                {
                    var following = new FollowingService(followingContext)
                                    .GetByEntityAndPerson(personAliasEntityTypeId, existingFollowingPersonAliasId, followerPersonId)
                                    .FirstOrDefault();

                    following.EntityId = followedEntityId;

                    var suggested = new FollowingSuggestedService(followingContext)
                                    .GetByEntityAndPerson(personAliasEntityTypeId, existingFollowingPersonAliasId, followerPersonId)
                                    .FirstOrDefault();

                    if (suggested != null)
                    {
                        // Get the Followed Person's Primary PersonAliasId from FollowingSuggestions if it exists, PersonEntitySuggestions is already filtered for EntityType
                        int existingFollowingSuggestedPrimaryPersonAliasId = suggestionTypeComponent.PersonEntitySuggestions
                                                                             .Where(s => s.PersonId == followerPersonId && s.EntityId == followedEntityId)
                                                                             .Select(s => s.EntityId)
                                                                             .FirstOrDefault();

                        if (existingFollowingSuggestedPrimaryPersonAliasId != 0)
                        {
                            // Since the Primary PersonAlias is already in FollowingSuggestions then just delete this one as a duplicate
                            new FollowingSuggestedService(followingContext).Delete(suggested);
                        }
                        else
                        {
                            // Update the outdated PersonAliasId to the primary PersonAliasId which is the followedEntityId
                            suggested.EntityId = followedEntityId;
                        }
                    }

                    followingContext.SaveChanges();
                }
            }
        }