public override void ExecuteOption(OptionReport report)
        {
            string urlLoginName = _databaseUpdateMailrelayFromContact.urlLoginName;
            int    pageSize     = _databaseUpdateMailrelayFromContact.pageSize;
            Guid?  contactId    = _databaseUpdateMailrelayFromContact.contactId;

            DatabaseUrlLogin      login = DatabaseUrlLogin.GetUrlLogin(Connection, urlLoginName);
            DynamicsCrmConnection dynamicsCrmConnection = DynamicsCrmConnection.GetConnection(login.Url, login.Username, login.Password);

            PagingInformation pagingInformation = new PagingInformation();

            List <MailrelayInformation> mailrelayInformations = new List <MailrelayInformation>();

            UpdateReport <int> intReport = new UpdateReport <int>();

            while (pagingInformation.FirstRun || pagingInformation.MoreRecords)
            {
                mailrelayInformations = MailrelayInformation.GetMailrelayFromContact(dynamicsCrmConnection, Config.GetResourcePath, pagingInformation, pageSize, contactId);
                mailrelayInformations.ForEach(information => UpdateIfNeeded(dynamicsCrmConnection, information, intReport));
            }

            Log.Write(Connection, intReport.AsLogText("UpdateMailrelayFromContact"), typeof(UpdateMailrelayFromContact), DataLayer.MongoData.Config.LogLevelEnum.OptionReport);

            report.Success = true;
            return;
        }
        private MailrelayInformation GetInformationFromFetchXml(DynamicsCrmConnection dynamicsCrmConnection, DatabaseWebCampaign webCampaign, string email)
        {
            Guid campaignId = webCampaign.FormId;

            MailrelayInformation information = MailrelayInformation.GetMailrelayFromLead(dynamicsCrmConnection, Config.GetResourcePath, email, campaignId);

            return(information);
        }
Beispiel #3
0
        public void GetMailrelayFromLeadReturnsALead()
        {
            string email      = "test";
            Guid   campaignId = Guid.Parse("ff052597-5538-e611-80ef-001c4215c4a0");

            MailrelayInformation information = MailrelayInformation.GetMailrelayFromLead(_dynamicsCrmConnection, _config.GetResourcePath, email, campaignId);

            Assert.IsNotNull(information);
        }
Beispiel #4
0
        private void AddToMailrelayGroup(DynamicsCrmConnection dynamicsCrmConnection, UpdateReport <int> report, Subscriber subscriber, List <KeyValueEntity <Guid, int?> > crmIdsAndSubscriberIds, int groupInCrm)
        {
            foreach (KeyValueEntity <Guid, int?> idPair in crmIdsAndSubscriberIds)
            {
                MailrelayInformation mailrelayInformation = MailrelayInformation.GetMailrelayFromContact(dynamicsCrmConnection, Config.GetResourcePath, new PagingInformation(), 1, idPair.key).Single();

                UpdateResultEnum result = subscriber.UpdateIfNeeded(idPair.value.Value, mailrelayInformation.fullname, mailrelayInformation.emailaddress1, mailrelayInformation.GetCustomFields(), groupInCrm);

                report.CollectUpdate(result, idPair.value.Value);
            }
        }
