Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/database/queries/mentorExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ module.exports = class MentorExtensionQueries {
additionalFilter = `${searchFilter.whereClause}`
}

const filterClause = filter?.query.length > 0 ? `${filter.query}` : ''
let filterClause = filter?.query.length > 0 ? `${filter.query}` : ''

let saasFilterClause = saasFilter !== '' ? saasFilter : ''
const defaultFilterClause = defaultFilter != '' ? 'AND ' + defaultFilter : ''
Expand Down
3 changes: 2 additions & 1 deletion src/database/queries/userExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,9 @@ module.exports = class MenteeExtensionQueries {
replacements.limit = limit
} else {
// Provide defaults if page/limit not specified
// 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
}
Comment on lines +574 to 577
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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.


let results = await Sequelize.query(query, {
Expand Down
55 changes: 24 additions & 31 deletions src/services/mentees.js
Original file line number Diff line number Diff line change
Expand Up @@ -1525,17 +1525,34 @@ module.exports = class MenteesHelper {
const query = utils.processQueryParametersWithExclusions(queryParams)
const userExtensionModelName = await menteeQueries.getModelName()

const defaults = await getDefaults()
if (!defaults.tenantCode)
return responses.failureResponse({
message: 'DEFAULT_ORG_CODE_NOT_SET',
statusCode: httpStatusCode.bad_request,
responseCode: 'CLIENT_ERROR',
})
Comment on lines +1529 to +1534
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

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.

Suggested change
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.


let validationData = await entityTypeCache.getEntityTypesAndEntitiesWithCache(
{
status: common.ACTIVE_STATUS,
model_names: { [Op.overlap]: [userExtensionModelName] },
},
tenantCode,
organizationCode,
userExtensionModelName
)

let filteredQuery = utils.validateAndBuildFilters(query, validationData)

let connectedMenteeIds = []
let connectedMenteesCount
if (queryParams.connected_mentees === 'true') {
const connectedQueryParams = { ...queryParams }
delete connectedQueryParams.connected_mentees
const connectedQuery = utils.processQueryParametersWithExclusions(connectedQueryParams)

const selectAll = queryParams.select_all === 'true'
const connectionDetails = await connectionQueries.getConnectionsDetails(
pageNo,
pageSize,
connectedQuery,
selectAll ? null : pageNo,
selectAll ? null : pageSize,
filteredQuery,
searchText,
queryParams.mentorId ? queryParams.mentorId : userId,
organization_codes,
Expand All @@ -1547,9 +1564,6 @@ module.exports = class MenteesHelper {
pageNo = null
pageSize = null
connectedMenteeIds = connectionDetails.data.map((item) => item.user_id)
// if (!connectedMenteeIds.includes(userId)) {
// connectedMenteeIds.push(userId)
// }
}
if (typeof connectionDetails?.count === 'number') {
connectedMenteesCount = connectionDetails.count
Expand All @@ -1567,27 +1581,6 @@ module.exports = class MenteesHelper {
})
}
}

const defaults = await getDefaults()
if (!defaults.tenantCode)
return responses.failureResponse({
message: 'DEFAULT_ORG_CODE_NOT_SET',
statusCode: httpStatusCode.bad_request,
responseCode: 'CLIENT_ERROR',
})

let validationData = await entityTypeCache.getEntityTypesAndEntitiesWithCache(
{
status: common.ACTIVE_STATUS,
model_names: { [Op.overlap]: [userExtensionModelName] },
},
tenantCode,
organizationCode,
userExtensionModelName
)

let filteredQuery = utils.validateAndBuildFilters(query, validationData)

const emailIds = []
const searchTextArray = searchText ? searchText.split(',') : []

Expand Down
24 changes: 16 additions & 8 deletions src/services/mentors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1389,34 +1389,38 @@ module.exports = class MentorsHelper {
}

let connectedMentorsIds = []
let connectedMentorsCount

if (queryParams.connected_mentors === 'true') {
const connectedQueryParams = { ...queryParams }
delete connectedQueryParams.connected_mentors
const connectedQuery = utils.processQueryParametersWithExclusions(connectedQueryParams)

const connectionDetails = await connectionQueries.getConnectionsDetails(
pageNo,
pageSize,
connectedQuery,
filteredQuery,
searchText,
queryParams.mentorId ? queryParams.mentorId : userId,
organization_codes,
[] // roles can be passed if needed
[], // roles can be passed if needed
tenantCode
)

if (connectionDetails?.data?.length > 0) {
pageNo = null
pageSize = null
connectedMentorsIds = connectionDetails.data.map((item) => item.user_id)
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
}
Comment on lines 1410 to +1417
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

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.


// If there are no connected mentors, short-circuit and return empty
if (connectedMentorsIds.length === 0) {
return responses.successResponse({
statusCode: httpStatusCode.ok,
message: 'MENTEE_LIST',
message: 'MENTOR_LIST',
result: {
data: [],
count: 0,
Expand Down Expand Up @@ -1570,6 +1574,10 @@ module.exports = class MentorsHelper {
}
}

if (connectedMentorsCount !== undefined) {
extensionDetails.count = connectedMentorsCount
}

return responses.successResponse({
statusCode: httpStatusCode.ok,
message: 'MENTOR_LIST',
Expand Down