Merge pull request #2864 from overleaf/ta-institutions-decaf

Decaffeinate Institutions Code

GitOrigin-RevId: 520565d58518e499d2522653ba1ff9c232ebdb88
This commit is contained in:
Timothée Alby 2020-06-01 10:37:56 +02:00 committed by Copybot
parent 460be2d99e
commit 3f5e90078e
4 changed files with 78 additions and 198 deletions

View file

@ -1,16 +1,3 @@
/* eslint-disable
handle-callback-err,
max-len,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex')
const settings = require('settings-sharelatex')
@ -21,10 +8,7 @@ const { V1ConnectionError } = require('../Errors/Errors')
const InstitutionsAPI = {
getInstitutionAffiliations(institutionId, callback) {
if (callback == null) {
callback = function(error, body) {}
}
return makeAffiliationRequest(
makeAffiliationRequest(
{
method: 'GET',
path: `/api/v2/institutions/${institutionId.toString()}/affiliations`,
@ -35,10 +19,7 @@ const InstitutionsAPI = {
},
getInstitutionLicences(institutionId, startDate, endDate, lag, callback) {
if (callback == null) {
callback = function(error, body) {}
}
return makeAffiliationRequest(
makeAffiliationRequest(
{
method: 'GET',
path: `/api/v2/institutions/${institutionId.toString()}/institution_licences`,
@ -50,10 +31,7 @@ const InstitutionsAPI = {
},
getInstitutionNewLicences(institutionId, startDate, endDate, lag, callback) {
if (callback == null) {
callback = function(error, body) {}
}
return makeAffiliationRequest(
makeAffiliationRequest(
{
method: 'GET',
path: `/api/v2/institutions/${institutionId.toString()}/new_institution_licences`,
@ -65,10 +43,7 @@ const InstitutionsAPI = {
},
getUserAffiliations(userId, callback) {
if (callback == null) {
callback = function(error, body) {}
}
return makeAffiliationRequest(
makeAffiliationRequest(
{
method: 'GET',
path: `/api/v2/users/${userId.toString()}/affiliations`,
@ -79,14 +54,14 @@ const InstitutionsAPI = {
},
addAffiliation(userId, email, affiliationOptions, callback) {
if (callback == null) {
if (!callback) {
// affiliationOptions is optional
callback = affiliationOptions
affiliationOptions = {}
}
const { university, department, role, confirmedAt } = affiliationOptions
return makeAffiliationRequest(
makeAffiliationRequest(
{
method: 'POST',
path: `/api/v2/users/${userId.toString()}/affiliations`,
@ -97,9 +72,13 @@ const InstitutionsAPI = {
if (error) {
return callback(error, body)
}
if (!university) {
return callback(null, body)
}
// have notifications delete any ip matcher notifications for this university
return NotificationsBuilder.ipMatcherAffiliation(userId).read(
university != null ? university.id : undefined,
NotificationsBuilder.ipMatcherAffiliation(userId).read(
university.id,
function(err) {
if (err) {
// log and ignore error
@ -108,7 +87,7 @@ const InstitutionsAPI = {
'Something went wrong marking ip notifications read'
)
}
return callback(null, body)
callback(null, body)
}
)
}
@ -116,10 +95,7 @@ const InstitutionsAPI = {
},
removeAffiliation(userId, email, callback) {
if (callback == null) {
callback = function(error) {}
}
return makeAffiliationRequest(
makeAffiliationRequest(
{
method: 'POST',
path: `/api/v2/users/${userId.toString()}/affiliations/remove`,
@ -132,10 +108,7 @@ const InstitutionsAPI = {
},
endorseAffiliation(userId, email, role, department, callback) {
if (callback == null) {
callback = function(error) {}
}
return makeAffiliationRequest(
makeAffiliationRequest(
{
method: 'POST',
path: `/api/v2/users/${userId.toString()}/affiliations/endorse`,
@ -147,10 +120,7 @@ const InstitutionsAPI = {
},
deleteAffiliations(userId, callback) {
if (callback == null) {
callback = function(error) {}
}
return makeAffiliationRequest(
makeAffiliationRequest(
{
method: 'DELETE',
path: `/api/v2/users/${userId.toString()}/affiliations`,
@ -161,7 +131,7 @@ const InstitutionsAPI = {
},
addEntitlement(userId, email, callback) {
return makeAffiliationRequest(
makeAffiliationRequest(
{
method: 'POST',
path: `/api/v2/users/${userId}/affiliations/add_entitlement`,
@ -173,7 +143,7 @@ const InstitutionsAPI = {
},
removeEntitlement(userId, email, callback) {
return makeAffiliationRequest(
makeAffiliationRequest(
{
method: 'POST',
path: `/api/v2/users/${userId}/affiliations/remove_entitlement`,
@ -186,16 +156,13 @@ const InstitutionsAPI = {
}
var makeAffiliationRequest = function(requestOptions, callback) {
if (callback == null) {
callback = function(error) {}
}
if (!settings.apis.v1.url) {
return callback(null)
} // service is not configured
if (!requestOptions.extraSuccessStatusCodes) {
requestOptions.extraSuccessStatusCodes = []
}
return request(
request(
{
method: requestOptions.method,
url: `${settings.apis.v1.url}${requestOptions.path}`,
@ -205,7 +172,7 @@ var makeAffiliationRequest = function(requestOptions, callback) {
timeout: 20 * 1000
},
function(error, response, body) {
if (error != null) {
if (error) {
return callback(
new V1ConnectionError('error getting affiliations from v1').withCause(
error
@ -225,13 +192,13 @@ var makeAffiliationRequest = function(requestOptions, callback) {
}
let isSuccess = response.statusCode >= 200 && response.statusCode < 300
if (!isSuccess) {
isSuccess = Array.from(requestOptions.extraSuccessStatusCodes).includes(
isSuccess = requestOptions.extraSuccessStatusCodes.includes(
response.statusCode
)
}
if (!isSuccess) {
let errorMessage
if (body != null ? body.errors : undefined) {
if (body && body.errors) {
errorMessage = `${response.statusCode}: ${body.errors}`
} else {
errorMessage = `${requestOptions.defaultErrorMessage}: ${
@ -246,7 +213,7 @@ var makeAffiliationRequest = function(requestOptions, callback) {
return callback(new Error(errorMessage))
}
return callback(null, body)
callback(null, body)
}
)
}

View file

@ -1,17 +1,3 @@
/* eslint-disable
handle-callback-err,
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let InstitutionsController
const logger = require('logger-sharelatex')
const UserGetter = require('../User/UserGetter')
const { addAffiliation } = require('../Institutions/InstitutionsAPI')
@ -19,49 +5,41 @@ const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
const async = require('async')
const ASYNC_AFFILIATIONS_LIMIT = 10
module.exports = InstitutionsController = {
module.exports = {
confirmDomain(req, res, next) {
const { hostname } = req.body
return affiliateUsers(hostname, function(error) {
if (error != null) {
affiliateUsers(hostname, function(error) {
if (error) {
return next(error)
}
return res.sendStatus(200)
res.sendStatus(200)
})
}
}
var affiliateUsers = function(hostname, callback) {
if (callback == null) {
callback = function(error) {}
}
const reversedHostname = hostname
.trim()
.split('')
.reverse()
.join('')
return UserGetter.getUsersByHostname(
hostname,
{ _id: 1, emails: 1 },
function(error, users) {
if (error != null) {
logger.warn({ error }, 'problem fetching users by hostname')
return callback(error)
}
return async.mapLimit(
users,
ASYNC_AFFILIATIONS_LIMIT,
(user, innerCallback) =>
affiliateUserByReversedHostname(
user,
reversedHostname,
innerCallback
),
callback
)
UserGetter.getUsersByHostname(hostname, { _id: 1, emails: 1 }, function(
error,
users
) {
if (error) {
logger.warn({ error }, 'problem fetching users by hostname')
return callback(error)
}
)
async.mapLimit(
users,
ASYNC_AFFILIATIONS_LIMIT,
(user, innerCallback) =>
affiliateUserByReversedHostname(user, reversedHostname, innerCallback),
callback
)
})
}
var affiliateUserByReversedHostname = function(
@ -72,7 +50,7 @@ var affiliateUserByReversedHostname = function(
const matchingEmails = user.emails.filter(
email => email.reversedHostname === reversedHostname
)
return async.mapSeries(
async.mapSeries(
matchingEmails,
(email, innerCallback) =>
addAffiliation(
@ -80,14 +58,14 @@ var affiliateUserByReversedHostname = function(
email.email,
{ confirmedAt: email.confirmedAt },
error => {
if (error != null) {
if (error) {
logger.warn(
{ error },
'problem adding affiliation while confirming hostname'
)
return innerCallback(error)
}
return FeaturesUpdater.refreshFeatures(user._id, innerCallback)
FeaturesUpdater.refreshFeatures(user._id, innerCallback)
}
),
callback

View file

@ -1,88 +1,52 @@
/* eslint-disable
camelcase,
handle-callback-err,
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let InstitutionsGetter
const UserGetter = require('../User/UserGetter')
const UserMembershipsHandler = require('../UserMembership/UserMembershipsHandler')
const UserMembershipEntityConfigs = require('../UserMembership/UserMembershipEntityConfigs')
const logger = require('logger-sharelatex')
module.exports = InstitutionsGetter = {
getConfirmedAffiliations(userId, callback) {
if (callback == null) {
callback = function(error, institutions) {}
}
return UserGetter.getUserFullEmails(userId, function(error, emailsData) {
if (error != null) {
UserGetter.getUserFullEmails(userId, function(error, emailsData) {
if (error) {
return callback(error)
}
const confirmedAffiliations = emailsData
.filter(
emailData =>
emailData.confirmedAt != null &&
__guard__(
emailData.affiliation != null
? emailData.affiliation.institution
: undefined,
x => x.confirmed
)
emailData.confirmedAt &&
emailData.affiliation &&
emailData.affiliation.institution &&
emailData.affiliation.institution.confirmed
)
.map(emailData => emailData.affiliation)
return callback(null, confirmedAffiliations)
callback(null, confirmedAffiliations)
})
},
getConfirmedInstitutions(userId, callback) {
if (callback == null) {
callback = function(error, institutions) {}
}
InstitutionsGetter.getConfirmedAffiliations(
userId,
(error, confirmedAffiliations) => {
if (error != null) {
if (error) {
return callback(error)
}
const confirmedInstitutions = confirmedAffiliations.map(
confirmedAffiliation =>
confirmedAffiliation != null
? confirmedAffiliation.institution
: undefined
confirmedAffiliation ? confirmedAffiliation.institution : undefined
)
return callback(null, confirmedInstitutions)
callback(null, confirmedInstitutions)
}
)
},
getManagedInstitutions(user_id, callback) {
if (callback == null) {
callback = function(error, managedInstitutions) {}
}
return UserMembershipsHandler.getEntitiesByUser(
getManagedInstitutions(userId, callback) {
UserMembershipsHandler.getEntitiesByUser(
UserMembershipEntityConfigs.institution,
user_id,
userId,
callback
)
}
}
function __guard__(value, transform) {
return typeof value !== 'undefined' && value !== null
? transform(value)
: undefined
}

View file

@ -1,21 +1,4 @@
/* eslint-disable
handle-callback-err,
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let InstitutionsManager
const logger = require('logger-sharelatex')
const async = require('async')
const { db } = require('../../infrastructure/mongojs')
const _ = require('underscore')
const { ObjectId } = require('../../infrastructure/mongojs')
const { getInstitutionAffiliations } = require('./InstitutionsAPI')
@ -28,12 +11,9 @@ const { Institution } = require('../../models/Institution')
const { Subscription } = require('../../models/Subscription')
const ASYNC_LIMIT = 10
module.exports = InstitutionsManager = {
module.exports = {
upgradeInstitutionUsers(institutionId, callback) {
if (callback == null) {
callback = function(error) {}
}
return async.waterfall(
async.waterfall(
[
cb => fetchInstitutionAndAffiliations(institutionId, cb),
function(institution, affiliations, cb) {
@ -42,11 +22,8 @@ module.exports = InstitutionsManager = {
affiliation.institutionId = institutionId
return affiliation
})
return async.eachLimit(
affiliations,
ASYNC_LIMIT,
refreshFeatures,
err => cb(err)
async.eachLimit(affiliations, ASYNC_LIMIT, refreshFeatures, err =>
cb(err)
)
}
],
@ -55,33 +32,27 @@ module.exports = InstitutionsManager = {
},
checkInstitutionUsers(institutionId, callback) {
if (callback == null) {
callback = function(error) {}
}
return getInstitutionAffiliations(institutionId, (error, affiliations) =>
getInstitutionAffiliations(institutionId, (error, affiliations) => {
if (error) {
return callback(error)
}
UserGetter.getUsersByAnyConfirmedEmail(
affiliations.map(affiliation => affiliation.email),
{ features: 1, samlIdentifiers: 1 },
(error, users) => callback(error, checkFeatures(institutionId, users))
)
)
})
},
getInstitutionUsersSubscriptions(institutionId, callback) {
if (callback == null) {
callback = function(error, subscriptions) {}
}
return getInstitutionAffiliations(institutionId, function(
error,
affiliations
) {
if (error != null) {
getInstitutionAffiliations(institutionId, function(error, affiliations) {
if (error) {
return callback(error)
}
const userIds = affiliations.map(affiliation =>
ObjectId(affiliation.user_id)
)
return Subscription.find({
Subscription.find({
admin_id: userIds,
planCode: { $not: /trial/ }
})
@ -110,7 +81,7 @@ var fetchInstitutionAndAffiliations = (institutionId, callback) =>
var refreshFeatures = function(affiliation, callback) {
const userId = ObjectId(affiliation.user_id)
return async.waterfall(
async.waterfall(
[
cb =>
FeaturesUpdater.refreshFeatures(
@ -145,26 +116,26 @@ var notifyUser = (user, affiliation, subscription, featuresChanged, callback) =>
[
function(cb) {
if (featuresChanged) {
return NotificationsBuilder.featuresUpgradedByAffiliation(
NotificationsBuilder.featuresUpgradedByAffiliation(
affiliation,
user
).create(cb)
} else {
return cb()
cb()
}
},
function(cb) {
if (
subscription != null &&
subscription.planCode.match(/(free|trial)/) == null &&
subscription &&
!subscription.planCode.match(/(free|trial)/) &&
!subscription.groupPlan
) {
return NotificationsBuilder.redundantPersonalSubscription(
NotificationsBuilder.redundantPersonalSubscription(
affiliation,
user
).create(cb)
} else {
return cb()
cb()
}
}
],