Refactor successful subcription to use async/await (#8377)

GitOrigin-RevId: 5748486848c6e0576c974e5595acbcdee3daf7f3
This commit is contained in:
M Fahru 2022-06-14 05:36:49 -04:00 committed by Copybot
parent 34bc2ac9ec
commit 0204d7947f
2 changed files with 33 additions and 43 deletions

View file

@ -300,24 +300,21 @@ function createSubscription(req, res, next) {
)
}
function successfulSubscription(req, res, next) {
async function successfulSubscription(req, res) {
const user = SessionManager.getSessionUser(req.session)
return SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
user,
function (error, { personalSubscription } = {}) {
if (error) {
return next(error)
}
if (personalSubscription == null) {
res.redirect('/user/subscription/plans')
} else {
res.render('subscriptions/successful_subscription', {
title: 'thank_you',
personalSubscription,
})
}
}
)
const { personalSubscription } =
await SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel(
user
)
if (!personalSubscription) {
res.redirect('/user/subscription/plans')
} else {
res.render('subscriptions/successful_subscription', {
title: 'thank_you',
personalSubscription,
})
}
}
function cancelSubscription(req, res, next) {
@ -585,7 +582,7 @@ module.exports = {
userSubscriptionPage: expressify(userSubscriptionPage),
interstitialPaymentPage: expressify(interstitialPaymentPage),
createSubscription,
successfulSubscription,
successfulSubscription: expressify(successfulSubscription),
cancelSubscription,
canceledSubscription,
cancelV1Subscription,

View file

@ -381,31 +381,26 @@ describe('SubscriptionController', function () {
})
describe('successfulSubscription', function () {
it('without a personnal subscription', function (done) {
this.SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(
1,
null,
it('without a personal subscription', function (done) {
this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
{}
)
this.res.callback = () => {
assert.equal(this.res.redirectedTo, '/user/subscription/plans')
this.res.redirect = url => {
url.should.equal('/user/subscription/plans')
done()
}
this.SubscriptionController.successfulSubscription(this.req, this.res)
})
it('with a personnal subscription', function (done) {
this.SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(
1,
null,
{ personalSubscription: 'foo' }
it('with a personal subscription', function (done) {
this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
{
personalSubscription: 'foo',
}
)
this.res.callback = () => {
assert.equal(
this.res.renderedTemplate,
'subscriptions/successful_subscription'
)
assert.deepEqual(this.res.renderedVariables, {
this.res.render = (url, variables) => {
url.should.equal('subscriptions/successful_subscription')
assert.deepEqual(variables, {
title: 'thank_you',
personalSubscription: 'foo',
})
@ -414,20 +409,18 @@ describe('SubscriptionController', function () {
this.SubscriptionController.successfulSubscription(this.req, this.res)
})
it('with an error', function () {
const next = sinon.stub()
const error = new Error('test')
this.SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(
1,
error,
it('with an error', function (done) {
this.SubscriptionViewModelBuilder.promises.buildUsersSubscriptionViewModel.resolves(
undefined
)
this.SubscriptionController.successfulSubscription(
this.req,
this.res,
next
error => {
assert.isNotNull(error)
done()
}
)
sinon.assert.calledWith(next, error)
})
})