mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
institution upgrade
This commit is contained in:
parent
63c7f756ce
commit
66bcdbcac9
5 changed files with 88 additions and 0 deletions
|
@ -4,6 +4,14 @@ settings = require "settings-sharelatex"
|
||||||
request = require "request"
|
request = require "request"
|
||||||
|
|
||||||
module.exports = InstitutionsAPI =
|
module.exports = InstitutionsAPI =
|
||||||
|
getInstitutionAffiliations: (institutionId, callback = (error, body) ->) ->
|
||||||
|
makeAffiliationRequest {
|
||||||
|
method: 'GET'
|
||||||
|
path: "/api/v2/institutions/#{institutionId.toString()}/affiliations"
|
||||||
|
defaultErrorMessage: "Couldn't get institution affiliations"
|
||||||
|
}, callback
|
||||||
|
|
||||||
|
|
||||||
getUserAffiliations: (userId, callback = (error, body) ->) ->
|
getUserAffiliations: (userId, callback = (error, body) ->) ->
|
||||||
makeAffiliationRequest {
|
makeAffiliationRequest {
|
||||||
method: 'GET'
|
method: 'GET'
|
||||||
|
@ -77,6 +85,7 @@ makeAffiliationRequest = (requestOptions, callback = (error) ->) ->
|
||||||
callback(null, body)
|
callback(null, body)
|
||||||
|
|
||||||
[
|
[
|
||||||
|
'getInstitutionAffiliations'
|
||||||
'getUserAffiliations',
|
'getUserAffiliations',
|
||||||
'addAffiliation',
|
'addAffiliation',
|
||||||
'removeAffiliation',
|
'removeAffiliation',
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
logger = require 'logger-sharelatex'
|
||||||
|
async = require 'async'
|
||||||
|
db = require("../../infrastructure/mongojs").db
|
||||||
|
ObjectId = require("../../infrastructure/mongojs").ObjectId
|
||||||
|
{ getInstitutionAffiliations } = require('./InstitutionsAPI')
|
||||||
|
FeaturesUpdater = require('../Subscription/FeaturesUpdater')
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
refreshFeatures = (affiliation, callback) ->
|
||||||
|
userId = ObjectId(affiliation.user_id)
|
||||||
|
FeaturesUpdater.refreshFeatures(userId, true, callback)
|
16
services/web/scripts/upgrade_institution_users.js
Normal file
16
services/web/scripts/upgrade_institution_users.js
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
const InstitutionsManager = require(
|
||||||
|
'../app/js/Features/Institutions/InstitutionsManager'
|
||||||
|
)
|
||||||
|
|
||||||
|
const institutionId = parseInt(process.argv[2])
|
||||||
|
if (isNaN(institutionId)) throw new Error('No institution id')
|
||||||
|
console.log('Upgrading users of institution', institutionId)
|
||||||
|
|
||||||
|
InstitutionsManager.upgradeInstitutionUsers(institutionId, function (error) {
|
||||||
|
if (error) {
|
||||||
|
console.log(error)
|
||||||
|
} else {
|
||||||
|
console.log('DONE 👌')
|
||||||
|
}
|
||||||
|
process.exit()
|
||||||
|
})
|
|
@ -25,6 +25,22 @@ describe "InstitutionsAPI", ->
|
||||||
email:"hello@world.com"
|
email:"hello@world.com"
|
||||||
@newEmail = "bob@bob.com"
|
@newEmail = "bob@bob.com"
|
||||||
|
|
||||||
|
describe 'getInstitutionAffiliations', ->
|
||||||
|
it 'get affiliations', (done)->
|
||||||
|
@institutionId = 123
|
||||||
|
responseBody = ['123abc', '456def']
|
||||||
|
@request.yields(null, { statusCode: 200 }, responseBody)
|
||||||
|
@InstitutionsAPI.getInstitutionAffiliations @institutionId, (err, body) =>
|
||||||
|
should.not.exist(err)
|
||||||
|
@request.calledOnce.should.equal true
|
||||||
|
requestOptions = @request.lastCall.args[0]
|
||||||
|
expectedUrl = "v1.url/api/v2/institutions/#{@institutionId}/affiliations"
|
||||||
|
requestOptions.url.should.equal expectedUrl
|
||||||
|
requestOptions.method.should.equal 'GET'
|
||||||
|
should.not.exist(requestOptions.body)
|
||||||
|
body.should.equal responseBody
|
||||||
|
done()
|
||||||
|
|
||||||
describe 'getUserAffiliations', ->
|
describe 'getUserAffiliations', ->
|
||||||
it 'get affiliations', (done)->
|
it 'get affiliations', (done)->
|
||||||
responseBody = [{ foo: 'bar' }]
|
responseBody = [{ foo: 'bar' }]
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
should = require('chai').should()
|
||||||
|
SandboxedModule = require('sandboxed-module')
|
||||||
|
path = require('path')
|
||||||
|
sinon = require('sinon')
|
||||||
|
modulePath = path.join __dirname, "../../../../app/js/Features/Institutions/InstitutionsManager"
|
||||||
|
|
||||||
|
describe "InstitutionsManager", ->
|
||||||
|
beforeEach ->
|
||||||
|
@institutionId = 123
|
||||||
|
@logger = log: ->
|
||||||
|
@getInstitutionAffiliations = sinon.stub()
|
||||||
|
@refreshFeatures = sinon.stub().yields()
|
||||||
|
@InstitutionsManager = SandboxedModule.require modulePath, requires:
|
||||||
|
'logger-sharelatex': @logger
|
||||||
|
'./InstitutionsAPI':
|
||||||
|
getInstitutionAffiliations: @getInstitutionAffiliations
|
||||||
|
'../Subscription/FeaturesUpdater':
|
||||||
|
refreshFeatures: @refreshFeatures
|
||||||
|
|
||||||
|
describe 'upgradeInstitutionUsers', ->
|
||||||
|
it 'refresh all users Features', (done) ->
|
||||||
|
affiliations = [
|
||||||
|
{ user_id: '123abc123abc123abc123abc' }
|
||||||
|
{ user_id: '456def456def456def456def' }
|
||||||
|
]
|
||||||
|
@getInstitutionAffiliations.yields(null, affiliations)
|
||||||
|
@InstitutionsManager.upgradeInstitutionUsers @institutionId, (error) =>
|
||||||
|
should.not.exist(error)
|
||||||
|
sinon.assert.calledTwice(@refreshFeatures)
|
||||||
|
done()
|
Loading…
Reference in a new issue