public void Should_Get_Has_Any_Successful_Attempt_In_Last_X_Record_Sync()
        {
            _webhookSendAttemptStore.HasXConsecutiveFail(null,//if there is no record should return true
                                                         Guid.NewGuid(), 2).ShouldBe(false);

            var webhookEventId = CreateAndGetIdWebhookEvent();
            var sendAttempt    = new WebhookSendAttempt()
            {
                WebhookEventId        = webhookEventId,
                WebhookSubscriptionId = Guid.NewGuid(),
                ResponseStatusCode    = HttpStatusCode.OK
            };

            _webhookSendAttemptStore.Insert(sendAttempt);

            sendAttempt.ResponseStatusCode = HttpStatusCode.Forbidden;

            sendAttempt.Id           = Guid.Empty;
            sendAttempt.CreationTime = default;
            Thread.Sleep(1000);
            _webhookSendAttemptStore.Insert(sendAttempt);

            sendAttempt.Id           = Guid.Empty;
            sendAttempt.CreationTime = default;
            Thread.Sleep(1000);
            _webhookSendAttemptStore.Insert(sendAttempt);

            _webhookSendAttemptStore.HasXConsecutiveFail(sendAttempt.TenantId,
                                                         sendAttempt.WebhookSubscriptionId, 2).ShouldBe(true);

            _webhookSendAttemptStore.HasXConsecutiveFail(sendAttempt.TenantId,
                                                         sendAttempt.WebhookSubscriptionId, 3).ShouldBe(false);
        }
        private bool TryDeactivateSubscriptionIfReachedMaxConsecutiveFailCount(long?tenantId, Guid subscriptionId)
        {
            if (!_webhooksConfiguration.IsAutomaticSubscriptionDeactivationEnabled)
            {
                return(false);
            }

            var hasXConsecutiveFail = _webhookSendAttemptStore
                                      .HasXConsecutiveFail(
                tenantId,
                subscriptionId,
                _webhooksConfiguration.MaxConsecutiveFailCountBeforeDeactivateSubscription
                );

            if (!hasXConsecutiveFail)
            {
                return(false);
            }

            using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.Required))
            {
                _webhookSubscriptionManager.ActivateWebhookSubscription(subscriptionId, false);
                uow.Complete();
                return(true);
            }
        }