/// <summary>
        /// Deletes or Archives (Soft-Deletes) GroupMember record depending on GroupType.EnableGroupHistory and if the GroupMember has history snapshots
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public override bool Delete(GroupMember item)
        {
            var rockContext = this.Context as RockContext;
            int?groupTypeId = item.Group?.GroupTypeId;

            if (!groupTypeId.HasValue)
            {
                groupTypeId = new GroupService(rockContext).GetSelect(item.GroupId, a => a.GroupTypeId);
            }

            var groupTypeCache = GroupTypeCache.Get(groupTypeId.Value);

            if (groupTypeCache?.EnableGroupHistory == true)
            {
                var groupMemberHistoricalService = new GroupMemberHistoricalService(rockContext);
                if (groupMemberHistoricalService.Queryable().Any(a => a.GroupMemberId == item.Id))
                {
                    // if the group's GroupType has GroupHistory enabled, and this group member has group member history snapshots, then we need to Archive instead of Delete
                    this.Archive(item, null, false);
                    return(true);
                }
            }

            return(base.Delete(item));
        }
Example #2
0
        /// <summary>
        /// Deletes or Archives (Soft-Deletes) GroupMember record depending on GroupType.EnableGroupHistory and if the GroupMember has history snapshots
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public override bool Delete(GroupMember item)
        {
            var rockContext = this.Context as RockContext;
            int?groupTypeId = item.Group?.GroupTypeId;

            if (!groupTypeId.HasValue)
            {
                groupTypeId = new GroupService(rockContext).GetSelect(item.GroupId, a => a.GroupTypeId);
            }

            var groupTypeCache = GroupTypeCache.Get(groupTypeId.Value);

            if (groupTypeCache?.EnableGroupHistory == true)
            {
                var groupMemberHistoricalService = new GroupMemberHistoricalService(rockContext);
                if (groupMemberHistoricalService.Queryable().Any(a => a.GroupMemberId == item.Id))
                {
                    // if the group's GroupType has GroupHistory enabled, and this group member has group member history snapshots, then we need to Archive instead of Delete
                    this.Archive(item, null, false);
                    return(true);
                }
            }

            // As discussed in https://github.com/SparkDevNetwork/Rock/issues/4697, we are going to delete
            // the association from any group member assignments that have a reference to this group member.
            var groupMemberAssignmentService = new GroupMemberAssignmentService(this.Context as RockContext);

            foreach (var groupMemberAssignment in groupMemberAssignmentService.Queryable().Where(a => a.GroupMemberId == item.Id))
            {
                groupMemberAssignmentService.Delete(groupMemberAssignment);
            }

            return(base.Delete(item));
        }
Example #3
0
        /// <summary>
        /// Does a direct Bulk Delete of group history for all groups and group members of the specified group type and commits the changes to the database.
        /// </summary>
        /// <param name="groupTypeId">The group type identifier.</param>
        public void BulkDeleteGroupHistory(int groupTypeId)
        {
            var rockContext = this.Context as RockContext;
            var groupHistoryRecordsToDelete       = new GroupHistoricalService(rockContext).Queryable().Where(a => a.GroupTypeId == groupTypeId);
            var groupMemberHistoryRecordsToDelete = new GroupMemberHistoricalService(rockContext).Queryable().Where(a => a.Group.GroupTypeId == groupTypeId);

            rockContext.BulkDelete(groupHistoryRecordsToDelete);
            rockContext.BulkDelete(groupMemberHistoryRecordsToDelete);
        }