public async Task Should_Get_Has_Any_Successful_Attempt_In_Last_X_Record_Async()
        {
            (await _webhookSendAttemptStore.HasXConsecutiveFailAsync(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
            };

            await _webhookSendAttemptStore.InsertAsync(sendAttempt);

            sendAttempt.ResponseStatusCode = HttpStatusCode.Forbidden;

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

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

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

            (await _webhookSendAttemptStore.HasXConsecutiveFailAsync(sendAttempt.TenantId,
                                                                     sendAttempt.WebhookSubscriptionId, 3)).ShouldBe(false);
        }
Ejemplo n.º 2
0
        private async Task <bool> TryDeactivateSubscriptionIfReachedMaxConsecutiveFailCount(int?tenantId,
                                                                                            Guid subscriptionId)
        {
            if (!_webhooksConfiguration.IsAutomaticSubscriptionDeactivationEnabled)
            {
                return(false);
            }

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

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

            using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.Required))
            {
                await _webhookSubscriptionManager.ActivateWebhookSubscriptionAsync(subscriptionId, false);

                await uow.CompleteAsync();

                return(true);
            }
        }