This commit is contained in:
Shubhankar45
2026-10-27 12:03:31 +00:00
parent 2ed3945b5d
commit 4e5e95775d
104 changed files with 5210 additions and 606 deletions
+8 -8
View File
@@ -14,23 +14,23 @@ from sqlalchemy import select, text
from sqlalchemy.ext.asyncio import AsyncSession
from app.auth.dependencies import active_hq_ids_for_am
from app.common.enums import Role
from app.common.enums import Role, FIELD_ROLES
from app.common.exceptions import ConflictError, ForbiddenError, NotFoundError, ValidationError
from app.csv_data.models import FormApproval
from app.organization.models import AMHeadquarters, Headquarters
from app.users.models import User
APPROVAL_TABLES = {"standard_tour_plan", "tour_plan", "fare_chart"}
APPROVAL_TABLES = {"standard_tour_plan", "tour_plan", "fare_chart", "expense_statement"}
async def owners_in_scope(db: AsyncSession, user: User) -> list[User]:
"""The MRs whose forms this user may see: themselves, the MRs of their headquarters, or (admin) all."""
stmt = select(User).where(User.role == Role.MR, User.is_active.is_(True)).order_by(User.full_name)
if user.role == Role.MR:
"""The MRs whose forms this user may see: themselves, their headquarters' MRs, or (admin, GM) all."""
stmt = select(User).where(User.role.in_(FIELD_ROLES), User.is_active.is_(True)).order_by(User.full_name)
if user.role in FIELD_ROLES:
stmt = stmt.where(User.id == user.id)
elif user.role == Role.AM:
stmt = stmt.where(User.headquarters_id.in_(await active_hq_ids_for_am(db, user.id)))
elif user.role != Role.ADMIN:
elif user.role not in (Role.ADMIN, Role.GM):
return []
return list((await db.execute(stmt)).scalars())
@@ -49,7 +49,7 @@ async def assert_owner_in_scope(db: AsyncSession, user: User, owner_id: uuid.UUI
for owner in await owners_in_scope(db, user):
if owner.id == owner_id:
return owner
if user.role == Role.ADMIN or (await db.get(User, owner_id)) is None:
if user.role in (Role.ADMIN, Role.GM) or (await db.get(User, owner_id)) is None:
raise NotFoundError("Medical representative not found", {"id": str(owner_id)})
raise ForbiddenError("This form belongs to a medical representative outside your headquarters")
@@ -80,7 +80,7 @@ async def list_approvals(db: AsyncSession, user: User) -> list[tuple[FormApprova
for r in (await db.execute(select(FormApproval).where(FormApproval.owner_user_id.in_(owners)))).scalars()
}
out: list[tuple[FormApproval, User, str | None]] = []
if user.role == Role.MR:
if user.role in FIELD_ROLES:
for name in sorted(APPROVAL_TABLES):
out.append((existing.get((name, user.id)) or FormApproval(table_name=name, owner_user_id=user.id, status="draft"), user, hq_names.get(user.headquarters_id)))
return out