public override void HandleWebHook(UserConnection userConnection)
        {
            int hyperLinkRId = WebHookUtilities.GetHyperLinkRId(userConnection, HyperLink, EmailId);

            if (hyperLinkRId == 0)
            {
                return;
            }
            var selectQuery = new Select(userConnection)
                              .Column("BulkEmail", "RId")
                              .Column("Contact", "RId")
                              .Column(Column.Parameter(hyperLinkRId))
                              .Column(Column.Parameter(EventDate))
                              .Column(Column.Parameter(RecipientUId))
                              .From("MandrillRecipient")
                              .InnerJoin("BulkEmail").On("MandrillRecipient", "BulkEmailRId").IsEqual("BulkEmail", "RId")
                              .InnerJoin("Contact").On("MandrillRecipient", "ContactRId").IsEqual("Contact", "RId")
                              .Where().Not().Exists(new Select(userConnection)
                                                    .Column(Column.Const(1))
                                                    .From("MandrillClicks")
                                                    .Where("MandrillClicks", "RecipientUId").IsEqual(Column.Parameter(RecipientUId))
                                                    .And("MandrillClicks", "HyperlinkRId").IsEqual(Column.Parameter(hyperLinkRId)))
                              .And("MandrillRecipient", "UId").IsEqual(Column.Parameter(RecipientUId)) as Select;

            selectQuery.SpecifyNoLockHints();
            new InsertSelect(userConnection).Into("MandrillClicks")
            .Set("BulkEmailRId", "ContactRId", "HyperlinkRId", "TimeStamp", "RecipientUId")
            .FromSelect(selectQuery).Execute();
        }
        private IEnumerable <Guid> ProcessWebHooks(Dictionary <Guid, IEnumerable <BaseWebHook> > webHooks)
        {
            var lastWebHooks = WebHookUtilities.GetLastWebHooks(webHooks);

            lastWebHooks = WebHookUtilities.FilterUnsubscribeWebHook(_userConnection, lastWebHooks);
            var failedWebhooksBatch = new List <Guid>();

            using (DBExecutor dbExecutor = _userConnection.EnsureDBConnection()) {
                foreach (var webHook in lastWebHooks)
                {
                    var result = webHook.Value.TryHandleWebHook(dbExecutor, _userConnection);
                    if (!result)
                    {
                        failedWebhooksBatch.AddRange(webHook.Key);
                    }
                }
            }
            var processedWebHooksId = lastWebHooks.SelectMany(x => x.Key).Where(x => !failedWebhooksBatch.Contains(x));

            if (processedWebHooksId.Any())
            {
                DeleteWebHooks(processedWebHooksId.Distinct());
            }
            return(failedWebhooksBatch);
        }
        public override void HandleWebHook(UserConnection userConnection)
        {
            int hyperLinkRId = WebHookUtilities.GetHyperLinkRId(userConnection, HyperLink, EmailId);

            if (hyperLinkRId == 0)
            {
                return;
            }
            new Update(userConnection, "BulkEmailHyperlink")
            .Set("ClicksCount", Column.Parameter(Clicks))
            .Where("BulkEmailId").IsEqual(Column.Parameter(EmailId))
            .And("RId").IsEqual(Column.Parameter(hyperLinkRId)).Execute();
        }
        public override void HandleWebHook(UserConnection userConnection)
        {
            long timeStamp = WebHookUtilities.GetEpochTime(EventDate);

            new Insert(userConnection)
            .Into("MandrillMessageResponse")
            .Set("MandrillId", Column.Parameter(RecipientUId))
            .Set("ResponseCode", Column.Parameter(ResponseCode))
            .Set("Opens", Column.Parameter(Opens))
            .Set("Clicks", Column.Parameter(Clicks))
            .Set("TimeStamp", Column.Parameter(timeStamp))
            .Set("BulkEmailId", Column.Parameter(EmailId))
            .Execute();
        }
        /// <summary>
        /// Processes all web hooks from storage.
        /// </summary>
        public void ProcessWebHooks()
        {
            if (!IsHandlePeriod())
            {
                return;
            }
            int actualizeAudiencePackagesNumber = GetNumberOfPackagesForActualizingAudience();
            int packagesCounter = 0;

            try {
                int         numberOfReadPackages;
                List <Guid> failedIds;
                Dictionary <Guid, JArray> webHooks = new Dictionary <Guid, JArray>();
                while ((numberOfReadPackages = GetRawWebHooks(out webHooks, out failedIds)) > 0)
                {
                    packagesCounter += numberOfReadPackages;
                    var webHookList = webHooks.ToDictionary(x => x.Key,
                                                            x => x.Value.AsJEnumerable()
                                                            .Select(jToken => WebHookUtilities.DeserializeWebHook(jToken.ToString())));
                    if (webHookList.Any())
                    {
                        var failedIdsBatch = ProcessWebHooks(webHookList);
                        failedIds.AddRange(failedIdsBatch);
                    }
                    if (packagesCounter >= actualizeAudiencePackagesNumber)
                    {
                        packagesCounter = 0;
                        WebHookUtilities.ActualizeMailingRecipients(_userConnection);
                    }
                    if (failedIds != null && failedIds.Any())
                    {
                        SetWebHooksFailed(failedIds);
                    }
                }
                if (packagesCounter > 0)
                {
                    WebHookUtilities.ActualizeMailingRecipients(_userConnection);
                }
            } catch (Exception e) {
                MailingUtilities.WebHookLog.ErrorFormat("[WebHookHandler.ProcessWebHooks]", e);
            }
        }