Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions src/controller/cve-id.controller/cve-id.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ async function getFilteredCveId (req, res, next) {

// Create map of orgUUID to shortnames and users to simplify aggregation later
// Only project the fields needed for the maps to avoid fetching full documents
const orgs = await orgRepo.getAllOrgs({}, { UUID: 1, short_name: 1, _id: 0 })
const users = await userRepo.getAllUsers({}, { UUID: 1, username: 1, org_UUID: 1, _id: 0 })
const orgs = await orgRepo.getCveIdMapOrgs()
const users = await userRepo.getCveIdMapUsers()

const orgMap = {}
const userMap = {}
Expand Down Expand Up @@ -694,7 +694,7 @@ async function nonSequentialReservation (year, amount, shortName, orgShortName,
}
}

available = await cveIdRepo.find({ cve_year: year, state: 'AVAILABLE' }, { limit: availableLimit }) // get available ids
available = await cveIdRepo.find({ cve_year: year, state: 'AVAILABLE' }, { limit: availableLimit, lean: true }) // get available ids

// Case 1: Not enough IDs in the 'AVAILABLE' pool
if (available.length < availableLimit) {
Expand All @@ -708,7 +708,7 @@ async function nonSequentialReservation (year, amount, shortName, orgShortName,
}

await allocateAvailableCveIds(result.ids, year, req) // Pool was incremented. Create 'AVAILABLE' cve ids.
available = await cveIdRepo.find({ cve_year: year, state: 'AVAILABLE' }, { limit: availableLimit }) // get available ids
available = await cveIdRepo.find({ cve_year: year, state: 'AVAILABLE' }, { limit: availableLimit, lean: true }) // get available ids
}

// Case 2: Enough IDs in the 'AVAILABLE' pool
Expand All @@ -734,7 +734,7 @@ async function nonSequentialReservation (year, amount, shortName, orgShortName,
available.splice(index, 1) // remove reserved cve id from the 'AVAILABLE' pool
counter++
} else {
available = await cveIdRepo.find({ cve_year: year, state: 'AVAILABLE' }, { limit: availableLimit }) // get available ids
available = await cveIdRepo.find({ cve_year: year, state: 'AVAILABLE' }, { limit: availableLimit, lean: true }) // get available ids
availableLimit = Math.max(3 * (amount - counter), CONSTANTS.DEFAULT_AVAILABLE_POOL) // recalculate the available limit since some ids might have been reserved

// Case 1: Not enough IDs in the 'AVAILABLE' pool
Expand All @@ -750,7 +750,7 @@ async function nonSequentialReservation (year, amount, shortName, orgShortName,
}

await allocateAvailableCveIds(result.ids, year, req) // Pool was incremented. Create 'AVAILABLE' cve ids.
available = await cveIdRepo.find({ cve_year: year, state: 'AVAILABLE' }, { limit: availableLimit }) // get available ids
available = await cveIdRepo.find({ cve_year: year, state: 'AVAILABLE' }, { limit: availableLimit, lean: true }) // get available ids
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/repositories/auditRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ class AuditRepository extends BaseRepository {
return null
}
const query = { target_uuid: org.UUID }
return this.collection.findOne(query, null, options)
return this.collection.findOne(query, null, options).lean()
}

/**
* Find audit document by target UUID
*/
async findOneByTargetUUID (targetUUID, options = {}) {
const query = { target_uuid: targetUUID }
const auditObject = await Audit.findOne(query, null, options)
const auditObject = await Audit.findOne(query, null, options).lean()
return auditObject
}

