private bool SetCurrentWebhook(WebhookApiResponse webhook)
        {
            UserConnection userConnection = AppConnection.SystemUserConnection;
            EntitySchema   schema         = userConnection.EntitySchemaManager.FindInstanceByName("MandrillWebhook");
            Entity         entity         = schema.CreateEntity(userConnection);

            entity.SetDefColumnValues();
            entity.SetColumnValue("WebhookId", webhook.id);
            entity.SetColumnValue("AuthKey", webhook.auth_key);
            entity.SetColumnValue("WebhookURL", webhook.url);
            entity.SetColumnValue("Name", string.Format("{0}", webhook.id));
            return(entity.Save());
        }
        private WebhookApiResponse GetCurrentWebhook()
        {
            UserConnection userConnection = AppConnection.SystemUserConnection;
            var            result         = new WebhookApiResponse();
            var            esq            = new EntitySchemaQuery(userConnection.EntitySchemaManager, "MandrillWebhook");

            esq.PrimaryQueryColumn.IsAlwaysSelect = true;
            esq.AddColumn("WebhookId");
            esq.AddColumn("AuthKey");
            esq.AddColumn("WebhookURL");
            EntityCollection webhooks = esq.GetEntityCollection(userConnection);

            foreach (Entity webhook in webhooks)
            {
                var webhookId  = webhook.GetTypedColumnValue <int>("WebhookId");
                var authKey    = webhook.GetTypedColumnValue <string>("AuthKey");
                var webhookUrl = webhook.GetTypedColumnValue <string>("WebhookURL");
                result.auth_key = authKey;
                result.id       = webhookId;
                result.url      = webhookUrl;
                return(result);
            }
            return(result);
        }
        public WebhookResult SubmitWebhook()
        {
            var result = new WebhookResult();
            WebhookApiResponse currentWebhook = GetCurrentWebhook();
            //List webhooks registered on Mandrill account.
            WebhookResponse listWebhooks           = ListWebhook();
            UserConnection  userConnection         = AppConnection.SystemUserConnection;
            string          webhooksApplicationUrl =
                (string)Terrasoft.Core.Configuration.SysSettings.GetValue(userConnection, "WebhooksApplicationUrl");
            string webhookUrl = !string.IsNullOrEmpty(webhooksApplicationUrl)
                                ? webhooksApplicationUrl
                                : string.Format(
                "{0}{1}", WebUtilities.GetBaseApplicationUrl(HttpContext.Current.Request), WebHookWebService);
            string previousWebhookUrl = currentWebhook.url;

            currentWebhook.url = webhookUrl;
            if (!listWebhooks.Success)
            {
                result.ErrorDescription = Error;
                return(result);
            }
            if (listWebhooks.Webhook.Any(
                    x => (x.id == currentWebhook.id) && (x.auth_key == currentWebhook.auth_key) &&
                    (x.url == currentWebhook.url)))
            {
                result.Success = true;
                return(result);
            }
            //Remove webhooks that's already registered on the same url.
            foreach (WebhookApiResponse item in listWebhooks.Webhook)
            {
                if ((item.url != currentWebhook.url) && (item.url != previousWebhookUrl))
                {
                    continue;
                }
                if (!DeleteWebhook(item.id).Success)
                {
                    result.ErrorDescription = Error;
                    return(result);
                }
            }
            //Register new webhooks.
            WebhookResponse newWebhook = AddWebhook(webhookUrl, string.Empty);

            if (!newWebhook.Success)
            {
                result.ErrorDescription = Error;
                return(result);
            }
            //Remove old webhooks from DB.
            RemoveCurrentWebhook();
            //Save new webhooks to DB.
            bool isSaved = SetCurrentWebhook(newWebhook.Webhook[0]);

            if (!isSaved)
            {
                result.ErrorDescription = Error;
                return(result);
            }
            result.Success = true;
            return(result);
        }