Skip to content

release: v3.3.22 – develop → staging#844

Merged
nevil-mathew merged 36 commits intostagingfrom
develop
Oct 15, 2025
Merged

release: v3.3.22 – develop → staging#844
nevil-mathew merged 36 commits intostagingfrom
develop

Conversation

@nevil-mathew
Copy link
Collaborator

@nevil-mathew nevil-mathew commented Oct 15, 2025

Summary by CodeRabbit

  • New Features
    • Admins can execute read-only SQL queries with pagination and clear success/error feedback.
  • Security
    • Robust validation prevents unsafe SQL (forbidden operations, injections, unbalanced quotes).
  • Configuration
    • Configurable header names for organization and tenant; added support for an organization ID header.
  • Improvements
    • Admins can override organization/tenant via headers (with validation) when authorized.
    • Update events now include old and new values for clearer audit trails.
  • Localization
    • Added translations for query outcomes and header/validation errors.

…leForApprovedRequest to broadcast user update events
Adopt old/new event payloads and replace change-sets with extractDelta
feat(auth): allow admin override of tenant/org
@nevil-mathew
Copy link
Collaborator Author

@coderabbitai summary

@coderabbitai
Copy link

coderabbitai bot commented Oct 15, 2025

✅ Actions performed

Summary regeneration triggered.

@coderabbitai
Copy link

coderabbitai bot commented Oct 15, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (1)
  • develop

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch develop

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.

@coderabbitai
Copy link

coderabbitai bot commented Oct 15, 2025

Walkthrough

Introduces env-driven header constants and admin override logic, adds raw SQL query execution feature for admins with validation utilities, switches DTO event payloads from changedValues to oldValues/newValues, implements delta extraction, updates tenant/organization services to use deltas and raw Sequelize returns, adjusts controllers to new headers, extends locales, and registers a new distributed table.

Changes

Cohort / File(s) Summary
Headers and configuration
src/constants/common.js, src/envVariables.js, src/constants/blacklistConfig.js
Headers pulled from environment, new ORG_ID_HEADER added; tenant/org header names updated; blacklist allows organization.update.code.
Controllers header updates and new admin endpoint
src/controllers/v1/admin.js, src/controllers/v1/form.js, src/controllers/v1/public.js, src/controllers/v1/tenant.js
Controllers now read tenant/org headers via common constants; admin controller adds executeRawQuery endpoint.
Middleware admin overrides
src/middlewares/authenticator.js
Adds admin-only header overrides for organization and tenant, validates, fetches organization id, and short-circuits post-override path.
DTOs switch to old/new values
src/dtos/organizationDTO.js, src/dtos/tenantDTO.js, src/dtos/userDTO.js
eventBodyDTO signatures changed to accept oldValues/newValues; removed changes construction; sanitized inputs accordingly.
Delta utilities
src/generics/utils.js
Adds extractDelta and helper flattenLeafPaths; exports updated.
Raw query utilities and validation
src/utils/rawQueryUtils.js
New module for SQL validation, normalization, forbidden pattern checks, and pagination helpers.
Admin services and events
src/services/admin.js
Adds executeRawQuery with validation, pagination, and results; deactivateOrg event payload reflects deactivation and includes deactivated_users_count.
Organization service delta adoption
src/services/organization.js
Uses extractDelta; builds oldValues/newValues snapshots; resolves related_org_details for both states; early return on no changes.
Tenant service refactor
src/services/tenant.js
Uses raw: true fetches; simplifies update; switches all event emissions to oldValues/newValues and delta-based computation; updates domain operations.
Org-admin role update async refactor
src/services/org-admin.js
Rewrites updateRoleForApprovedRequest to async/await, adds guards and throws, emits delta-based events, returns { success: true }.
Database behavior adjustments
src/database/queries/tenants.js, src/distributionColumns.sql
Tenant.update returns raw objects; registers organization_registration_codes as distributed by tenant_code.
Event broadcaster debug
src/helpers/eventBroadcasterMain.js
Logs requestBody via util.inspect before broadcasting.
Locales and validation
src/locales/en.json, src/validators/v1/admin.js
Adds query- and org-related messages; adds executeRawQuery validator.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant AdminClient as Admin Client
  participant AdminController as Controller: admin.executeRawQuery
  participant Validator as Validator
  participant RawUtils as rawQueryUtils
  participant AdminService as Service: AdminHelper
  participant DB as Database

  AdminClient->>AdminController: POST /v1/admin/raw-query { query, pageNo, pageSize }
  AdminController->>Validator: validate(req)
  Validator-->>AdminController: ok | error
  alt validation ok
    AdminController->>AdminService: executeRawQuery(query, adminUserId, pageNo, pageSize)
    AdminService->>RawUtils: validateQuerySecurity(query)
    RawUtils-->>AdminService: ok | error
    alt secure
      AdminService->>RawUtils: getPaginationParams(pageNo, pageSize)
      RawUtils-->>AdminService: { limit, offset }
      AdminService->>DB: SELECT ... LIMIT/OFFSET
      DB-->>AdminService: rows
      AdminService->>DB: SELECT COUNT(*) FROM (query) t
      DB-->>AdminService: total
      AdminService-->>AdminController: { rows, pageNo, pageSize, total }
      AdminController-->>AdminClient: 200 payload
    else insecure
      AdminService-->>AdminController: error
      AdminController-->>AdminClient: 400/422 error
    end
  else validation error
    AdminController-->>AdminClient: 400 error
  end
