From b8dc589303e30ae127d6445bba4cf36df07e7a39 Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Wed, 16 Aug 2023 16:26:14 +0200 Subject: [PATCH] Merge pull request #14241 from overleaf/msm-skip-subscription-deletion-groups Temporarily skip subscription deletion for managed groups GitOrigin-RevId: 6452a156e255fc599bdd25e38edb15659009a15a --- .../Subscription/SubscriptionUpdater.js | 14 ++++++++++++- .../Subscription/SubscriptionUpdaterTests.js | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/services/web/app/src/Features/Subscription/SubscriptionUpdater.js b/services/web/app/src/Features/Subscription/SubscriptionUpdater.js index 5c3e4c3ae2..b6c76c9461 100644 --- a/services/web/app/src/Features/Subscription/SubscriptionUpdater.js +++ b/services/web/app/src/Features/Subscription/SubscriptionUpdater.js @@ -8,6 +8,7 @@ const FeaturesHelper = require('./FeaturesHelper') const AnalyticsManager = require('../Analytics/AnalyticsManager') const { DeletedSubscription } = require('../../models/DeletedSubscription') const logger = require('@overleaf/logger') +const Features = require('../../infrastructure/Features') /** * Change the admin of the given subscription. @@ -228,7 +229,18 @@ async function updateSubscriptionFromRecurly( requesterData ) { if (recurlySubscription.state === 'expired') { - await deleteSubscription(subscription, requesterData) + const hasManagedUsersFeature = + Features.hasFeature('saas') && subscription?.groupPolicy != null + if (hasManagedUsersFeature) { + // If a payment lapses and if the group is managed, as a temporary measure we need to + // make sure that the group continues as-is and no destructive actions are taken. + logger.warn( + { subscriptionId: subscription._id }, + 'expired subscription has managedUsers feature, skipping deletion' + ) + } else { + await deleteSubscription(subscription, requesterData) + } return } const updatedPlanCode = recurlySubscription.plan.plan_code diff --git a/services/web/test/unit/src/Subscription/SubscriptionUpdaterTests.js b/services/web/test/unit/src/Subscription/SubscriptionUpdaterTests.js index 0eb28532fd..2c7d40ba32 100644 --- a/services/web/test/unit/src/Subscription/SubscriptionUpdaterTests.js +++ b/services/web/test/unit/src/Subscription/SubscriptionUpdaterTests.js @@ -143,6 +143,10 @@ describe('SubscriptionUpdater', function () { setUserPropertyForUser: sinon.stub(), } + this.Features = { + hasFeature: sinon.stub().returns(false), + } + this.SubscriptionUpdater = SandboxedModule.require(modulePath, { requires: { '../../models/Subscription': { @@ -157,6 +161,7 @@ describe('SubscriptionUpdater', function () { DeletedSubscription: this.DeletedSubscription, }, '../Analytics/AnalyticsManager': this.AnalyticsManager, + '../../infrastructure/Features': this.Features, }, }) }) @@ -267,6 +272,22 @@ describe('SubscriptionUpdater', function () { this.subscription, {} ) + this.SubscriptionModel.deleteOne.should.have.been.calledWith({ + _id: this.subscription._id, + }) + }) + + it('should not remove the subscription when expired if it has "managedUsers" feature', async function () { + this.Features.hasFeature.withArgs('saas').returns(true) + this.subscription.groupPolicy = { policy: true } + + this.recurlySubscription.state = 'expired' + await this.SubscriptionUpdater.promises.updateSubscriptionFromRecurly( + this.recurlySubscription, + this.subscription, + {} + ) + this.SubscriptionModel.deleteOne.should.not.have.been.called }) it('should update all the users features', async function () {