2022-01-19 10:10:39 -05:00
|
|
|
const { promisify, callbackify } = require('util')
|
2019-05-29 05:21:06 -04:00
|
|
|
const UserGetter = require('../User/UserGetter')
|
|
|
|
const UserMembershipsHandler = require('../UserMembership/UserMembershipsHandler')
|
|
|
|
const UserMembershipEntityConfigs = require('../UserMembership/UserMembershipEntityConfigs')
|
|
|
|
|
2021-07-27 09:23:14 -04:00
|
|
|
async function _getCurrentAffiliations(userId) {
|
|
|
|
const fullEmails = await UserGetter.promises.getUserFullEmails(userId)
|
|
|
|
// current are those confirmed and not with lapsed reconfirmations
|
|
|
|
return fullEmails
|
|
|
|
.filter(
|
|
|
|
emailData =>
|
|
|
|
emailData.confirmedAt &&
|
|
|
|
emailData.affiliation &&
|
|
|
|
emailData.affiliation.institution &&
|
|
|
|
emailData.affiliation.institution.confirmed &&
|
|
|
|
!emailData.affiliation.pastReconfirmDate
|
|
|
|
)
|
|
|
|
.map(emailData => emailData.affiliation)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getCurrentInstitutionIds(userId) {
|
|
|
|
// current are those confirmed and not with lapsed reconfirmations
|
|
|
|
// only 1 record returned per current institutionId
|
|
|
|
const institutionIds = new Set()
|
|
|
|
const currentAffiliations = await _getCurrentAffiliations(userId)
|
|
|
|
currentAffiliations.forEach(affiliation => {
|
|
|
|
institutionIds.add(affiliation.institution.id)
|
|
|
|
})
|
|
|
|
return [...institutionIds]
|
|
|
|
}
|
|
|
|
|
2021-08-16 09:08:16 -04:00
|
|
|
async function getCurrentInstitutionsWithLicence(userId) {
|
|
|
|
// current are those confirmed and not with lapsed reconfirmations
|
|
|
|
// only 1 record returned per current institution
|
|
|
|
const institutions = {}
|
|
|
|
const currentAffiliations = await _getCurrentAffiliations(userId)
|
|
|
|
currentAffiliations.forEach(affiliation => {
|
|
|
|
if (affiliation.licence && affiliation.licence !== 'free') {
|
|
|
|
institutions[affiliation.institution.id] = affiliation.institution
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return Object.values(institutions)
|
|
|
|
}
|
|
|
|
|
2021-07-27 09:23:14 -04:00
|
|
|
const InstitutionsGetter = {
|
2020-02-18 06:36:53 -05:00
|
|
|
getConfirmedAffiliations(userId, callback) {
|
2021-04-14 09:17:21 -04:00
|
|
|
UserGetter.getUserFullEmails(userId, function (error, emailsData) {
|
2020-06-01 04:37:56 -04:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
2020-02-18 06:36:53 -05:00
|
|
|
const confirmedAffiliations = emailsData
|
2019-05-29 05:21:06 -04:00
|
|
|
.filter(
|
|
|
|
emailData =>
|
2020-06-01 04:37:56 -04:00
|
|
|
emailData.confirmedAt &&
|
|
|
|
emailData.affiliation &&
|
|
|
|
emailData.affiliation.institution &&
|
|
|
|
emailData.affiliation.institution.confirmed
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2020-02-18 06:36:53 -05:00
|
|
|
.map(emailData => emailData.affiliation)
|
|
|
|
|
2020-06-01 04:37:56 -04:00
|
|
|
callback(null, confirmedAffiliations)
|
2020-02-18 06:36:53 -05:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2021-07-27 09:23:14 -04:00
|
|
|
getCurrentInstitutionIds: callbackify(getCurrentInstitutionIds),
|
2021-08-16 09:08:16 -04:00
|
|
|
getCurrentInstitutionsWithLicence: callbackify(
|
|
|
|
getCurrentInstitutionsWithLicence
|
|
|
|
),
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-06-01 04:37:56 -04:00
|
|
|
getManagedInstitutions(userId, callback) {
|
|
|
|
UserMembershipsHandler.getEntitiesByUser(
|
2019-05-29 05:21:06 -04:00
|
|
|
UserMembershipEntityConfigs.institution,
|
2020-06-01 04:37:56 -04:00
|
|
|
userId,
|
2019-05-29 05:21:06 -04:00
|
|
|
callback
|
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-07-27 09:23:14 -04:00
|
|
|
|
|
|
|
InstitutionsGetter.promises = {
|
|
|
|
getCurrentInstitutionIds,
|
2021-08-16 09:08:16 -04:00
|
|
|
getCurrentInstitutionsWithLicence,
|
2022-01-19 10:10:39 -05:00
|
|
|
getManagedInstitutions: promisify(InstitutionsGetter.getManagedInstitutions),
|
2021-07-27 09:23:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = InstitutionsGetter
|