05/25 Update inspectors contract assignment

This commit is contained in:
2026-05-25 13:58:37 -04:00
parent e703675370
commit 688308fca2
13 changed files with 463 additions and 79 deletions
+53 -14
View File
@@ -1,23 +1,18 @@
"""
app/utils/scope.py
------------------
Customer-scoping utility for the Janitorial QC portal.
Facility-scoping utilities for the Janitorial QC portal.
Provides a single entry-point — get_customer_scope(user) — that returns the
set of facility IDs a customer is authorised to view, derived from their
CustomerAssignment rows.
get_customer_scope(user) -> list[int] | None
Facility IDs a customer may access via CustomerAssignment rows.
Usage (inside any route that serves customer users):
get_inspector_scope(user) -> list[int] | None
Facility IDs an inspector may access via InspectorAssignment rows.
Returns [] (empty list) when the inspector has no contract assignments,
meaning they see nothing (strict mode).
from app.utils.scope import get_customer_scope
facility_ids = get_customer_scope(current_user)
inspections = Inspection.query.filter(
Inspection.facility_id.in_(facility_ids)
).all()
For non-customer roles the function returns None, signalling that no
facility-level scoping is required (full access applies).
For non-customer / non-inspector roles both functions return None, signalling
that no facility-level scoping is required (full access applies).
"""
import logging
@@ -76,4 +71,48 @@ def get_customer_scope(user) -> list[int] | None:
user.id, user.username, sorted(facility_ids),
)
return sorted(facility_ids)
def get_inspector_scope(user) -> list[int] | None:
"""Return the list of facility IDs accessible to a contract-scoped inspector.
Parameters
----------
user : User
The currently authenticated user.
Returns
-------
list[int]
Facility IDs the inspector may access. An empty list means the
inspector has no contract assignments and should see nothing.
None
Returned for non-inspector roles, indicating unrestricted access.
"""
if user.role != 'inspector':
return None
from app.models.inspector_assignment import InspectorAssignment
project_ids = [
a.project_id
for a in InspectorAssignment.query.filter_by(user_id=user.id).all()
]
if not project_ids:
return [] # strict: no assignments = no access
facility_ids = [
f.id for f in Facility.query.filter(
Facility.project_id.in_(project_ids),
Facility.active == True,
).all()
]
logger.debug(
'SCOPE | inspector_scope | user_id=%s username=%s facility_ids=%s',
user.id, user.username, sorted(facility_ids),
)
return sorted(facility_ids)