2014-02-12 05:23:40 -05:00
logger = require ( " logger-sharelatex " )
Project = require ( " ../../models/Project " ) . Project
2017-01-27 10:26:58 -05:00
UserGetter = require ( " ../User/UserGetter " )
2014-02-12 05:23:40 -05:00
SubscriptionLocator = require ( " ./SubscriptionLocator " )
Settings = require ( " settings-sharelatex " )
2016-03-07 10:32:04 -05:00
CollaboratorsHandler = require ( " ../Collaborators/CollaboratorsHandler " )
2016-08-02 10:42:50 -04:00
CollaboratorsInvitesHandler = require ( " ../Collaborators/CollaboratorsInviteHandler " )
2014-02-12 05:23:40 -05:00
module.exports =
allowedNumberOfCollaboratorsInProject: (project_id, callback) ->
2017-01-27 10:26:58 -05:00
Project . findById project_id , ' owner_ref ' , (error, project) =>
return callback ( error ) if error ?
@ allowedNumberOfCollaboratorsForUser project . owner_ref , callback
allowedNumberOfCollaboratorsForUser: (user_id, callback) ->
UserGetter . getUser user_id , { features: 1 } , (error, user) ->
2014-02-12 05:23:40 -05:00
return callback ( error ) if error ?
2017-01-27 10:26:58 -05:00
if user . features ? and user . features . collaborators ?
callback null , user . features . collaborators
2014-02-12 05:23:40 -05:00
else
callback null , Settings . defaultPlanCode . collaborators
2017-01-27 10:26:58 -05:00
2014-02-12 05:23:40 -05:00
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 ?
2017-09-20 05:29:47 -04:00
CollaboratorsHandler . getInvitedCollaboratorCount project_id , (error, current_number) =>
2014-02-12 05:23:40 -05:00
return callback ( error ) if error ?
2016-08-02 10:42:50 -04:00
CollaboratorsInvitesHandler . getInviteCount project_id , (error, invite_count) =>
return callback ( error ) if error ?
if current_number + invite_count + x_collaborators <= allowed_number or allowed_number < 0
callback null , true
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 )
2016-07-19 05:28:43 -04:00
hasValidSubscription = subscription ? and ( subscription . recurlySubscription_id ? or subscription ? . customAccount == true )
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
2016-08-02 10:42:50 -04:00
2014-08-07 10:29:06 -04:00
userIsMemberOfGroupSubscription: ( user , callback = (error, isMember, subscriptions) -> ) ->
2016-06-15 13:34:56 -04:00
logger . log user_id: user . _id , " checking is user is member of subscription groups "
2014-08-07 10:29:06 -04:00
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
2016-02-22 11:50:41 -05:00
hasGroupMembersLimitReached: ( user_id , callback = (err, limitReached, subscription)-> ) ->
2014-02-12 05:23:40 -05:00
SubscriptionLocator . getUsersSubscription user_id , (err, subscription)->
2016-02-22 11:50:41 -05:00
if err ?
logger . err err : err , user_id : user_id , " error getting users subscription "
return callback ( err )
if ! subscription ?
logger . err user_id : user_id , " no subscription found for user "
return callback ( " no subscription found " )
2017-06-08 07:12:08 -04:00
currentTotal = ( subscription . member_ids or [ ] ) . length + ( subscription . invited_emails or [ ] ) . length
limitReached = currentTotal >= subscription . membersLimit
logger . log user_id : user_id , limitReached : limitReached , currentTotal: currentTotal , membersLimit: subscription . membersLimit , " checking if subscription members limit has been reached "
2016-02-22 11:50:41 -05:00
callback ( err , limitReached , subscription )
2014-02-12 05:23:40 -05:00
2017-01-27 10:26:58 -05:00
getOwnerIdOfProject = (project_id, callback)->