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
4 changes: 2 additions & 2 deletions src/generics/cacheHelper.js
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

getMentorKafka (src/generics/cacheHelper.js:1036) still has the same .get() parameter mismatch.

It calls this.get(tenantCode, organizationCode, userIds), but mentor.get (line 884) only accepts (tenantCode, mentorId). The fallback at line 1105 has the same issue. The PR fixed this for session and mentee, but not here. No impact for now since it’s not used.

Also, in getMenteeKafka (src/generics/cacheHelper.js:1332–1333), userIsMentorMap is referenced but never defined. This throws a ReferenceError (caught at line 1337), causing errors to be logged every time DB users are found, skipping the success log at 1335, and also shadowing userMentorEntries from line 1311.

Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ const sessions = {

if (sessionDetails) {
// Cache the session data directly
await this.set(tenantCode, organizationCode, sessionId, sessionDetails)
await this.set(tenantCode, sessionId, sessionDetails)
dbFetchedSessions.push(sessionDetails)
console.log(`✅ [getSessionKafka] Session ${sessionId} fetched and cached`)
}
Expand Down Expand Up @@ -1322,7 +1322,7 @@ const mentee = {
if (usersFromDb && usersFromDb.length > 0) {
// Cache each fetched user individually
for (const user of usersFromDb) {
await this.set(tenantCode, organizationCode, user.user_id, user)
await this.set(tenantCode, user.user_id, user)
}

// Add fetched users to result
Expand Down
17 changes: 9 additions & 8 deletions src/services/entity-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,12 @@ module.exports = class EntityHelper {
}

// Clear cache for affected models before deletion
await this._clearUserCachesForEntityTypeChange(organizationCode, tenantCode, {
id: entityToDelete.id,
value: entityToDelete.value,
modelNames: entityToDelete.model_names,
})
await this._clearUserCachesForEntityTypeChange(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why are we calling _clearUserCachesForEntityTypeChange twice(line 343)?

organizationCode,
tenantCode,
entityToDelete.model_names ? entityToDelete.model_names[0] : null,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why are we using just the first element from the array? What happens if there are two models for the same entity type?

entityToDelete.value
)

// SECOND: Delete from database
const deleteCount = await entityTypeQueries.deleteOneEntityType(id, organizationCode, tenantCode)
Expand Down Expand Up @@ -544,13 +545,13 @@ module.exports = class EntityHelper {
})
)

// 2. Clear entity type caches for unified model strategy
// 2. Clear entity type cache for the specific model + value
if (modelName) {
clearPromises.push(
cacheHelper.entityTypes
.delete(tenantCode, organizationCode, `model:${modelName}:__ALL__`)
.delete(tenantCode, organizationCode, modelName, entityValue)
Comment thread
sumanvpacewisdom marked this conversation as resolved.
.catch((error) => {
/* Failed to clear unified entity type cache - continue operation */
/* Failed to clear entity type cache - continue operation */
})
)
}
Expand Down
82 changes: 7 additions & 75 deletions src/services/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,6 @@ module.exports = class EntityHelper {
}
}

// Invalidate entity list caches after creation
if (entity && sanitizedData.entity_type_id) {
// Separate try-catch for each cache deletion to ensure all caches are cleared
try {
await cacheHelper.forms.delete(
tenantCode,
common.SYSTEM,
'entity_list',
sanitizedData.entity_type_id
)
} catch (cacheError) {
console.error(`❌ Failed to invalidate entity_list cache after creation:`, cacheError)
}

try {
await cacheHelper.forms.delete(tenantCode, common.SYSTEM, 'entity_list_all', 'all_entities')
} catch (cacheError) {
console.error(`❌ Failed to invalidate entity_list_all cache after creation:`, cacheError)
}
}

