Exemplo n.º 1
0
        private QueryExpression GetBulkSyncFilter(CampaignMonitorConfiguration config, BulkSyncData syncData, string primaryEmail)
        {
            // retrieve contacts based on the filter, grabbing the columns specified either
            // in the fields to sync (on config entity) or fields specified in the bulkdata sync fields
            QueryExpression viewFilter;

            if (config.SyncViewId != null && config.SyncViewId != Guid.Empty)
            {
                viewFilter = SharedLogic.GetConfigFilterQuery(orgService, config.SyncViewId);
            }
            else
            {
                // if no view filter, sync all active contacts
                viewFilter = new QueryExpression("contact");
                viewFilter.Criteria.AddCondition(
                    new ConditionExpression("statecode", ConditionOperator.Equal, 0));
            }

            viewFilter.ColumnSet.Columns.Clear();
            foreach (var link in viewFilter.LinkEntities)
            {
                link.Columns.Columns.Clear();
            }

            if (syncData.UpdatedFields != null && syncData.UpdatedFields.Length > 0)
            {
                viewFilter.ColumnSet.Columns.AddRange(syncData.UpdatedFields);
            }
            else
            {
                viewFilter.ColumnSet.Columns.AddRange(config.SyncFields);
            }

            // add required fields for syncing if they are not a part of the filter
            if (!viewFilter.ColumnSet.Columns.Contains(primaryEmail))
            {
                viewFilter.ColumnSet.Columns.Add(primaryEmail);
            }

            if (!viewFilter.ColumnSet.Columns.Contains("fullname"))
            {
                viewFilter.ColumnSet.Columns.Add("fullname");
            }

            viewFilter.AddOrder("modifiedon", OrderType.Ascending);
            viewFilter.PageInfo.Count = BATCH_AMOUNT;
            viewFilter.PageInfo.ReturnTotalRecordCount = true;

            return(viewFilter);
        }
Exemplo n.º 2
0
        internal void SyncContact(Entity target, Entity postImage, Entity preImage, bool isUpdate)
        {
            if (target == null || postImage == null)
            {
                tracer.Trace("Invalid target or postImage entity.");
                return;
            }

            if (campaignMonitorConfig == null)
            {
                tracer.Trace("Missing or invalid campaign monitor configuration.");
                return;
            }

            string emailField = SharedLogic.GetPrimaryEmailField(campaignMonitorConfig.SubscriberEmail);

            if (string.IsNullOrWhiteSpace(emailField) ||
                !postImage.Contains(emailField) || string.IsNullOrWhiteSpace(postImage[emailField].ToString()))
            {
                tracer.Trace("The email field to sync is missing or contains invalid data.");
                return;
            }

            if (campaignMonitorConfig.SyncViewId != Guid.Empty)
            {
                tracer.Trace("Testing the contact against the filter.");

                // Retrieve the view specified in the campmon_syncviewid field of the configuration record.
                var filterQuery = SharedLogic.GetConfigFilterQuery(orgService, campaignMonitorConfig.SyncViewId);

                // Modify the sync view fetch query to include a filter condition for the current contact id.Execute the modified query and check if the contact is returned.If it is, exit the plugin.
                if (!TestContactFitsFilter(filterQuery, target.Id))
                {
                    tracer.Trace("Contact does not fit the filter.");
                    return;
                }
            }
            else
            {
                if (target.Contains("statecode") && (target["statecode"] as OptionSetValue).Value == 1)
                {
                    tracer.Trace("Contact was not synced: no view is selected and this contact is deactivated.");
                    return;
                }
            }

            tracer.Trace("Contact fits the filter, or no filter is selected.");

            /*
             *  Create a campmon_message record with the following data:
             *      • campmon_sdkmessage = Plugin message (create or update)
             *      • campmon_data = JSON serialized sync data
             */
            var syncMessage = new Entity("campmon_message");
            var fields      = SharedLogic.ContactAttributesToSubscriberFields(orgService, tracer, target, campaignMonitorConfig.SyncFields.ToList());

            // Check that the plugin target has modified attributes that are included in the campmon_syncfields data. If there are not any sync fields in the target, exit the plugin.
            if (fields.Count <= 0)
            {
                tracer.Trace("There are no fields in the target that match the current fields being synced with Campaign Monitor.");
                return;
            }

            if (isUpdate && !target.Attributes.Contains(emailField))
            {
                fields.Add(new SubscriberCustomField {
                    Key = emailField, Value = postImage[emailField].ToString()
                });
            }
            else if (isUpdate && target.Attributes.Contains(emailField) && preImage[emailField].ToString() != postImage[emailField].ToString())
            {
                // if it contains primary email and isUpdate, then the primary email was changed and we need to do something to handle that
                var auth       = Authenticator.GetAuthentication(campaignMonitorConfig, orgService);
                var subscriber = new Subscriber(auth, campaignMonitorConfig.ListId);
                try
                {
                    subscriber.Update(preImage[emailField].ToString(), postImage[emailField].ToString(), postImage["fullname"].ToString(), null, false);
                }
                catch (CreatesendException csEx)
                {
                    if (csEx.Error.Code == "203")
                    {
                        subscriber.Add(postImage[emailField].ToString(), postImage["fullname"].ToString(), null, false);
                    }
                }
                catch (Exception ex)
                {
                    tracer.Trace("Exception type: {0}", ex.GetType().Name);
                    throw;
                }
            }

            if (isUpdate && !target.Attributes.Contains("fullname"))
            {
                fields.Add(new SubscriberCustomField {
                    Key = "fullname", Value = postImage["fullname"].ToString()
                });
            }

            var syncData = JsonConvert.SerializeObject(fields);

            syncMessage["campmon_name"]  = isUpdate ? "update" : "create";
            syncMessage["campmon_data"]  = syncData;
            syncMessage["campmon_email"] = emailField;
            orgService.Create(syncMessage);
        }