Beispiel #5
0
        public void GetMailrelayFromContactCanReturnInformation()
        {
            PagingInformation pagingInformation = new PagingInformation();

            int subscriberId = new Random().Next(1, int.MaxValue);

            Contact contact = CreateTestContact();

            contact.new_mailrelaysubscriberid = subscriberId;
            contact.InsertWithoutRead();

            List <MailrelayInformation> informations = MailrelayInformation.GetMailrelayFromContact(_dynamicsCrmConnection, _config.GetResourcePath, pagingInformation, 1, contact.Id);

            contact.Delete();

            Assert.AreEqual(1, informations.Count);
        }
        private void UpdateIfNeeded(DynamicsCrmConnection dynamicsCrmConnection, MailrelayInformation information, UpdateReport <int> report)
        {
            bool needsUpdate = information.RecalculateContactCheck();
            int  id          = information.new_mailrelaysubscriberid.Value;

            if (needsUpdate == false)
            {
                report.CollectUpdate(UpdateResultEnum.AlreadyUpToDate, id);
                return;
            }

            information.UpdateContactMailrelaycheck(dynamicsCrmConnection);

            Subscriber       subscriber = new Subscriber(_mailrelayConnection);
            UpdateResultEnum result     = subscriber.UpdateIfNeeded(id, information.fullname, information.emailaddress1, information.GetCustomFields());

            report.CollectUpdate(result, id);
        }
        private addSubscriber GetSubscriberFromFetchXml(MailrelayInformation information, string email)
        {
            Dictionary <string, string> customFields = information.GetCustomFields();

            List <int> groups = new List <int>()
            {
                information.campaign_new_mailrelaygroupid.Value
            };

            addSubscriber add = new addSubscriber()
            {
                customFields = customFields,
                email        = email,
                name         = information.fullname,
                groups       = groups,
            };

            return(add);
        }
        private bool FindNewCustomFields(MailrelayInformation information, getSubscribersReply ExistingSubscriber, Dictionary <string, string> customFieldsResult)
        {
            Dictionary <string, string> customFieldsInInformation        = information.GetCustomFields();
            Dictionary <string, string> customFieldsInExistingSubscriber = ExistingSubscriber.fields;

            List <string> allKeys = customFieldsInInformation.Keys.Union(customFieldsInExistingSubscriber.Keys).ToList();

            bool isChanged = false;

            foreach (string key in allKeys)
            {
                if (customFieldsInExistingSubscriber.ContainsKey(key))
                {
                    customFieldsResult.Add(key, customFieldsInExistingSubscriber[key]);
                    continue;
                }
                isChanged = true;
                customFieldsResult.Add(key, customFieldsInInformation[key]);
            }

            return(isChanged);
        }
        private void UdpateExistingSubscriberIfNeeded(MailrelayInformation information, getSubscribersReply ExistingSubscriber, string email, int subscriberId)
        {
            Dictionary <string, string> customFieldsResult = new Dictionary <string, string>();

            bool isChanged = FindNewCustomFields(information, ExistingSubscriber, customFieldsResult);

            List <int> groups = ExistingSubscriber.groups.Select(group => int.Parse(group)).ToList();

            if (groups.Contains(information.campaign_new_mailrelaygroupid.Value) == false)
            {
                groups.Add(information.campaign_new_mailrelaygroupid.Value);
                isChanged = true;
            }

            if (information.fullname != ExistingSubscriber.name)
            {
                isChanged = true;
            }

            if (isChanged)
            {
                UpdateSubscriber(email, customFieldsResult, groups, information.fullname, subscriberId);
            }
        }
        private UpdateReport <Guid> ExportMailrelayContactFromList(DynamicsCrmConnection dynamicsCrmConnection, MarketingList marketingList)
        {
            if (marketingList.new_mailrelaygroupid.HasValue == false)
            {
                marketingList.new_mailrelaygroupid = Subscriber.CreateGroup(_mailrelayConnection, $"crm_{marketingList.listname}", "crm kontrolleret gruppe");

                marketingList.UpdateMailrelaygroupid(dynamicsCrmConnection);
            }

            IEnumerable <Guid> contactIdsToUpdate = marketingList.ContentIdsForNonMailrelaySubscribers;

            UpdateReport <Guid> updateReport = new UpdateReport <Guid>();

            foreach (Guid contactId in contactIdsToUpdate)
            {
                MailrelayInformation mailrelayInformation = MailrelayInformation.GetInformationNotInMailrelayFromContact(dynamicsCrmConnection, Config.GetResourcePath, contactId);

                UpdateResultEnum result = AddToMailrelay(dynamicsCrmConnection, mailrelayInformation, marketingList.new_mailrelaygroupid.Value);

                updateReport.CollectUpdate(result, contactId);
            }

            return(null);
        }
        public override void ExecuteOption(OptionReport report)
        {
            string urlLoginName = _databaseAddMailrelaySubscriberFromLead.urlLoginName;
            string email        = _databaseAddMailrelaySubscriberFromLead.email;
            Guid   leadId       = _databaseAddMailrelaySubscriberFromLead.leadId;

            DatabaseUrlLogin      login = DatabaseUrlLogin.GetUrlLogin(Connection, urlLoginName);
            DynamicsCrmConnection dynamicsCrmConnection = DynamicsCrmConnection.GetConnection(login.Url, login.Username, login.Password);

            DatabaseWebCampaign webCampaign = DatabaseWebCampaign.ReadByIdBytesSingleOrDefault(Connection, _databaseAddMailrelaySubscriberFromLead.WebCampaignIdValue());

            if (webCampaign == null)
            {
                Log.Write(Connection, $"Could not find campaign for {_databaseAddMailrelaySubscriberFromLead.Name}", typeof(AddMailrelaySubscriberFromLead), DataLayer.MongoData.Config.LogLevelEnum.OptionError);
                report.Success = false;
                return;
            }

            MailrelayInformation information = GetInformationFromFetchXml(dynamicsCrmConnection, webCampaign, email);

            if (information == null)
            {
                Log.Write(Connection, $"Information for lead {leadId} on {email} could not be found", typeof(AddMailrelaySubscriberFromLead), DataLayer.MongoData.Config.LogLevelEnum.OptionMessage);
                report.Success = true;
                return;
            }

            if (information.campaign_new_mailrelaygroupid.HasValue == false)
            {
                Log.Write(Connection, $"Subscriber not added, campaign {webCampaign.FormId} has no group", typeof(AddMailrelaySubscriberFromLead), DataLayer.MongoData.Config.LogLevelEnum.OptionMessage);
                report.Success = true;
                return;
            }

            getSubscribersReply ExistingSubscriber = GetExistingSubscribers(email);

            int subscriberId;

            if (ExistingSubscriber == null)
            {
                addSubscriber add = GetSubscriberFromFetchXml(information, email);

                subscriberId = SendSubscriberToMailrelay(add);
            }
            else
            {
                subscriberId = int.Parse(ExistingSubscriber.id);
                UdpateExistingSubscriberIfNeeded(information, ExistingSubscriber, email, subscriberId);
            }

            try
            {
                Lead.UpdateSubscriberId(dynamicsCrmConnection, leadId, subscriberId);
            }
            catch (Exception exception)
            {
                Log.Write(Connection, exception.Message, typeof(AddMailrelaySubscriberFromLead), exception.StackTrace, DataLayer.MongoData.Config.LogLevelEnum.OptionError);
            }

            report.Success = true;
        }
        private UpdateResultEnum AddToMailrelay(DynamicsCrmConnection dynamicsCrmConnection, MailrelayInformation mailrelayInformation, int groupId)
        {
            if (mailrelayInformation.new_mailrelaysubscriberid != null)
            {
                return(UpdateResultEnum.AlreadyUpToDate);
            }

            Subscriber subscriber = new Subscriber(_mailrelayConnection);

            try
            {
                getSubscribersReply reply = subscriber.GetMailrelaySubscribers(mailrelayInformation.emailaddress1);

                if (reply == null)
                {
                    mailrelayInformation.new_mailrelaysubscriberid = subscriber.AddNewSubscriber(mailrelayInformation.fullname, mailrelayInformation.emailaddress1, new List <int>()
                    {
                        groupId
                    }, mailrelayInformation.GetCustomFields());
                }
                else
                {
                    reply.groups.Add(groupId.ToString());

                    subscriber.UpdateFromReply(reply);

                    mailrelayInformation.new_mailrelaysubscriberid = int.Parse(reply.id);
                }
            }
            catch (Exception exception)
            {
                Log.Write(Connection, exception.Message, typeof(ExportContactToMailrelay), exception.StackTrace, DataLayer.MongoData.Config.LogLevelEnum.OptionError);
                return(UpdateResultEnum.Failed);
            }

            mailrelayInformation.UpdateContactMailrelaySubscriberid(dynamicsCrmConnection);

            return(UpdateResultEnum.Updated);
        }