fix(task6): scope dashboard and reports to empty for unassigned department user

This commit is contained in:
Atharv1705
2026-11-17 18:21:18 +05:30
parent cb47017eac
commit b8639bc52a
6 changed files with 645 additions and 26 deletions
+9 -5
View File
@@ -6,6 +6,7 @@ from ..core.db import get_db
from ..core.deps import get_current_user
from ..models import Asset, Complaint, User
from ..services.permissions import Role
from ..services.scoping import resolve_department_scope
router = APIRouter(prefix="/search", tags=["search"])
@@ -20,8 +21,11 @@ def global_search(
if not term:
return {"assets": [], "complaints": []}
scope = resolve_department_scope(current_user)
if scope.is_empty:
return {"assets": [], "complaints": []}
pattern = f"%{term}%"
is_dept_scoped = current_user.role == Role.department_user.value
asset_q = db.query(Asset).options(joinedload(Asset.department)).filter(
or_(
@@ -30,8 +34,8 @@ def global_search(
Asset.serial_no.ilike(pattern),
)
)
if is_dept_scoped:
asset_q = asset_q.filter(Asset.department_id == current_user.department_id)
if scope.department_id is not None:
asset_q = asset_q.filter(Asset.department_id == scope.department_id)
assets = asset_q.order_by(Asset.asset_code.asc()).limit(8).all()
complaint_q = (
@@ -47,8 +51,8 @@ def global_search(
)
)
)
if is_dept_scoped:
complaint_q = complaint_q.filter(Asset.department_id == current_user.department_id)
if scope.department_id is not None:
complaint_q = complaint_q.filter(Asset.department_id == scope.department_id)
complaints = (
complaint_q.order_by(Complaint.registered_at.desc(), Complaint.id.desc())
.limit(8)