Loading
sequenceDiagram
  autonumber
  participant Client as Client
  participant Auth as Middleware: authenticator
  participant OrgQueries as organizationQueries
  participant Next as Next Handler

  Client->>Auth: Request with token + headers
  Auth->>Auth: Decode token -> decodedToken
  alt role === ADMIN and override headers present
    Auth->>Auth: Read headers (ORG_CODE_HEADER, TENANT_CODE_HEADER)
    Auth->>OrgQueries: getByCode(tenant_code, org_code)
    OrgQueries-->>Auth: organization { id, code } | null
    alt organization found
      Auth->>Auth: Override decodedToken { organization_id, organization_code, tenant_code }
      Auth-->>Next: proceed early
    else not found
      Auth-->>Client: 400 INVALID_ORG_OR_TENANT_CODE
    end
  else non-admin or no overrides
    Auth->>Auth: Use tenant_code from header/domain
    Auth-->>Next: proceed
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Possibly related PRs

Poem

I thump my paws on SQL ground,
With guarded hops, no drops around.
Old to new, our deltas sing,
Headers whisper, envs take wing.
Admins query, safely roam—
Carrots cached, events flown home. 🥕✨

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch develop

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 47357a0 and 07cfafd.

📒 Files selected for processing (22)
  • src/constants/blacklistConfig.js (1 hunks)
  • src/constants/common.js (1 hunks)
  • src/controllers/v1/admin.js (3 hunks)
  • src/controllers/v1/form.js (2 hunks)
  • src/controllers/v1/public.js (1 hunks)
  • src/controllers/v1/tenant.js (1 hunks)
  • src/database/queries/tenants.js (1 hunks)
  • src/distributionColumns.sql (1 hunks)
  • src/dtos/organizationDTO.js (1 hunks)
  • src/dtos/tenantDTO.js (2 hunks)
  • src/dtos/userDTO.js (2 hunks)
  • src/envVariables.js (1 hunks)
  • src/generics/utils.js (2 hunks)
  • src/helpers/eventBroadcasterMain.js (2 hunks)
  • src/locales/en.json (1 hunks)
  • src/middlewares/authenticator.js (4 hunks)
  • src/services/admin.js (3 hunks)
  • src/services/org-admin.js (2 hunks)
  • src/services/organization.js (1 hunks)
  • src/services/tenant.js (7 hunks)
  • src/utils/rawQueryUtils.js (1 hunks)
  • src/validators/v1/admin.js (1 hunks)

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.

@nevil-mathew nevil-mathew merged commit 9b991c5 into staging Oct 15, 2025
1 of 2 checks passed
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.

1 participant