/// <summary> /// Processes the connection type. /// </summary> /// <param name="connectionTypeView">The connection type view.</param> /// <param name="updatedResults">The updated results.</param> /// <param name="errorMessages">The error message.</param> private void ProcessConnectionType( ConnectionTypeView connectionTypeView, ConcurrentBag <int> updatedResults, out List <string> errorMessages) { errorMessages = new List <string>(); var groupViews = new List <GroupView>(); foreach (var connectionStatus in connectionTypeView.ConnectionStatuses) { foreach (var connectionStatusAutomation in connectionStatus.ConnectionStatusAutomations) { var rockContext = new RockContext(); var connectionRequestService = new ConnectionRequestService(rockContext); var connectionRequestQry = connectionRequestService.Queryable().Include(a => a.AssignedGroup.Members).Where(a => a.ConnectionStatusId == connectionStatus.Id); if (connectionStatusAutomation.DataViewId.HasValue) { // Get the dataview configured for the connection request var dataViewService = new DataViewService(rockContext); var dataview = dataViewService.Get(connectionStatusAutomation.DataViewId.Value); if (dataview == null) { errorMessages.Add($"The dataview {connectionStatusAutomation.DataViewId} for Connection Type {connectionTypeView.ConnectionTypeId} did not resolve"); continue; } // Now we'll filter our connection request query to only include the ones that are in the configured data view. var dataViewGetQueryArgs = new DataViewGetQueryArgs { DbContext = rockContext }; IQueryable <ConnectionRequest> dataviewQuery; try { dataviewQuery = dataview.GetQuery(dataViewGetQueryArgs) as IQueryable <ConnectionRequest>; } catch (Exception ex) { errorMessages.Add(ex.Message); ExceptionLogService.LogException(ex); continue; } if (dataviewQuery == null) { errorMessages.Add($"Generating a query for dataview {connectionStatusAutomation.DataViewId} for Status {connectionStatus.Id} in Connection Type {connectionTypeView.ConnectionTypeId} was not successful"); continue; } connectionRequestQry = connectionRequestQry.Where(a => dataviewQuery.Any(b => b.Id == a.Id)); } var eligibleConnectionRequests = new List <ConnectionRequest>(); if (connectionStatusAutomation.GroupRequirementsFilter != GroupRequirementsFilter.Ignore) { var connectionRequests = connectionRequestQry.ToList(); foreach (var connectionRequest in connectionRequests) { // Group Requirement can't be met when either placement group or placement group role id is missing if (connectionRequest.AssignedGroupId.HasValue && connectionRequest.AssignedGroupMemberRoleId.HasValue) { var groupView = GetGroupView(connectionRequest, groupViews, rockContext); if (groupView != null && groupView.HasGroupRequirement) { var isRequirementMet = IsGroupRequirementMet(connectionRequest, groupView, rockContext); // connection request based on if group requirement is met or not is added to list for status update if ((connectionStatusAutomation.GroupRequirementsFilter == GroupRequirementsFilter.DoesNotMeet && !isRequirementMet) || (connectionStatusAutomation.GroupRequirementsFilter == GroupRequirementsFilter.MustMeet && isRequirementMet)) { eligibleConnectionRequests.Add(connectionRequest); } } } } } else { eligibleConnectionRequests = connectionRequestQry.ToList(); } var updatedCount = 0; foreach (var connectionRequest in eligibleConnectionRequests) { connectionRequest.ConnectionStatusId = connectionStatusAutomation.DestinationStatusId; updatedCount++; } rockContext.SaveChanges(); updatedResults.Add(updatedCount); } } }
/// <summary> /// Processes the connection type. /// </summary> /// <param name="connectionTypeView">The connection type view.</param> /// <param name="updatedResults">The updated results.</param> /// <param name="errorMessages">The error message.</param> private void ProcessConnectionType( ConnectionTypeView connectionTypeView, ConcurrentBag <int> updatedResults, out List <string> errorMessages) { errorMessages = new List <string>(); var groupViews = new List <GroupView>(); foreach (var connectionStatus in connectionTypeView.ConnectionStatuses) { foreach (var connectionStatusAutomation in connectionStatus.ConnectionStatusAutomations.OrderBy(a => a.AutomationName)) { var rockContext = new RockContext(); rockContext.Database.CommandTimeout = commandTimeout; var connectionRequestService = new ConnectionRequestService(rockContext); var destinationStatusId = connectionStatusAutomation.DestinationStatusId; // Limit to connection requests that don't already have the same connection status that this automation sets it to var connectionRequestQry = connectionRequestService.Queryable() .Where(a => a.ConnectionStatusId == connectionStatus.Id && a.ConnectionStatusId != connectionStatusAutomation.DestinationStatusId) .Include(a => a.ConnectionOpportunity).Include(a => a.PersonAlias); if (connectionStatusAutomation.GroupRequirementsFilter != GroupRequirementsFilter.Ignore) { // if we need to process GroupRequirements, include AssignedGroup.Members to avoid some lazy loading. connectionRequestQry = connectionRequestQry.Include(a => a.AssignedGroup.Members); } if (connectionStatusAutomation.DataViewId.HasValue) { // Get the dataview configured for the connection request var dataViewService = new DataViewService(rockContext); var dataview = dataViewService.Get(connectionStatusAutomation.DataViewId.Value); if (dataview == null) { errorMessages.Add($"The dataview {connectionStatusAutomation.DataViewId} for Connection Type {connectionTypeView.ConnectionTypeId} did not resolve"); continue; } IQueryable <ConnectionRequest> dataviewQuery; try { dataviewQuery = connectionRequestService.GetQueryUsingDataView(dataview); } catch (Exception ex) { errorMessages.Add(ex.Message); ExceptionLogService.LogException(ex); continue; } if (dataviewQuery == null) { errorMessages.Add($"Generating a query for dataview {connectionStatusAutomation.DataViewId} for Status {connectionStatus.Id} in Connection Type {connectionTypeView.ConnectionTypeId} was not successful"); continue; } connectionRequestQry = connectionRequestQry.Where(a => dataviewQuery.Any(b => b.Id == a.Id)); } var eligibleConnectionRequests = new List <ConnectionRequest>(); if (connectionStatusAutomation.GroupRequirementsFilter != GroupRequirementsFilter.Ignore) { var connectionRequests = connectionRequestQry.ToList(); foreach (var connectionRequest in connectionRequests) { // Group Requirement can't be met when either placement group or placement group role id is missing if (connectionRequest.AssignedGroupId.HasValue && connectionRequest.AssignedGroupMemberRoleId.HasValue) { var groupView = GetGroupView(connectionRequest, groupViews, rockContext); if (groupView != null && groupView.HasGroupRequirement) { var isRequirementMet = IsGroupRequirementMet(connectionRequest, groupView, rockContext); // connection request based on if group requirement is met or not is added to list for status update if ((connectionStatusAutomation.GroupRequirementsFilter == GroupRequirementsFilter.DoesNotMeet && !isRequirementMet) || (connectionStatusAutomation.GroupRequirementsFilter == GroupRequirementsFilter.MustMeet && isRequirementMet)) { eligibleConnectionRequests.Add(connectionRequest); } } } } } else { eligibleConnectionRequests = connectionRequestQry.ToList(); } var updatedCount = 0; foreach (var connectionRequest in eligibleConnectionRequests) { if (connectionRequest.ConnectionStatusId == connectionStatusAutomation.DestinationStatusId) { continue; } connectionRequest.SetConnectionStatusFromAutomationLoop(connectionStatusAutomation); updatedCount++; } if (updatedCount > 0) { rockContext.SaveChanges(); updatedResults.Add(updatedCount); } } } }