public static MailChimpPersonAlias AddOrUpdatePerson(MailChimpApi api, Person person, RockContext rockContext) { try { MailChimpPersonAliasService mailChimpPersonAliasService = new MailChimpPersonAliasService(rockContext); Member member = MakeMailChimpMember(person); member = api.AddOrUpdateMailChimpMember(member).Result; if (person.PrimaryAliasId.HasValue) { MailChimpPersonAlias mailChimpPersonAlias = mailChimpPersonAliasService.GetByPersonAliasId(person.PrimaryAliasId.Value); if (mailChimpPersonAlias != null && mailChimpPersonAlias.LastUpdated >= RockDateTime.Now.AddMinutes(-5)) { // Skip sync, synced recently return(mailChimpPersonAlias); } if (mailChimpPersonAlias == null) { mailChimpPersonAlias = new MailChimpPersonAlias(); mailChimpPersonAlias.PersonAliasId = person.PrimaryAliasId.Value; } mailChimpPersonAlias.Email = member.EmailAddress; mailChimpPersonAlias.MailChimpUniqueId = member.UniqueEmailId; mailChimpPersonAlias.LastUpdated = RockDateTime.Now; if (mailChimpPersonAlias.Id == 0) { mailChimpPersonAliasService.Add(mailChimpPersonAlias); } rockContext.SaveChanges(); return(mailChimpPersonAlias); } throw new Exception("No person primary alias found"); } catch (Exception e) { throw new Exception("Error adding or updating a person", e); } }
public async Task RemoveFromMailChimp(MailChimpPersonAlias mailChimpPersonAlias) { await Manager.Members.DeleteAsync(ListId, mailChimpPersonAlias.Email).ConfigureAwait(false); }
private HashSet <int> SyncFromMailChimp(IEnumerable <Member> listMembers) { RockContext rockContext = GenerateRockContext(); MailChimpPersonAliasService mailChimpPersonAliasService = new MailChimpPersonAliasService(rockContext); var personService = new PersonService(rockContext); HashSet <int> seenPesonAliasIds = new HashSet <int>(); int count = 0; int recordTypeId = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSON_RECORD_TYPE_PERSON.AsGuid()).Id; int recordStatusId = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_ACTIVE.AsGuid()).Id; int connectionStatusValueId = DefinedValueCache.Read(_newPersonConnectionStatusGuid).Id; foreach (Member listMember in listMembers.Where(l => l.Status != Status.Undefined && l.Status != Status.Unsubscribed)) { count++; if (count > 200) { count = 0; rockContext.SaveChanges(); rockContext = GenerateRockContext(); mailChimpPersonAliasService = new MailChimpPersonAliasService(rockContext); personService = new PersonService(rockContext); } MailChimpPersonAlias mailChimpPersonAlias = null; // Get by unique ID if no personaliasid seen, else use personaliasid mailChimpPersonAlias = ((!listMember.MergeFields.ContainsKey(Utils.PERSON_ALIAS_KEY) || !listMember.MergeFields[Utils.PERSON_ALIAS_KEY].ToString().AsIntegerOrNull().HasValue) ? mailChimpPersonAliasService.GetByMailChimpUniqueId(listMember.UniqueEmailId) : mailChimpPersonAliasService.GetByPersonAliasId(listMember.MergeFields[Utils.PERSON_ALIAS_KEY].ToString().AsInteger())); if (mailChimpPersonAlias == null) { // Can't find a match in our database, guess we better try and find or create a person if (_workflowTypeGuid.HasValue) { // defer to workflow LaunchWorkflow(listMember.MergeFields[Utils.FIRST_NAME_KEY].ToString(), listMember.MergeFields[Utils.LAST_NAME_KEY].ToString(), listMember.EmailAddress); } else { // do it ourselves mailChimpPersonAlias = CreatePerson(rockContext, personService, recordTypeId, recordStatusId, connectionStatusValueId, listMember); } } else { var person = mailChimpPersonAlias.PersonAlias.Person; if (person.EmailPreference != EmailPreference.EmailAllowed) { _api.RemoveFromMailChimp(mailChimpPersonAlias).Wait(); } // Handle change of email address, Rock always wins else if (person.Email != mailChimpPersonAlias.Email) { _api.RemoveFromMailChimp(mailChimpPersonAlias).Wait(); try { Utils.AddOrUpdatePerson(_api, person, rockContext); _syncCount++; } catch (Exception e) { _exceptions.Add(new Exception("Failed to add or update person", e)); } } else { // Check to see if person has been updated if (Utils.IsSyncNeeded(person, listMember)) { try { Utils.AddOrUpdatePerson(_api, mailChimpPersonAlias.PersonAlias.Person, rockContext); _syncCount++; } catch (Exception e) { _exceptions.Add(new Exception("Failed to add or update person", e)); } } } } seenPesonAliasIds.Add(mailChimpPersonAlias.PersonAliasId); } return(seenPesonAliasIds); }