[web] Redirect to plans page when previewing subscription change without an existing subscription (#33925)

GitOrigin-RevId: feb47fb519dd7872149d787a8543293cae66a908
This commit is contained in:
Antoine Clausse
2026-06-01 12:43:31 +02:00
committed by Copybot
parent db1deb1617
commit 58884231c1
2 changed files with 31 additions and 5 deletions
@@ -885,11 +885,22 @@ async function previewSubscription(req, res, next) {
}
}
const subscriptionChange =
await SubscriptionHandler.promises.previewSubscriptionChange(
userId,
planCode
)
let subscriptionChange
try {
subscriptionChange =
await SubscriptionHandler.promises.previewSubscriptionChange(
userId,
planCode
)
} catch (err) {
if (
err instanceof Error &&
err.constructor.name === 'PaymentServiceResourceNotFoundError'
) {
return res.redirect('/user/subscription/plans')
}
throw err
}
/** @type {PaymentMethod[]} */
const paymentMethod = await Modules.promises.hooks.fire(
'getPaymentMethod',
@@ -3,6 +3,7 @@ import sinon from 'sinon'
import MockRequest from '../helpers/MockRequest.mjs'
import MockResponse from '../helpers/MockResponse.mjs'
import SubscriptionErrors from '../../../../app/src/Features/Subscription/Errors.mjs'
import { PaymentServiceResourceNotFoundError } from '../../../../modules/subscriptions/app/src/PaymentServiceErrors.mjs'
import SubscriptionHelper from '../../../../app/src/Features/Subscription/SubscriptionHelper.mjs'
import { AI_ADD_ON_CODE } from '../../../../app/src/Features/Subscription/AiHelper.mjs'
@@ -1772,6 +1773,20 @@ describe('SubscriptionController', function () {
expect(ctx.res.render).not.to.have.been.called
})
it('redirects to the plans page when the user has no subscription to preview', async function (ctx) {
ctx.SubscriptionHandler.promises.previewSubscriptionChange = sinon
.stub()
.rejects(new PaymentServiceResourceNotFoundError('no subscription'))
ctx.res.redirect = sinon.stub()
await ctx.SubscriptionController.previewSubscription(ctx.req, ctx.res)
expect(ctx.res.redirect).to.have.been.calledWith(
'/user/subscription/plans'
)
expect(ctx.res.render).not.to.have.been.called
})
it('passes trialDisabledReason to the view when the user is ineligible for a free trial', async function (ctx) {
ctx.req.query = { planCode: 'collaborator_free_trial_7_days' }
ctx.PlansLocator.findLocalPlanInSettings.returns({