bonus allocater does not need to check if user has subscription any more.

This commit is contained in:
Henry Oswald 2015-10-05 16:50:04 +01:00
parent fae7d431f4
commit b69ec9768d
2 changed files with 10 additions and 106 deletions

View file

@ -32,27 +32,15 @@ module.exports = ReferalAllocator =
assignBonus: (user_id, callback = (error) ->) -> assignBonus: (user_id, callback = (error) ->) ->
SubscriptionLocator.getUsersSubscription user_id, (error, subscription) -> query = _id: user_id
return callback(error) if error? User.findOne query, (error, user) ->
logger.log return callback(error) if error
subscription: subscription, return callback(new Error("user not found")) if !user?
user_id: user_id, logger.log user_id: user_id, refered_user_count: user.refered_user_count, "assigning bonus"
"checking user doesn't have a subsciption before assigning bonus" if user.refered_user_count? and user.refered_user_count > 0
if !subscription? or !subscription.planCode? newFeatures = ReferalAllocator._calculateBonuses(user)
query = _id: user_id User.update query, { $set: features: newFeatures }, callback
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
newFeatures = ReferalAllocator._calculateBonuses(user)
User.update query, { $set: features: newFeatures }, callback
else
callback()
else else
callback() callback()

View file

@ -4,7 +4,7 @@ require('chai').should()
sinon = require('sinon') sinon = require('sinon')
modulePath = require('path').join __dirname, '../../../../app/js/Features/Referal/ReferalAllocator.js' modulePath = require('path').join __dirname, '../../../../app/js/Features/Referal/ReferalAllocator.js'
describe 'Referal allocator', -> describe 'Referalallocator', ->
beforeEach -> beforeEach ->
@ReferalAllocator = SandboxedModule.require modulePath, requires: @ReferalAllocator = SandboxedModule.require modulePath, requires:
@ -75,7 +75,6 @@ describe 'Referal allocator', ->
@callback.called.should.equal true @callback.called.should.equal true
describe "assignBonus", -> describe "assignBonus", ->
describe "when user does not have a subscription", ->
beforeEach -> beforeEach ->
@refered_user_count = 3 @refered_user_count = 3
@Settings.bonus_features = @Settings.bonus_features =
@ -90,14 +89,8 @@ describe 'Referal allocator', ->
@User.findOne = sinon.stub().callsArgWith 1, null,stubbedUser @User.findOne = sinon.stub().callsArgWith 1, null,stubbedUser
@User.update = sinon.stub().callsArgWith 2, null @User.update = sinon.stub().callsArgWith 2, null
@SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith 1, null, null
@ReferalAllocator.assignBonus @user_id, @callback @ReferalAllocator.assignBonus @user_id, @callback
it "should get the users subscription", ->
@SubscriptionLocator.getUsersSubscription
.calledWith(@user_id)
.should.equal true
it "should get the users number of refered user", -> it "should get the users number of refered user", ->
@User.findOne @User.findOne
.calledWith(_id: @user_id) .calledWith(_id: @user_id)
@ -117,51 +110,6 @@ describe 'Referal allocator', ->
it "should call the callback", -> it "should call the callback", ->
@callback.called.should.equal true @callback.called.should.equal true
describe "when user does not have a recurlySubscription_id", ->
beforeEach ->
@refered_user_count = 4
@Settings.bonus_features =
"2":
collaborators: 2
dropbox: false
versioning: false
"5":
collaborators: 5
dropbox: true
versioning: false
"3":
collaborators: 3
dropbox: false
versioning: false
stubbedUser = { refered_user_count: @refered_user_count, features:{collaborators:1, dropbox:false, versioning:false} }
@User.findOne = sinon.stub().callsArgWith 1, null, stubbedUser
@User.update = sinon.stub().callsArgWith 2, null
@SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith 1, null, {}
@ReferalAllocator.assignBonus @user_id, @callback
it "should get the users subscription", ->
@SubscriptionLocator.getUsersSubscription
.calledWith(@user_id)
.should.equal true
it "should get the users number of refered user", ->
@User.findOne
.calledWith(_id: @user_id)
.should.equal true
it "should update the user to bonus features with the closest level", ->
@User.update
.calledWith({
_id: @user_id
}, {
$set:
features:
@Settings.bonus_features["3"]
})
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
describe "when the user has better features already", -> describe "when the user has better features already", ->
@ -181,7 +129,6 @@ describe 'Referal allocator', ->
@User.findOne = sinon.stub().callsArgWith 1, null, @stubbedUser @User.findOne = sinon.stub().callsArgWith 1, null, @stubbedUser
@User.update = sinon.stub().callsArgWith 2, null @User.update = sinon.stub().callsArgWith 2, null
@SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith 1, null,null
it "should not set in in mongo when the feature is better", (done)-> it "should not set in in mongo when the feature is better", (done)->
@ReferalAllocator.assignBonus @user_id, => @ReferalAllocator.assignBonus @user_id, =>
@ -204,14 +151,8 @@ describe 'Referal allocator', ->
versioning: false versioning: false
@User.findOne = sinon.stub().callsArgWith 1, null, { refered_user_count: @refered_user_count } @User.findOne = sinon.stub().callsArgWith 1, null, { refered_user_count: @refered_user_count }
@User.update = sinon.stub().callsArgWith 2, null @User.update = sinon.stub().callsArgWith 2, null
@SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith 1, null, {}
@ReferalAllocator.assignBonus @user_id, @callback @ReferalAllocator.assignBonus @user_id, @callback
it "should get the users subscription", ->
@SubscriptionLocator.getUsersSubscription
.calledWith(@user_id)
.should.equal true
it "should get the users number of refered user", -> it "should get the users number of refered user", ->
@User.findOne @User.findOne
.calledWith(_id: @user_id) .calledWith(_id: @user_id)
@ -223,29 +164,4 @@ describe 'Referal allocator', ->
it "should call the callback", -> it "should call the callback", ->
@callback.called.should.equal true @callback.called.should.equal true
describe "when user has a subscription", ->
beforeEach ->
@refered_user_count = 3
@Settings.bonus_features =
"3":
collaborators: 3
dropbox: false
versioning: false
@User.findOne = sinon.stub().callsArgWith 1, null, { refered_user_count: @refered_user_count }
@User.update = sinon.stub().callsArgWith 2, null
@SubscriptionLocator.getUsersSubscription = sinon.stub().callsArgWith 1, null, { planCode: "collaborator" }
@ReferalAllocator.assignBonus @user_id, @callback
it "should get the users subscription", ->
@SubscriptionLocator.getUsersSubscription
.calledWith(@user_id)
.should.equal true
it "should not get the users number of refered user", ->
@User.findOne.called.should.equal false
it "should not update the user to bonus features", ->
@User.update.called.should.equal false
it "should call the callback", ->
@callback.called.should.equal true