--
This commit is contained in:
@@ -4,7 +4,7 @@ from datetime import date
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.common.enums import DCRStatus
|
||||
from app.common.enums import DCRStatus, ReportType
|
||||
from app.common.repository import BaseRepository
|
||||
from app.reports.models import ChemistCall, DailyCallReport, DoctorCall
|
||||
|
||||
@@ -19,8 +19,17 @@ class DCRRepository(BaseRepository[DailyCallReport]):
|
||||
.options(
|
||||
selectinload(DailyCallReport.doctor_calls).selectinload(DoctorCall.doctor),
|
||||
selectinload(DailyCallReport.chemist_calls).selectinload(ChemistCall.chemist),
|
||||
selectinload(DailyCallReport.missed_visits),
|
||||
selectinload(DailyCallReport.mr),
|
||||
selectinload(DailyCallReport.headquarters),
|
||||
# The plan's visits are the checklist an end-of-day report is measured against.
|
||||
selectinload(DailyCallReport.plan)
|
||||
.selectinload(DailyCallReport.doctor_calls)
|
||||
.selectinload(DoctorCall.doctor),
|
||||
selectinload(DailyCallReport.plan)
|
||||
.selectinload(DailyCallReport.chemist_calls)
|
||||
.selectinload(ChemistCall.chemist),
|
||||
selectinload(DailyCallReport.actual),
|
||||
)
|
||||
# The session keeps objects after commit (expire_on_commit=False), so without this the
|
||||
# report comes back with the call lists it had *before* an add or remove.
|
||||
@@ -29,12 +38,13 @@ class DCRRepository(BaseRepository[DailyCallReport]):
|
||||
return (await self.db.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
async def get_for_date(
|
||||
self, mr_user_id: uuid.UUID, report_date: date
|
||||
self, mr_user_id: uuid.UUID, report_date: date, report_type: ReportType
|
||||
) -> DailyCallReport | None:
|
||||
"""V1 pre-check. The database unique constraint is the real guarantee."""
|
||||
stmt = select(DailyCallReport).where(
|
||||
DailyCallReport.mr_user_id == mr_user_id,
|
||||
DailyCallReport.report_date == report_date,
|
||||
DailyCallReport.report_type == report_type,
|
||||
)
|
||||
return (await self.db.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
@@ -46,6 +56,7 @@ class DCRRepository(BaseRepository[DailyCallReport]):
|
||||
status: DCRStatus | None,
|
||||
date_from: date | None,
|
||||
date_to: date | None,
|
||||
report_type: ReportType | None = None,
|
||||
):
|
||||
stmt = select(DailyCallReport)
|
||||
if mr_user_id is not None:
|
||||
@@ -55,6 +66,12 @@ class DCRRepository(BaseRepository[DailyCallReport]):
|
||||
stmt = stmt.where(DailyCallReport.headquarters_id.in_(hq_ids or [None]))
|
||||
if status is not None:
|
||||
stmt = stmt.where(DailyCallReport.status == status)
|
||||
if report_type is not None:
|
||||
stmt = stmt.where(DailyCallReport.report_type == report_type)
|
||||
else:
|
||||
# Without a type, lists and counts cover the MR approval workflow; an Area
|
||||
# Manager's own SELF reports are asked for by type.
|
||||
stmt = stmt.where(DailyCallReport.report_type != ReportType.SELF)
|
||||
if date_from is not None:
|
||||
stmt = stmt.where(DailyCallReport.report_date >= date_from)
|
||||
if date_to is not None:
|
||||
@@ -69,13 +86,14 @@ class DCRRepository(BaseRepository[DailyCallReport]):
|
||||
status: DCRStatus | None = None,
|
||||
date_from: date | None = None,
|
||||
date_to: date | None = None,
|
||||
report_type: ReportType | None = None,
|
||||
limit: int = 50,
|
||||
offset: int = 0,
|
||||
) -> list[DailyCallReport]:
|
||||
stmt = (
|
||||
self._scoped(
|
||||
mr_user_id=mr_user_id, hq_ids=hq_ids, status=status,
|
||||
date_from=date_from, date_to=date_to,
|
||||
date_from=date_from, date_to=date_to, report_type=report_type,
|
||||
)
|
||||
.options(
|
||||
selectinload(DailyCallReport.doctor_calls),
|
||||
|
||||
Reference in New Issue
Block a user