Skip to content

Cache issues related to entity_type , entity and notifications #1607

Open
sumanvpacewisdom wants to merge 2 commits into
tenant_phase2_tempfrom
tenant_phase_2
Open

Cache issues related to entity_type , entity and notifications #1607
sumanvpacewisdom wants to merge 2 commits into
tenant_phase2_tempfrom
tenant_phase_2

Conversation

@sumanvpacewisdom
Copy link
Copy Markdown
Collaborator

@sumanvpacewisdom sumanvpacewisdom commented Mar 23, 2026

Release Notes

  • Cache alignment in cacheHelper: Updated session and mentee caching methods to use correct parameter signatures (tenantCode, id, data instead of including organizationCode as the second argument)
  • Entity type cache invalidation: Refined cache deletion logic to target specific model + value entries instead of broad namespace keys
  • Simplified entity caching: Removed read-through caching logic and associated cache invalidation during entity CRUD operations
  • Notification template cache optimization: Refactored cache invalidation to capture pre-update template code values, eliminating redundant post-update queries and improving cache invalidation accuracy

Lines Changed by Author

Author Added Removed Net
sumanvpacewisdom 29 93 -64

sumanvpacewisdom and others added 2 commits March 23, 2026 15:35
…ficiency. Updated cache set and delete methods in cacheHelper for session and user data. Simplified cache invalidation logic in EntityHelper and NotificationTemplateHelper by capturing old values before updates. Removed redundant cache invalidation after entity operations to streamline processing.
@sumanvpacewisdom sumanvpacewisdom changed the title Tenant phase 2 Cache issues related to entity_type , entity and notifications Mar 23, 2026
@sumanvpacewisdom
Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 23, 2026

✅ Actions performed

Full review triggered.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 23, 2026

Walkthrough

This pull request refactors caching logic across multiple services. Changes include aligning cache API call signatures to remove organizationCode from certain paths, modifying cache invalidation workflows for entity types and notifications, and removing read-through caching for entity list operations.

Changes

Cohort / File(s) Summary
Cache API Signature Alignment
src/generics/cacheHelper.js
Updated sessions.getSessionKafka and mentee.getMenteeKafka to use 3-argument set(...) calls (tenantCode, id, data) instead of 4-argument calls with organizationCode, aligning with method signatures defined in the same module.
Cache Invalidation Refactoring
src/services/entity-type.js, src/services/notification.js
Modified entity-type cache deletion to call cacheHelper.entityTypes.delete() with specific model/value arguments instead of unified namespace keys. Simplified notification template cache invalidation by querying templates once before update to capture old code, then conditionally deleting old/new cache entries.
Entity List Caching Removal
src/services/entity.js
Eliminated read-through caching logic in list() method and removed entity list cache invalidation from create(), update(), and delete() operations. Entity list queries now always hit the database without caching layers.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • nevil-mathew

Poem

🐰 Whiskers twitch with cache delight,
Keys reshape in morning light,
Signatures align so true,
Rabbits hop through caches new!
Faster lists without the cache,
Hopping through with cleaner stash. 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly addresses cache-related issues in entity_type, entity, and notification handling, which aligns with the primary focus of the changes across four modified files (cacheHelper.js, entity-type.js, entity.js, notification.js).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tenant_phase_2

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (3)
src/generics/cacheHelper.js (3)

1331-1333: ⚠️ Potential issue | 🔴 Critical

Bug: Reference to undefined variable userIsMentorMap.

Line 1333 references userIsMentorMap which is never declared in the getMenteeKafka method. This will throw a ReferenceError at runtime when users are fetched from the database.

🐛 Proposed fix - remove dead code

