mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-01 23:11:09 -05:00
Refactor createSubscription in SubscriptionController to use async/await (#8938)
GitOrigin-RevId: 47fd56f35ac1d5727c7e53eeecedf200738c4e11
This commit is contained in:
parent
574d0eab12
commit
66998e2a00
2 changed files with 48 additions and 52 deletions
|
@ -261,7 +261,7 @@ async function interstitialPaymentPage(req, res) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSubscription(req, res, next) {
|
async function createSubscription(req, res) {
|
||||||
const user = SessionManager.getSessionUser(req.session)
|
const user = SessionManager.getSessionUser(req.session)
|
||||||
const recurlyTokenIds = {
|
const recurlyTokenIds = {
|
||||||
billing: req.body.recurly_token_id,
|
billing: req.body.recurly_token_id,
|
||||||
|
@ -270,47 +270,41 @@ function createSubscription(req, res, next) {
|
||||||
}
|
}
|
||||||
const { subscriptionDetails } = req.body
|
const { subscriptionDetails } = req.body
|
||||||
|
|
||||||
LimitationsManager.userHasV1OrV2Subscription(
|
const hasSubscription =
|
||||||
user,
|
await LimitationsManager.promises.userHasV1OrV2Subscription(user)
|
||||||
function (err, hasSubscription) {
|
|
||||||
if (err) {
|
|
||||||
return next(err)
|
|
||||||
}
|
|
||||||
if (hasSubscription) {
|
|
||||||
logger.warn({ user_id: user._id }, 'user already has subscription')
|
|
||||||
return res.sendStatus(409) // conflict
|
|
||||||
}
|
|
||||||
return SubscriptionHandler.createSubscription(
|
|
||||||
user,
|
|
||||||
subscriptionDetails,
|
|
||||||
recurlyTokenIds,
|
|
||||||
function (err) {
|
|
||||||
if (!err) {
|
|
||||||
return res.sendStatus(201)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (hasSubscription) {
|
||||||
err instanceof SubscriptionErrors.RecurlyTransactionError ||
|
logger.warn({ user_id: user._id }, 'user already has subscription')
|
||||||
err instanceof Errors.InvalidError
|
return res.sendStatus(409) // conflict
|
||||||
) {
|
}
|
||||||
logger.error({ err }, 'recurly transaction error, potential 422')
|
|
||||||
HttpErrorHandler.unprocessableEntity(
|
try {
|
||||||
req,
|
await SubscriptionHandler.promises.createSubscription(
|
||||||
res,
|
user,
|
||||||
err.message,
|
subscriptionDetails,
|
||||||
OError.getFullInfo(err).public
|
recurlyTokenIds
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
logger.warn(
|
res.sendStatus(201)
|
||||||
{ err, user_id: user._id },
|
} catch (err) {
|
||||||
'something went wrong creating subscription'
|
if (
|
||||||
)
|
err instanceof SubscriptionErrors.RecurlyTransactionError ||
|
||||||
next(err)
|
err instanceof Errors.InvalidError
|
||||||
}
|
) {
|
||||||
}
|
logger.error({ err }, 'recurly transaction error, potential 422')
|
||||||
|
HttpErrorHandler.unprocessableEntity(
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
err.message,
|
||||||
|
OError.getFullInfo(err).public
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
logger.warn(
|
||||||
|
{ err, user_id: user._id },
|
||||||
|
'something went wrong creating subscription'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function successfulSubscription(req, res) {
|
async function successfulSubscription(req, res) {
|
||||||
|
@ -605,7 +599,7 @@ module.exports = {
|
||||||
paymentPage: expressify(paymentPage),
|
paymentPage: expressify(paymentPage),
|
||||||
userSubscriptionPage: expressify(userSubscriptionPage),
|
userSubscriptionPage: expressify(userSubscriptionPage),
|
||||||
interstitialPaymentPage: expressify(interstitialPaymentPage),
|
interstitialPaymentPage: expressify(interstitialPaymentPage),
|
||||||
createSubscription,
|
createSubscription: expressify(createSubscription),
|
||||||
successfulSubscription: expressify(successfulSubscription),
|
successfulSubscription: expressify(successfulSubscription),
|
||||||
cancelSubscription,
|
cancelSubscription,
|
||||||
canceledSubscription,
|
canceledSubscription,
|
||||||
|
|
|
@ -492,7 +492,7 @@ describe('SubscriptionController', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should send the user and subscriptionId to the handler', function (done) {
|
it('should send the user and subscriptionId to the handler', function (done) {
|
||||||
this.SubscriptionHandler.createSubscription
|
this.SubscriptionHandler.promises.createSubscription
|
||||||
.calledWithMatch(
|
.calledWithMatch(
|
||||||
this.user,
|
this.user,
|
||||||
this.subscriptionDetails,
|
this.subscriptionDetails,
|
||||||
|
@ -502,7 +502,7 @@ describe('SubscriptionController', function () {
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should redurect to the subscription page', function (done) {
|
it('should redirect to the subscription page', function (done) {
|
||||||
this.res.sendStatus.calledWith(201).should.equal(true)
|
this.res.sendStatus.calledWith(201).should.equal(true)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
@ -510,11 +510,13 @@ describe('SubscriptionController', function () {
|
||||||
|
|
||||||
describe('createSubscription with errors', function () {
|
describe('createSubscription with errors', function () {
|
||||||
it('should handle users with subscription', function (done) {
|
it('should handle users with subscription', function (done) {
|
||||||
this.LimitationsManager.userHasV1OrV2Subscription.yields(null, true)
|
this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(true)
|
||||||
this.SubscriptionController.createSubscription(this.req, {
|
this.SubscriptionController.createSubscription(this.req, {
|
||||||
sendStatus: status => {
|
sendStatus: status => {
|
||||||
expect(status).to.equal(409)
|
expect(status).to.equal(409)
|
||||||
this.SubscriptionHandler.createSubscription.called.should.equal(false)
|
this.SubscriptionHandler.promises.createSubscription.called.should.equal(
|
||||||
|
false
|
||||||
|
)
|
||||||
|
|
||||||
done()
|
done()
|
||||||
},
|
},
|
||||||
|
@ -523,8 +525,8 @@ describe('SubscriptionController', function () {
|
||||||
|
|
||||||
it('should handle 3DSecure errors', function (done) {
|
it('should handle 3DSecure errors', function (done) {
|
||||||
this.next = sinon.stub()
|
this.next = sinon.stub()
|
||||||
this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
|
this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(false)
|
||||||
this.SubscriptionHandler.createSubscription.yields(
|
this.SubscriptionHandler.promises.createSubscription.rejects(
|
||||||
new SubscriptionErrors.RecurlyTransactionError({})
|
new SubscriptionErrors.RecurlyTransactionError({})
|
||||||
)
|
)
|
||||||
this.HttpErrorHandler.unprocessableEntity = sinon.spy(
|
this.HttpErrorHandler.unprocessableEntity = sinon.spy(
|
||||||
|
@ -540,8 +542,8 @@ describe('SubscriptionController', function () {
|
||||||
|
|
||||||
it('should handle validation errors', function (done) {
|
it('should handle validation errors', function (done) {
|
||||||
this.next = sinon.stub()
|
this.next = sinon.stub()
|
||||||
this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
|
this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(false)
|
||||||
this.SubscriptionHandler.createSubscription.yields(
|
this.SubscriptionHandler.promises.createSubscription.rejects(
|
||||||
new Errors.InvalidError('invalid error test')
|
new Errors.InvalidError('invalid error test')
|
||||||
)
|
)
|
||||||
this.HttpErrorHandler.unprocessableEntity = sinon.spy(
|
this.HttpErrorHandler.unprocessableEntity = sinon.spy(
|
||||||
|
@ -556,8 +558,8 @@ describe('SubscriptionController', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle recurly errors', function (done) {
|
it('should handle recurly errors', function (done) {
|
||||||
this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
|
this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(false)
|
||||||
this.SubscriptionHandler.createSubscription.yields(
|
this.SubscriptionHandler.promises.createSubscription.rejects(
|
||||||
new SubscriptionErrors.RecurlyTransactionError({})
|
new SubscriptionErrors.RecurlyTransactionError({})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -574,8 +576,8 @@ describe('SubscriptionController', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle invalid error', function (done) {
|
it('should handle invalid error', function (done) {
|
||||||
this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
|
this.LimitationsManager.promises.userHasV1OrV2Subscription.resolves(false)
|
||||||
this.SubscriptionHandler.createSubscription.yields(
|
this.SubscriptionHandler.promises.createSubscription.rejects(
|
||||||
new Errors.InvalidError({})
|
new Errors.InvalidError({})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue