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
7 changes: 6 additions & 1 deletion src/controllers/v1/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ module.exports = class Entities extends Abstract {
return new Promise(async (resolve, reject) => {
try {
// Calls the 'find' function from 'entitiesHelper' to retrieve entity data
let entityData = await entitiesHelper.find(req.body.query, req.body.projection)
let entityData = await entitiesHelper.find(
req.body.query,
req.body.projection,
req.pageNo,
req.pageSize
)
return resolve(entityData)
} catch (error) {
return reject({
Expand Down
71 changes: 66 additions & 5 deletions src/module/entities/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1099,14 +1099,40 @@ module.exports = class UserProjectsHelper {
* @name find
* @param {Object} bodyQuery - body data
* @param {Object} projection - projection to filter data
* @param {Number} pageNo - page number
* @param {Number} pageSize - page limit
*/

static find(bodyQuery, projection) {
static find(bodyQuery, projection, pageNo, pageSize) {
return new Promise(async (resolve, reject) => {
try {
// Fetch entities based on the provided query and projection
const result = await entitiesQueries.entityDocuments(bodyQuery, projection)
if (result.length < 1) {
// Create facet object to attain pagination
let facetQuery = {}
facetQuery['$facet'] = {}
facetQuery['$facet']['totalCount'] = [{ $count: 'count' }]
if (pageSize === '' && pageNo === '') {
facetQuery['$facet']['data'] = [{ $skip: 0 }]
} else {
facetQuery['$facet']['data'] = [{ $skip: pageSize * (pageNo - 1) }, { $limit: pageSize }]
}
bodyQuery = convertMongoIds(bodyQuery)

// Create projection object
let projection1 = {}
if (projection.length > 0) {
projection.forEach((projectedData) => {
projection1[projectedData] = 1
})
}
const result = await entitiesQueries.getAggregate([
{ $match: bodyQuery },
{
$sort: { updatedAt: -1 },
},
{ $project: projection1 },
facetQuery,
])
if (!(result.length > 0) || !result[0].data || !(result[0].data.length > 0)) {
throw {
status: HTTP_STATUS_CODE.not_found.status,
message: CONSTANTS.apiResponses.ENTITY_NOT_FOUND,
Expand All @@ -1115,7 +1141,7 @@ module.exports = class UserProjectsHelper {
return resolve({
success: true,
message: CONSTANTS.apiResponses.ASSETS_FETCHED_SUCCESSFULLY,
result: result,
result: result[0].data,
})
} catch (error) {
return reject(error)
Expand Down Expand Up @@ -2151,3 +2177,38 @@ function addTagsInEntities(entityMetaInformation) {
}
return entityMetaInformation
}

// Helper function to convert mongo ids to objectIds to facilitate proper query in aggregate function
function convertMongoIds(query) {
const keysToConvert = ['_id'] // Add other fields if needed

const convertValue = (value) => {
if (Array.isArray(value)) {
return value.map((v) => (isValidObjectId(v) ? new ObjectId(v) : v))
} else if (isValidObjectId(value)) {
return new ObjectId(value)
}
return value
}

const isValidObjectId = (id) => {
return typeof id === 'string' && /^[a-fA-F0-9]{24}$/.test(id)
}

const recurse = (obj) => {
for (const key in obj) {
if (keysToConvert.includes(key)) {
if (typeof obj[key] === 'object' && obj[key] !== null && '$in' in obj[key]) {
obj[key]['$in'] = convertValue(obj[key]['$in'])
} else {
obj[key] = convertValue(obj[key])
}
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
recurse(obj[key])
}
}
}

recurse(query)
return query
}