mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Refactor successful subcription to use async/await (#8377)
GitOrigin-RevId: 5748486848c6e0576c974e5595acbcdee3daf7f3
This commit is contained in:
parent
34bc2ac9ec
commit
0204d7947f
2 changed files with 33 additions and 43 deletions
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue