Facilities Controller for proper querying #5507
Open
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.
Overview
In the Admin module, the user controller handles both users and facilities. The mixing of concepts is a violation of bounded context. The controller should focus solely on users, and the interaction with facilities should handled separately. The index action retrieves facilities based on the @district variable, selecting all accessible facilities or filtering by the district. It then queries users based on these facilities creating a dependency on users and facilities within the same action.
Regarding aggregate usage, the controller deals with facilities and users that should block different aggregates. Users should not be able to manage facilities directly; these operations should be mediated through application services. The current implementation crosses these boundaries by combining the management of users and facilities in the same controller, which can cause confusion in the domain.
Creating Facility Service
Beginning the refactoring, we need a service to handle the admin’s calls for the facilities specifically. This service was added as a class FacilityService within a new facility_service.rb file.
In the initialization of the service, we use an admin's permissions to initiate the query for the facilities, so the currently logged-in admin is stored as the admin requesting the facilities.
The function handling the query for the facilities uses the admin’s profile and credentials to query the database for all queries. Depending on the district search term, only facilities within the district will be returned. Searching for district “All” will return all districts.
Refactor User Controller
We wanted to make changes to the User Controller so it would no longer violate Domain-Driven Design Principles, which it would handle both users and facilities rather than just users.
To fix this, we needed to add a new before_action and refactor the index method among the public functions in this class. Below is the new before_action and the refactored index method:
The before_action will make a call to set_facility_service, which then allows the Users Controller to begin using the new FacilityService prior to executing the index method. Here is the set_facility_service method the before_action calls and executes before the index method:
With this change, The Users Controller will now only focus on users and has the management of facilities removed and handled by the FacilityService class. This will then separate the bounded contexts as they are supposed to and comply with DDD principles.