Expand All @@ -97,22 +97,21 @@ class AuditRepository extends BaseRepository {
*/
async findOneByUUID (auditUUID, options = {}) {
const query = { uuid: auditUUID }
return this.collection.findOne(query, null, options)
return this.collection.findOne(query, null, options).lean()
}

/**
* Find all audit documents
*/
async findAllAuditDocuments (options = {}) {
const audits = await Audit.find({}, null, options)
return audits.map(audit => audit.toObject())
return Audit.find({}, null, options).lean()
}

/**
* Get the last X changes for a target UUID
*/
async getLastXChanges (targetUUID, numberOfChanges, options = {}) {
const audit = await Audit.findOne({ target_uuid: targetUUID }, null, options)
const audit = await Audit.findOne({ target_uuid: targetUUID }, null, options).lean()
if (!audit || !audit.history || audit.history.length === 0) {
return []
}
Expand Down
73 changes: 55 additions & 18 deletions src/repositories/baseOrgRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,45 @@ function getOrgProjection (isSecretariat = false) {
return projection
}

function applyLeanRegistryOrgDefaults (org) {
const defaultArrayFields = [
'aliases',
'authority',
'users',
'admins',
'private_contacts',
'advisory_locations'
]

defaultArrayFields.forEach(field => {
if (!Array.isArray(org[field])) {
org[field] = []
}
})

if (!org.contact_info || typeof org.contact_info !== 'object') {
org.contact_info = {}
}
if (!Array.isArray(org.contact_info.websites)) {
org.contact_info.websites = []
}
if (!Array.isArray(org.contact_info.emails)) {
org.contact_info.emails = []
}

return org
}

function applyLeanLegacyOrgDefaults (org) {
if (!org.authority || typeof org.authority !== 'object' || Array.isArray(org.authority)) {
org.authority = {}
}
if (!Array.isArray(org.authority.active_roles)) {
org.authority.active_roles = []
}
return org
}

function filterOrg (orgObj, isSecretariat = false, applyResponseMask = false, fieldsToPreserve = []) {
const CONSTANTS = getConstants()
const _ = require('lodash')
Expand Down Expand Up @@ -197,7 +236,8 @@ class BaseOrgRepository extends BaseRepository {
const OrgRepository = require('./orgRepository')
const legacyOrgRepo = new OrgRepository()
if (returnLegacyFormat) return await legacyOrgRepo.findOneByShortName(shortName, options, projection)
const data = await BaseOrgModel.findOne({ short_name: shortName }, projection, options)
const query = BaseOrgModel.findOne({ short_name: shortName }, projection, options)
const data = await (options.lean ? query.lean() : query)
return data
}

Expand All @@ -215,7 +255,8 @@ class BaseOrgRepository extends BaseRepository {
const OrgRepository = require('./orgRepository')
const legacyOrgRepo = new OrgRepository()
if (returnLegacyFormat) return await legacyOrgRepo.findOneByUUID(UUID, options, projection)
return await BaseOrgModel.findOne({ UUID: UUID }, projection, options)
const query = BaseOrgModel.findOne({ UUID: UUID }, projection, options)
return await (options.lean ? query.lean() : query)
}

/**
Expand Down Expand Up @@ -257,7 +298,7 @@ class BaseOrgRepository extends BaseRepository {
{ users: { $in: userUUIDs } },
{ _id: 0, UUID: 1, short_name: 1, users: 1 },
options
)
).lean()
}

/**
Expand Down Expand Up @@ -311,7 +352,7 @@ class BaseOrgRepository extends BaseRepository {
async orgExists (shortName, options = {}, returnLegacyFormat = false) {
if (!shortName) return false
const query = { short_name: exactCaseInsensitiveRegex(shortName) }
const exists = await BaseOrgModel.findOne(query, null, options)
const exists = await BaseOrgModel.findOne(query, { _id: 1 }, options).lean()
if (exists) {
return true
}
Expand Down Expand Up @@ -349,7 +390,7 @@ class BaseOrgRepository extends BaseRepository {
]
}

const collisionOrg = await BaseOrgModel.findOne(query, 'short_name long_name aliases', options)
const collisionOrg = await BaseOrgModel.findOne(query, 'short_name long_name aliases', options).lean()
if (collisionOrg) {
// Determine which string collided for better error reporting
for (const str of searchStrings) {
Expand Down Expand Up @@ -594,10 +635,12 @@ class BaseOrgRepository extends BaseRepository {
const { deepRemoveEmpty } = require('../utils/utils')
const projection = getOrgProjection(isSecretariat)
const data = identifierIsUUID
? await this.findOneByUUID(identifier, options, returnLegacyFormat, projection)
: await this.findOneByShortName(identifier, options, returnLegacyFormat, projection)
? await this.findOneByUUID(identifier, { ...options, lean: true }, returnLegacyFormat, projection)
: await this.findOneByShortName(identifier, { ...options, lean: true }, returnLegacyFormat, projection)
if (!data) return null
const result = data.toObject()
const result = returnLegacyFormat
? applyLeanLegacyOrgDefaults(data)
: applyLeanRegistryOrgDefaults(data)

const parentOrg = await BaseOrgModel.findOne({ oversees: result.UUID }).select('UUID').lean()
if (parentOrg) {
Expand Down Expand Up @@ -1268,11 +1311,8 @@ class BaseOrgRepository extends BaseRepository {
* @returns {Promise<boolean>} True if the organization is a Secretariat, false otherwise.
*/
async isSecretariatByShortName (shortname, options = {}, isLegacyObject = false) {
const org = await BaseOrgModel.findOne({ short_name: shortname }, null, options)
if (org.authority.includes('SECRETARIAT')) {
return true
}
return false
const org = await BaseOrgModel.findOne({ short_name: shortname }, 'authority', options).lean()
return Array.isArray(org?.authority) && org.authority.includes('SECRETARIAT')
}

/**
Expand All @@ -1297,11 +1337,8 @@ class BaseOrgRepository extends BaseRepository {
* @returns {Promise<boolean>} True if the organization is a Bulk Download provider, false otherwise.
*/
async isBulkDownloadByShortname (orgShortname, options = {}, isLegacyObject = false) {
const org = await BaseOrgModel.findOne({ short_name: orgShortname }, null, options)
if (org.authority.includes('BULK_DOWNLOAD')) {
return true
}
return false
const org = await BaseOrgModel.findOne({ short_name: orgShortname }, 'authority', options).lean()
return Array.isArray(org?.authority) && org.authority.includes('BULK_DOWNLOAD')
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/repositories/baseRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ class BaseRepository {

if (count) {
return results.countDocuments().exec()
} else if (lean) {
return results.lean().exec()
} else if (limit) {
return results.limit(limit).exec()
} else {
return results.exec()
}
if (limit) results.limit(limit)
if (lean) results.lean()
return results.exec()
}

async findOne (query = {}) {
Expand Down
14 changes: 7 additions & 7 deletions src/repositories/baseUserRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class BaseUserRepository extends BaseRepository {
* @returns {Promise<boolean>} True if the organization has the user, false otherwise.
*/
async orgHasUserByUUID (orgShortName, uuid, options = {}, isLegacyObject = false) {
const org = await BaseOrgModel.findOne({ short_name: orgShortName }, null, options)
const org = await BaseOrgModel.findOne({ short_name: orgShortName }, 'users', options).lean()
if (!org || !Array.isArray(org.users)) {
return false
}
Expand All @@ -108,13 +108,13 @@ class BaseUserRepository extends BaseRepository {
*/
async orgHasUser (orgShortName, username, options = {}, isLegacyObject = false) {
// 1. Find the org
const org = await BaseOrgModel.findOne({ short_name: orgShortName }, null, options)
const org = await BaseOrgModel.findOne({ short_name: orgShortName }, 'users', options).lean()
if (!org || !Array.isArray(org.users)) {
return false
}

// 2. Check if a user with this username exists in the org
const user = await BaseUser.findOne({ username, UUID: { $in: org.users } }, null, options)
const user = await BaseUser.findOne({ username, UUID: { $in: org.users } }, { _id: 1 }, options).lean()
return !!user
}

Expand Down Expand Up @@ -213,7 +213,7 @@ class BaseUserRepository extends BaseRepository {
{ UUID: { $in: uuids } },
{ _id: 0, UUID: 1, username: 1, name: 1 },
options
)
).lean()
}

/**
Expand All @@ -231,7 +231,7 @@ class BaseUserRepository extends BaseRepository {
return false
}

const org = await BaseOrgModel.findOne({ UUID: orgUUID }, null, options).select('admins users')
const org = await BaseOrgModel.findOne({ UUID: orgUUID }, null, options).select('admins users').lean()
if (!org) {
return false
}
Expand Down Expand Up @@ -326,8 +326,8 @@ class BaseUserRepository extends BaseRepository {
* @returns {Promise<string[]>} An array of user UUIDs.
*/
async findUsersByOrgShortname (shortName, options = {}) {
const org = await BaseOrgModel.findOne({ short_name: shortName }, null, options)
return org.users
const org = await BaseOrgModel.findOne({ short_name: shortName }, 'users', options).lean()
return Array.isArray(org?.users) ? org.users : []
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/repositories/conversationRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class ConversationRepository extends BaseRepository {
posted_at: 1,
UUID: 1
}
})
return conversations.map(convo => convo.toObject()).filter(conv => isSecretariat || conv.visibility === 'public').map(conv => {
}).lean()
return conversations.filter(conv => isSecretariat || conv.visibility === 'public').map(conv => {
normalizeConversationAuthorName(conv)
if (!isSecretariat && conv.author_role === 'Secretariat') {
delete conv.author_id
Expand All @@ -77,7 +77,7 @@ class ConversationRepository extends BaseRepository {
posted_at: 1,
UUID: 1
}
}).skip(index).limit(1)
}).skip(index).limit(1).lean()
return conversation[0]
}

Expand Down
2 changes: 1 addition & 1 deletion src/repositories/cveIdRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CveIdRepository extends BaseRepository {
}

async findOneByCveId (id) {
return this.collection.findOne().byCveId(id)
return this.collection.findOne().byCveId(id).lean()
}

async updateByCveId (id, cveIdObj, options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/repositories/cveRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CveRepository extends BaseRepository {
}

async findOneByCveId (id) {
const results = this.collection.findOne().byCveId(id)
const results = this.collection.findOne().byCveId(id).lean()
return results
}

Expand Down
4 changes: 2 additions & 2 deletions src/repositories/glossaryRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class GlossaryRepository extends BaseRepository {
}

async getAll () {
return this.collection.find({}, { _id: 0, __v: 0, createdAt: 0, updatedAt: 0 }).exec()
return this.collection.find({}, { _id: 0, __v: 0, createdAt: 0, updatedAt: 0 }).lean().exec()
}

async findOneByServicesShortName (servicesShortName) {
return this.collection.findOne({ services_short_name: servicesShortName }, { _id: 0, __v: 0, createdAt: 0, updatedAt: 0 }).exec()
return this.collection.findOne({ services_short_name: servicesShortName }, { _id: 0, __v: 0, createdAt: 0, updatedAt: 0 }).lean().exec()
}

async updateByServicesShortName (servicesShortName, newGlossaryData) {
Expand Down
10 changes: 8 additions & 2 deletions src/repositories/orgRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class OrgRepository extends BaseRepository {

async findOneByShortName (shortName, options = {}, projection = {}) {
const query = { short_name: shortName }
return this.collection.findOne(query, projection, options)
const result = this.collection.findOne(query, projection, options)
return options.lean ? result.lean() : result
}

async findOneByUUID (UUID, options = {}, projection = {}) {
return this.collection.findOne({ UUID: UUID }, projection, options)
const result = this.collection.findOne({ UUID: UUID }, projection, options)
return options.lean ? result.lean() : result
}

async getOrgUUID (shortName, options = {}) {
Expand Down Expand Up @@ -51,6 +53,10 @@ class OrgRepository extends BaseRepository {
return this.collection.find({}, projection, options)
}

async getCveIdMapOrgs (options = {}) {
return this.collection.find({}, { _id: 0, UUID: 1, short_name: 1 }, options).lean()
}

async deleteOneByShortName (shortName, options = {}) {
return this.collection.deleteOne({ short_name: shortName }, options)
}
Expand Down
Loading
Loading