Esempio n. 1
0
        public void ResetStaticSegment_Successful()
        {
            MailChimpManager           mc       = new MailChimpManager(TestGlobal.Test_APIKey);
            ListResult                 lists    = mc.GetLists();
            List <StaticSegmentResult> segments = mc.GetStaticSegmentsForList(lists.Data[1].Id);
            StaticSegmentActionResult  result   = mc.ResetStaticSegment(lists.Data[1].Id, segments[0].StaticSegmentId);

            Assert.IsTrue(result.Complete);
        }
Esempio n. 2
0
 public void ResetStaticSegment_Successful()
 {
     MailChimpManager mc = new MailChimpManager(TestGlobal.Test_APIKey);
     ListResult lists = mc.GetLists();
     List<StaticSegmentResult> segments = mc.GetStaticSegmentsForList(lists.Data[1].Id);
     StaticSegmentActionResult result = mc.ResetStaticSegment(lists.Data[1].Id, segments[0].StaticSegmentId);
     Assert.IsTrue(result.Complete);
 }
Esempio n. 3
0
        private bool SaveSegmentSubscribers(int listId, out string error)
        {
            error = string.Empty;

            CHIMP_UserLists chimpKeysEntity;

            if (!GetChimpKeys(listId, out chimpKeysEntity, out error))
            {
                return(false);
            }

            var segmentEntities = ChimpListSegmentRepository.GetMany(x => x.ListId == listId).ToList();

            if (segmentEntities.Count.Equals(0))
            {
                error = "Mailchimp segments not found";
                return(false);
            }

            var dateFrom = DateTime.Now.Subtract(new TimeSpan(0, 24, 0, 0));
            var mc       = new MailChimpManager(chimpKeysEntity.ApiKey);

            var allSubscribers = ChimpUserListRepository.GetAuthorSubscribers(chimpKeysEntity.UserId);

            try
            {
                foreach (var segEntity in segmentEntities)
                {
                    List <CHIMP_ItemSubscribers> subscribers = null;
                    var emailParams = new List <EmailParameter>();
                    var emailList   = new List <string>();
                    var segmentId   = Int32.Parse(segEntity.Uid);
                    var itemId      = segEntity.BundleId ?? segEntity.CourseId;
                    var itemType    = segEntity.CourseId != null ? 1 : 2;

                    mc.ResetStaticSegment(chimpKeysEntity.Uid, segmentId);


                    var segmentType = Utils.ParseEnum <eSegmentTypes>(segEntity.SegmentTypeId);
                    switch (segmentType)
                    {
                    case eSegmentTypes.Active:
                        if (allSubscribers != null)
                        {
                            emailList = allSubscribers.Where(x => x.StatusId == (int)BillingEnums.eAccessStatuses.ACTIVE).Select(x => x.Email).ToList();
                        }
                        break;

                    case eSegmentTypes.InActive:
                        if (allSubscribers != null)
                        {
                            emailList = allSubscribers.Where(x => x.StatusId != (int)BillingEnums.eAccessStatuses.ACTIVE).Select(x => x.Email).ToList();
                        }
                        break;

                    case eSegmentTypes.Item:
                        subscribers = ChimpListSegmentRepository.GetItemSubscribers(itemId, itemType);
                        if (subscribers != null)
                        {
                            emailList = subscribers.Where(x => x.StatusId == (int)BillingEnums.eAccessStatuses.ACTIVE).Select(x => x.Email).ToList();
                        }
                        break;

                    case eSegmentTypes.ItemNew:
                        subscribers = ChimpListSegmentRepository.GetItemSubscribers(itemId, itemType);
                        if (subscribers != null)
                        {
                            emailList = subscribers.Where(x => x.StatusId == (int)BillingEnums.eAccessStatuses.ACTIVE && x.AddOn >= dateFrom).Select(x => x.Email).ToList();
                        }
                        break;
                    }


                    var count = 0;
                    foreach (var email in emailList)
                    {
                        emailParams.Add(email.GetEmailParameter());

                        if ((++count) % MAX_SEGMENT_SUBSCRIBE != 0)
                        {
                            continue;
                        }

                        var result = mc.AddStaticSegmentMembers(chimpKeysEntity.Uid, segmentId, emailParams);
                        HandleAddStaticSegmentMembersError(chimpKeysEntity.UserId, result);
                        emailParams.Clear();
                    }
                    if (emailParams.Count > 0)
                    {
                        var result = mc.AddStaticSegmentMembers(chimpKeysEntity.Uid, segmentId, emailParams);
                        HandleAddStaticSegmentMembersError(chimpKeysEntity.UserId, result);
                    }

                    segEntity.UpdateSegemntEntity(emailList.Count);
                    ChimpListSegmentRepository.Update(segEntity);
                    if (!ChimpListSegmentRepository.UnitOfWork.CommitAndRefreshChanges(out error))
                    {
                        return(false);
                    }
                }
            }
            catch (Exception exception)
            {
                error = FormatError(exception);
                Logger.Error("SaveListSubscribers", exception, listId, CommonEnums.LoggerObjectTypes.Mailchimp);
                return(false);
            }

            return(true);
        }