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:
+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