Skip to content

[Entity-Service] Support sortBy on POST /users/search for the Postgres data source - #1875

Merged
Rashmika998 merged 1 commit into
wso2-open-operations:dev-app-csm-portalfrom
Rashmika998:fix/entity-service-users-search-sort
Sep 21, 2026
Merged

Rashmika998 merged 1 commit into
wso2-open-operations:dev-app-csm-portalfrom
Rashmika998:fix/entity-service-users-search-sort

Conversation

@Rashmika998

Copy link
Copy Markdown
Contributor

Summary

POST /users/search on the Postgres data source rejected any sortBy:

Bad request: POST /users/search: sortBy is only supported for the ServiceNow data source

The OpenAPI contract already advertises sortBy (name / createdOn / updatedOn) for this endpoint, and the CSM users page always sends {"field": "name", "order": "asc"}, so that page's search returned a 400.

  • The service now validates the sort the same way the ServiceNow adapter does (unknown field, order without a field, unknown order are still validation errors).
  • The repository maps the validated field to a fixed SQL expression; request text never reaches SQL. name sorts case-insensitively on the display name, falling back to first + last name and then the user name, because "user".name is empty for a few synced rows (5 of 2,937 in staging). u.id is always the last tie-break so pages are stable across offsets.
  • No sortBy keeps the previous newest-first default.

Verification

  • Ran the real service and repository against staging: five sorts (name asc, name desc, createdOn desc, updatedOn asc, name with order omitted), each compared to hand-written SQL across four pages, including page boundaries and the tail. All match. The default order still works (total 2,937).
  • New unit tests for the service validation (7 cases) and for userOrderBy, including that an unknown field falls back to the default and never reaches SQL.
  • gofmt, go vet, gosec (0 issues), govulncheck clean. go test has one failure, TestSNCaseService_CreateCase_PublishesCaseCreated, which fails the same way without this change.

🤖 Generated with Claude Code

…s data source

The Postgres path rejected any sortBy with "sortBy is only supported for the
ServiceNow data source", although the OpenAPI contract advertises
name/createdOn/updatedOn for this endpoint and the CSM users page always sends
name/asc, so that page's user search returned a 400.

Validate the field and order the same way the ServiceNow adapter does, and map
them to fixed SQL expressions in the repository. name sorts case-insensitively
on the display name, falling back to first + last name and then user name for
the few rows with no display name; u.id is always the final tie-break so pages
are stable. No sortBy keeps the newest-first default.

Verified against staging through the real service and repository: five sorts
(name asc/desc, createdOn desc, updatedOn asc, name with order omitted), each
compared to hand-written SQL across four pages including the tail.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 30 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository: wso2-open-operations/cs-tools/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: c97c8894-4850-4c45-bfea-3635d5949121

📥 Commits

Reviewing files that changed from the base of the PR and between d827781 and e720c1c.

📒 Files selected for processing (7)
  • entity-service/CLAUDE.md
  • entity-service/internal/repository/user_repo.go
  • entity-service/internal/repository/user_repo_test.go
  • entity-service/internal/service/case_service_test.go
  • entity-service/internal/service/user_service.go
  • entity-service/internal/service/user_service_test.go
  • entity-service/openapi.yaml

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.

@Rashmika998
Rashmika998 merged commit 3efe101 into wso2-open-operations:dev-app-csm-portal Sep 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants