overleaf/services/web/app/coffee/Features/Institutions/InstitutionsManager.coffee
Timothée Alby b311f997aa Merge pull request #1444 from sharelatex/ta-ns-check-institution-users
add script to check all if users on institution are upgraded

GitOrigin-RevId: 903f3a65fc038862178a82f43d97a5a4630cf470
2019-01-28 14:38:29 +00:00

42 lines
No EOL
1.5 KiB
CoffeeScript

logger = require 'logger-sharelatex'
async = require 'async'
db = require("../../infrastructure/mongojs").db
ObjectId = require("../../infrastructure/mongojs").ObjectId
{ getInstitutionAffiliations } = require('./InstitutionsAPI')
FeaturesUpdater = require('../Subscription/FeaturesUpdater')
UserGetter = require('../User/UserGetter')
ASYNC_LIMIT = 10
module.exports = InstitutionsManager =
upgradeInstitutionUsers: (institutionId, callback = (error) ->) ->
getInstitutionAffiliations institutionId, (error, affiliations) ->
return callback(error) if error
async.eachLimit affiliations, ASYNC_LIMIT, refreshFeatures, callback
checkInstitutionUsers: (institutionId, callback = (error) ->) ->
getInstitutionAffiliations institutionId, (error, affiliations) ->
UserGetter.getUsersByAnyConfirmedEmail(
affiliations.map((affiliation) -> affiliation.email),
{ features: 1 },
(error, users) ->
callback(error, checkFeatures(users))
)
refreshFeatures = (affiliation, callback) ->
userId = ObjectId(affiliation.user_id)
FeaturesUpdater.refreshFeatures(userId, true, callback)
checkFeatures = (users) ->
usersSummary = {
totalConfirmedUsers: users.length
totalConfirmedProUsers: 0
totalConfirmedNonProUsers: 0
confirmedNonProUsers: []
}
users.forEach((user) ->
if user.features.collaborators == -1 and user.features.trackChanges
usersSummary.totalConfirmedProUsers += 1
else
usersSummary.totalConfirmedNonProUsers += 1
usersSummary.confirmedNonProUsers.push user._id
)
return usersSummary