public bool SaveListSubscribers(int listId, out string error) { CHIMP_UserLists chimpKeysEntity; if (!GetChimpKeys(listId, out chimpKeysEntity, out error)) { return(false); } var mc = new MailChimpManager(chimpKeysEntity.ApiKey); try { var subscriberList = ChimpUserListRepository.GetAuthorSubscribers(chimpKeysEntity.UserId); var emailList = new List <BatchEmailParameter>(); var count = 0; var rejected = 0; foreach (var crsLearnerToken in subscriberList) { emailList.Add(crsLearnerToken.GetBatchEmailParameter()); if ((++count) % MAX_LIST_SUBSCRIBE != 0) { continue; } var batchResult = mc.BatchSubscribe(chimpKeysEntity.Uid, emailList, false, true); rejected += batchResult.ErrorCount; HandleBatchSubscribeRejects(chimpKeysEntity.UserId, batchResult); emailList.Clear(); } if (emailList.Count > 0) { var batchResult = mc.BatchSubscribe(chimpKeysEntity.Uid, emailList, false, true); rejected += batchResult.ErrorCount; HandleBatchSubscribeRejects(chimpKeysEntity.UserId, batchResult); } chimpKeysEntity.UpdateUserListEntity(count - rejected); ChimpUserListRepository.Update(chimpKeysEntity); if (!ChimpUserListRepository.UnitOfWork.CommitAndRefreshChanges(out error)) { return(false); } } catch (Exception exception) { error = FormatError(exception); Logger.Error("SaveListSubscribers", exception, listId, CommonEnums.LoggerObjectTypes.Mailchimp); return(false); } return(SaveSegmentSubscribers(listId, out error)); }
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); }