mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Merge pull request #16911 from overleaf/dp-mongoose-callback-referal-allocator
Promisify ReferalAllocator and ReferalAllocatorTests GitOrigin-RevId: 0c68ec6176ac440504fc6501e0fad161d83d3541
This commit is contained in:
parent
d4e9d1bd89
commit
68f011378d
2 changed files with 47 additions and 72 deletions
|
@ -1,33 +1,22 @@
|
||||||
const OError = require('@overleaf/o-error')
|
const OError = require('@overleaf/o-error')
|
||||||
const { User } = require('../../models/User')
|
const { User } = require('../../models/User')
|
||||||
const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
|
const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
|
||||||
const { promisify } = require('@overleaf/promise-utils')
|
const { callbackify } = require('@overleaf/promise-utils')
|
||||||
|
|
||||||
function allocate(
|
async function allocate(referalId, newUserId, referalSource, referalMedium) {
|
||||||
referalId,
|
|
||||||
newUserId,
|
|
||||||
referalSource,
|
|
||||||
referalMedium,
|
|
||||||
callback
|
|
||||||
) {
|
|
||||||
if (callback == null) {
|
|
||||||
callback = function () {}
|
|
||||||
}
|
|
||||||
if (referalId == null) {
|
if (referalId == null) {
|
||||||
return callback(null)
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const query = { referal_id: referalId }
|
const query = { referal_id: referalId }
|
||||||
User.findOne(query, { _id: 1 }, function (error, user) {
|
const user = await User.findOne(query, { _id: 1 }).exec()
|
||||||
if (error != null) {
|
if (user == null || user._id == null) {
|
||||||
return callback(error)
|
return null
|
||||||
}
|
}
|
||||||
if (user == null || user._id == null) {
|
|
||||||
return callback(null)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (referalSource === 'bonus') {
|
if (referalSource === 'bonus') {
|
||||||
User.updateOne(
|
try {
|
||||||
|
await User.updateOne(
|
||||||
query,
|
query,
|
||||||
{
|
{
|
||||||
$push: {
|
$push: {
|
||||||
|
@ -37,27 +26,23 @@ function allocate(
|
||||||
refered_user_count: 1,
|
refered_user_count: 1,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{},
|
{}
|
||||||
function (err) {
|
).exec()
|
||||||
if (err != null) {
|
} catch (err) {
|
||||||
OError.tag(err, 'something went wrong allocating referal', {
|
OError.tag(err, 'something went wrong allocating referal', {
|
||||||
referalId,
|
referalId,
|
||||||
newUserId,
|
newUserId,
|
||||||
})
|
})
|
||||||
return callback(err)
|
throw err
|
||||||
}
|
|
||||||
FeaturesUpdater.refreshFeatures(user._id, 'referral', callback)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
callback()
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
return await FeaturesUpdater.promises.refreshFeatures(user._id, 'referral')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
allocate,
|
allocate: callbackify(allocate),
|
||||||
promises: {
|
promises: {
|
||||||
allocate: promisify(allocate),
|
allocate,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,28 +16,30 @@ describe('ReferalAllocator', function () {
|
||||||
'@overleaf/settings': (this.Settings = {}),
|
'@overleaf/settings': (this.Settings = {}),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
this.callback = sinon.stub()
|
|
||||||
this.referal_id = 'referal-id-123'
|
this.referal_id = 'referal-id-123'
|
||||||
this.referal_medium = 'twitter'
|
this.referal_medium = 'twitter'
|
||||||
this.user_id = 'user-id-123'
|
this.user_id = 'user-id-123'
|
||||||
this.new_user_id = 'new-user-id-123'
|
this.new_user_id = 'new-user-id-123'
|
||||||
this.FeaturesUpdater.refreshFeatures = sinon.stub().yields()
|
this.FeaturesUpdater.promises = {
|
||||||
this.User.updateOne = sinon.stub().callsArgWith(3, null)
|
refreshFeatures: sinon.stub().resolves(),
|
||||||
this.User.findOne = sinon
|
}
|
||||||
.stub()
|
this.User.updateOne = sinon.stub().returns({
|
||||||
.callsArgWith(2, null, { _id: this.user_id })
|
exec: sinon.stub().resolves(),
|
||||||
|
})
|
||||||
|
this.User.findOne = sinon.stub().returns({
|
||||||
|
exec: sinon.stub().resolves({ _id: this.user_id }),
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('allocate', function () {
|
describe('allocate', function () {
|
||||||
describe('when the referal was a bonus referal', function () {
|
describe('when the referal was a bonus referal', function () {
|
||||||
beforeEach(function () {
|
beforeEach(async function () {
|
||||||
this.referal_source = 'bonus'
|
this.referal_source = 'bonus'
|
||||||
this.ReferalAllocator.allocate(
|
await this.ReferalAllocator.promises.allocate(
|
||||||
this.referal_id,
|
this.referal_id,
|
||||||
this.new_user_id,
|
this.new_user_id,
|
||||||
this.referal_source,
|
this.referal_source,
|
||||||
this.referal_medium,
|
this.referal_medium
|
||||||
this.callback
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -66,27 +68,24 @@ describe('ReferalAllocator', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should refresh the user's subscription", function () {
|
it("should refresh the user's subscription", function () {
|
||||||
this.FeaturesUpdater.refreshFeatures
|
this.FeaturesUpdater.promises.refreshFeatures
|
||||||
.calledWith(this.user_id)
|
.calledWith(this.user_id)
|
||||||
.should.equal(true)
|
.should.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should call the callback', function () {
|
|
||||||
this.callback.called.should.equal(true)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('when there is no user for the referal id', function () {
|
describe('when there is no user for the referal id', function () {
|
||||||
beforeEach(function () {
|
beforeEach(async function () {
|
||||||
this.referal_source = 'bonus'
|
this.referal_source = 'bonus'
|
||||||
this.referal_id = 'wombat'
|
this.referal_id = 'wombat'
|
||||||
this.User.findOne = sinon.stub().callsArgWith(2, null, null)
|
this.User.findOne = sinon.stub().returns({
|
||||||
this.ReferalAllocator.allocate(
|
exec: sinon.stub().resolves(null),
|
||||||
|
})
|
||||||
|
await this.ReferalAllocator.promises.allocate(
|
||||||
this.referal_id,
|
this.referal_id,
|
||||||
this.new_user_id,
|
this.new_user_id,
|
||||||
this.referal_source,
|
this.referal_source,
|
||||||
this.referal_medium,
|
this.referal_medium
|
||||||
this.callback
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -101,23 +100,18 @@ describe('ReferalAllocator', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not assign the user a bonus', function () {
|
it('should not assign the user a bonus', function () {
|
||||||
this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
|
this.FeaturesUpdater.promises.refreshFeatures.called.should.equal(false)
|
||||||
})
|
|
||||||
|
|
||||||
it('should call the callback', function () {
|
|
||||||
this.callback.called.should.equal(true)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('when the referal is not a bonus referal', function () {
|
describe('when the referal is not a bonus referal', function () {
|
||||||
beforeEach(function () {
|
beforeEach(async function () {
|
||||||
this.referal_source = 'public_share'
|
this.referal_source = 'public_share'
|
||||||
this.ReferalAllocator.allocate(
|
await this.ReferalAllocator.promises.allocate(
|
||||||
this.referal_id,
|
this.referal_id,
|
||||||
this.new_user_id,
|
this.new_user_id,
|
||||||
this.referal_source,
|
this.referal_source,
|
||||||
this.referal_medium,
|
this.referal_medium
|
||||||
this.callback
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -132,11 +126,7 @@ describe('ReferalAllocator', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not assign the user a bonus', function () {
|
it('should not assign the user a bonus', function () {
|
||||||
this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
|
this.FeaturesUpdater.promises.refreshFeatures.called.should.equal(false)
|
||||||
})
|
|
||||||
|
|
||||||
it('should call the callback', function () {
|
|
||||||
this.callback.called.should.equal(true)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue