Преглед изворни кода

Check if AI assist standalone plan is used in shouldPlanChangeAtTermEnd (#26272)

GitOrigin-RevId: d6737ea28071d565109dba695876b6fbf3f5daa2
Domagoj Kriskovic пре 1 година
родитељ
комит
92626393ec

+ 4 - 1
services/web/app/src/Features/Subscription/PaymentProviderEntities.js

@@ -8,7 +8,8 @@
 const OError = require('@overleaf/o-error')
 const { DuplicateAddOnError, AddOnNotPresentError } = require('./Errors')
 const PlansLocator = require('./PlansLocator')
-const SubscriptionHelper = require('./SubscriptionHelper')
+
+let SubscriptionHelper = null // Work around circular import (loaded at the bottom of the file)
 
 const AI_ADD_ON_CODE = 'assistant'
 const MEMBERS_LIMIT_ADD_ON_CODE = 'additional-license'
@@ -636,3 +637,5 @@ module.exports = {
   subscriptionChangeIsAiAssistUpgrade,
   PaymentProviderImmediateCharge,
 }
+
+SubscriptionHelper = require('./SubscriptionHelper')

+ 9 - 0
services/web/app/src/Features/Subscription/SubscriptionHelper.js

@@ -1,11 +1,20 @@
 const { formatCurrency } = require('../../util/currency')
 const GroupPlansData = require('./GroupPlansData')
+const { isStandaloneAiAddOnPlanCode } = require('./PaymentProviderEntities')
 
 /**
  * If the user changes to a less expensive plan, we shouldn't apply the change immediately.
  * This is to avoid unintended/artifical credits on users Recurly accounts.
  */
 function shouldPlanChangeAtTermEnd(oldPlan, newPlan) {
+  if (
+    oldPlan.annual === newPlan.annual &&
+    isStandaloneAiAddOnPlanCode(oldPlan.planCode) &&
+    !isStandaloneAiAddOnPlanCode(newPlan.planCode)
+  ) {
+    // changing from an standalone AI add-on plan to a non-AI plan should not be considered a downgrade
+    return false
+  }
   return oldPlan.price_in_cents > newPlan.price_in_cents
 }
 

+ 3 - 1
services/web/test/unit/src/Subscription/PaymentProviderEntitiesTest.js

@@ -11,6 +11,7 @@ const {
   PaymentProviderSubscription,
   PaymentProviderSubscriptionAddOnUpdate,
 } = require('../../../../app/src/Features/Subscription/PaymentProviderEntities')
+const SubscriptionHelper = require('../../../../app/src/Features/Subscription/SubscriptionHelper')
 
 const MODULE_PATH =
   '../../../../app/src/Features/Subscription/PaymentProviderEntities'
@@ -32,6 +33,7 @@ describe('PaymentProviderEntities', function () {
         requires: {
           '@overleaf/settings': this.Settings,
           './Errors': Errors,
+          './SubscriptionHelper': SubscriptionHelper,
         },
       })
     })
@@ -154,7 +156,7 @@ describe('PaymentProviderEntities', function () {
           expect(changeRequest).to.deep.equal(
             new PaymentProviderSubscriptionChangeRequest({
               subscription: this.subscription,
-              timeframe: 'term_end',
+              timeframe: 'now',
               planCode: 'cheap-plan',
               addOnUpdates: [
                 new PaymentProviderSubscriptionAddOnUpdate({