mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-25 19:42:27 +00:00
Merge pull request #16912 from overleaf/dp-mongoose-callback-referal-features
Promisify ReferalFeatures and ReferalFeaturesTests GitOrigin-RevId: 25c1d80c5aea2883f62b95b8646a5670340c5cf2
This commit is contained in:
parent
144e22c209
commit
d4e9d1bd89
2 changed files with 40 additions and 39 deletions
|
@ -1,30 +1,23 @@
|
|||
const _ = require('lodash')
|
||||
const { promisify } = require('util')
|
||||
const { callbackify } = require('util')
|
||||
const { User } = require('../../models/User')
|
||||
const Settings = require('@overleaf/settings')
|
||||
|
||||
let ReferalFeatures
|
||||
|
||||
module.exports = ReferalFeatures = {
|
||||
getBonusFeatures(userId, callback) {
|
||||
if (callback == null) {
|
||||
callback = function () {}
|
||||
}
|
||||
const ReferalFeatures = {
|
||||
async getBonusFeatures(userId) {
|
||||
const query = { _id: userId }
|
||||
User.findOne(query, { refered_user_count: 1 }, function (error, user) {
|
||||
if (error) {
|
||||
return callback(error)
|
||||
}
|
||||
if (user == null) {
|
||||
return callback(new Error(`user not found ${userId} for assignBonus`))
|
||||
}
|
||||
if (user.refered_user_count != null && user.refered_user_count > 0) {
|
||||
const newFeatures = ReferalFeatures._calculateFeatures(user)
|
||||
callback(null, newFeatures)
|
||||
} else {
|
||||
callback(null, {})
|
||||
}
|
||||
})
|
||||
const user = await User.findOne(query, { refered_user_count: 1 }).exec()
|
||||
|
||||
if (user == null) {
|
||||
throw new Error(`user not found ${userId} for assignBonus`)
|
||||
}
|
||||
|
||||
if (user.refered_user_count != null && user.refered_user_count > 0) {
|
||||
const newFeatures = ReferalFeatures._calculateFeatures(user)
|
||||
return newFeatures
|
||||
}
|
||||
|
||||
return {}
|
||||
},
|
||||
|
||||
_calculateFeatures(user) {
|
||||
|
@ -49,6 +42,7 @@ module.exports = ReferalFeatures = {
|
|||
},
|
||||
}
|
||||
|
||||
module.exports.promises = {
|
||||
getBonusFeatures: promisify(module.exports.getBonusFeatures),
|
||||
module.exports = {
|
||||
getBonusFeatures: callbackify(ReferalFeatures.getBonusFeatures),
|
||||
promises: ReferalFeatures,
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const SandboxedModule = require('sandboxed-module')
|
||||
const sinon = require('sinon')
|
||||
const { expect } = require('chai')
|
||||
const modulePath = require('path').join(
|
||||
__dirname,
|
||||
'../../../../app/src/Features/Referal/ReferalFeatures.js'
|
||||
|
@ -15,7 +16,6 @@ describe('ReferalFeatures', function () {
|
|||
'@overleaf/settings': (this.Settings = {}),
|
||||
},
|
||||
})
|
||||
this.callback = sinon.stub()
|
||||
this.referal_id = 'referal-id-123'
|
||||
this.referal_medium = 'twitter'
|
||||
this.user_id = 'user-id-123'
|
||||
|
@ -23,7 +23,7 @@ describe('ReferalFeatures', function () {
|
|||
})
|
||||
|
||||
describe('getBonusFeatures', function () {
|
||||
beforeEach(function () {
|
||||
beforeEach(async function () {
|
||||
this.refered_user_count = 3
|
||||
this.Settings.bonus_features = {
|
||||
3: {
|
||||
|
@ -37,23 +37,25 @@ describe('ReferalFeatures', function () {
|
|||
features: { collaborators: 1, dropbox: false, versioning: false },
|
||||
}
|
||||
|
||||
this.User.findOne = sinon.stub().callsArgWith(2, null, stubbedUser)
|
||||
this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
|
||||
this.User.findOne = sinon.stub().returns({
|
||||
exec: sinon.stub().resolves(stubbedUser),
|
||||
})
|
||||
this.features = await this.ReferalFeatures.promises.getBonusFeatures(
|
||||
this.user_id
|
||||
)
|
||||
})
|
||||
|
||||
it('should get the users number of refered user', function () {
|
||||
this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
|
||||
})
|
||||
|
||||
it('should call the callback with the features', function () {
|
||||
this.callback
|
||||
.calledWith(null, this.Settings.bonus_features[3])
|
||||
.should.equal(true)
|
||||
it('should return the features', function () {
|
||||
expect(this.features).to.equal(this.Settings.bonus_features[3])
|
||||
})
|
||||
})
|
||||
|
||||
describe('when the user is not at a bonus level', function () {
|
||||
beforeEach(function () {
|
||||
beforeEach(async function () {
|
||||
this.refered_user_count = 0
|
||||
this.Settings.bonus_features = {
|
||||
1: {
|
||||
|
@ -62,18 +64,23 @@ describe('ReferalFeatures', function () {
|
|||
versioning: false,
|
||||
},
|
||||
}
|
||||
this.User.findOne = sinon
|
||||
.stub()
|
||||
.callsArgWith(2, null, { refered_user_count: this.refered_user_count })
|
||||
this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
|
||||
this.User.findOne = sinon.stub().returns({
|
||||
exec: sinon
|
||||
.stub()
|
||||
.resolves({ refered_user_count: this.refered_user_count }),
|
||||
})
|
||||
|
||||
this.features = await this.ReferalFeatures.promises.getBonusFeatures(
|
||||
this.user_id
|
||||
)
|
||||
})
|
||||
|
||||
it('should get the users number of refered user', function () {
|
||||
this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
|
||||
})
|
||||
|
||||
it('should call the callback with no features', function () {
|
||||
this.callback.calledWith(null, {}).should.equal(true)
|
||||
it('should return an empty feature set', function () {
|
||||
expect(this.features).to.be.empty
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue