Esempio n. 1
0
        /// <summary>
        /// Mails the chimp list sync.
        /// </summary>
        /// <param name="BranchId">The branch id.</param>
        /// <param name="mailChimpAPIKey">The mail chimp API key.</param>
        /// <param name="dualSync">if set to <c>true</c> [dual sync].</param>
        private void MailChimpListSync(int BranchId, string mailChimpAPIKey, bool dualSync)
        {
            var mailChimpLists     = _mailChimpApi.GetMailChimpLists(mailChimpAPIKey);
            var mailChimpListTable = _dataAccess.GetMailChimpList(BranchId.ToString());

            var mclistIds      = (from item in mailChimpLists select item.id).ToList();
            var mclistTableIds = (from item in mailChimpListTable select item.ListId).ToList();

            /*mc-server   local-db
             * 0            1
             */

            var mclistIsNotInMcSvrIds = mclistTableIds.Except(mclistIds);

            foreach (string listId in mclistIsNotInMcSvrIds)
            {
                DeleteMailChimpListAndpcampaigns(listId, BranchId);
            }

            /*mc-server   local-db
             * 1           0
             */

            if (dualSync)
            {
                var newListIdsFromMc = mclistIds.Except(mclistTableIds);
                var newListsFromMc   = from list in mailChimpLists where newListIdsFromMc.Contains(list.id) select list;
                var mailChimpList    = new Common.Table.MailChimpList();
                foreach (var list in newListsFromMc)
                {
                    mailChimpList          = new Common.Table.MailChimpList();
                    mailChimpList.ListId   = list.id;
                    mailChimpList.Name     = list.name;
                    mailChimpList.BranchId = BranchId;
                    mailChimpList.UserId   = GetUserId(list.default_from_email);
                    _dataAccess.AddMailChimpLists(mailChimpList);
                }

                var updateListIdsFromMc = mclistIds.Intersect(mclistTableIds);
                var updateListsFromMc   = from list in mailChimpLists where updateListIdsFromMc.Contains(list.id) select list;
                foreach (var list in updateListsFromMc)
                {
                    mailChimpList        = new Common.Table.MailChimpList();
                    mailChimpList.ListId = list.id;
                    mailChimpList.Name   = list.name;
                    //mailChimpList.BranchId = BranchId;
                    mailChimpList.UserId = GetUserId(list.default_from_email);
                    _dataAccess.UpdateMailChimpLists(mailChimpList);
                }
            }
        }