-
Notifications
You must be signed in to change notification settings - Fork 8
Sorting in subEntityList API #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sorting in subEntityList API #202
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe changes extend entity listing functionality to support sorting by adding sortOrder and sortKey parameters to the controller handler and propagating them through helper methods. These parameters are integrated into MongoDB aggregation pipelines to apply sorting during entity queries and traversals. Changes
Sequence DiagramsequenceDiagram
participant Client
participant Controller as Controller<br/>(entities.js)
participant Helper as Helper<br/>(helper.js)
participant MongoDB as MongoDB<br/>Aggregation
Client->>Controller: GET /entities (with sortOrder, sortKey)
Controller->>Helper: subEntityList(..., sortOrder, sortKey)
Helper->>Helper: Check if sortOrder & sortKey provided
alt Sorting Enabled
Helper->>MongoDB: Build pipeline with $sort stage
Note over MongoDB: Apply sort before<br/>facet/count operations
else No Sorting
Helper->>MongoDB: Execute pipeline without $sort
end
MongoDB-->>Helper: Return sorted/unsorted results
Helper-->>Controller: Return entity data
Controller-->>Client: Return response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai full review |
|
@adithyadinesh0412, I'll conduct a full review of all the changes in this PR focusing on the sorting implementation in the subEntityList API. ✅ Actions performedFull review triggered. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/module/entities/helper.js (1)
540-600: Critical: Missing sortOrder and sortKey parameters in immediateEntities signature.The
immediateEntitiesmethod callsthis.searchwithsortOrderandsortKeyparameters (lines 588-589), but these parameters are not defined in the method signature (line 540). This will result inundefinedbeing passed to the search method, breaking the sorting functionality for immediate entities.Apply this diff to fix the signature and propagate the parameters:
- static immediateEntities(entityId, searchText = '', pageSize = '', pageNo = '', tenantId) { + static immediateEntities(entityId, searchText = '', pageSize = '', pageNo = '', tenantId, sortOrder = '', sortKey = '') { return new Promise(async (resolve, reject) => {And update the call in
subEntities(line 511):} else { // Retrieve immediate entities entitiesDocument = await this.immediateEntities( entitiesData.entityId, entitiesData.search, entitiesData.limit, entitiesData.pageNo, language, - tenantId + tenantId, + sortOrder, + sortKey ) }
🧹 Nitpick comments (2)
src/module/entities/helper.js (2)
261-261: Remove commented debug code.The commented
console.logstatement should be removed to keep the codebase clean.Apply this diff:
obj['entityId'] = entity let entitiesDocument = await this.subEntities(obj, language, tenantId, sortOrder, sortKey) - // console.log('entitiesDocument----------->>>>> ',entitiesDocument) if (Array.isArray(entitiesDocument.data) && entitiesDocument.data.length > 0) {
703-706: Validate language parameter to prevent field path injection.The
languageparameter is used directly to construct a MongoDB field path ($translations.${language}.nameon line 706). While language codes are typically controlled values, it's a best practice to validate them against a whitelist to prevent potential field path injection.Consider adding validation:
let finalEntityDocuments = [] // check the language criteria is set to english or not const isEnglish = !language || language === CONSTANTS.common.ENGLISH_LANGUGE_CODE + + // Validate language code if provided + if (language && !isEnglish) { + const validLanguageCodes = /^[a-z]{2,3}(-[A-Z]{2})?$/ // e.g., 'en', 'hi', 'en-US' + if (!validLanguageCodes.test(language)) { + throw { + status: HTTP_STATUS_CODE.bad_request.status, + message: 'Invalid language code format' + } + } + } + // construct the name expression based on language const nameExpr = isEnglish ? '$metaInformation.name' : `$translations.${language}.name`
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/controllers/v1/entities.js(1 hunks)src/module/entities/helper.js(10 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-01T11:20:44.813Z
Learnt from: MallanagoudaB
Repo: ELEVATE-Project/entity-management PR: 172
File: src/generics/kafka/producers.js:22-37
Timestamp: 2025-08-01T11:20:44.813Z
Learning: In the ELEVATE-Project/entity-management service, the team consistently uses the async Promise executor pattern `return new Promise(async (resolve, reject) => { ... })` throughout the entire codebase including controllers, helpers, database queries, and services. This is an established coding standard that should be maintained for consistency.
Applied to files:
src/module/entities/helper.js
🔇 Additional comments (4)
src/module/entities/helper.js (4)
237-239: LGTM!The new sorting parameters are correctly added with appropriate default values and will be falsy in conditional checks when not provided.
616-626: LGTM!The
entityTraversalsignature is correctly updated with the sorting parameters, and they are properly propagated to thesearchmethod.
730-748: LGTM!The pipeline construction and aggregation logic are correct. The use of
$arrayElemAtto extract the count (line 742) and the spread operator to combine pipeline stages (lines 730-746) are appropriate.
491-508: No action needed for sorting parameters inimmediateEntities.The original concern can be dismissed.
immediateEntitiesandentityTraversalare mutually exclusive code paths serving different purposes: the former retrieves direct related entities from groups, while the latter performs hierarchical traversal.immediateEntitieshas no sorting capability by design and is never called with sorting expectations elsewhere in the codebase. The implementation is correct as-is.
VISHNUDAS-tunerlabs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reviewed on nov 14
| parentInfoRequired = false | ||
| parentInfoRequired = false, | ||
| sortOrder = '', | ||
| sortKey = '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update function signature
Summary by CodeRabbit
New Features
Refactor