2018-06-27 12:29:56 -04:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
metrics = require("metrics-sharelatex")
|
|
|
|
settings = require "settings-sharelatex"
|
|
|
|
request = require "request"
|
|
|
|
|
2018-07-10 15:49:24 -04:00
|
|
|
module.exports = InstitutionsAPI =
|
2018-07-10 17:53:06 -04:00
|
|
|
getInstitutionAffiliations: (institutionId, callback = (error, body) ->) ->
|
|
|
|
makeAffiliationRequest {
|
|
|
|
method: 'GET'
|
|
|
|
path: "/api/v2/institutions/#{institutionId.toString()}/affiliations"
|
|
|
|
defaultErrorMessage: "Couldn't get institution affiliations"
|
2018-08-20 10:29:44 -04:00
|
|
|
}, (error, body) -> callback(error, body or [])
|
2018-07-10 17:53:06 -04:00
|
|
|
|
2018-08-22 13:31:29 -04:00
|
|
|
getInstitutionLicences: (institutionId, startDate, endDate, lag, callback = (error, body) ->) ->
|
|
|
|
makeAffiliationRequest {
|
|
|
|
method: 'GET'
|
|
|
|
path: "/api/v2/institutions/#{institutionId.toString()}/institution_licences"
|
|
|
|
body: {start_date: startDate, end_date: endDate, lag}
|
2018-08-23 09:39:48 -04:00
|
|
|
defaultErrorMessage: "Couldn't get institution licences"
|
2018-08-22 13:31:29 -04:00
|
|
|
}, callback
|
2018-07-10 17:53:06 -04:00
|
|
|
|
2018-07-10 15:49:24 -04:00
|
|
|
getUserAffiliations: (userId, callback = (error, body) ->) ->
|
2018-06-27 12:29:56 -04:00
|
|
|
makeAffiliationRequest {
|
|
|
|
method: 'GET'
|
|
|
|
path: "/api/v2/users/#{userId.toString()}/affiliations"
|
2018-07-10 15:49:24 -04:00
|
|
|
defaultErrorMessage: "Couldn't get user affiliations"
|
2018-08-20 10:29:44 -04:00
|
|
|
}, (error, body) -> callback(error, body or [])
|
2018-06-27 12:29:56 -04:00
|
|
|
|
|
|
|
|
2018-07-04 09:12:54 -04:00
|
|
|
addAffiliation: (userId, email, affiliationOptions, callback) ->
|
|
|
|
unless callback? # affiliationOptions is optional
|
|
|
|
callback = affiliationOptions
|
|
|
|
affiliationOptions = {}
|
|
|
|
|
2018-08-09 03:20:34 -04:00
|
|
|
{ university, department, role, confirmedAt } = affiliationOptions
|
2018-06-27 12:29:56 -04:00
|
|
|
makeAffiliationRequest {
|
|
|
|
method: 'POST'
|
|
|
|
path: "/api/v2/users/#{userId.toString()}/affiliations"
|
2018-08-09 03:20:34 -04:00
|
|
|
body: { email, university, department, role, confirmedAt }
|
2018-06-27 12:29:56 -04:00
|
|
|
defaultErrorMessage: "Couldn't create affiliation"
|
|
|
|
}, callback
|
|
|
|
|
|
|
|
|
|
|
|
removeAffiliation: (userId, email, callback = (error) ->) ->
|
|
|
|
makeAffiliationRequest {
|
2018-06-27 03:33:06 -04:00
|
|
|
method: 'POST'
|
|
|
|
path: "/api/v2/users/#{userId.toString()}/affiliations/remove"
|
|
|
|
body: { email }
|
2018-06-27 12:29:56 -04:00
|
|
|
extraSuccessStatusCodes: [404] # `Not Found` responses are considered successful
|
|
|
|
defaultErrorMessage: "Couldn't remove affiliation"
|
|
|
|
}, callback
|
|
|
|
|
|
|
|
|
2018-07-05 04:46:06 -04:00
|
|
|
endorseAffiliation: (userId, email, role, department, callback = (error) ->) ->
|
|
|
|
makeAffiliationRequest {
|
|
|
|
method: 'POST'
|
|
|
|
path: "/api/v2/users/#{userId.toString()}/affiliations/endorse"
|
|
|
|
body: { email, role, department }
|
|
|
|
defaultErrorMessage: "Couldn't endorse affiliation"
|
|
|
|
}, callback
|
|
|
|
|
|
|
|
|
2018-06-27 12:43:20 -04:00
|
|
|
deleteAffiliations: (userId, callback = (error) ->) ->
|
|
|
|
makeAffiliationRequest {
|
|
|
|
method: 'DELETE'
|
|
|
|
path: "/api/v2/users/#{userId.toString()}/affiliations"
|
|
|
|
defaultErrorMessage: "Couldn't delete affiliations"
|
|
|
|
}, callback
|
|
|
|
|
|
|
|
|
2018-06-27 12:29:56 -04:00
|
|
|
makeAffiliationRequest = (requestOptions, callback = (error) ->) ->
|
|
|
|
return callback(null) unless settings?.apis?.v1?.url # service is not configured
|
|
|
|
requestOptions.extraSuccessStatusCodes ||= []
|
|
|
|
request {
|
|
|
|
method: requestOptions.method
|
|
|
|
url: "#{settings.apis.v1.url}#{requestOptions.path}"
|
|
|
|
body: requestOptions.body
|
|
|
|
auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass }
|
|
|
|
json: true,
|
|
|
|
timeout: 20 * 1000
|
|
|
|
}, (error, response, body) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
isSuccess = 200 <= response.statusCode < 300
|
|
|
|
isSuccess ||= response.statusCode in requestOptions.extraSuccessStatusCodes
|
|
|
|
unless isSuccess
|
|
|
|
if body?.errors
|
|
|
|
errorMessage = "#{response.statusCode}: #{body.errors}"
|
|
|
|
else
|
|
|
|
errorMessage = "#{requestOptions.defaultErrorMessage}: #{response.statusCode}"
|
2018-08-17 09:13:24 -04:00
|
|
|
|
|
|
|
logger.err path: requestOptions.path, body: requestOptions.body, errorMessage
|
2018-06-27 12:29:56 -04:00
|
|
|
return callback(new Error(errorMessage))
|
|
|
|
|
|
|
|
callback(null, body)
|
|
|
|
|
|
|
|
[
|
2018-07-10 17:53:06 -04:00
|
|
|
'getInstitutionAffiliations'
|
2018-07-10 15:49:24 -04:00
|
|
|
'getUserAffiliations',
|
2018-06-27 12:29:56 -04:00
|
|
|
'addAffiliation',
|
|
|
|
'removeAffiliation',
|
|
|
|
].map (method) ->
|
|
|
|
metrics.timeAsyncMethod(
|
2018-07-10 15:49:24 -04:00
|
|
|
InstitutionsAPI, method, 'mongo.InstitutionsAPI', logger
|
2018-06-27 12:29:56 -04:00
|
|
|
)
|