UserMembershipHandler.coffee 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. async = require("async")
  2. Errors = require('../Errors/Errors')
  3. SubscriptionLocator = require('../Subscription/SubscriptionLocator')
  4. InstitutionsLocator = require('../Institutions/InstitutionsLocator')
  5. UserMembershipViewModel = require('./UserMembershipViewModel')
  6. UserGetter = require('../User/UserGetter')
  7. logger = require('logger-sharelatex')
  8. module.exports =
  9. getEntity: (entityName, userId, callback = (error, entity) ->) ->
  10. switch entityName
  11. when 'group' then getGroupSubscription(userId, callback)
  12. when 'groupManagers'
  13. getGroupSubscription userId, (error, subscription) ->
  14. subscription.membersLimit = null if subscription # managers are unlimited
  15. callback(error, subscription)
  16. when 'institution' then getInstitution(userId, callback)
  17. else callback(new Errors.NotFoundError("No such entity: #{entityName}"))
  18. getUsers: (entityName, entity, callback = (error, users) ->) ->
  19. attributes = switch entityName
  20. when 'group' then ['invited_emails', 'teamInvites', 'member_ids']
  21. when 'groupManagers' then ['manager_ids']
  22. when 'institution' then ['managerIds']
  23. getPopulatedListOfMembers(entity, attributes, callback)
  24. addUser: (entityName, entity, email, callback = (error, user) ->) ->
  25. attribute = switch entityName
  26. when 'groupManagers' then 'manager_ids'
  27. when 'institution' then 'managerIds'
  28. unless attribute
  29. return callback(new Errors.NotFoundError("Cannot add user to entity: #{entityName}"))
  30. UserGetter.getUserByAnyEmail email, (error, user) ->
  31. error ||= new Errors.NotFoundError("No user found with email #{email}") unless user
  32. return callback(error) if error?
  33. addUserToEntity entity, attribute, user, (error) ->
  34. callback(error, UserMembershipViewModel.build(user))
  35. removeUser: (entityName, entity, userId, callback = (error) ->) ->
  36. attribute = switch entityName
  37. when 'groupManagers' then 'manager_ids'
  38. when 'institution' then 'managerIds'
  39. else callback(new Errors.NotFoundError("Cannot remove user from entity: #{entityName}"))
  40. removeUserFromEntity entity, attribute, userId, callback
  41. getGroupSubscription = (managerId, callback = (error, subscription) ->) ->
  42. SubscriptionLocator.findManagedSubscription managerId, (err, subscription)->
  43. if subscription? and subscription.groupPlan
  44. logger.log managerId: managerId, 'got managed subscription'
  45. else
  46. err ||= new Errors.NotFoundError("No subscription found managed by user #{managerId}")
  47. callback(err, subscription)
  48. getInstitution = (managerId, callback = (error, institution) ->) ->
  49. InstitutionsLocator.findManagedInstitution managerId, (err, institution)->
  50. if institution?
  51. logger.log managerId: managerId, 'got managed subscription'
  52. else
  53. err ||= new Errors.NotFoundError("No institution found managed by user #{managerId}")
  54. callback(err, institution)
  55. getPopulatedListOfMembers = (entity, attributes, callback = (error, users)->)->
  56. userObjects = []
  57. for attribute in attributes
  58. for userObject in entity[attribute] or []
  59. # userObject can be an email as String, a user id as ObjectId or an
  60. # invite as Object with an email attribute as String. We want to pass to
  61. # UserMembershipViewModel either an email as (String) or a user id (ObjectId)
  62. userIdOrEmail = userObject.email || userObject
  63. userObjects.push userIdOrEmail
  64. async.map userObjects, UserMembershipViewModel.buildAsync, callback
  65. addUserToEntity = (entity, attribute, user, callback = (error)->) ->
  66. fieldUpdate = {}
  67. fieldUpdate[attribute] = user._id
  68. entity.update { $addToSet: fieldUpdate }, callback
  69. removeUserFromEntity = (entity, attribute, userId, callback = (error)->) ->
  70. fieldUpdate = {}
  71. fieldUpdate[attribute] = userId
  72. entity.update { $pull: fieldUpdate }, callback