Explorar o código

Merge pull request #34297 from overleaf/rh-prev-plan-type-cancel

Update previous_plan_type on subscription expiry

GitOrigin-RevId: 19381e5516fdbfd2650a9a5b94b61791e0da909f
roo hutton hai 4 meses
pai
achega
757735b075

+ 23 - 0
services/web/app/src/Features/Subscription/SubscriptionUpdater.mjs

@@ -508,10 +508,33 @@ async function handleExpiredSubscription(subscription, requesterData) {
       )
     } else {
       await deleteSubscription(subscription, requesterData)
+      _setPreviousPlanTypeOnExpiry(subscription)
     }
   }
 }
 
+function _setPreviousPlanTypeOnExpiry(subscription) {
+  const previousPlanType = CustomerIoPlanHelpers.normalizePlanType({
+    plan: {
+      planCode: subscription.planCode,
+      groupPlan: subscription.groupPlan,
+    },
+  })
+  if (!previousPlanType) {
+    return
+  }
+  Modules.promises.hooks
+    .fire('setUserProperties', subscription.admin_id, {
+      previous_plan_type: previousPlanType,
+    })
+    .catch(err => {
+      logger.warn(
+        { err, userId: subscription.admin_id },
+        'Failed to set previous_plan_type in customer.io'
+      )
+    })
+}
+
 async function _sendSubscriptionEvent(userId, subscriptionId, event) {
   const subscription = await Subscription.findOne(
     { _id: subscriptionId },

+ 42 - 0
services/web/test/unit/src/Subscription/SubscriptionUpdater.test.mjs

@@ -707,6 +707,48 @@ describe('SubscriptionUpdater', function () {
     })
   })
 
+  describe('handleExpiredSubscription', function () {
+    it("should set the admin's previous_plan_type in customer.io to the expired plan when the subscription is deleted", async function (ctx) {
+      await ctx.SubscriptionUpdater.promises.handleExpiredSubscription(
+        ctx.subscription,
+        {}
+      )
+      expect(ctx.Modules.promises.hooks.fire).to.have.been.calledWith(
+        'setUserProperties',
+        ctx.subscription.admin_id,
+        { previous_plan_type: 'student' }
+      )
+    })
+
+    it('should not set previous_plan_type when the subscription is not deleted (managed users)', async function (ctx) {
+      ctx.Features.hasFeature.withArgs('saas').returns(true)
+      ctx.subscription.managedUsersEnabled = true
+      await ctx.SubscriptionUpdater.promises.handleExpiredSubscription(
+        ctx.subscription,
+        {}
+      )
+      expect(ctx.Modules.promises.hooks.fire).to.not.have.been.calledWith(
+        'setUserProperties',
+        ctx.subscription.admin_id,
+        { previous_plan_type: 'student' }
+      )
+    })
+
+    it('should not set previous_plan_type when the subscription is not deleted (group SSO)', async function (ctx) {
+      ctx.Features.hasFeature.withArgs('saas').returns(true)
+      ctx.subscription.ssoConfig = new ObjectId('abc123abc123abc123abc123')
+      await ctx.SubscriptionUpdater.promises.handleExpiredSubscription(
+        ctx.subscription,
+        {}
+      )
+      expect(ctx.Modules.promises.hooks.fire).to.not.have.been.calledWith(
+        'setUserProperties',
+        ctx.subscription.admin_id,
+        { previous_plan_type: 'student' }
+      )
+    })
+  })
+
   describe('addUserToGroup', function () {
     it('should add the user ids to the group as a set', async function (ctx) {
       ctx.SubscriptionModel.findOne = sinon