Example #1
0
        public static MailChimpPersonAlias AddOrUpdatePerson(Person person, string listId, string apiKey)
        {
            var rockContext = new RockContext();
            var api         = new MailChimpApi(apiKey, listId);

            return(AddOrUpdatePerson(api, person, rockContext));
        }
Example #2
0
        public Form1()
        {
            InitializeComponent();

            _api        = new MailChimpApi();
            _api.ApiKey = "1ff36b16ef1a8d2006e6a9f72f367351-us6"; // PAAutism.org
            _api.ApiKey = "a9636855aef143bce54a98f4405d326e-us5"; // agis
        }
Example #3
0
        protected override void HandleSubscription(MailAddress mailAddress, IEnumerable <Field> allCollectedValues, IPublishedContent content)
        {
            var valueFields = allCollectedValues.OfType <FieldWithValue>().ToArray();
            var mergeFields = valueFields
                              .Except(new[] { this })
                              .ToDictionary(f => f.Name, f => f.SubmittedValue);

            var api = new MailChimpApi();

            api.Subscribe(ListId, mailAddress, mergeFields, ApiKey);
        }
Example #4
0
        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);
            }
        }
Example #5
0
 public RestClient(MailChimpApi mailChimpApi)
 {
     this.MailChimpApi = mailChimpApi;
 }
Example #6
0
        public void Execute(IJobExecutionContext context)
        {
            var    jobDataMap    = context.JobDetail.JobDataMap;
            string apiKey        = jobDataMap.GetString("APIKey");
            var    groupTypeGuid = jobDataMap.GetString("GroupType").AsGuidOrNull();

            _listId           = jobDataMap.GetString("ListId");
            _timeout          = jobDataMap.GetIntFromString("Timeout");
            _workflowTypeGuid = jobDataMap.GetString("NewPersonWorkflow").AsGuidOrNull();
            _newPersonConnectionStatusGuid = jobDataMap.GetString("NewPersonConnectionStatus").AsGuid();
            _groupTypeId = ValidateParameters(apiKey, groupTypeGuid);

            _api = new MailChimpApi(apiKey, _listId);

            // Get segments
            IEnumerable <ListSegment> segments;

            try
            {
                segments = _api.GetSegments().Result;
            }
            catch (Exception e)
            {
                throw new Exception("Unable to fetch Mailchimp segments", e);
            }

            // Get list members
            IEnumerable <Member> mailChimpMembers;

            try
            {
                mailChimpMembers = _api.GetListMembers().Result;
            }
            catch (Exception innerException)
            {
                throw new Exception("Unable to fetch Mailchimp list members", innerException);
            }

            // Find who's on the list and update anyone if their mergefields would be different
            HashSet <int> existingPersonAliasIds = SyncFromMailChimp(mailChimpMembers);

            // Get all people who should be synced
            var rockContext = GenerateRockContext();
            var groups      = new GroupService(rockContext)
                              .Queryable("Members.Select(l1 => l1.Person)")
                              .AsNoTracking()
                              .Where(g => g.GroupTypeId == _groupTypeId && g.IsActive);

            var segmentIdsByName = segments.Select(s => new { s.Name, s.Id }).ToDictionary(a => a.Name, s => s.Id);
            var groupNamesByID   = groups.Select(g => new { g.Name, g.Id }).ToDictionary(a => a.Id, g => g.Name);


            int activeRecordStatusValueId = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSON_RECORD_STATUS_ACTIVE.AsGuid(), null).Id;
            var peopleByGroup             = groups
                                            .SelectMany(g => g.Members)
                                            .Where(p => !p.Person.IsDeceased && p.Person.RecordStatusValueId == activeRecordStatusValueId && p.Person.IsEmailActive && p.Person.Email != null && p.Person.Email != String.Empty && p.Person.EmailPreference == EmailPreference.EmailAllowed)
                                            .GroupBy(gm => gm.GroupId, gm => gm.Person);

            // Sync anyone missing
            SyncToMailChimp(existingPersonAliasIds, peopleByGroup);

            // Ensure segments are updated
            SyncSegments(groupNamesByID, peopleByGroup, segmentIdsByName).Wait();

            context.Result = string.Format("Synced a total of {0} people", _syncCount);

            if (_exceptions.Any())
            {
                throw new AggregateException("One or more syncs failed ", _exceptions);
            }
        }