return responses.successResponse({
statusCode: httpStatusCode.created,
message: 'ENTITY_CREATED_SUCCESSFULLY',
Expand Down Expand Up @@ -163,23 +142,6 @@ module.exports = class EntityHelper {
}
}

// Invalidate entity list caches after update
if (updatedEntity && (updatedEntity.entity_type_id || sanitizedData.entity_type_id)) {
const entityTypeId = updatedEntity.entity_type_id || sanitizedData.entity_type_id
// Separate try-catch for each cache deletion to ensure all caches are cleared
try {
await cacheHelper.forms.delete(tenantCode, common.SYSTEM, 'entity_list', entityTypeId)
} catch (cacheError) {
console.error(`❌ Failed to invalidate entity_list cache after update:`, cacheError)
}

try {
await cacheHelper.forms.delete(tenantCode, common.SYSTEM, 'entity_list_all', 'all_entities')
} catch (cacheError) {
console.error(`❌ Failed to invalidate entity_list_all cache after update:`, cacheError)
}
}

return responses.successResponse({
statusCode: httpStatusCode.accepted,
message: 'ENTITY_UPDATED_SUCCESSFULLY',
Expand Down Expand Up @@ -387,14 +349,6 @@ module.exports = class EntityHelper {
}
}

// Invalidate entity list caches after deletion
try {
// Clear all entity list caches since we don't know the entity_type_id after deletion
await cacheHelper.forms.delete(tenantCode, common.SYSTEM, 'entity_list_all', 'all_entities')
} catch (cacheError) {
console.error(`❌ Failed to invalidate entity list cache after deletion:`, cacheError)
}

return responses.successResponse({
statusCode: httpStatusCode.accepted,
message: 'ENTITY_DELETED_SUCCESSFULLY',
Expand Down Expand Up @@ -441,35 +395,13 @@ module.exports = class EntityHelper {
filter['entity_type_id'] = entityType
}

// Try to get entities from cache first (only cache paginated lists without search)
const cacheKey = `${entityType || 'all'}_page${pageNo}_limit${pageSize}`
let entities = null

if (!searchText) {
entities = await cacheHelper.forms.get(tenantCode, common.SYSTEM, 'entity_list', cacheKey)
if (entities) {
}
}

if (!entities) {
// Optimized: Get entities with entity_type details included - eliminates N+1 queries for clients
entities = await entityQueries.getAllEntitiesWithEntityTypeDetails(
filter,
[tenantCode],
pageNo,
pageSize,
searchText
)

// Cache the result if no search text (searchable results shouldn't be cached)
if (!searchText && entities) {
try {
await cacheHelper.forms.set(tenantCode, common.SYSTEM, 'entity_list', cacheKey, entities)
} catch (cacheError) {
console.error(`❌ Failed to cache entity list:`, cacheError)
}
}
}
const entities = await entityQueries.getAllEntitiesWithEntityTypeDetails(
filter,
[tenantCode],
pageNo,
pageSize,
searchText
)

if (entities.rows == 0 || entities.count == 0) {
return responses.failureResponse({
Expand Down
19 changes: 11 additions & 8 deletions src/services/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ module.exports = class NotificationTemplateHelper {
bodyData['organization_code'] = tokenInformation.organization_code
bodyData['updated_by'] = tokenInformation.id

// Fetch original template BEFORE update to capture old code for cache invalidation
const existingTemplates = await notificationTemplateQueries.findTemplatesByFilter(filter)
const existingTemplate = existingTemplates?.[0]
const oldCode = existingTemplate?.code || filter.code

const result = await notificationTemplateQueries.updateTemplate(filter, bodyData, tenantCode)
if (result == 0) {
return responses.failureResponse({
Expand All @@ -85,23 +90,21 @@ module.exports = class NotificationTemplateHelper {
})
}

// Delete old cache
const existingTemplates = await notificationTemplateQueries.findTemplatesByFilter(filter)
const existingTemplate = existingTemplates?.[0]
const templateCode = bodyData.code || existingTemplate?.code || filter.code
// Delete cache using old code (captured before update) and new code (if changed)
const newCode = bodyData.code || oldCode
try {
if (templateCode) {
if (oldCode) {
await cacheHelper.notificationTemplates.delete(
tenantCode,
tokenInformation.organization_code,
templateCode
oldCode
)
}
if (existingTemplate?.code && existingTemplate.code !== templateCode) {
if (newCode && newCode !== oldCode) {
await cacheHelper.notificationTemplates.delete(
tenantCode,
tokenInformation.organization_code,
existingTemplate.code
newCode
)
}
} catch (cacheError) {
Expand Down