Skip to content

Conversation

@nevil-mathew
Copy link
Collaborator

@nevil-mathew nevil-mathew commented Sep 29, 2025

Summary by CodeRabbit

  • New Features

    • Added support to fetch user profiles by ID or contact info with cross-field validation.
    • Introduced validation for organization deactivation requests.
  • Bug Fixes

    • Prevented false duplicate errors by ignoring soft-deleted user roles in uniqueness checks.
    • Deactivation now returns clear errors when an organization is not found or already inactive.
    • Standardized and tightened validations for user roles (create/update/list) with clearer messages.
  • Chores

    • Added new localization keys (ORG_DEACTIVATION_FAILED, ORG_NOT_FOUND, ORG_ALREADY_INACTIVE, USER_PROFILE_FETCHED_SUCCESSFULLY) and refined UNIQUE_CONSTRAINT_ERROR text.

adithyadinesh0412 and others added 16 commits September 16, 2025 17:41
…k-upload

Refactor keying and queries for external entities and CSV invite meta
fix[validation]: organization deactivation validation and error messages(BUG-3834)
fix[db]: create migration for unique user roles index with soft-delete(BUG-3842)
fix[validation]: add profileById validation for user queries(BUG-3833)
fix[validation]: improve validation messages for user role apis (BUG-3835)
@coderabbitai
Copy link

coderabbitai bot commented Sep 29, 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.

Walkthrough

Introduces a migration replacing a hard unique constraint with a partial unique index on user_roles. Updates external entity mapping logic and user invite lookups to composite keys. Adds org deactivation pre-checks and a new validator. Tightens user-role validators. Adds a user profile lookup validator. Updates localization strings.

Changes

Cohort / File(s) Summary
Database Migration: user_roles unique constraint update
src/database/migrations/20250925122507-update-unique-user-roles.js
Drops existing unique constraint on (title, organization_id, tenant_code) and creates a partial unique index excluding soft-deleted rows (WHERE deleted_at IS NULL); down migration restores original constraint.
External entity mapping and invite lookups
src/generics/utils.js, src/helpers/userInvite.js
Switches mapping to composite keys based on normalized externalId + entityType; updates invite meta construction to use value+suffix composite keys for entities and subroles; adds normalization and skip logic for missing parts.
Org deactivation flow
src/services/admin.js, src/validators/v1/admin.js
Adds deactivateOrg validator (params and headers); service adds pre-update existence/already-inactive checks, updates row-affected checks, and standardizes failure message to ORG_DEACTIVATION_FAILED.
User-role validators tightening
src/validators/v1/user-role.js
Refines messages; constrains user_type to 0/1; standardizes status validation via common constants; adds optional label rules; replaces generic list validation with explicit per-field checks including organization_id.
Localization updates
src/locales/en.json
Adds ORG_DEACTIVATION_FAILED, ORG_NOT_FOUND, ORG_ALREADY_INACTIVE, USER_PROFILE_FETCHED_SUCCESSFULLY; tweaks UNIQUE_CONSTRAINT_ERROR punctuation.
User profile validator
src/validators/v1/user.js
Adds profileById validator supporting query by id or email/username/phone (+required phone_code when phone present); enforces at least one identifier if id absent; optional tenant_code.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor C as Client
  participant V as Validator (v1/admin.deactivateOrg)
  participant S as AdminService.deactivateOrg
  participant DB as Database

  C->>V: Request: DELETE /orgs/:id (tenant-id)
  V-->>C: 400 on validation error
  V->>S: Validated request
  S->>DB: SELECT org by id & tenant
  alt Not found
    S-->>C: 404 ORG_NOT_FOUND
  else Already inactive
    S-->>C: 409 ORG_ALREADY_INACTIVE
  else Active
    S->>DB: UPDATE org set inactive
    alt Rows affected == 0
      S-->>C: 500 ORG_DEACTIVATION_FAILED
    else Success
      S->>DB: UPDATE related users inactive (async/next)
      S-->>C: 200 Organization deactivated
    end
  end
Loading
sequenceDiagram
  autonumber
  participant Caller
  participant U as utils.fetchAndMapAllExternalEntities
  participant Ext as External Source

  Caller->>U: fetchAndMapAllExternalEntities()
  U->>Ext: Fetch entities
  Ext-->>U: Entities with metaInformation
  U->>U: normalize(externalId,type) -> key = "externalid|type"
  alt externalId or type missing
    U->>U: skip entry, keep accumulator
  else
    U->>U: map[key] = { _id, externalId, entityType, ... }
  end
  U-->>Caller: composite-keyed map
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

A nibble of indexes, neat as a nib,
I thump and hop through validators’ crib.
Keys now in pairs, like carrots in twos,
Deactivate gently, with well-phrased news.
Soft deletes whisper, “Mind what you see,”
And profiles peek out—bun-approved, carefully. 🥕🐇

✨ Finishing touches
🧪 Generate unit tests
  • 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
🧪 Early access (Sonnet 4.5): enabled

We are currently testing the Sonnet 4.5 model, which is expected to improve code review quality. However, this model may lead to increased noise levels in the review comments. Please disable the early access features if the noise level causes any inconvenience.

Note:

  • Public repositories are always opted into early access features.
  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

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

@nevil-mathew
Copy link
Collaborator Author

@coderabbitai summary

@coderabbitai
Copy link

coderabbitai bot commented Sep 29, 2025

✅ Actions performed

Summary regeneration triggered.

@nevil-mathew
Copy link
Collaborator Author

@coderabbitai summary

@coderabbitai
Copy link

coderabbitai bot commented Sep 29, 2025

✅ Actions performed

Summary regeneration triggered.

@coderabbitai coderabbitai bot changed the title @coderabbitai Partial unique index on user_roles; composite lookups, new validators Sep 29, 2025
Allow digits in organization names for create/update in v1 validator
@nevil-mathew nevil-mathew marked this pull request as draft September 29, 2025 20:32
@nevil-mathew nevil-mathew marked this pull request as ready for review September 30, 2025 11:30
@nevil-mathew nevil-mathew merged commit 8f99088 into staging Sep 30, 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.

4 participants