-
Notifications
You must be signed in to change notification settings - Fork 217
feat(users): Enhance /users/ endpoint with search, filtering, and pagination #1165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
…ponse Introduce a structured pagination model and update the AllUsersResponse schema to improve response consistency for the /users/ endpoint. This change adds a Pagination model with page, limit, total_pages, and total_users fields, and restructures the response data as a dictionary containing 'users' and 'pagination' keys, aligning with API design standards.
…a import Resolve a NameError caused by an undefined 'user' reference in the /users/ endpoint by explicitly importing required schemas from api.v1.schemas.user. This ensures proper recognition of AllUsersResponse and other schema classes, preventing runtime errors during application initialization.
…ervice Enhance the fetch_all method to support case-insensitive search across first_name, last_name, and email, filtering by is_active status, and robust pagination with configurable page and limit parameters (default: page=1, limit=20, max: 50). Optimize database queries using SQLAlchemy's or_ and func.lower for efficient execution, and update response formatting to include detailed pagination metadata.
Add comprehensive unit tests to validate the fetch_all method's search, filtering, and pagination functionality in UserService. Include test cases for case-insensitive search accuracy, is_active filtering correctness, and pagination behavior under varying conditions, using a mocked SQLAlchemy session to ensure robust and reliable implementation.
ensure all checks are complete , resolve the conflicts too |
Resolved Git merge conflict between filter and dev branches by adopting the enhanced AllUsersResponse with Pagination from filter branch. Removed outdated version from dev branch and preserved confirm_password addition to UserCreate schema.
Signed-off-by: Abdurrahman Idris <[email protected]>
Signed-off-by: Abdurrahman Idris <[email protected]>
- Updated test_fetch_all_* tests to include all required UserData fields in mock User objects, fixing ValidationErrors. - Modified test_get_all_users to use updated AllUsersResponse structure with data dict (users and pagination), replacing outdated page/per_page/total fields. Adjusted assertions and JSON comparison accordingly. modified: tests/v1/user/test_get_all_users.py modified: tests/v1/user/test_user_service.py
all checks passed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need .idea in prod?
Please remove it.
Added a default value 'HNG Boilerplate' to APP_NAME in Settings class to prevent UndefinedValueError when the environment variable is not set, improving local development reliability.
removed .idea and added to .gitignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attend to my comment
Signed-off-by: Abdurrahman Idris <[email protected]>
Revised comments in the fetch_all method of UserService for improved clarity and precision: - Updated class docstring to reflect broader service purpose. - Rewrote method docstring with detailed Args, Returns, and Raises sections. - Streamlined inline comments to be concise, action-oriented, and descriptive of functionality. No functional changes made to the code logic.
conflict resolved. |
Description
This pull request enhances the
/users/
API endpoint by adding search, filtering, and pagination capabilities, while resolving multiple issues encountered during development and testing. The changes improve user management efficiency, fix runtime errors, ensure proper routing and authentication, update Pydantic configurations, and address a missing database table to enable superadmin creation.Key changes include:
first_name
,last_name
, andemail
.is_active
status.page
andlimit
(default: 1, 20; max: 50).NameError
inroutes/user.py
due to missing schema import./api/v1/users/
instead of/users/
.401 Not Authenticated
by adding a superadmin creation script.
Motivation and Context
The
/users/
endpoint initially lacked efficient data retrieval options, making it challenging to manage large user datasets. Additionally, several runtime errors and configuration issues prevented the application from functioning correctly:
How Has This Been Tested?
Pagination
and updatedAllUsersResponse
inschema/user.py
.from_attributes=True
).UserService.fetch_all
with search, filtering, and pagination logic.routes/user.py
to fixNameError
./api/v1/users/
routing with superadmin token.curl -H "Authorization: Bearer <token>" "http://127.0.0.1:7001/api/v1/users?search=john"
curl -H "Authorization: Bearer <token>" "http://127.0.0.1:7001/api/v1/users?is_active=true"
curl -H "Authorization: Bearer <token>" "http://127.0.0.1:7001/api/v1/users?page=2&limit=20"
UserService.fetch_all
(search, filter, pagination).
Types of changes