Skip to content

fix: cache invalidation and org priority bugs in forms#1613

Open
borkarsaish65 wants to merge 2 commits into
ELEVATE-Project:tenant_phase2_tempfrom
borkarsaish65:fix/cache_bugs_forms
Open

fix: cache invalidation and org priority bugs in forms#1613
borkarsaish65 wants to merge 2 commits into
ELEVATE-Project:tenant_phase2_tempfrom
borkarsaish65:fix/cache_bugs_forms

Conversation

@borkarsaish65
Copy link
Copy Markdown
Contributor

@borkarsaish65 borkarsaish65 commented Mar 27, 2026

  • Add cache invalidation on create to prevent stale fallback (default org) data
  • Fix read() priority to prefer user's org over default org (was using tenant_code, should be organization_code)
  • Fix readAllFormsVersion() to cache user org form over default org form when both exist

Release Notes

  • Cache invalidation on form creation: Added cache invalidation after successful form creation to prevent stale default organization data from being served
  • Fixed org priority in form read: Updated read() method to prioritize user's organization form over default organization form by using organization_code instead of tenant_code for comparison
  • Fixed org priority in form version caching: Updated readAllFormsVersion() to cache user organization forms over default organization forms when both exist, preferring custom organization entries

Lines Changed by Author

Author Lines Added Lines Removed
borkarsaish65 15 3

borkarsaish65 and others added 2 commits March 27, 2026 08:58
- Add cache invalidation on create to prevent stale fallback (default org) data
- Fix read() priority to prefer user's org over default org (was using tenant_code, should be organization_code)
- Fix readAllFormsVersion() to cache user org form over default org form when both exist

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@borkarsaish65
Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 27, 2026

✅ Actions performed

Full review triggered.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 27, 2026

Walkthrough

The form service now invalidates cached entries after creating a form and adjusts organization selection logic in the read and readAllFormsVersion methods to prioritize organization_code over tenant_code when matching forms.

Changes

Cohort / File(s) Summary
Cache and Organization Selection Logic
src/services/form.js
Added cache invalidation (delete) on successful form creation with error logging. Changed org-selection preference from tenant_code to organization_code in read method. Modified readAllFormsVersion to prioritize custom org forms (non-default organization_code) when populating per-form caches.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A cache was cleared with a whispered spell,
Forms now remember their orgs so well,
Selection shifted from tenant's old way,
To organization's code for the day,
The rabbit hops on, bugs fixed with care!

🚥 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 'fix: cache invalidation and org priority bugs in forms' directly and accurately summarizes the main changes: adding cache invalidation on form creation and fixing organization priority logic in read operations.
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 unit tests (beta)
  • Create PR with unit tests

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 (1)
src/services/form.js (1)

252-259: ⚠️ Potential issue | 🟠 Major

Default-org entries written here are still invisible to normal read() calls.

When this branch falls back to a default-org form, forms.set() writes the cache entry under defaults.orgCode, but cacheHelper.forms.get() only looks up the caller's orgCode before hitting the database. So default-only forms still are not actually prewarmed for user-org reads. Either pass the requester org into this flow and cache under that key, or teach forms.get() to consult the default-org key before querying the database.

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

In `@src/services/form.js` around lines 252 - 259, The code currently caches a
fallback default-org form under defaults.orgCode which makes it invisible to
reads from the requesting org; update the cache key so fallback default forms
are stored under the requester org. Concretely, in the branch where you compute
formData (using completeFormData and formData.sub_type) change the
cacheHelper.forms.set call to use formData.organization_code || tenantCode
(instead of defaults.orgCode) so the cached entry is available to
cacheHelper.forms.get for the calling org; alternatively, if you prefer the
other approach, make cacheHelper.forms.get consult defaults.orgCode when the
caller key misses.
🤖 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/services/form.js`:
- Around line 252-259: The code currently caches a fallback default-org form
under defaults.orgCode which makes it invisible to reads from the requesting
org; update the cache key so fallback default forms are stored under the
requester org. Concretely, in the branch where you compute formData (using
completeFormData and formData.sub_type) change the cacheHelper.forms.set call to
use formData.organization_code || tenantCode (instead of defaults.orgCode) so
the cached entry is available to cacheHelper.forms.get for the calling org;
alternatively, if you prefer the other approach, make cacheHelper.forms.get
consult defaults.orgCode when the caller key misses.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 353d5a2c-1639-43c6-9ed7-a04c0d8e50ef

📥 Commits

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

📒 Files selected for processing (1)
  • src/services/form.js

Comment thread src/services/form.js
Comment on lines 33 to +41

// Invalidate cache so stale fallback (default org) is not served
try {
if (bodyData.type && bodyData.sub_type) {
await cacheHelper.forms.delete(tenantCode, orgCode, bodyData.type, bodyData.sub_type)
}
} catch (cacheError) {
console.error('Failed to invalidate form cache:', cacheError)
}
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 do we need to delete the cache when a new form is created? It’s new, so it wouldn’t have been cached anyway. Are we caching a list somewhere? It doesn’t look like we’re clearing any list cache either.

Comment thread src/services/form.js
@@ -240,7 +249,10 @@
)
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 caching this? We’re not even using cache for the list.

Here we’re caching individual forms, but await form.getAllFormsVersion only returns the version without the rest of the data. That means reads will fail, because the cache ends up serving incomplete, unusable form data.

Comment thread src/services/form.js
Comment on lines 112 to 118
await cacheHelper.forms.delete(
tenantCode,
originalForm.organization_code || orgCode,
originalForm.type,
originalForm.sub_type
)
}
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.

It looks like we’re only clearing the cache for the current org. What happens to other orgs when default org data is updated (same for delete, etc.)? Since default org data is shared across all orgs in the tenant, shouldn’t we be invalidating their caches as well?

@nevil-mathew
Copy link
Copy Markdown
Collaborator

OT
In cacheHelper.forms.get (cacheHelper.js), formFromDb is initialized as null, and although formQueries.findFormsByFilter(...) is called at line 748, its result is never assigned back to formFromDb. So if (formFromDb) will always evaluate to false.

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