Server backup 2026-11-07 of uncommitted work in projects/pharma-web-celtic
Snapshot of the working tree on srv1071960 (branch main), taken during server cleanup. .env files, node_modules, builds, logs and media were excluded.
This commit is contained in:
+10
-10
@@ -1,6 +1,6 @@
|
||||
"""Approval workflow for the company forms that carry an Area Manager signature.
|
||||
"""Approval workflow for the company forms that carry an Area Business Manager signature.
|
||||
|
||||
draft --request(MR)--> pending --approve(AM)--> approved
|
||||
draft --request(MR)--> pending --approve(ABM)--> approved
|
||||
pending / approved --any change to the rows--> draft (a signature never covers edited data)
|
||||
|
||||
Every MR has their own copy of each form, so approval is per (form, MR). The request goes to the Area
|
||||
@@ -24,13 +24,13 @@ APPROVAL_TABLES = {"standard_tour_plan", "tour_plan", "fare_chart", "expense_sta
|
||||
|
||||
|
||||
async def owners_in_scope(db: AsyncSession, user: User) -> list[User]:
|
||||
"""The MRs whose forms this user may see: themselves, their headquarters' MRs, or (admin, GM) all."""
|
||||
"""The MRs whose forms this user may see: themselves, their headquarters' MRs, or (admin, AGM) 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:
|
||||
elif user.role == Role.ABM:
|
||||
stmt = stmt.where(User.headquarters_id.in_(await active_hq_ids_for_am(db, user.id)))
|
||||
elif user.role not in (Role.ADMIN, Role.GM):
|
||||
elif user.role not in (Role.ADMIN, Role.AGM):
|
||||
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 in (Role.ADMIN, Role.GM) or (await db.get(User, owner_id)) is None:
|
||||
if user.role in (Role.ADMIN, Role.AGM) 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")
|
||||
|
||||
@@ -70,7 +70,7 @@ async def _get_or_create(db: AsyncSession, table_name: str, owner_id: uuid.UUID)
|
||||
async def list_approvals(db: AsyncSession, user: User) -> list[tuple[FormApproval, User, str | None]]:
|
||||
"""(approval, MR, headquarters name) for what `user` can see.
|
||||
|
||||
An MR always gets all three forms ("draft" until they act); an Area Manager or admin only
|
||||
An MR always gets all three forms ("draft" until they act); an Area Business Manager or admin only
|
||||
gets forms that have been sent, since a draft is nothing for them to act on.
|
||||
"""
|
||||
owners = {o.id: o for o in await owners_in_scope(db, user)}
|
||||
@@ -99,10 +99,10 @@ async def _row_count(db: AsyncSession, table_name: str, owner_id: uuid.UUID) ->
|
||||
|
||||
async def request_approval(db: AsyncSession, table_name: str, actor: User) -> FormApproval:
|
||||
if await assigned_area_manager(db, actor) is None:
|
||||
raise ValidationError("No Area Manager is assigned to your headquarters yet. Ask the administrator to assign one.")
|
||||
raise ValidationError("No Area Business Manager is assigned to your headquarters yet. Ask the administrator to assign one.")
|
||||
row = await _get_or_create(db, table_name, actor.id)
|
||||
if row.status == "pending":
|
||||
return row # already with the Area Manager — a second click changes nothing
|
||||
return row # already with the Area Business Manager — a second click changes nothing
|
||||
if row.status == "approved":
|
||||
raise ConflictError("This form is already approved. Change a row to send it again.")
|
||||
if await _row_count(db, table_name, actor.id) == 0:
|
||||
@@ -115,7 +115,7 @@ async def request_approval(db: AsyncSession, table_name: str, actor: User) -> Fo
|
||||
|
||||
|
||||
async def approve(db: AsyncSession, table_name: str, owner_id: uuid.UUID, actor: User) -> FormApproval:
|
||||
await assert_owner_in_scope(db, actor, owner_id) # only the Area Manager assigned to this MR's headquarters
|
||||
await assert_owner_in_scope(db, actor, owner_id) # only the Area Business Manager assigned to this MR's headquarters
|
||||
row = await _get_or_create(db, table_name, owner_id)
|
||||
if row.status == "approved":
|
||||
return row # a double click
|
||||
|
||||
Reference in New Issue
Block a user