| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- const { callbackify } = require('util')
- const OError = require('@overleaf/o-error')
- const logger = require('@overleaf/logger')
- const settings = require('@overleaf/settings')
- const request = require('requestretry')
- const { promisifyAll } = require('@overleaf/promise-utils')
- const NotificationsBuilder = require('../Notifications/NotificationsBuilder')
- const {
- V1ConnectionError,
- InvalidInstitutionalEmailError,
- } = require('../Errors/Errors')
- const { fetchJson, fetchNothing } = require('@overleaf/fetch-utils')
- const { promiseMapWithLimit } = require('@overleaf/promise-utils')
- const Modules = require('../../infrastructure/Modules')
- function _makeRequestOptions(options) {
- const requestOptions = {
- method: options.method,
- basicAuth: { user: settings.apis.v1.user, password: settings.apis.v1.pass },
- signal: AbortSignal.timeout(settings.apis.v1.timeout),
- }
- if (options.body) {
- requestOptions.json = options.body
- }
- return requestOptions
- }
- function _responseErrorHandling(options, error) {
- const status = error.response.status
- if (status >= 500) {
- throw new V1ConnectionError({
- message: 'error getting affiliations from v1',
- info: {
- status,
- body: error.body,
- },
- })
- }
- let errorBody
- try {
- if (error.body) {
- errorBody = JSON.parse(error.body)
- }
- } catch (e) {}
- let errorMessage
- if (errorBody?.errors) {
- errorMessage = `${status}: ${errorBody.errors}`
- } else {
- errorMessage = `${options.defaultErrorMessage}: ${status}`
- }
- throw new OError(errorMessage, { status })
- }
- async function _affiliationRequestFetchJson(options) {
- if (!settings.apis.v1.url) {
- return
- } // service is not configured
- const url = `${settings.apis.v1.url}${options.path}`
- const requestOptions = _makeRequestOptions(options)
- try {
- return await fetchJson(url, requestOptions)
- } catch (error) {
- _responseErrorHandling(options, error)
- }
- }
- async function _affiliationRequestFetchNothing(options) {
- if (!settings.apis.v1.url) {
- return
- } // service is not configured
- const url = `${settings.apis.v1.url}${options.path}`
- const requestOptions = _makeRequestOptions(options)
- try {
- await fetchNothing(url, requestOptions)
- } catch (error) {
- _responseErrorHandling(options, error)
- }
- }
- async function _affiliationRequestFetchNothing404Ok(options) {
- try {
- await _affiliationRequestFetchNothing(options)
- } catch (error) {
- const status = error.info?.status
- if (status !== 404) {
- throw error
- }
- }
- }
- function getInstitutionAffiliations(institutionId, callback) {
- makeAffiliationRequest(
- {
- method: 'GET',
- path: `/api/v2/institutions/${institutionId.toString()}/affiliations`,
- defaultErrorMessage: "Couldn't get institution affiliations",
- },
- (error, body) => callback(error, body || [])
- )
- }
- function getConfirmedInstitutionAffiliations(institutionId, callback) {
- makeAffiliationRequest(
- {
- method: 'GET',
- path: `/api/v2/institutions/${institutionId.toString()}/confirmed_affiliations`,
- defaultErrorMessage: "Couldn't get institution affiliations",
- },
- (error, body) => callback(error, body || [])
- )
- }
- function getInstitutionAffiliationsCounts(institutionId, callback) {
- makeAffiliationRequest(
- {
- method: 'GET',
- path: `/api/v2/institutions/${institutionId.toString()}/affiliations_counts`,
- defaultErrorMessage: "Couldn't get institution counts",
- },
- (error, body) => callback(error, body || [])
- )
- }
- function getLicencesForAnalytics(lag, queryDate, callback) {
- makeAffiliationRequest(
- {
- method: 'GET',
- path: `/api/v2/institutions/institutions_licences`,
- body: { query_date: queryDate, lag },
- defaultErrorMessage: 'Could not get institutions licences',
- },
- callback
- )
- }
- function getUserAffiliations(userId, callback) {
- makeAffiliationRequest(
- {
- method: 'GET',
- path: `/api/v2/users/${userId.toString()}/affiliations`,
- defaultErrorMessage: "Couldn't get user affiliations",
- },
- async (error, body) => {
- if (error) {
- return callback(error, [])
- }
- const affiliations = []
- if (body?.length > 0) {
- const concurrencyLimit = 10
- await promiseMapWithLimit(concurrencyLimit, body, async affiliation => {
- const group = (
- await Modules.promises.hooks.fire(
- 'getGroupWithDomainCaptureByV1Id',
- affiliation.institution.id
- )
- )?.[0]
- if (group) {
- affiliation.group = {
- _id: group._id,
- managedUsersEnabled: Boolean(group.managedUsersEnabled),
- domainCaptureEnabled: Boolean(group.domainCaptureEnabled),
- }
- }
- affiliations.push(affiliation)
- })
- }
- callback(null, affiliations)
- }
- )
- }
- async function getUsersNeedingReconfirmationsLapsedProcessed() {
- return await _affiliationRequestFetchJson({
- method: 'GET',
- path: '/api/v2/institutions/need_reconfirmation_lapsed_processed',
- defaultErrorMessage:
- 'Could not get users that need reconfirmations lapsed processed',
- })
- }
- async function addAffiliation(userId, email, affiliationOptions) {
- const {
- university,
- department,
- role,
- confirmedAt,
- entitlement,
- rejectIfBlocklisted,
- } = affiliationOptions
- try {
- await _affiliationRequestFetchNothing({
- method: 'POST',
- path: `/api/v2/users/${userId.toString()}/affiliations`,
- body: {
- email,
- university,
- department,
- role,
- confirmedAt,
- entitlement,
- rejectIfBlocklisted,
- },
- defaultErrorMessage: "Couldn't create affiliation",
- })
- } catch (error) {
- if (error.info?.status === 422) {
- throw new InvalidInstitutionalEmailError(error.message).withCause(error)
- }
- throw error
- }
- if (!university) {
- return
- }
- // have notifications delete any ip matcher notifications for this university
- try {
- await NotificationsBuilder.promises
- .ipMatcherAffiliation(userId)
- .read(university.id)
- } catch (err) {
- // log and ignore error
- logger.err({ err }, 'Something went wrong marking ip notifications read')
- }
- }
- async function removeAffiliation(userId, email) {
- await _affiliationRequestFetchNothing404Ok({
- method: 'POST',
- path: `/api/v2/users/${userId.toString()}/affiliations/remove`,
- body: { email },
- defaultErrorMessage: "Couldn't remove affiliation",
- })
- }
- function endorseAffiliation(userId, email, role, department, callback) {
- makeAffiliationRequest(
- {
- method: 'POST',
- path: `/api/v2/users/${userId.toString()}/affiliations/endorse`,
- body: { email, role, department },
- defaultErrorMessage: "Couldn't endorse affiliation",
- },
- callback
- )
- }
- function deleteAffiliations(userId, callback) {
- makeAffiliationRequest(
- {
- method: 'DELETE',
- path: `/api/v2/users/${userId.toString()}/affiliations`,
- defaultErrorMessage: "Couldn't delete affiliations",
- },
- callback
- )
- }
- function addEntitlement(userId, email, callback) {
- makeAffiliationRequest(
- {
- method: 'POST',
- path: `/api/v2/users/${userId}/affiliations/add_entitlement`,
- body: { email },
- defaultErrorMessage: "Couldn't add entitlement",
- },
- callback
- )
- }
- function removeEntitlement(userId, email, callback) {
- makeAffiliationRequest(
- {
- method: 'POST',
- path: `/api/v2/users/${userId}/affiliations/remove_entitlement`,
- body: { email },
- defaultErrorMessage: "Couldn't remove entitlement",
- extraSuccessStatusCodes: [404],
- },
- callback
- )
- }
- function sendUsersWithReconfirmationsLapsedProcessed(users, callback) {
- makeAffiliationRequest(
- {
- method: 'POST',
- path: '/api/v2/institutions/reconfirmation_lapsed_processed',
- body: { users },
- defaultErrorMessage:
- 'Could not update reconfirmation_lapsed_processed_at',
- },
- (error, body) => callback(error, body || [])
- )
- }
- const InstitutionsAPI = {
- getInstitutionAffiliations,
- getConfirmedInstitutionAffiliations,
- getInstitutionAffiliationsCounts,
- getLicencesForAnalytics,
- getUserAffiliations,
- getUsersNeedingReconfirmationsLapsedProcessed: callbackify(
- getUsersNeedingReconfirmationsLapsedProcessed
- ),
- addAffiliation: callbackify(addAffiliation),
- removeAffiliation: callbackify(removeAffiliation),
- endorseAffiliation,
- deleteAffiliations,
- addEntitlement,
- removeEntitlement,
- sendUsersWithReconfirmationsLapsedProcessed,
- }
- function makeAffiliationRequest(options, callback) {
- if (!settings.apis.v1.url) {
- return callback(null)
- } // service is not configured
- if (!options.extraSuccessStatusCodes) {
- options.extraSuccessStatusCodes = []
- }
- const requestOptions = {
- method: options.method,
- url: `${settings.apis.v1.url}${options.path}`,
- body: options.body,
- auth: { user: settings.apis.v1.user, pass: settings.apis.v1.pass },
- json: true,
- timeout: settings.apis.v1.timeout,
- }
- if (options.method === 'GET') {
- requestOptions.maxAttempts = 3
- requestOptions.retryDelay = 500
- } else {
- requestOptions.maxAttempts = 0
- }
- request(requestOptions, function (error, response, body) {
- if (error) {
- return callback(
- new V1ConnectionError('error getting affiliations from v1').withCause(
- error
- )
- )
- }
- if (response && response.statusCode >= 500) {
- return callback(
- new V1ConnectionError({
- message: 'error getting affiliations from v1',
- info: {
- status: response.statusCode,
- body,
- },
- })
- )
- }
- let isSuccess = response.statusCode >= 200 && response.statusCode < 300
- if (!isSuccess) {
- isSuccess = options.extraSuccessStatusCodes.includes(response.statusCode)
- }
- if (!isSuccess) {
- let errorMessage
- if (body && body.errors) {
- errorMessage = `${response.statusCode}: ${body.errors}`
- } else {
- errorMessage = `${options.defaultErrorMessage}: ${response.statusCode}`
- }
- logger.warn({ path: options.path, body: options.body }, errorMessage)
- return callback(
- new OError(errorMessage, { statusCode: response.statusCode })
- )
- }
- callback(null, body)
- })
- }
- InstitutionsAPI.promises = promisifyAll(InstitutionsAPI, {
- without: [
- 'addAffiliation',
- 'removeAffiliation',
- 'getUsersNeedingReconfirmationsLapsedProcessed',
- ],
- })
- InstitutionsAPI.promises.addAffiliation = addAffiliation
- InstitutionsAPI.promises.removeAffiliation = removeAffiliation
- InstitutionsAPI.promises.getUsersNeedingReconfirmationsLapsedProcessed =
- getUsersNeedingReconfirmationsLapsedProcessed
- module.exports = InstitutionsAPI
|