Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 23 additions & 42 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,61 +57,42 @@ function getUserFullName (user) {
}

async function isSecretariat (shortName, useRegistry = false, options = {}) {
let result = false
let orgUUID = null
let secretariats = []

const CONSTANTS = getConstants()
if (useRegistry) {
orgUUID = await getOrgUUID(shortName, useRegistry, options) // may be null if org does not exists
secretariats = await BaseOrg.find({ authority: { $in: [CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT] } })
} else {
orgUUID = await getOrgUUID(shortName, false, options) // may be null if org does not exists
secretariats = await Org.find({ 'authority.active_roles': { $in: [CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT] } })
const ModelToQuery = useRegistry ? BaseOrg : Org
const roleField = useRegistry ? 'authority' : 'authority.active_roles'
const query = {
short_name: shortName,
[roleField]: CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT
}

if (orgUUID) {
secretariats.forEach((obj) => {
if (obj.UUID === orgUUID) {
result = true // org is secretariat
}
})
}
const executionOptions = { ...options }
if (executionOptions.lean === undefined) executionOptions.lean = true

return result
const org = await ModelToQuery.findOne(query, 'UUID', executionOptions)
return !!org // org is secretariat only if a match was found
}

async function isSecretariatUUID (orgUUID) {
let result = false
if (!orgUUID) return false
const CONSTANTS = getConstants()
const secretariats = await BaseOrg.find({ authority: { $in: [CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT] } })

if (orgUUID) {
secretariats.forEach((obj) => {
if (obj.UUID === orgUUID) {
result = true // org is secretariat
}
})
}

return result // org is not secretariat
const org = await BaseOrg.findOne(
{ UUID: orgUUID, authority: CONSTANTS.AUTH_ROLE_ENUM.SECRETARIAT },
'UUID',
{ lean: true }
)
return !!org // org is secretariat only if a match was found
}

async function isBulkDownload (shortName) {
let result = false
const CONSTANTS = getConstants()
const orgUUID = await getOrgUUID(shortName) // may be null if org does not exists
const bulkDownloadOrgs = await BaseOrg.find({ authority: { $in: [CONSTANTS.AUTH_ROLE_ENUM.BULK_DOWNLOAD] } })

if (orgUUID) {
bulkDownloadOrgs.forEach((obj) => {
if (obj.UUID === orgUUID) {
result = true // org has the bulk download role
}
})
}

return result // org does not have bulk download as a role
if (!orgUUID) return false
const org = await BaseOrg.findOne(
{ UUID: orgUUID, authority: CONSTANTS.AUTH_ROLE_ENUM.BULK_DOWNLOAD },
'UUID',
{ lean: true }
)
return !!org // org has bulk download as a role only if a match was found
}

async function isAdmin (requesterUsername, requesterShortName, isRegistry = false, options = {}) {
Expand Down
Loading