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
+45
View File
@@ -0,0 +1,45 @@
from dataclasses import dataclass
from typing import Optional
from ..models import User
from .permissions import Role
@dataclass
class DepartmentScope:
is_unconstrained: bool
department_id: Optional[int]
is_empty: bool
def resolve_department_scope(
user: User,
requested_dept_id: Optional[int] = None,
) -> DepartmentScope:
"""Resolve department scoping based on user role and assigned department.
Rules:
- If user.role != 'department_user': unconstrained, department_id=requested_dept_id, is_empty=False.
- If user.role == 'department_user' and user.department_id is None: is_empty=True, department_id=None, is_unconstrained=False.
- If user.role == 'department_user' and user.department_id is not None: is_empty=False, department_id=user.department_id
(requested_dept_id is ignored, cannot widen or change), is_unconstrained=False.
"""
if user.role != Role.department_user.value:
return DepartmentScope(
is_unconstrained=True,
department_id=requested_dept_id,
is_empty=False,
)
if user.department_id is None:
return DepartmentScope(
is_unconstrained=False,
department_id=None,
is_empty=True,
)
return DepartmentScope(
is_unconstrained=False,
department_id=user.department_id,
is_empty=False,
)