This commit is contained in:
Shubhankar45
2026-10-27 12:03:31 +00:00
parent 2ed3945b5d
commit 4e5e95775d
104 changed files with 5210 additions and 606 deletions
+49 -11
View File
@@ -5,7 +5,7 @@ from fastapi import APIRouter, Depends, Query, Request, Response, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.auth.dependencies import client_ip, get_current_user, require_role
from app.common.enums import DCRStatus, Role
from app.common.enums import DCRStatus, ReportType, Role
from app.common.exceptions import ValidationError
from app.database.session import get_db
from app.pdf.service import PDFService
@@ -17,6 +17,7 @@ from app.reports.schemas import (
DCRRead,
DCRUpdate,
DoctorCallCreate,
MissedVisitSet,
ReturnRequest,
StatusCounts,
)
@@ -33,17 +34,22 @@ router = APIRouter(prefix="/dcr", tags=["reports"])
@router.get("", response_model=list[DCRListItem])
async def list_reports(
status_filter: DCRStatus | None = Query(default=None, alias="status"),
report_type: ReportType | None = Query(default=None, alias="type"),
date_from: date | None = None,
date_to: date | None = None,
limit: int = Query(default=50, ge=1, le=200),
offset: int = Query(default=0, ge=0),
headquarters_id: uuid.UUID | None = None,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
) -> list[DCRListItem]:
"""S2 (MR) and S6 (manager queue). Scope is applied in the service, never here."""
"""S2 (MR) and S6 (manager queue). Scope is applied in the service, never here;
`headquarters_id` only narrows a manager's list to one of their headquarters."""
reports = await DCRService(db).list_for(
current_user,
headquarters_id=headquarters_id,
status=status_filter,
report_type=report_type,
date_from=date_from,
date_to=date_to,
limit=limit,
@@ -54,10 +60,12 @@ async def list_reports(
@router.get("/counts", response_model=StatusCounts)
async def status_counts(
current_user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)
headquarters_id: uuid.UUID | None = None,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
) -> StatusCounts:
"""Header counts — the deliberate, cheap replacement for the cut dashboards."""
return StatusCounts(**await DCRService(db).counts_for(current_user))
return StatusCounts(**await DCRService(db).counts_for(current_user, headquarters_id))
@router.get("/{dcr_id}", response_model=DCRRead)
@@ -87,7 +95,8 @@ async def download_pdf(
# uses the current form layout, even for reports approved before the layout changed.
content = await PDFService(db).render_bytes(dcr)
filename = f"DCR-{dcr.mr.employee_code}-{dcr.report_date.isoformat()}.pdf"
prefix = {ReportType.PLAN: "PLAN", ReportType.SELF: "DCR-SELF"}.get(dcr.report_type, "DCR")
filename = f"{prefix}-{dcr.mr.employee_code}-{dcr.report_date.isoformat()}.pdf"
return Response(
content=content,
media_type="application/pdf",
@@ -102,7 +111,7 @@ async def download_pdf(
async def create_report(
payload: DCRCreate,
request: Request,
current_user: User = Depends(require_role(Role.MR)),
current_user: User = Depends(require_role(Role.MR, Role.AM)),
db: AsyncSession = Depends(get_db),
) -> DCRRead:
return to_read(await DCRService(db).create(payload, current_user, client_ip(request)))
@@ -113,7 +122,7 @@ async def update_report(
dcr_id: uuid.UUID,
payload: DCRUpdate,
request: Request,
current_user: User = Depends(require_role(Role.MR)),
current_user: User = Depends(require_role(Role.MR, Role.AM)),
db: AsyncSession = Depends(get_db),
) -> DCRRead:
return to_read(
@@ -128,7 +137,7 @@ async def add_doctor_call(
dcr_id: uuid.UUID,
payload: DoctorCallCreate,
request: Request,
current_user: User = Depends(require_role(Role.MR)),
current_user: User = Depends(require_role(Role.MR, Role.AM)),
db: AsyncSession = Depends(get_db),
) -> DCRRead:
return to_read(
@@ -141,7 +150,7 @@ async def add_chemist_call(
dcr_id: uuid.UUID,
payload: ChemistCallCreate,
request: Request,
current_user: User = Depends(require_role(Role.MR)),
current_user: User = Depends(require_role(Role.MR, Role.AM)),
db: AsyncSession = Depends(get_db),
) -> DCRRead:
return to_read(
@@ -154,7 +163,7 @@ async def delete_call(
dcr_id: uuid.UUID,
call_id: uuid.UUID,
request: Request,
current_user: User = Depends(require_role(Role.MR)),
current_user: User = Depends(require_role(Role.MR, Role.AM)),
db: AsyncSession = Depends(get_db),
) -> DCRRead:
return to_read(
@@ -162,6 +171,35 @@ async def delete_call(
)
@router.put("/{dcr_id}/missed", response_model=DCRRead)
async def set_missed_reason(
dcr_id: uuid.UUID,
payload: MissedVisitSet,
request: Request,
current_user: User = Depends(require_role(Role.MR)),
db: AsyncSession = Depends(get_db),
) -> DCRRead:
"""End-of-day report: why a doctor or chemist on the day plan was not visited."""
return to_read(
await DCRService(db).set_missed_reason(dcr_id, payload, current_user, client_ip(request))
)
@router.delete("/{dcr_id}/missed/{missed_id}", response_model=DCRRead)
async def clear_missed_reason(
dcr_id: uuid.UUID,
missed_id: uuid.UUID,
request: Request,
current_user: User = Depends(require_role(Role.MR)),
db: AsyncSession = Depends(get_db),
) -> DCRRead:
return to_read(
await DCRService(db).clear_missed_reason(
dcr_id, missed_id, current_user, client_ip(request)
)
)
# ------------------------------------------------------------- transitions
@@ -169,7 +207,7 @@ async def delete_call(
async def submit(
dcr_id: uuid.UUID,
request: Request,
current_user: User = Depends(require_role(Role.MR)),
current_user: User = Depends(require_role(Role.MR, Role.AM)),
db: AsyncSession = Depends(get_db),
) -> DCRRead:
return to_read(await DCRService(db).submit(dcr_id, current_user, client_ip(request)))