/// <summary> /// Removes the non-primary person alias recipients. /// </summary> /// <param name="rockContext">The rock context.</param> private void RemoveNonPrimaryPersonAliasRecipients(RockContext rockContext) { /* * 4-MAY-2022 DMV * * In tracking down alleged duplicate communications we discovered * that duplicates could be sent to the same person if they are in the * recipient list more that once with mulitple Person Alias IDs. * This could have occured through a person merge or other data changes * in Rock. This method removes those duplicates from the list before * sending the communication. * */ var communicationRecipientService = new CommunicationRecipientService(rockContext); var recipientsQry = GetRecipientsQry(rockContext); int?smsMediumEntityTypeId = EntityTypeCache.GetId(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_SMS.AsGuid()); if (smsMediumEntityTypeId.HasValue) { IQueryable <CommunicationRecipient> duplicateSMSRecipientsQuery = recipientsQry.Where(a => a.MediumEntityTypeId == smsMediumEntityTypeId.Value) .Where(a => a.PersonAlias.PersonId != a.PersonAlias.AliasPersonId); // Only non-primary aliases. var duplicateSMSRecipients = duplicateSMSRecipientsQuery.ToList(); communicationRecipientService.DeleteRange(duplicateSMSRecipients); } int?emailMediumEntityTypeId = EntityTypeCache.GetId(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_EMAIL.AsGuid()); if (emailMediumEntityTypeId.HasValue) { IQueryable <CommunicationRecipient> duplicateEmailRecipientsQry = recipientsQry.Where(a => a.MediumEntityTypeId == emailMediumEntityTypeId.Value) .Where(a => a.PersonAlias.PersonId != a.PersonAlias.AliasPersonId); // Only non-primary aliases. var duplicateEmailRecipients = duplicateEmailRecipientsQry.ToList(); communicationRecipientService.DeleteRange(duplicateEmailRecipients); } rockContext.SaveChanges(); }
/// <summary> /// if <see cref="ExcludeDuplicateRecipientAddress" /> is set to true, removes <see cref="CommunicationRecipient"></see>s that have the same SMS/Email address as another recipient /// </summary> /// <param name="rockContext">The rock context.</param> public void RemoveRecipientsWithDuplicateAddress(RockContext rockContext) { if (!ExcludeDuplicateRecipientAddress) { return; } var communicationRecipientService = new CommunicationRecipientService(rockContext); var recipientsQry = GetRecipientsQry(rockContext); int?smsMediumEntityTypeId = EntityTypeCache.GetId(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_SMS.AsGuid()); if (smsMediumEntityTypeId.HasValue) { IQueryable <CommunicationRecipient> duplicateSMSRecipientsQuery = recipientsQry.Where(a => a.MediumEntityTypeId == smsMediumEntityTypeId.Value) .Where(a => a.PersonAlias.Person.PhoneNumbers.Where(pn => pn.IsMessagingEnabled).Any()) .GroupBy(a => a.PersonAlias.Person.PhoneNumbers.Where(pn => pn.IsMessagingEnabled).FirstOrDefault().Number) .Where(a => a.Count() > 1) .Select(a => a.OrderBy(x => x.Id).Skip(1).ToList()) .SelectMany(a => a); var duplicateSMSRecipients = duplicateSMSRecipientsQuery.ToList(); communicationRecipientService.DeleteRange(duplicateSMSRecipients); } int?emailMediumEntityTypeId = EntityTypeCache.GetId(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_EMAIL.AsGuid()); if (emailMediumEntityTypeId.HasValue) { IQueryable <CommunicationRecipient> duplicateEmailRecipientsQry = recipientsQry.Where(a => a.MediumEntityTypeId == emailMediumEntityTypeId.Value) .GroupBy(a => a.PersonAlias.Person.Email) .Where(a => a.Count() > 1) .Select(a => a.OrderBy(x => x.Id).Skip(1).ToList()) .SelectMany(a => a); var duplicateEmailRecipients = duplicateEmailRecipientsQry.ToList(); communicationRecipientService.DeleteRange(duplicateEmailRecipients); } rockContext.SaveChanges(); }