Browse Source

Merge pull request #25372 from overleaf/kh-support-canceling-pending-stripe-change

[web] cancel pending Stripe subscription change

GitOrigin-RevId: c1d21a7d1c3962c20d589b1dd10f6c2a4c8e4be4
M Fahru 1 year ago
parent
commit
c50bd6af89

+ 3 - 2
services/web/app/src/Features/Subscription/SubscriptionHandler.js

@@ -123,8 +123,9 @@ async function cancelPendingSubscriptionChange(user) {
     await LimitationsManager.promises.userHasSubscription(user)
 
   if (hasSubscription && subscription != null) {
-    await RecurlyClient.promises.removeSubscriptionChangeByUuid(
-      subscription.recurlySubscription_id
+    await Modules.promises.hooks.fire(
+      'cancelPendingPaidSubscriptionChange',
+      subscription
     )
   }
 }

+ 42 - 0
services/web/test/unit/src/Subscription/SubscriptionHandlerTests.js

@@ -319,6 +319,48 @@ describe('SubscriptionHandler', function () {
     })
   })
 
+  describe('cancelPendingSubscriptionChange', function () {
+    beforeEach(function () {
+      this.user.id = this.activeRecurlySubscription.account.account_code
+      this.User.findById = (userId, projection) => ({
+        exec: () => {
+          userId.should.equal(this.user.id)
+          return Promise.resolve(this.user)
+        },
+      })
+    })
+
+    it('should not fire cancelPendingPaidSubscriptionChange hook if user has no subscription', async function () {
+      this.LimitationsManager.promises.userHasSubscription.resolves({
+        hasSubscription: false,
+        subscription: null,
+      })
+      await this.SubscriptionHandler.promises.cancelPendingSubscriptionChange(
+        this.user,
+        this.plan_code
+      )
+      expect(this.Modules.promises.hooks.fire).to.not.have.been.calledWith(
+        'cancelPendingPaidSubscriptionChange',
+        sinon.match.any
+      )
+    })
+
+    it('should fire cancelPendingPaidSubscriptionChange to update a valid subscription', async function () {
+      this.LimitationsManager.promises.userHasSubscription.resolves({
+        hasSubscription: true,
+        subscription: this.subscription,
+      })
+      await this.SubscriptionHandler.promises.cancelPendingSubscriptionChange(
+        this.user,
+        this.plan_code
+      )
+      expect(this.Modules.promises.hooks.fire).to.have.been.calledWith(
+        'cancelPendingPaidSubscriptionChange',
+        this.subscription
+      )
+    })
+  })
+
   describe('cancelSubscription', function () {
     describe('with a user without a subscription', function () {
       beforeEach(async function () {