feat(users): Enhance /users/ endpoint with search, filtering, and pagination#1165
Open
AbdurrahmanIdr wants to merge 18 commits intohngprojects:devfrom
Open
feat(users): Enhance /users/ endpoint with search, filtering, and pagination#1165AbdurrahmanIdr wants to merge 18 commits intohngprojects:devfrom
AbdurrahmanIdr wants to merge 18 commits intohngprojects:devfrom
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.
Contributor
|
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 <45592479+AbdurrahmanIdr@users.noreply.github.com>
Signed-off-by: Abdurrahman Idris <45592479+AbdurrahmanIdr@users.noreply.github.com>
- 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
Author
|
all checks passed |
samuelogboye
requested changes
Mar 1, 2025
Contributor
samuelogboye
left a comment
There was a problem hiding this comment.
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.
Author
|
removed .idea and added to .gitignore |
Signed-off-by: Abdurrahman Idris <45592479+AbdurrahmanIdr@users.noreply.github.com>
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.
Author
|
conflict resolved. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_activestatus.pageandlimit(default: 1, 20; max: 50).NameErrorinroutes/user.pydue to missing schema import./api/v1/users/instead of/users/.401 Not Authenticatedby 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?
Paginationand updatedAllUsersResponseinschema/user.py.from_attributes=True).UserService.fetch_allwith search, filtering, and pagination logic.routes/user.pyto 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