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
+71 -8
View File
@@ -1,22 +1,49 @@
import uuid
from datetime import date, datetime, time
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field
from pydantic import BaseModel, ConfigDict, Field, field_validator
from app.common.enums import CallOutcome, ConversionStatus, DCRStatus, Role
from app.common.enums import CallOutcome, ConversionStatus, DCRStatus, ReportType, Role
from app.config.settings import settings
#: How many promoted (or sample) products one doctor visit may name.
MAX_PRODUCTS_PER_VISIT = 10
def _product_list(value: str | list[str] | None) -> list[str]:
"""Trimmed, de-duplicated product names. A single string is still accepted."""
items = [value] if isinstance(value, str) else value or []
names: list[str] = []
for item in items:
if not isinstance(item, str):
raise ValueError("Product names must be text")
name = item.strip()
if len(name) > 200:
raise ValueError("A product name must be 200 characters or fewer")
if name and name.casefold() not in {n.casefold() for n in names}:
names.append(name)
if len(names) > MAX_PRODUCTS_PER_VISIT:
raise ValueError(f"Choose at most {MAX_PRODUCTS_PER_VISIT} products")
return names
class DoctorCallCreate(BaseModel):
doctor_id: uuid.UUID
visit_time: time
call_outcome: CallOutcome
#: Required on an end-of-day report; ignored on a plan (a planned visit has no outcome).
call_outcome: CallOutcome | None = None
#: V7 — accepted only when the doctor is flagged as a core doctor.
conversion_status: ConversionStatus | None = None
product_promote: str | None = Field(default=None, max_length=200)
product_sample: str | None = Field(default=None, max_length=200)
product_promote: list[str] = Field(default_factory=list)
product_sample: list[str] = Field(default_factory=list)
gift: str | None = Field(default=None, max_length=200)
remarks: str | None = Field(default=None, max_length=2000)
_products = field_validator("product_promote", "product_sample", mode="before")(
_product_list
)
class ChemistCallCreate(BaseModel):
chemist_id: uuid.UUID
@@ -34,10 +61,11 @@ class DoctorCallRead(BaseModel):
doctor_speciality: str | None
is_core_doctor: bool
visit_time: time
call_outcome: CallOutcome
call_outcome: CallOutcome | None
conversion_status: ConversionStatus | None
product_promote: str | None
product_sample: str | None
product_promote: list[str]
product_sample: list[str]
gift: str | None = None
remarks: str | None
@@ -54,6 +82,9 @@ class ChemistCallRead(BaseModel):
class DCRCreate(BaseModel):
report_date: date
report_type: ReportType = ReportType.ACTUAL
#: SELF reports only: which of the Area Manager's headquarters the visits were in.
headquarters_id: uuid.UUID | None = None
mr_remarks: str | None = Field(default=None, max_length=2000)
doctor_calls: list[DoctorCallCreate] = Field(default_factory=list)
chemist_calls: list[ChemistCallCreate] = Field(default_factory=list)
@@ -65,6 +96,31 @@ class DCRUpdate(BaseModel):
order_value: str | None = Field(default=None, max_length=100)
class MissedVisitSet(BaseModel):
"""Why one planned doctor OR chemist was not visited (end-of-day report only)."""
doctor_id: uuid.UUID | None = None
chemist_id: uuid.UUID | None = None
reason: str = Field(min_length=3, max_length=500)
class PlannedVisitRead(BaseModel):
"""One line of the approved plan, checked against the end-of-day report.
VISITED — the MR recorded a visit; MISSED — the MR explained why not;
PENDING — neither yet (the report cannot be submitted while any remain).
"""
kind: Literal["DOCTOR", "CHEMIST"]
target_id: uuid.UUID
name: str
is_core_doctor: bool
planned_time: time
status: Literal["VISITED", "MISSED", "PENDING"]
missed_visit_id: uuid.UUID | None
missed_reason: str | None
class ReturnRequest(BaseModel):
"""V8 — a manager must say WHY. This reason is what makes the audit trail useful."""
@@ -82,6 +138,7 @@ class DCRListItem(BaseModel):
id: uuid.UUID
report_date: date
report_type: ReportType
status: DCRStatus
mr_user_id: uuid.UUID
mr_name: str
@@ -104,8 +161,12 @@ class DCRRead(BaseModel):
headquarters_id: uuid.UUID
headquarters_name: str
report_date: date
report_type: ReportType
status: DCRStatus
is_locked: bool
#: End-of-day report → its plan; plan → its end-of-day report, once started.
plan_id: uuid.UUID | None
actual_report_id: uuid.UUID | None
mr_remarks: str | None
stockist_visit: str | None
order_value: str | None
@@ -115,6 +176,8 @@ class DCRRead(BaseModel):
approved_at: datetime | None
doctor_calls: list[DoctorCallRead]
chemist_calls: list[ChemistCallRead]
#: End-of-day report only: the approved plan, line by line, with what became of it.
planned_visits: list[PlannedVisitRead]
class StatusCounts(BaseModel):