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
|
|
|
|
Settings = require "settings-sharelatex"
|
2018-05-21 12:50:49 -04:00
|
|
|
FeaturesUpdater = require "../Subscription/FeaturesUpdater"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = ReferalAllocator =
|
|
|
|
allocate: (referal_id, new_user_id, referal_source, referal_medium, callback = ->)->
|
|
|
|
if !referal_id?
|
2018-10-23 07:24:34 -04:00
|
|
|
logger.log new_user_id:new_user_id, "no referal for user"
|
|
|
|
return callback(null)
|
|
|
|
|
|
|
|
logger.log referal_id:referal_id, new_user_id:new_user_id, referal_source:referal_source, referal_medium:referal_medium, "allocating users referal"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
query = {"referal_id":referal_id}
|
|
|
|
User.findOne query, (error, user) ->
|
|
|
|
return callback(error) if error?
|
2018-10-23 07:24:34 -04:00
|
|
|
if !user? or !user._id?
|
|
|
|
logger.log new_user_id:new_user_id, referal_id:referal_id, "no user found for referal id"
|
|
|
|
return callback(null)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
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)
|
2018-10-23 07:24:34 -04:00
|
|
|
|
2018-05-21 12:50:49 -04:00
|
|
|
FeaturesUpdater.refreshFeatures user._id, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
else
|
|
|
|
callback()
|