From 837a0bb02ec64829a8f32385a77e9c19e62401ed Mon Sep 17 00:00:00 2001 From: gbuomprisco Date: Sun, 14 Jul 2024 11:05:18 +0800 Subject: [PATCH] Update subscription cancellation logic The previous implementation attempted to cancel a subscription without checking its status. This commit adds a check to see if the subscription is already cancelled before attempting to cancel it, avoiding unnecessary cancellation requests. This improves the efficiency and reliability of the subscription management process. --- .../billing-webhooks/billing-webhooks.service.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/billing/gateway/src/server/services/billing-webhooks/billing-webhooks.service.ts b/packages/billing/gateway/src/server/services/billing-webhooks/billing-webhooks.service.ts index 587b14aaa..2c38625de 100644 --- a/packages/billing/gateway/src/server/services/billing-webhooks/billing-webhooks.service.ts +++ b/packages/billing/gateway/src/server/services/billing-webhooks/billing-webhooks.service.ts @@ -23,7 +23,14 @@ class BillingWebhooksService { async handleSubscriptionDeletedWebhook(subscription: Subscription) { const gateway = createBillingGatewayService(subscription.billing_provider); - await gateway.cancelSubscription({ + const subscriptionData = await gateway.getSubscription(subscription.id); + const isCanceled = subscriptionData.status === 'canceled'; + + if (isCanceled) { + return; + } + + return gateway.cancelSubscription({ subscriptionId: subscription.id, }); }