These lines appear to be leftover from a previous implementation. Since userMentorEntries and userIsMentorMap are not used anywhere in the getMenteeKafka method's return flow, they should be removed:

 if (usersFromDb && usersFromDb.length > 0) {
     // Cache each fetched user individually
     for (const user of usersFromDb) {
         await this.set(tenantCode, user.user_id, user)
     }

     // Add fetched users to result
     dbFetchedUsers.push(...usersFromDb)

-    // Create user ID to is_mentor mapping using map
-    const userMentorEntries = usersFromDb.map((user) => [user.user_id, user.is_mentor || false])
-    Object.assign(userIsMentorMap, Object.fromEntries(userMentorEntries))
-
     console.log(`✅ [getMenteeKafka] ${usersFromDb.length} users fetched from DB and cached`)
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/generics/cacheHelper.js` around lines 1331 - 1333, The block in
getMenteeKafka that builds userMentorEntries and calls Object.assign on the
undeclared userIsMentorMap should be removed; locate the lines creating
userMentorEntries and the Object.assign(userIsMentorMap, ...) inside the
getMenteeKafka function and delete them (they are unused and cause a
ReferenceError for userIsMentorMap). Ensure no other code in getMenteeKafka
depends on userMentorEntries or userIsMentorMap after removal.

1064-1082: ⚠️ Potential issue | 🔴 Critical

Bug: Reassignment to const variable causes runtime error.

Line 1064 declares const userMentorEntries = [], but line 1074 attempts to reassign it with userMentorEntries = usersFromDb.map(...). This will throw a TypeError: Assignment to constant variable at runtime.

🐛 Proposed fix
 const dbFetchedUsers = []
-const userMentorEntries = []
+let userMentorEntries = []
 if (missingUserIds.length > 0) {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/generics/cacheHelper.js` around lines 1064 - 1082, The code declares
userMentorEntries as a const and later reassigns it with usersFromDb.map,
causing a runtime TypeError; change the declaration of userMentorEntries to a
mutable binding (let) or avoid reassignment by populating the existing array
(e.g., push/concat) so that the assignment at userMentorEntries =
usersFromDb.map(...) is removed; update the declaration near userMentorEntries
and ensure the rest of the logic that uses userMentorEntries (the mapping
derived from usersFromDb) continues to work with the chosen approach.

1154-1158: ⚠️ Potential issue | 🔴 Critical

Fix incorrect cache invalidation call with wrong parameters.

The cacheHelper.mentee.delete() method signature accepts 2 parameters (tenantCode, menteeId), but the call at src/services/admin.js:697 passes 3 arguments:

await cacheHelper.mentee.delete(tenantCode, menteeData.organization_code, menteeData.mentee_id)

Since organization_code is not a field in the SessionAttendee model (only mentee_id exists), this passes undefined as the menteeId parameter while the actual mentee_id is ignored. This results in an invalid cache key being constructed, and the mentee's cache entry is never properly invalidated.

Fix: Change the call to pass only the correct parameters:

await cacheHelper.mentee.delete(tenantCode, menteeData.mentee_id)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/generics/cacheHelper.js` around lines 1154 - 1158, The call to
cacheHelper.mentee.delete is passing three args causing menteeId to be
undefined; update the caller in admin (where it calls
cacheHelper.mentee.delete(tenantCode, menteeData.organization_code,
menteeData.mentee_id)) to pass only the correct two parameters — tenantCode and
menteeData.mentee_id — so cacheHelper.mentee.delete(tenantCode,
menteeData.mentee_id) constructs the right key (buildKey /
nsUseInternal('mentee')) and properly invalidates the mentee cache.
🧹 Nitpick comments (1)
src/services/entity-type.js (1)

343-349: Note: Duplicate cache clearing call.

_clearUserCachesForEntityTypeChange is called twice in the delete flow - once at line 292-297 (before deletion) and again here at lines 344-349 (after deletion). While redundant, this is defensive and not harmful. Consider consolidating to a single call after deletion for clarity.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/services/entity-type.js` around lines 343 - 349, Duplicate call to
_clearUserCachesForEntityTypeChange is made in the delete flow; consolidate to a
single call after the entity is deleted by removing the redundant pre-deletion
invocation and keeping the post-deletion call that passes (organizationCode,
tenantCode, entityToDelete.model_names ? entityToDelete.model_names[0] : null,
entityToDelete.value); ensure the single remaining call is executed after the
deletion completes so caches reflect the final state.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/generics/cacheHelper.js`:
- Around line 1331-1333: The block in getMenteeKafka that builds
userMentorEntries and calls Object.assign on the undeclared userIsMentorMap
should be removed; locate the lines creating userMentorEntries and the
Object.assign(userIsMentorMap, ...) inside the getMenteeKafka function and
delete them (they are unused and cause a ReferenceError for userIsMentorMap).
Ensure no other code in getMenteeKafka depends on userMentorEntries or
userIsMentorMap after removal.
- Around line 1064-1082: The code declares userMentorEntries as a const and
later reassigns it with usersFromDb.map, causing a runtime TypeError; change the
declaration of userMentorEntries to a mutable binding (let) or avoid
reassignment by populating the existing array (e.g., push/concat) so that the
assignment at userMentorEntries = usersFromDb.map(...) is removed; update the
declaration near userMentorEntries and ensure the rest of the logic that uses
userMentorEntries (the mapping derived from usersFromDb) continues to work with
the chosen approach.
- Around line 1154-1158: The call to cacheHelper.mentee.delete is passing three
args causing menteeId to be undefined; update the caller in admin (where it
calls cacheHelper.mentee.delete(tenantCode, menteeData.organization_code,
menteeData.mentee_id)) to pass only the correct two parameters — tenantCode and
menteeData.mentee_id — so cacheHelper.mentee.delete(tenantCode,
menteeData.mentee_id) constructs the right key (buildKey /
nsUseInternal('mentee')) and properly invalidates the mentee cache.

---

Nitpick comments:
In `@src/services/entity-type.js`:
- Around line 343-349: Duplicate call to _clearUserCachesForEntityTypeChange is
made in the delete flow; consolidate to a single call after the entity is
deleted by removing the redundant pre-deletion invocation and keeping the
post-deletion call that passes (organizationCode, tenantCode,
entityToDelete.model_names ? entityToDelete.model_names[0] : null,
entityToDelete.value); ensure the single remaining call is executed after the
deletion completes so caches reflect the final state.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e9e54ed1-ac6e-4dfe-8046-e3f8130b39bd

📥 Commits

Reviewing files that changed from the base of the PR and between 72659c4 and 2664f77.

📒 Files selected for processing (4)
  • src/generics/cacheHelper.js
  • src/services/entity-type.js
  • src/services/entity.js
  • src/services/notification.js

await this._clearUserCachesForEntityTypeChange(
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?

Comment thread src/services/entity-type.js
Copy link
Copy Markdown
Collaborator

@nevil-mathew nevil-mathew left a comment

Choose a reason for hiding this comment

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

While you’ve fixed the issue in cacheHelper.js line 487, it still persists in 442 and 511. Shouldn’t we fix this as well? Are there any other occurrences that haven’t been updated?

@nevil-mathew
Copy link
Copy Markdown
Collaborator

Similar issue in line 1283, 1352

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)?


// Create user ID to is_mentor mapping using map
const userMentorEntries = usersFromDb.map((user) => [user.user_id, user.is_mentor || false])
Object.assign(userIsMentorMap, Object.fromEntries(userMentorEntries))
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.

Is userIsMentorMap declared in this fn?

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.

@sumanvpacewisdom sumanvpacewisdom changed the base branch from tenant_phase2_temp to develop March 27, 2026 13:47
@sumanvpacewisdom sumanvpacewisdom changed the base branch from develop to tenant_phase2_temp March 30, 2026 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants