Merge pull request #2992 from overleaf/msm-add-user-projection-referal

Added projection to User.find() queries in Referal feature

GitOrigin-RevId: 4929bcd9c1b242b7e35cc2632bbd8da3f378cd1d
This commit is contained in:
Miguel Serrano 2020-07-16 08:48:12 +02:00 committed by Copybot
parent e692802690
commit 5b40eca697
10 changed files with 129 additions and 246 deletions

View file

@ -1,34 +1,18 @@
/* eslint-disable
camelcase,
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let ReferalAllocator
const _ = require('underscore')
const logger = require('logger-sharelatex')
const { User } = require('../../models/User')
const Settings = require('settings-sharelatex')
const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
module.exports = ReferalAllocator = {
allocate(referal_id, new_user_id, referal_source, referal_medium, callback) {
module.exports = {
allocate(referalId, newUserId, referalSource, referalMedium, callback) {
if (callback == null) {
callback = function() {}
}
if (referal_id == null) {
if (referalId == null) {
return callback(null)
}
const query = { referal_id: referal_id }
return User.findOne(query, function(error, user) {
const query = { referal_id: referalId }
return User.findOne(query, { _id: 1 }, function(error, user) {
if (error != null) {
return callback(error)
}
@ -36,12 +20,12 @@ module.exports = ReferalAllocator = {
return callback(null)
}
if (referal_source === 'bonus') {
return User.update(
if (referalSource === 'bonus') {
User.update(
query,
{
$push: {
refered_users: new_user_id
refered_users: newUserId
},
$inc: {
refered_user_count: 1
@ -51,17 +35,16 @@ module.exports = ReferalAllocator = {
function(err) {
if (err != null) {
logger.warn(
{ err, referal_id, new_user_id },
{ err, referalId, newUserId },
'something went wrong allocating referal'
)
return callback(err)
}
return FeaturesUpdater.refreshFeatures(user._id, callback)
FeaturesUpdater.refreshFeatures(user._id, callback)
}
)
} else {
return callback()
callback()
}
})
}

View file

@ -1,14 +1,3 @@
/* eslint-disable
max-len,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
module.exports = {
use(req, res, next) {
if (req.query != null) {
@ -58,6 +47,6 @@ module.exports = {
}
}
return next()
next()
}
}

View file

@ -1,31 +1,22 @@
/* eslint-disable
camelcase,
handle-callback-err,
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const logger = require('logger-sharelatex')
const ReferalHandler = require('./ReferalHandler')
const AuthenticationController = require('../Authentication/AuthenticationController')
module.exports = {
bonus(req, res) {
const user_id = AuthenticationController.getLoggedInUserId(req)
return ReferalHandler.getReferedUsers(
user_id,
(err, refered_users, refered_user_count) =>
res.render('referal/bonus', {
title: 'bonus_please_recommend_us',
refered_users,
refered_user_count
})
bonus(req, res, next) {
const userId = AuthenticationController.getLoggedInUserId(req)
ReferalHandler.getReferedUsers(
userId,
(err, referedUsers, referedUserCount) => {
if (err) {
next(err)
} else {
res.render('referal/bonus', {
title: 'bonus_please_recommend_us',
refered_users: referedUsers,
refered_user_count: referedUserCount
})
}
}
)
}
}

View file

@ -1,40 +1,27 @@
/* eslint-disable
camelcase,
handle-callback-err,
max-len,
no-return-assign,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let ReferalFeatures
const _ = require('underscore')
const { User } = require('../../models/User')
const Settings = require('settings-sharelatex')
let ReferalFeatures
module.exports = ReferalFeatures = {
getBonusFeatures(user_id, callback) {
getBonusFeatures(userId, callback) {
if (callback == null) {
callback = function(error) {}
callback = function() {}
}
const query = { _id: user_id }
return User.findOne(query, function(error, user) {
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 ${user_id} for assignBonus`))
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)
return callback(null, newFeatures)
callback(null, newFeatures)
} else {
return callback(null, {})
callback(null, {})
}
})
},

View file

@ -1,23 +1,14 @@
/* eslint-disable
camelcase,
handle-callback-err,
max-len,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const { User } = require('../../models/User')
module.exports = {
getReferedUsers(user_id, callback) {
return User.findById(user_id, function(err, user) {
const refered_users = user.refered_users || []
const refered_user_count = user.refered_user_count || refered_users.length
return callback(null, refered_users, refered_user_count)
getReferedUsers(userId, callback) {
User.findById(userId, { refered_users: 1 }, function(err, user) {
if (err) {
return callback(err)
}
const referedUsers = user.refered_users || []
const referedUserCount = user.refered_user_count || referedUsers.length
callback(null, referedUsers, referedUserCount)
})
}
}

View file

@ -1,17 +1,4 @@
/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
@ -44,16 +31,16 @@ describe('ReferalAllocator', function() {
this.new_user_id = 'new-user-id-123'
this.FeaturesUpdater.refreshFeatures = sinon.stub().yields()
this.User.update = sinon.stub().callsArgWith(3, null)
return (this.User.findOne = sinon
this.User.findOne = sinon
.stub()
.callsArgWith(1, null, { _id: this.user_id }))
.callsArgWith(2, null, { _id: this.user_id })
})
describe('allocate', function() {
describe('when the referal was a bonus referal', function() {
beforeEach(function() {
this.referal_source = 'bonus'
return this.ReferalAllocator.allocate(
this.ReferalAllocator.allocate(
this.referal_id,
this.new_user_id,
this.referal_source,
@ -63,7 +50,7 @@ describe('ReferalAllocator', function() {
})
it('should update the referring user with the refered users id', function() {
return this.User.update
this.User.update
.calledWith(
{
referal_id: this.referal_id
@ -81,19 +68,19 @@ describe('ReferalAllocator', function() {
})
it('find the referring users id', function() {
return this.User.findOne
this.User.findOne
.calledWith({ referal_id: this.referal_id })
.should.equal(true)
})
it("should refresh the user's subscription", function() {
return this.FeaturesUpdater.refreshFeatures
this.FeaturesUpdater.refreshFeatures
.calledWith(this.user_id)
.should.equal(true)
})
it('should call the callback', function() {
return this.callback.called.should.equal(true)
this.callback.called.should.equal(true)
})
})
@ -101,8 +88,8 @@ describe('ReferalAllocator', function() {
beforeEach(function() {
this.referal_source = 'bonus'
this.referal_id = 'wombat'
this.User.findOne = sinon.stub().callsArgWith(1, null, null)
return this.ReferalAllocator.allocate(
this.User.findOne = sinon.stub().callsArgWith(2, null, null)
this.ReferalAllocator.allocate(
this.referal_id,
this.new_user_id,
this.referal_source,
@ -112,28 +99,28 @@ describe('ReferalAllocator', function() {
})
it('should find the referring users id', function() {
return this.User.findOne
this.User.findOne
.calledWith({ referal_id: this.referal_id })
.should.equal(true)
})
it('should not update the referring user with the refered users id', function() {
return this.User.update.called.should.equal(false)
this.User.update.called.should.equal(false)
})
it('should not assign the user a bonus', function() {
return this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
})
it('should call the callback', function() {
return this.callback.called.should.equal(true)
this.callback.called.should.equal(true)
})
})
describe('when the referal is not a bonus referal', function() {
beforeEach(function() {
this.referal_source = 'public_share'
return this.ReferalAllocator.allocate(
this.ReferalAllocator.allocate(
this.referal_id,
this.new_user_id,
this.referal_source,
@ -143,21 +130,21 @@ describe('ReferalAllocator', function() {
})
it('should not update the referring user with the refered users id', function() {
return this.User.update.called.should.equal(false)
this.User.update.called.should.equal(false)
})
it('find the referring users id', function() {
return this.User.findOne
this.User.findOne
.calledWith({ referal_id: this.referal_id })
.should.equal(true)
})
it('should not assign the user a bonus', function() {
return this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
this.FeaturesUpdater.refreshFeatures.called.should.equal(false)
})
it('should call the callback', function() {
return this.callback.called.should.equal(true)
this.callback.called.should.equal(true)
})
})
})

View file

@ -1,19 +1,5 @@
/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
__dirname,
'../../../../app/src/Features/Referal/ReferalConnect.js'
@ -21,7 +7,7 @@ const modulePath = require('path').join(
describe('Referal connect middle wear', function() {
beforeEach(function() {
return (this.connect = SandboxedModule.require(modulePath, {
this.connect = SandboxedModule.require(modulePath, {
globals: {
console: console
},
@ -31,7 +17,7 @@ describe('Referal connect middle wear', function() {
err() {}
}
}
}))
})
})
it('should take a referal query string and put it on the session if it exists', function(done) {
@ -39,9 +25,9 @@ describe('Referal connect middle wear', function() {
query: { referal: '12345' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_id.should.equal(req.query.referal)
return done()
done()
})
})
@ -50,9 +36,9 @@ describe('Referal connect middle wear', function() {
query: {},
session: { referal_id: 'same' }
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_id.should.equal('same')
return done()
done()
})
})
@ -61,9 +47,9 @@ describe('Referal connect middle wear', function() {
query: { fb_ref: '12345' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_id.should.equal(req.query.fb_ref)
return done()
done()
})
})
@ -72,9 +58,9 @@ describe('Referal connect middle wear', function() {
query: { rm: 'fb' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_medium.should.equal('facebook')
return done()
done()
})
})
@ -83,9 +69,9 @@ describe('Referal connect middle wear', function() {
query: { rm: 't' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_medium.should.equal('twitter')
return done()
done()
})
})
@ -94,9 +80,9 @@ describe('Referal connect middle wear', function() {
query: { rm: 'gp' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_medium.should.equal('google_plus')
return done()
done()
})
})
@ -105,9 +91,9 @@ describe('Referal connect middle wear', function() {
query: { rm: 'e' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_medium.should.equal('email')
return done()
done()
})
})
@ -116,9 +102,9 @@ describe('Referal connect middle wear', function() {
query: { rm: 'd' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_medium.should.equal('direct')
return done()
done()
})
})
@ -127,9 +113,9 @@ describe('Referal connect middle wear', function() {
query: { rs: 'b' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_source.should.equal('bonus')
return done()
done()
})
})
@ -138,9 +124,9 @@ describe('Referal connect middle wear', function() {
query: { rs: 'ps' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_source.should.equal('public_share')
return done()
done()
})
})
@ -149,9 +135,9 @@ describe('Referal connect middle wear', function() {
query: { rs: 'ci' },
session: {}
}
return this.connect.use(req, {}, () => {
this.connect.use(req, {}, () => {
req.session.referal_source.should.equal('collaborator_invite')
return done()
done()
})
})
})

View file

@ -1,19 +1,5 @@
/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
__dirname,
'../../../../app/src/Features/Referal/ReferalController.js'
@ -21,7 +7,7 @@ const modulePath = require('path').join(
describe('Referal controller', function() {
beforeEach(function() {
return (this.controller = SandboxedModule.require(modulePath, {
this.controller = SandboxedModule.require(modulePath, {
globals: {
console: console
},
@ -31,6 +17,6 @@ describe('Referal controller', function() {
err() {}
}
}
}))
})
})
})

View file

@ -1,17 +1,4 @@
/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
@ -40,7 +27,7 @@ describe('ReferalFeatures', function() {
this.referal_id = 'referal-id-123'
this.referal_medium = 'twitter'
this.user_id = 'user-id-123'
return (this.new_user_id = 'new-user-id-123')
this.new_user_id = 'new-user-id-123'
})
describe('getBonusFeatures', function() {
@ -58,18 +45,16 @@ describe('ReferalFeatures', function() {
features: { collaborators: 1, dropbox: false, versioning: false }
}
this.User.findOne = sinon.stub().callsArgWith(1, null, stubbedUser)
return this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
this.User.findOne = sinon.stub().callsArgWith(2, null, stubbedUser)
this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
})
it('should get the users number of refered user', function() {
return this.User.findOne
.calledWith({ _id: this.user_id })
.should.equal(true)
this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
})
it('should call the callback with the features', function() {
return this.callback
this.callback
.calledWith(null, this.Settings.bonus_features[3])
.should.equal(true)
})
@ -87,18 +72,16 @@ describe('ReferalFeatures', function() {
}
this.User.findOne = sinon
.stub()
.callsArgWith(1, null, { refered_user_count: this.refered_user_count })
return this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
.callsArgWith(2, null, { refered_user_count: this.refered_user_count })
this.ReferalFeatures.getBonusFeatures(this.user_id, this.callback)
})
it('should get the users number of refered user', function() {
return this.User.findOne
.calledWith({ _id: this.user_id })
.should.equal(true)
this.User.findOne.calledWith({ _id: this.user_id }).should.equal(true)
})
it('should call the callback with no features', function() {
return this.callback.calledWith(null, {}).should.equal(true)
this.callback.calledWith(null, {}).should.equal(true)
})
})
})

View file

@ -1,19 +1,5 @@
/* eslint-disable
handle-callback-err,
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
require('chai').should()
const should = require('chai').should()
const sinon = require('sinon')
const modulePath = require('path').join(
__dirname,
@ -37,7 +23,7 @@ describe('Referal handler', function() {
}
}
})
return (this.user_id = '12313')
this.user_id = '12313'
})
describe('getting refered user_ids', function() {
@ -46,53 +32,57 @@ describe('Referal handler', function() {
refered_users: ['1234', '312312', '3213129'],
refered_user_count: 3
}
this.User.findById.callsArgWith(1, null, user)
this.User.findById.callsArgWith(2, null, user)
return this.handler.getReferedUsers(
this.handler.getReferedUsers(
this.user_id,
(err, passedReferedUserIds, passedReferedUserCount) => {
should.not.exist(err)
passedReferedUserIds.should.deep.equal(user.refered_users)
passedReferedUserCount.should.equal(3)
return done()
done()
}
)
})
it('should return an empty array if it is not set', function(done) {
const user = {}
this.User.findById.callsArgWith(1, null, user)
this.User.findById.callsArgWith(2, null, user)
return this.handler.getReferedUsers(
this.handler.getReferedUsers(
this.user_id,
(err, passedReferedUserIds, passedReferedUserCount) => {
should.not.exist(err)
passedReferedUserIds.length.should.equal(0)
return done()
done()
}
)
})
it('should return a zero count if netither it or the array are set', function(done) {
it('should return a zero count if neither it or the array are set', function(done) {
const user = {}
this.User.findById.callsArgWith(1, null, user)
this.User.findById.callsArgWith(2, null, user)
return this.handler.getReferedUsers(
this.handler.getReferedUsers(
this.user_id,
(err, passedReferedUserIds, passedReferedUserCount) => {
should.not.exist(err)
passedReferedUserCount.should.equal(0)
return done()
done()
}
)
})
it('should return the array length if count is not set', function(done) {
const user = { refered_users: ['1234', '312312', '3213129'] }
this.User.findById.callsArgWith(1, null, user)
this.User.findById.callsArgWith(2, null, user)
return this.handler.getReferedUsers(
this.handler.getReferedUsers(
this.user_id,
(err, passedReferedUserIds, passedReferedUserCount) => {
should.not.exist(err)
passedReferedUserCount.should.equal(3)
return done()
done()
}
)
})
@ -102,15 +92,25 @@ describe('Referal handler', function() {
refered_users: ['1234', '312312', '3213129'],
refered_user_count: 5
}
this.User.findById.callsArgWith(1, null, user)
this.User.findById.callsArgWith(2, null, user)
return this.handler.getReferedUsers(
this.handler.getReferedUsers(
this.user_id,
(err, passedReferedUserIds, passedReferedUserCount) => {
should.not.exist(err)
passedReferedUserCount.should.equal(5)
return done()
done()
}
)
})
it('should error if finding the user fails', function(done) {
this.User.findById.callsArgWith(2, new Error('user not found'))
this.handler.getReferedUsers(this.user_id, err => {
err.should.match(/user not found/)
done()
})
})
})
})