Conversation
… improve filter logic and pagination defaults. Enhance mentees.js and mentors.js by streamlining connection details retrieval and ensuring consistent handling of connected entities. This update improves code clarity and maintains uniformity across services.
WalkthroughChanges unify query validation and paging logic across mentee/mentor service layers, moving critical setup earlier in execution flow. Database queries updated for flexible pagination defaults. Variable reassignment enabled in mentorExtension. No public API signatures altered. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/database/queries/userExtension.js`:
- Around line 574-577: When the no-pagination branch sets replacements.offset =
0 and replacements.limit = ids.length > 0 ? ids.length : 5000, the separate
count query can still return the full total and cause count/data length mismatch
for large datasets; modify the logic so the count reflects the applied limit by
either (a) passing the same replacements.limit into the count SQL and using
LEAST(total_count, :limit) in the COUNT query, or (b) after running the full
count, clamp the returned count to replacements.limit (e.g., count =
Math.min(count, replacements.limit)); update the code that builds/executes the
count query to consume this limit and reference the variables
replacements.limit, replacements.offset, ids and the select_all/no-pagination
branch to ensure both data and count use the same effective limit.
In `@src/services/mentees.js`:
- Around line 1529-1534: The error response for the defaults.tenantCode check
uses the wrong message key 'DEFAULT_ORG_CODE_NOT_SET'; update the
responses.failureResponse call inside the defaults.tenantCode guard to use the
tenant-specific message key (e.g., 'DEFAULT_TENANT_CODE_NOT_SET') so the API
returns the correct tenant-related error; ensure you only change the message
string in the block that checks defaults.tenantCode and run/update any related
tests or message lookups that expect the tenant key.
In `@src/services/mentors.js`:
- Around line 1410-1417: When you push userId into connectedMentorsIds you must
also ensure connectedMentorsCount (and ultimately extensionDetails.count) stays
consistent with the updated array: instead of blindly assigning
connectedMentorsCount = connectionDetails.count, compute the count from the
updated set (e.g., derive it from connectedMentorsIds.length or take the max of
connectionDetails.count and connectedMentorsIds.length) so count >= data.length;
update the assignment that sets connectedMentorsCount/extensionDetails.count
(references: connectedMentorsIds, connectedMentorsCount, connectionDetails,
extensionDetails.count, userId) accordingly.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
src/database/queries/mentorExtension.jssrc/database/queries/userExtension.jssrc/services/mentees.jssrc/services/mentors.js
| // No pagination: return all provided IDs (e.g. select_all=true flow) | ||
| replacements.offset = 0 | ||
| replacements.limit = 5 // Default limit | ||
| replacements.limit = ids.length > 0 ? ids.length : 5000 | ||
| } |
There was a problem hiding this comment.
No-pagination path can still return mismatched count vs data.length.
When pagination is omitted and ids is empty, Line 576 caps results at 5000, but the count query still returns the full total. This can reintroduce list/count mismatch for large datasets.
As per coding guidelines src/database/queries/**: review queries for correctness/performance.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/database/queries/userExtension.js` around lines 574 - 577, When the
no-pagination branch sets replacements.offset = 0 and replacements.limit =
ids.length > 0 ? ids.length : 5000, the separate count query can still return
the full total and cause count/data length mismatch for large datasets; modify
the logic so the count reflects the applied limit by either (a) passing the same
replacements.limit into the count SQL and using LEAST(total_count, :limit) in
the COUNT query, or (b) after running the full count, clamp the returned count
to replacements.limit (e.g., count = Math.min(count, replacements.limit));
update the code that builds/executes the count query to consume this limit and
reference the variables replacements.limit, replacements.offset, ids and the
select_all/no-pagination branch to ensure both data and count use the same
effective limit.
| if (!defaults.tenantCode) | ||
| return responses.failureResponse({ | ||
| message: 'DEFAULT_ORG_CODE_NOT_SET', | ||
| statusCode: httpStatusCode.bad_request, | ||
| responseCode: 'CLIENT_ERROR', | ||
| }) |
There was a problem hiding this comment.
Fix tenant validation error message key.
Line 1531 returns DEFAULT_ORG_CODE_NOT_SET for a missing tenant default. This should use the tenant-specific message to avoid misleading API responses.
Proposed patch
if (!defaults.tenantCode)
return responses.failureResponse({
- message: 'DEFAULT_ORG_CODE_NOT_SET',
+ message: 'DEFAULT_TENANT_CODE_NOT_SET',
statusCode: httpStatusCode.bad_request,
responseCode: 'CLIENT_ERROR',
})As per coding guidelines src/services/**: check core business logic for correctness and edge cases.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (!defaults.tenantCode) | |
| return responses.failureResponse({ | |
| message: 'DEFAULT_ORG_CODE_NOT_SET', | |
| statusCode: httpStatusCode.bad_request, | |
| responseCode: 'CLIENT_ERROR', | |
| }) | |
| if (!defaults.tenantCode) | |
| return responses.failureResponse({ | |
| message: 'DEFAULT_TENANT_CODE_NOT_SET', | |
| statusCode: httpStatusCode.bad_request, | |
| responseCode: 'CLIENT_ERROR', | |
| }) |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/services/mentees.js` around lines 1529 - 1534, The error response for the
defaults.tenantCode check uses the wrong message key 'DEFAULT_ORG_CODE_NOT_SET';
update the responses.failureResponse call inside the defaults.tenantCode guard
to use the tenant-specific message key (e.g., 'DEFAULT_TENANT_CODE_NOT_SET') so
the API returns the correct tenant-related error; ensure you only change the
message string in the block that checks defaults.tenantCode and run/update any
related tests or message lookups that expect the tenant key.
| if (!connectedMentorsIds.includes(userId)) { | ||
| connectedMentorsIds.push(userId) | ||
| } | ||
| } | ||
|
|
||
| // If there are no connected mentees, short-circuit and return empty | ||
| if (typeof connectionDetails?.count === 'number') { | ||
| connectedMentorsCount = connectionDetails.count | ||
| } |
There was a problem hiding this comment.
Connected mentor count can become inconsistent after self-inclusion.
You add userId into connectedMentorsIds (when missing), but later Line 1578 sets extensionDetails.count from connectionDetails.count without adjusting for that added record. This can produce count < data.length.
Proposed patch
if (connectionDetails?.data?.length > 0) {
pageNo = null
pageSize = null
connectedMentorsIds = connectionDetails.data.map((item) => item.user_id)
+ if (typeof connectionDetails?.count === 'number') {
+ connectedMentorsCount = connectionDetails.count
+ }
if (!connectedMentorsIds.includes(userId)) {
connectedMentorsIds.push(userId)
+ if (typeof connectedMentorsCount === 'number') {
+ connectedMentorsCount += 1
+ }
}
}
-
- if (typeof connectionDetails?.count === 'number') {
- connectedMentorsCount = connectionDetails.count
- }As per coding guidelines src/services/**: check core business logic for correctness and edge cases.
Also applies to: 1577-1579
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/services/mentors.js` around lines 1410 - 1417, When you push userId into
connectedMentorsIds you must also ensure connectedMentorsCount (and ultimately
extensionDetails.count) stays consistent with the updated array: instead of
blindly assigning connectedMentorsCount = connectionDetails.count, compute the
count from the updated set (e.g., derive it from connectedMentorsIds.length or
take the max of connectionDetails.count and connectedMentorsIds.length) so count
>= data.length; update the assignment that sets
connectedMentorsCount/extensionDetails.count (references: connectedMentorsIds,
connectedMentorsCount, connectionDetails, extensionDetails.count, userId)
accordingly.
Refactor query handling in mentorExtension.js and userExtension.js to improve filter logic and pagination defaults. Enhance mentees.js and mentors.js by streamlining connection details retrieval and ensuring consistent handling of connected entities. This update improves code clarity and maintains uniformity across services.
Release Notes
Contributor Statistics