Пример #1
0
 public bool GetListSegments(ChimpUserListDTO token)
 {
     token.Segments = ChimpListSegmentRepository.GetMany(x => x.ListId == token.ListId).Select(x => x.Entity2SegmentDto()).ToList();
     return(token.Segments.Count > 0);
 }
Пример #2
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);
        }