fix: omit unchanged file field from document module#871
fix: omit unchanged file field from document module#871priscila-moneo wants to merge 1 commit intomasterfrom
Conversation
📝 WalkthroughWalkthroughExported Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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.
🧹 Nitpick comments (1)
src/actions/page-template-actions.js (1)
175-175: Use explicit nullish checks for persisted file identifiersOn Line 175,
!f.id && !f.file_idtreats falsy values as “missing”. If an identifier can be0/"", unchanged files may be incorrectly sent as new file payloads. Prefer existence checks (== null) instead of truthiness.Proposed adjustment
- if (f && typeof f === "object" && !f.id && !f.file_id) { + const hasPersistedId = f?.id != null || f?.file_id != null; + if (f && typeof f === "object" && !hasPersistedId) { normalizedModule.file = f; } else { delete normalizedModule.file; }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/actions/page-template-actions.js` at line 175, The condition treating missing file IDs uses truthiness and will misclassify valid falsy IDs; update the check on f.id and f.file_id to use explicit nullish checks (== null) instead of negation, e.g. change the clause in the block that uses variable f (the condition beginning with if (f && typeof f === "object" && ...)) so it tests f.id == null && f.file_id == null (or equivalent nullish checks) so only null/undefined are treated as missing while preserving the object/type guard.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/actions/page-template-actions.js`:
- Line 175: The condition treating missing file IDs uses truthiness and will
misclassify valid falsy IDs; update the check on f.id and f.file_id to use
explicit nullish checks (== null) instead of negation, e.g. change the clause in
the block that uses variable f (the condition beginning with if (f && typeof f
=== "object" && ...)) so it tests f.id == null && f.file_id == null (or
equivalent nullish checks) so only null/undefined are treated as missing while
preserving the object/type guard.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: c0d60cb6-b6ad-44ae-85c6-3f8ec5ba3775
📒 Files selected for processing (1)
src/actions/page-template-actions.js
| if (module.kind === PAGES_MODULE_KINDS.DOCUMENT) { | ||
| if (module.type === PAGE_MODULES_DOWNLOAD.FILE) { | ||
| normalizedModule.file = module.file?.[0] || null; | ||
| const f = Array.isArray(module.file) ? module.file[0] : null; |
There was a problem hiding this comment.
please use a better variable name
| if (module.type === PAGE_MODULES_DOWNLOAD.FILE) { | ||
| normalizedModule.file = module.file?.[0] || null; | ||
| const f = Array.isArray(module.file) ? module.file[0] : null; | ||
| if (f && typeof f === "object" && !f.id && !f.file_id) { |
There was a problem hiding this comment.
please comment why if file has no id and no file_id then we delete it from payload
There was a problem hiding this comment.
We only include the file in the payload if it doesn't have an id or file_id, which means it's a newly added file that hasn't been persisted yet.
If the file already has an id/file_id, we remove it from the payload. These represent existing files, and since they are already stored, sending them again would be redundant.
There was a problem hiding this comment.
I mean put a comment in the code so next dev knows whats going on. Also please check updating a file works with this logic.
There was a problem hiding this comment.
Already checked that all still works before pushing it, but now I've added new unit tests for this scenarios. Also I've added the comment. Thanks for your comments!
e67f267 to
bb59d39
Compare
bb59d39 to
1a4f6eb
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/actions/__tests__/page-template-actions.test.js (1)
14-68: LGTM!Tests correctly exercise the four key branches of
normalizeEntity's file normalization logic and align with the implementation insrc/actions/page-template-actions.js.Optional: consider adding a case for
file_id(existing file referenced byfile_idinstead ofid) to fully cover theisNewFilepredicate, since the implementation explicitly checks both.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/actions/__tests__/page-template-actions.test.js` around lines 14 - 68, Add a test that covers the branch where an existing file is referenced by file_id (not id) so the isNewFile predicate in normalizeEntity is fully exercised; create an entity using buildModule with PAGES_MODULE_KINDS.DOCUMENT and PAGE_MODULES_DOWNLOAD.FILE containing [{ file_id: 456, name: "existing_by_file_id.pdf" }] and assert that normalizeEntity(entity).modules[0].file is undefined (matching the existing-file behavior).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/actions/__tests__/page-template-actions.test.js`:
- Around line 14-68: Add a test that covers the branch where an existing file is
referenced by file_id (not id) so the isNewFile predicate in normalizeEntity is
fully exercised; create an entity using buildModule with
PAGES_MODULE_KINDS.DOCUMENT and PAGE_MODULES_DOWNLOAD.FILE containing [{
file_id: 456, name: "existing_by_file_id.pdf" }] and assert that
normalizeEntity(entity).modules[0].file is undefined (matching the existing-file
behavior).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 7c7bd75b-5562-43d5-beb5-ea31def82118
📒 Files selected for processing (2)
src/actions/__tests__/page-template-actions.test.jssrc/actions/page-template-actions.js
🚧 Files skipped from review as they are similar to previous changes (1)
- src/actions/page-template-actions.js
| }; | ||
|
|
||
| const normalizeEntity = (entity) => { | ||
| export const normalizeEntity = (entity) => { |
There was a problem hiding this comment.
do we need to export this method ? who is using it ?
There was a problem hiding this comment.
it's only for testing purposes
ref: https://app.clickup.com/t/86b92ac9w
Summary by CodeRabbit
Bug Fixes
Tests