2015-10-05 07:30:03 -04:00
|
|
|
_ = require("underscore")
|
2014-02-12 05:23:40 -05:00
|
|
|
logger = require('logger-sharelatex')
|
|
|
|
User = require('../../models/User').User
|
|
|
|
SubscriptionLocator = require "../Subscription/SubscriptionLocator"
|
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
|
|
|
|
module.exports = ReferalAllocator =
|
|
|
|
allocate: (referal_id, new_user_id, referal_source, referal_medium, callback = ->)->
|
|
|
|
if !referal_id?
|
|
|
|
return logger.log new_user_id:new_user_id, "no referal for user"
|
|
|
|
logger.log referal_id:referal_id, new_user_id:new_user_id, "allocating users referal"
|
|
|
|
|
|
|
|
query = {"referal_id":referal_id}
|
|
|
|
User.findOne query, (error, user) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback(new Error("user not found")) if !user? or !user._id?
|
|
|
|
|
|
|
|
if referal_source == "bonus"
|
|
|
|
User.update query, {
|
|
|
|
$push:
|
|
|
|
refered_users: new_user_id
|
|
|
|
$inc:
|
|
|
|
refered_user_count: 1
|
|
|
|
}, {}, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, referal_id:referal_id, new_user_id:new_user_id, "something went wrong allocating referal"
|
|
|
|
return callback(err)
|
|
|
|
ReferalAllocator.assignBonus user._id, callback
|
|
|
|
else
|
|
|
|
callback()
|
|
|
|
|
2015-10-05 07:30:03 -04:00
|
|
|
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
assignBonus: (user_id, callback = (error) ->) ->
|
2015-10-05 11:50:04 -04:00
|
|
|
query = _id: user_id
|
|
|
|
User.findOne query, (error, user) ->
|
|
|
|
return callback(error) if error
|
|
|
|
return callback(new Error("user not found")) if !user?
|
|
|
|
logger.log user_id: user_id, refered_user_count: user.refered_user_count, "assigning bonus"
|
|
|
|
if user.refered_user_count? and user.refered_user_count > 0
|
2015-10-07 07:42:34 -04:00
|
|
|
newFeatures = ReferalAllocator._calculateFeatures(user)
|
|
|
|
if _.isEqual newFeatures, user.features
|
|
|
|
return callback()
|
2015-10-05 11:50:04 -04:00
|
|
|
User.update query, { $set: features: newFeatures }, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
|
|
|
callback()
|
2015-10-05 07:30:03 -04:00
|
|
|
|
2015-10-07 07:42:34 -04:00
|
|
|
_calculateFeatures : (user)->
|
|
|
|
bonusLevel = ReferalAllocator._getBonusLevel(user)
|
|
|
|
currentFeatures = _.clone(user.features) #need to clone because we exend with underscore later
|
|
|
|
betterBonusFeatures = {}
|
2015-10-05 07:30:03 -04:00
|
|
|
_.each Settings.bonus_features["#{bonusLevel}"], (bonusLevel, key)->
|
|
|
|
currentLevel = user?.features?[key]
|
|
|
|
if _.isBoolean(currentLevel) and currentLevel == false
|
2015-10-07 07:42:34 -04:00
|
|
|
betterBonusFeatures[key] = bonusLevel
|
2015-10-05 07:30:03 -04:00
|
|
|
|
2015-10-07 07:42:34 -04:00
|
|
|
if _.isNumber(currentLevel)
|
2015-10-05 07:30:03 -04:00
|
|
|
if currentLevel == -1
|
|
|
|
return
|
|
|
|
bonusIsGreaterThanCurrent = currentLevel < bonusLevel
|
|
|
|
if bonusIsGreaterThanCurrent or bonusLevel == -1
|
2015-10-07 07:42:34 -04:00
|
|
|
betterBonusFeatures[key] = bonusLevel
|
|
|
|
newFeatures = _.extend(currentFeatures, betterBonusFeatures)
|
2015-10-05 07:30:03 -04:00
|
|
|
return newFeatures
|
|
|
|
|
|
|
|
|
|
|
|
_getBonusLevel: (user)->
|
|
|
|
highestBonusLevel = 0
|
|
|
|
_.each _.keys(Settings.bonus_features), (level)->
|
|
|
|
levelIsLessThanUser = level <= user.refered_user_count
|
|
|
|
levelIsMoreThanCurrentHighest = level >= highestBonusLevel
|
|
|
|
if levelIsLessThanUser and levelIsMoreThanCurrentHighest
|
|
|
|
highestBonusLevel = level
|
|
|
|
return highestBonusLevel
|