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:
@@ -26,7 +26,7 @@ class Chemist(Base, UUIDMixin, TimestampMixin):
|
||||
shop_address: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
contact_number: Mapped[str | None] = mapped_column(String(20))
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
||||
#: PENDING until the Area Manager approves one an MR added; only APPROVED ones are listed or visited.
|
||||
#: PENDING until the Area Business Manager approves one an MR added; only APPROVED ones are listed or visited.
|
||||
approval_status: Mapped[str] = mapped_column(
|
||||
String(10), default=MasterApproval.APPROVED.value, server_default=MasterApproval.APPROVED.value, nullable=False
|
||||
)
|
||||
|
||||
+31
-9
@@ -18,8 +18,19 @@ router = APIRouter(prefix="/chemists", tags=["chemists"])
|
||||
@router.get("", response_model=list[ChemistRead])
|
||||
async def list_chemists(
|
||||
current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)
|
||||
) -> list[Chemist]:
|
||||
return await ChemistService(db).list_for(current_user)
|
||||
) -> list[ChemistRead]:
|
||||
service = ChemistService(db)
|
||||
chemists = await service.list_for(current_user)
|
||||
names = await service.assigned_mr_names([c.id for c in chemists])
|
||||
return [
|
||||
ChemistRead.model_validate(c).model_copy(
|
||||
update={
|
||||
"assigned_mr_id": names[c.id][0] if c.id in names else None,
|
||||
"assigned_mr_name": names[c.id][1] if c.id in names else None,
|
||||
}
|
||||
)
|
||||
for c in chemists
|
||||
]
|
||||
|
||||
|
||||
@router.post("", response_model=ChemistRead, status_code=status.HTTP_201_CREATED)
|
||||
@@ -29,7 +40,7 @@ async def create_chemist(
|
||||
current_user: User = Depends(require_role(Role.ADMIN, Role.MR)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Chemist:
|
||||
"""An Administrator, or an MR within their own headquarters (AM and GM cannot)."""
|
||||
"""An Administrator, or an MR within their own headquarters (ABM and AGM cannot)."""
|
||||
return await ChemistService(db).create(payload, current_user, client_ip(request))
|
||||
|
||||
|
||||
@@ -44,12 +55,23 @@ async def update_chemist(
|
||||
return await ChemistService(db).update(chemist_id, payload, current_user, client_ip(request))
|
||||
|
||||
|
||||
@router.delete("/{chemist_id}/assign", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def unassign_chemist(
|
||||
chemist_id: uuid.UUID,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.ADMIN)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> None:
|
||||
"""Administrator only: leave the chemist with no MR."""
|
||||
await ChemistService(db).unassign(chemist_id, current_user, client_ip(request))
|
||||
|
||||
|
||||
@router.post("/{chemist_id}/assign", status_code=status.HTTP_201_CREATED)
|
||||
async def assign_chemist(
|
||||
chemist_id: uuid.UUID,
|
||||
payload: AssignToMR,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.ADMIN, Role.GM, Role.AM)),
|
||||
current_user: User = Depends(require_role(Role.ADMIN, Role.AGM, Role.ABM)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
assignment = await ChemistService(db).assign_to_mr(
|
||||
@@ -67,8 +89,8 @@ async def assign_chemist(
|
||||
async def list_chemist_requests(
|
||||
current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)
|
||||
) -> list[Chemist]:
|
||||
"""Chemists an MR added that wait for the Area Manager: an MR sees their own (with any
|
||||
rejected and why), an Area Manager what waits for them, the administrator and GM all."""
|
||||
"""Chemists an MR added that wait for the Area Business Manager: an MR sees their own (with any
|
||||
rejected and why), an Area Business Manager what waits for them, the administrator and AGM all."""
|
||||
return await ChemistService(db).requests_for(current_user)
|
||||
|
||||
|
||||
@@ -76,10 +98,10 @@ async def list_chemist_requests(
|
||||
async def approve_chemist(
|
||||
chemist_id: uuid.UUID,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.AM, Role.ADMIN)),
|
||||
current_user: User = Depends(require_role(Role.ABM, Role.ADMIN)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Chemist:
|
||||
"""The Area Manager of its headquarters adds it to the list, assigned to the MR who asked."""
|
||||
"""The Area Business Manager of its headquarters adds it to the list, assigned to the MR who asked."""
|
||||
return await ChemistService(db).review(chemist_id, True, None, current_user, client_ip(request))
|
||||
|
||||
|
||||
@@ -88,7 +110,7 @@ async def reject_chemist(
|
||||
chemist_id: uuid.UUID,
|
||||
payload: RejectMaster,
|
||||
request: Request,
|
||||
current_user: User = Depends(require_role(Role.AM, Role.ADMIN)),
|
||||
current_user: User = Depends(require_role(Role.ABM, Role.ADMIN)),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Chemist:
|
||||
return await ChemistService(db).review(chemist_id, False, payload.reason, current_user, client_ip(request))
|
||||
|
||||
@@ -12,6 +12,7 @@ class ChemistCreate(BaseModel):
|
||||
|
||||
|
||||
class ChemistUpdate(BaseModel):
|
||||
headquarters_id: uuid.UUID | None = None
|
||||
name: str | None = Field(default=None, min_length=2, max_length=200)
|
||||
shop_address: str | None = Field(default=None, min_length=2, max_length=500)
|
||||
contact_number: str | None = Field(default=None, max_length=20)
|
||||
@@ -32,4 +33,7 @@ class ChemistRead(BaseModel):
|
||||
rejection_reason: str | None
|
||||
requested_by_name: str | None
|
||||
created_at: datetime
|
||||
#: Filled by the list endpoint only.
|
||||
assigned_mr_name: str | None = None
|
||||
assigned_mr_id: uuid.UUID | None = None
|
||||
|
||||
|
||||
+38
-5
@@ -10,12 +10,13 @@ from app.auth.dependencies import assert_can_view_hq, visible_hq_ids
|
||||
from app.chemists.models import Chemist, ChemistAssignment
|
||||
from app.chemists.schemas import ChemistCreate, ChemistUpdate
|
||||
from app.common import master_approval
|
||||
from app.common.master_edit import commit_unique, end_assignments, move_headquarters
|
||||
from app.common.enums import AuditAction, EntityType, MasterApproval, Role, FIELD_ROLES
|
||||
from app.common.exceptions import ConflictError, NotFoundError, ValidationError
|
||||
from app.common.utils import snapshot
|
||||
from app.users.models import User
|
||||
|
||||
_AUDIT_FIELDS = ["name", "shop_address", "contact_number", "is_active"]
|
||||
_AUDIT_FIELDS = ["headquarters_id", "name", "shop_address", "contact_number", "is_active"]
|
||||
|
||||
|
||||
class ChemistService:
|
||||
@@ -27,7 +28,7 @@ class ChemistService:
|
||||
await assert_can_view_hq(self.db, actor, data.headquarters_id)
|
||||
|
||||
values = data.model_dump()
|
||||
# Added by an MR: it waits for the Area Manager. One they were refused is sent again.
|
||||
# Added by an MR: it waits for the Area Business Manager. One they were refused is sent again.
|
||||
twin = None
|
||||
if actor.role in FIELD_ROLES:
|
||||
twin = await master_approval.rejected_twin(
|
||||
@@ -97,7 +98,11 @@ class ChemistService:
|
||||
await assert_can_view_hq(self.db, actor, chemist.headquarters_id)
|
||||
|
||||
before = snapshot(chemist, _AUDIT_FIELDS)
|
||||
for field, value in data.model_dump(exclude_unset=True).items():
|
||||
changes = data.model_dump(exclude_unset=True)
|
||||
new_hq = changes.pop("headquarters_id", None)
|
||||
if new_hq is not None:
|
||||
await move_headquarters(self.db, actor, chemist, new_hq, ChemistAssignment, "chemist_id")
|
||||
for field, value in changes.items():
|
||||
setattr(chemist, field, value)
|
||||
|
||||
await self.audit.log(
|
||||
@@ -109,7 +114,7 @@ class ChemistService:
|
||||
after=snapshot(chemist, _AUDIT_FIELDS),
|
||||
ip_address=ip,
|
||||
)
|
||||
await self.db.commit()
|
||||
await commit_unique(self.db, "A chemist with this name already exists at this address in this headquarters")
|
||||
return chemist
|
||||
|
||||
async def list_for(self, actor: User) -> list[Chemist]:
|
||||
@@ -133,6 +138,34 @@ class ChemistService:
|
||||
stmt = stmt.where(Chemist.headquarters_id.in_(allowed or [None]))
|
||||
return list((await self.db.execute(stmt)).scalars().all())
|
||||
|
||||
async def assigned_mr_names(self, chemist_ids: list[uuid.UUID]) -> dict[uuid.UUID, tuple[uuid.UUID, str]]:
|
||||
"""The MR (id, name) each chemist is currently assigned to, by chemist id."""
|
||||
if not chemist_ids:
|
||||
return {}
|
||||
stmt = (
|
||||
select(ChemistAssignment.chemist_id, User.id, User.full_name)
|
||||
.join(User, User.id == ChemistAssignment.mr_user_id)
|
||||
.where(ChemistAssignment.chemist_id.in_(chemist_ids), ChemistAssignment.effective_to.is_(None))
|
||||
)
|
||||
return {chemist_id: (mr_id, name) for chemist_id, mr_id, name in (await self.db.execute(stmt)).all()}
|
||||
|
||||
async def unassign(self, chemist_id: uuid.UUID, actor: User, ip: str | None = None) -> None:
|
||||
"""Take the chemist away from its MR, leaving it unassigned."""
|
||||
chemist = await self.db.get(Chemist, chemist_id)
|
||||
if chemist is None:
|
||||
raise NotFoundError("Chemist not found")
|
||||
await assert_can_view_hq(self.db, actor, chemist.headquarters_id)
|
||||
if await end_assignments(self.db, ChemistAssignment, "chemist_id", chemist_id):
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.CHEMIST,
|
||||
entity_id=chemist_id,
|
||||
action=AuditAction.UPDATE,
|
||||
after={"unassigned": True},
|
||||
ip_address=ip,
|
||||
)
|
||||
await self.db.commit()
|
||||
|
||||
async def assign_to_mr(
|
||||
self,
|
||||
chemist_id: uuid.UUID,
|
||||
@@ -146,7 +179,7 @@ class ChemistService:
|
||||
raise NotFoundError("Chemist not found")
|
||||
await assert_can_view_hq(self.db, actor, chemist.headquarters_id)
|
||||
if chemist.approval_status != MasterApproval.APPROVED:
|
||||
raise ValidationError("This chemist is still waiting for the Area Manager's approval")
|
||||
raise ValidationError("This chemist is still waiting for the Area Business Manager's approval")
|
||||
|
||||
mr = await self.db.get(User, mr_user_id)
|
||||
if mr is None:
|
||||
|
||||
Reference in New Issue
Block a user