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:
Shubhankar45
2026-11-07 05:43:12 +00:00
parent b001e2d03c
commit 0d30c58bf0
98 changed files with 1878 additions and 573 deletions
+13 -12
View File
@@ -9,18 +9,18 @@ from enum import StrEnum
class Role(StrEnum):
"""Operational roles (MR/AM/GM) plus the technical ADMIN role.
"""Operational roles (MR, ABM, AGM; stored as MR/ABM/AGM) plus technical ADMIN.
ADMIN sits OUTSIDE the business hierarchy. It provisions users and masters but
can never approve a report or read report content (report §8, SEC-2).
ADMIN sits OUTSIDE the business hierarchy. It provisions users and masters and
may monitor reports, but can never approve one (report §8, SEC-2).
"""
ADMIN = "ADMIN"
GM = "GM"
AM = "AM"
AGM = "AGM"
ABM = "ABM"
MR = "MR"
#: Senior Field Executive: does an MR's work (daily reports, forms, doctors, chemists) and
#: reports to the same Area Manager, but may cover several headquarters.
#: reports to the same Area Business Manager, but may cover several headquarters.
SFE = "SFE"
@@ -29,25 +29,25 @@ FIELD_ROLES: frozenset[Role] = frozenset({Role.MR, Role.SFE})
class DCRStatus(StrEnum):
"""MR submits, the Area Manager approves or returns it.
"""MR submits, the Area Business Manager approves or returns it.
`RETURNED` captures the correction loop, and `APPROVED` remains terminal and
immutable. There is no General Manager step.
immutable. There is no Assistant General Manager step.
"""
DRAFT = "DRAFT"
PENDING_AM = "PENDING_AM"
PENDING_ABM = "PENDING_ABM"
RETURNED = "RETURNED"
APPROVED = "APPROVED"
class ReportType(StrEnum):
"""An MR files two reports a day, each approved by the Area Manager on its own.
"""An MR files two reports a day, each approved by the Area Business Manager on its own.
PLAN — in the morning: which doctors and chemists the MR intends to visit, and when.
ACTUAL — at the end of the day: the visits that happened, plus a short note for every
planned visit that did not. It can be started only once the plan is approved.
SELF — an Area Manager's own end-of-day report of the visits they made. It needs no
SELF — an Area Business Manager's own end-of-day report of the visits they made. It needs no
plan and no approval: submitting it makes it final.
"""
@@ -84,7 +84,7 @@ class ConversionStatus(StrEnum):
class MasterApproval(StrEnum):
"""A doctor or chemist an MR adds waits for their Area Manager before anyone can use it."""
"""A doctor or chemist an MR adds waits for their Area Business Manager before anyone can use it."""
PENDING = "PENDING"
APPROVED = "APPROVED"
@@ -109,6 +109,7 @@ class EntityType(StrEnum):
HEADQUARTERS = "headquarters"
DOCTOR = "doctor"
CHEMIST = "chemist"
STOCKIST = "stockist"
DCR = "dcr"
PRODUCT = "product"
#: A row of a company form (the table is named in the entry's data).
+18 -10
View File
@@ -1,11 +1,11 @@
"""Approval of the doctors and chemists an MR adds.
"""Approval of the doctors, chemists and stockists an MR adds.
MR adds one --> PENDING --approve(AM)--> APPROVED (listed, and visitable by that MR)
--reject(AM)---> REJECTED (the MR sees why; adding it again re-sends it)
MR adds one --> PENDING --approve(ABM)--> APPROVED (listed, and visitable by that MR)
--reject(ABM)---> REJECTED (the MR sees why; adding it again re-sends it)
Only APPROVED masters are listed or may be visited. The administrator's own additions are
APPROVED at once. The reviewer is the Area Manager of the master's headquarters (or the
administrator); the General Manager may look but not act.
APPROVED at once. The reviewer is the Area Business Manager of the master's headquarters (or the
administrator); the Assistant General Manager may look but not act.
"""
import uuid
@@ -34,14 +34,22 @@ async def rejected_twin(db: AsyncSession, model: Any, match: dict[str, Any]) ->
async def resubmit(
db: AsyncSession, record: Any, values: dict[str, Any], actor: User, assignment_model: Any, fk: str
db: AsyncSession,
record: Any,
values: dict[str, Any],
actor: User,
assignment_model: Any | None = None,
fk: str | None = None,
) -> None:
"""Re-send a rejected record with the MR's new details, assigned to that MR."""
"""Re-send a rejected record with the MR's new details, assigned to that MR (stockists have no
per-MR assignment: they belong to the headquarters)."""
for field, value in values.items():
setattr(record, field, value)
record.approval_status = MasterApproval.PENDING
record.requested_by_user_id = actor.id
record.rejection_reason = None
if assignment_model is None or fk is None:
return
today = date.today()
current = (
await db.execute(
@@ -59,7 +67,7 @@ async def resubmit(
async def requests_for(db: AsyncSession, model: Any, actor: User) -> list[Any]:
"""An MR: what they asked for that is still waiting or was rejected.
An Area Manager: what waits for them. Administrator and GM: everything waiting."""
An Area Business Manager: what waits for them. Administrator and AGM: everything waiting."""
stmt = select(model).order_by(model.created_at.desc())
if actor.role in FIELD_ROLES:
return list(
@@ -92,8 +100,8 @@ async def review(
record = await db.get(model, record_id)
if record is None:
raise NotFoundError(f"{entity_type.value.capitalize()} not found")
if actor.role not in (Role.AM, Role.ADMIN):
raise ForbiddenError("Only the Area Manager approves a new doctor or chemist")
if actor.role not in (Role.ABM, Role.ADMIN):
raise ForbiddenError("Only the Area Business Manager approves a new doctor, chemist or stockist")
await assert_can_view_hq(db, actor, record.headquarters_id)
target = MasterApproval.APPROVED if approve else MasterApproval.REJECTED
if record.approval_status == target:
+69
View File
@@ -0,0 +1,69 @@
"""Shared rules for an administrator editing a doctor, chemist or stockist."""
import uuid
from datetime import date
from sqlalchemy import select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession
from app.auth.dependencies import assert_can_view_hq
from app.common.exceptions import ConflictError, NotFoundError
from app.organization.models import Headquarters
from app.users.models import User
async def move_headquarters(
db: AsyncSession,
actor: User,
item,
new_hq_id: uuid.UUID,
assignment_model=None,
fk_field: str | None = None,
) -> None:
"""Move ``item`` to another headquarters. A current MR assignment ends when that MR does not
work the new headquarters, since an MR may only hold masters of the headquarters they cover."""
if new_hq_id == item.headquarters_id:
return
if await db.get(Headquarters, new_hq_id) is None:
raise NotFoundError("Headquarters not found")
await assert_can_view_hq(db, actor, new_hq_id)
item.headquarters_id = new_hq_id
if assignment_model is None:
return
current = (
await db.execute(
select(assignment_model).where(
getattr(assignment_model, fk_field) == item.id,
assignment_model.effective_to.is_(None),
)
)
).scalars().all()
for row in current:
mr = await db.get(User, row.mr_user_id)
if mr is None or new_hq_id not in mr.covered_headquarter_ids:
row.effective_to = date.today()
async def end_assignments(db: AsyncSession, assignment_model, fk_field: str, item_id: uuid.UUID) -> int:
"""Close every current MR assignment of one master; returns how many were closed."""
current = (
await db.execute(
select(assignment_model).where(
getattr(assignment_model, fk_field) == item_id,
assignment_model.effective_to.is_(None),
)
)
).scalars().all()
for row in current:
row.effective_to = date.today()
return len(current)
async def commit_unique(db: AsyncSession, message: str) -> None:
"""Commit, turning a unique-constraint clash (same name and address in a headquarters) into a 409."""
try:
await db.commit()
except IntegrityError as exc:
await db.rollback()
raise ConflictError(message) from exc