Skip to content

Refactor file upload queries and tenant scoping across codebase#783

Merged
aks30 merged 2 commits intodevelopfrom
admin-create-api
Aug 6, 2025
Merged

Refactor file upload queries and tenant scoping across codebase#783
aks30 merged 2 commits intodevelopfrom
admin-create-api

Conversation

@nevil-mathew
Copy link
Collaborator

@nevil-mathew nevil-mathew commented Aug 6, 2025

Summary by CodeRabbit

  • New Features
    • Improved file upload listing by supporting advanced filtering, including tenant-specific filters.
  • Bug Fixes
    • Corrected validation for the upload_type field to ensure only allowed values are accepted.
  • Refactor
    • Updated file upload listing and related methods to use a unified filter object for more flexible query handling.
  • Chores
    • Enhanced error handling in file upload queries for more consistent error reporting.

@coderabbitai
Copy link

coderabbitai bot commented Aug 6, 2025

Walkthrough

This update modifies the handling of file upload queries and tenant scoping across the codebase. Changes include refactoring the listUploads query interface, adjusting session lookups to require tenant codes, updating validation logic for bulk user creation, and altering the model definition for file uploads by removing a default value from the tenant_code field.

Changes

Cohort / File(s) Change Summary
FileUpload Model Update
src/database/models/fileUpload.js
Removed the default value for the tenant_code field in the FileUpload model; the field remains non-nullable but no longer defaults to an empty string.
FileUpload Query Refactor
src/database/queries/fileUpload.js
Changed import to use an alias path. Updated error handling to throw errors instead of returning them. Refactored the listUploads function to accept a destructured object parameter with a generic filter, simplified filtering logic, and adjusted the return to provide raw rows. Function signature and parameter structure were changed.
Account Session Lookup
src/services/account.js
Modified the generateToken method to require both session ID and tenant code when querying for a user session, ensuring session lookups are tenant-scoped.
Org Admin Bulk Invites Refactor
src/services/org-admin.js
Refactored the call to fileUploadQueries.listUploads in getBulkInvitesFilesList to use the new object parameter structure, including tenant_code in the filter. Adjusted filtering logic accordingly.
Bulk User Create Validation Fix
src/validators/v1/org-admin.js
Corrected the .isIn() validator for the upload_type field in bulkUserCreate to properly restrict values to the allowed set by passing the upload_type array as an argument.

Sequence Diagram(s)

sequenceDiagram
    participant OrgAdminHelper
    participant fileUploadQueries
    participant FileUploadModel

    OrgAdminHelper->>fileUploadQueries: listUploads({ page, limit, filter })
    fileUploadQueries->>FileUploadModel: findAll({ where: filter, ... })
    FileUploadModel-->>fileUploadQueries: Raw rows
    fileUploadQueries-->>OrgAdminHelper: Raw upload rows
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A rabbit hopped through code today,
Refactored queries, smoothed the way.
Tenant codes now tightly bound,
Validation checks are safe and sound.
No defaults where none belong—
This patch will help our code stay strong!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch admin-create-api

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai bot changed the title @coderabbitai Refactor file upload queries and tenant scoping across codebase Aug 6, 2025
Copy link

@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.

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8f4df7e and 785daa3.

📒 Files selected for processing (5)
  • src/database/models/fileUpload.js (0 hunks)
  • src/database/queries/fileUpload.js (3 hunks)
  • src/services/account.js (1 hunks)
  • src/services/org-admin.js (1 hunks)
  • src/validators/v1/org-admin.js (1 hunks)
💤 Files with no reviewable changes (1)
  • src/database/models/fileUpload.js
🧰 Additional context used
📓 Path-based instructions (3)
src/validators/**

⚙️ CodeRabbit Configuration File

Validate all incoming data thoroughly. Check for missing or incomplete validation rules.

Files:

  • src/validators/v1/org-admin.js
src/services/**

⚙️ CodeRabbit Configuration File

This is core business logic. Please check for correctness, efficiency, and potential edge cases.

Files:

  • src/services/account.js
  • src/services/org-admin.js
src/database/queries/**

⚙️ CodeRabbit Configuration File

Review database queries for performance. Check for N+1 problems and ensure indexes can be used.

Files:

  • src/database/queries/fileUpload.js
🧠 Learnings (3)
📓 Common learnings
Learnt from: nevil-mathew
PR: ELEVATE-Project/user#776
File: src/database/models/entityType.js:38-38
Timestamp: 2025-07-31T08:43:35.971Z
Learning: The migration for converting tenant_code to a primary key in the EntityType model was already handled in a previous PR, not in the current refactoring PR that focuses on organization codes instead of organization IDs.
📚 Learning: in the elevate-project/user codebase, organizationcode and tenantcode parameters passed to service m...
Learnt from: nevil-mathew
PR: ELEVATE-Project/user#776
File: src/services/entities.js:18-23
Timestamp: 2025-07-31T08:44:36.982Z
Learning: In the ELEVATE-Project/user codebase, organizationCode and tenantCode parameters passed to service methods always come from req.decodedToken.organization_code and req.decodedToken.tenant_code, which are guaranteed to be present after token validation. Additional validation for these parameters in service methods is unnecessary as the token validation process ensures they are always available.

Applied to files:

  • src/services/account.js
📚 Learning: the migration for converting tenant_code to a primary key in the entitytype model was already handle...
Learnt from: nevil-mathew
PR: ELEVATE-Project/user#776
File: src/database/models/entityType.js:38-38
Timestamp: 2025-07-31T08:43:35.971Z
Learning: The migration for converting tenant_code to a primary key in the EntityType model was already handled in a previous PR, not in the current refactoring PR that focuses on organization codes instead of organization IDs.

Applied to files:

  • src/services/account.js
🔇 Additional comments (5)
src/validators/v1/org-admin.js (1)

19-19: LGTM - Critical validation fix.

The .isIn() validator now correctly receives the upload_type array as an argument, which was missing before. This ensures proper validation of the upload_type field against the allowed values.

src/services/account.js (1)

910-919: LGTM! Proper tenant isolation for session lookups.

The addition of tenant_code filtering to the user session lookup ensures proper tenant-specific data isolation. This aligns well with the broader PR objective of enhancing tenant-aware querying across the codebase.

src/database/queries/fileUpload.js (2)

2-2: LGTM! Import path modernization.

Using absolute alias paths improves maintainability and consistency across the codebase.


10-10: LGTM! Improved error propagation.

Changing from return error to throw error provides better error handling by allowing errors to bubble up properly to calling code.

Also applies to: 22-22, 36-36, 57-57

src/services/org-admin.js (1)

194-202: LGTM! Proper implementation of updated listUploads interface with tenant isolation.

The refactored call correctly implements the new object-based parameter structure and properly includes tenant_code for tenant-aware filtering. The conditional status inclusion using the spread operator is clean and the overall change aligns well with the tenant isolation improvements across the codebase.

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