2014-02-12 05:23:40 -05:00
logger = require ( " logger-sharelatex " )
Project = require ( " ../../models/Project " ) . Project
User = require ( " ../../models/User " ) . User
SubscriptionLocator = require ( " ./SubscriptionLocator " )
Settings = require ( " settings-sharelatex " )
module.exports =
allowedNumberOfCollaboratorsInProject: (project_id, callback) ->
getOwnerOfProject project_id , (error, owner)->
return callback ( error ) if error ?
if owner . features ? and owner . features . collaborators ?
callback null , owner . features . collaborators
else
callback null , Settings . defaultPlanCode . collaborators
currentNumberOfCollaboratorsInProject: (project_id, callback) ->
Project . findById project_id , ' collaberator_refs readOnly_refs ' , (error, project) ->
return callback ( error ) if error ?
callback null , ( project . collaberator_refs . length + project . readOnly_refs . length )
2015-10-14 12:29:58 -04:00
canAddXCollaborators: ( project_id , x_collaborators , callback = (error, allowed)-> ) ->
2014-02-12 05:23:40 -05:00
@ allowedNumberOfCollaboratorsInProject project_id , (error, allowed_number) =>
return callback ( error ) if error ?
@ currentNumberOfCollaboratorsInProject project_id , (error, current_number) =>
return callback ( error ) if error ?
2015-10-14 12:29:58 -04:00
if current_number + x_collaborators <= allowed_number or allowed_number < 0
2014-02-12 05:23:40 -05:00
callback null , true
2015-10-14 12:29:58 -04:00
else
callback null , false
2014-02-12 05:23:40 -05:00
2014-08-07 10:29:06 -04:00
userHasSubscriptionOrIsGroupMember: ( user , callback = (err, hasSubscriptionOrIsMember)-> ) ->
2014-10-10 10:11:22 -04:00
@ userHasSubscription user , (err, hasSubscription, subscription)=>
2014-08-07 10:29:06 -04:00
return callback ( err ) if err ?
@ userIsMemberOfGroupSubscription user , (err, isMember)=>
return callback ( err ) if err ?
logger . log user_id : user . _id , isMember : isMember , hasSubscription : hasSubscription , " checking if user has subscription or is group member "
2014-10-10 10:11:22 -04:00
callback err , isMember or hasSubscription , subscription
2014-02-12 05:23:40 -05:00
userHasSubscription: ( user , callback = (err, hasSubscription, subscription)-> ) ->
logger . log user_id : user . _id , " checking if user has subscription "
SubscriptionLocator . getUsersSubscription user . _id , (err, subscription)->
2014-03-08 14:01:00 -05:00
if err ?
return callback ( err )
hasValidSubscription = subscription ? and subscription . recurlySubscription_id ?
2014-03-08 13:31:07 -05:00
logger . log user : user , hasValidSubscription : hasValidSubscription , subscription : subscription , " checking if user has subscription "
2014-02-12 05:23:40 -05:00
callback err , hasValidSubscription , subscription
2014-08-07 10:29:06 -04:00
userIsMemberOfGroupSubscription: ( user , callback = (error, isMember, subscriptions) -> ) ->
logger . log user_id: user . _ud , " checking is user is member of subscription groups "
SubscriptionLocator . getMemberSubscriptions user . _id , (err, subscriptions = []) ->
return callback ( err ) if err ?
callback err , subscriptions . length > 0 , subscriptions
2014-02-12 05:23:40 -05:00
hasGroupMembersLimitReached: (user_id, callback)->
SubscriptionLocator . getUsersSubscription user_id , (err, subscription)->
limitReached = subscription . member_ids . length >= subscription . membersLimit
logger . log user_id : user_id , limitReached : limitReached , currentTotal: subscription . member_ids . length , membersLimit: subscription . membersLimit , " checking if subscription members limit has been reached "
2014-03-08 14:01:00 -05:00
callback ( err , limitReached )
2014-02-12 05:23:40 -05:00
getOwnerOfProject = (project_id, callback)->
Project . findById project_id , ' owner_ref ' , (error, project) ->
return callback ( error ) if error ?
User . findById project . owner_ref , (error, owner) ->
callback ( error , owner )