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:
+50
-22
@@ -1,13 +1,14 @@
|
||||
import uuid
|
||||
from datetime import date
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import and_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.audit.service import AuditService
|
||||
from app.auth.dependencies import assert_can_view_hq, visible_hq_ids
|
||||
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
|
||||
@@ -16,6 +17,7 @@ from app.doctors.schemas import DoctorCreate, DoctorUpdate
|
||||
from app.users.models import User
|
||||
|
||||
_AUDIT_FIELDS = [
|
||||
"headquarters_id",
|
||||
"name",
|
||||
"clinic_address",
|
||||
"speciality",
|
||||
@@ -35,7 +37,7 @@ class DoctorService:
|
||||
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(
|
||||
@@ -105,7 +107,11 @@ class DoctorService:
|
||||
await assert_can_view_hq(self.db, actor, doctor.headquarters_id)
|
||||
|
||||
before = snapshot(doctor, _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, doctor, new_hq, DoctorAssignment, "doctor_id")
|
||||
for field, value in changes.items():
|
||||
setattr(doctor, field, value)
|
||||
|
||||
await self.audit.log(
|
||||
@@ -117,30 +123,52 @@ class DoctorService:
|
||||
after=snapshot(doctor, _AUDIT_FIELDS),
|
||||
ip_address=ip,
|
||||
)
|
||||
await self.db.commit()
|
||||
await commit_unique(self.db, "A doctor with this name already exists at this clinic in this headquarters")
|
||||
return doctor
|
||||
|
||||
async def list_for(self, actor: User) -> list[Doctor]:
|
||||
async def list_for(self, actor: User) -> list[tuple[Doctor, str | None, uuid.UUID | None]]:
|
||||
"""An MR sees ONLY doctors currently assigned to them (R5)."""
|
||||
if actor.role in FIELD_ROLES:
|
||||
stmt = (
|
||||
select(Doctor)
|
||||
.join(DoctorAssignment, DoctorAssignment.doctor_id == Doctor.id)
|
||||
.where(
|
||||
DoctorAssignment.mr_user_id == actor.id,
|
||||
stmt = (
|
||||
select(Doctor, User.full_name, User.id)
|
||||
.outerjoin(
|
||||
DoctorAssignment,
|
||||
and_(
|
||||
DoctorAssignment.doctor_id == Doctor.id,
|
||||
DoctorAssignment.effective_to.is_(None),
|
||||
Doctor.is_active.is_(True),
|
||||
Doctor.approval_status == MasterApproval.APPROVED,
|
||||
)
|
||||
.order_by(Doctor.name)
|
||||
),
|
||||
)
|
||||
return list((await self.db.execute(stmt)).scalars().all())
|
||||
.outerjoin(User, User.id == DoctorAssignment.mr_user_id)
|
||||
.where(Doctor.approval_status == MasterApproval.APPROVED)
|
||||
.order_by(Doctor.name)
|
||||
)
|
||||
if actor.role in FIELD_ROLES:
|
||||
stmt = stmt.where(
|
||||
DoctorAssignment.mr_user_id == actor.id,
|
||||
Doctor.is_active.is_(True),
|
||||
)
|
||||
else:
|
||||
allowed = await visible_hq_ids(self.db, actor)
|
||||
if allowed is not None:
|
||||
stmt = stmt.where(Doctor.headquarters_id.in_(allowed or [None]))
|
||||
rows = (await self.db.execute(stmt)).all()
|
||||
return [(doctor, assigned_mr_name, mr_id) for doctor, assigned_mr_name, mr_id in rows]
|
||||
|
||||
allowed = await visible_hq_ids(self.db, actor)
|
||||
stmt = select(Doctor).where(Doctor.approval_status == MasterApproval.APPROVED).order_by(Doctor.name)
|
||||
if allowed is not None:
|
||||
stmt = stmt.where(Doctor.headquarters_id.in_(allowed or [None]))
|
||||
return list((await self.db.execute(stmt)).scalars().all())
|
||||
async def unassign(self, doctor_id: uuid.UUID, actor: User, ip: str | None = None) -> None:
|
||||
"""Take the doctor away from its MR, leaving it unassigned."""
|
||||
doctor = await self.db.get(Doctor, doctor_id)
|
||||
if doctor is None:
|
||||
raise NotFoundError("Doctor not found")
|
||||
await assert_can_view_hq(self.db, actor, doctor.headquarters_id)
|
||||
if await end_assignments(self.db, DoctorAssignment, "doctor_id", doctor_id):
|
||||
await self.audit.log(
|
||||
actor_user_id=actor.id,
|
||||
entity_type=EntityType.DOCTOR,
|
||||
entity_id=doctor_id,
|
||||
action=AuditAction.UPDATE,
|
||||
after={"unassigned": True},
|
||||
ip_address=ip,
|
||||
)
|
||||
await self.db.commit()
|
||||
|
||||
async def assign_to_mr(
|
||||
self,
|
||||
@@ -155,7 +183,7 @@ class DoctorService:
|
||||
raise NotFoundError("Doctor not found")
|
||||
await assert_can_view_hq(self.db, actor, doctor.headquarters_id)
|
||||
if doctor.approval_status != MasterApproval.APPROVED:
|
||||
raise ValidationError("This doctor is still waiting for the Area Manager's approval")
|
||||
raise ValidationError("This doctor 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