245 lines
14 KiB
Python
245 lines
14 KiB
Python
"""Morning day plan → Area Manager approval → end-of-day report against it (rules P1-P5)."""
|
|
|
|
from datetime import date
|
|
|
|
from tests.e2e.test_full_approval_flow import _create_masters
|
|
|
|
API = "/api/v1"
|
|
TODAY = date.today().isoformat()
|
|
|
|
|
|
async def _plan(client, auth, doctors, chemists, *, approve=True):
|
|
"""A plan for today with the given doctors/chemists, submitted (and by default approved)."""
|
|
resp = await client.post(
|
|
f"{API}/dcr", headers=auth["mr1"],
|
|
json={
|
|
"report_date": TODAY, "report_type": "PLAN",
|
|
"doctor_calls": [{"doctor_id": d["id"], "visit_time": "23:00:00"} for d in doctors],
|
|
"chemist_calls": [{"chemist_id": c["id"], "visit_time": "23:30:00"} for c in chemists],
|
|
},
|
|
)
|
|
assert resp.status_code == 201, resp.text
|
|
plan_id = resp.json()["id"]
|
|
assert (await client.post(f"{API}/dcr/{plan_id}/submit", headers=auth["mr1"])).status_code == 200
|
|
if approve:
|
|
approved = await client.post(f"{API}/dcr/{plan_id}/approve", headers=auth["am1"], json={})
|
|
assert approved.status_code == 200, approved.text
|
|
return plan_id
|
|
|
|
|
|
async def _actual(client, auth):
|
|
resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "ACTUAL"})
|
|
assert resp.status_code == 201, resp.text
|
|
return resp.json()
|
|
|
|
|
|
async def _visit_doctor(client, auth, dcr_id, doctor):
|
|
return await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctor["id"], "visit_time": "09:30:00", "call_outcome": "MET"},
|
|
)
|
|
|
|
|
|
class TestDayPlan:
|
|
async def test_a_plan_has_planned_times_and_no_outcomes(self, client, auth, org):
|
|
doctors, chemists = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1)
|
|
resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "PLAN"})
|
|
assert resp.status_code == 201, resp.text
|
|
plan = resp.json()
|
|
assert plan["report_type"] == "PLAN" and plan["plan_id"] is None
|
|
# P1 — a planned time later than now is fine, and an outcome is not stored.
|
|
added = await client.post(
|
|
f"{API}/dcr/{plan['id']}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "visit_time": "23:59:00", "call_outcome": "MET"},
|
|
)
|
|
assert added.status_code == 201, added.text
|
|
assert added.json()["doctor_calls"][0]["call_outcome"] is None
|
|
|
|
async def test_an_empty_plan_cannot_be_submitted(self, client, auth, org):
|
|
resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "PLAN"})
|
|
submit = await client.post(f"{API}/dcr/{resp.json()['id']}/submit", headers=auth["mr1"])
|
|
assert submit.status_code == 422
|
|
assert "at least one" in submit.json()["error"]["message"]
|
|
|
|
async def test_one_plan_and_one_end_of_day_report_per_day(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
await _plan(client, auth, doctors[:1], [])
|
|
again = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "PLAN"})
|
|
assert again.status_code == 409
|
|
await _actual(client, auth)
|
|
twice = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "ACTUAL"})
|
|
assert twice.status_code == 409
|
|
|
|
async def test_the_area_manager_can_return_a_plan(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
plan_id = await _plan(client, auth, doctors, [], approve=False)
|
|
resp = await client.post(
|
|
f"{API}/dcr/{plan_id}/return", headers=auth["am1"], json={"reason": "Add the chemists on MG Road."}
|
|
)
|
|
assert resp.status_code == 200 and resp.json()["status"] == "RETURNED"
|
|
|
|
async def test_reports_can_be_listed_by_type(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
plan_id = await _plan(client, auth, doctors, [])
|
|
actual = await _actual(client, auth)
|
|
plans = (await client.get(f"{API}/dcr?type=PLAN", headers=auth["mr1"])).json()
|
|
actuals = (await client.get(f"{API}/dcr?type=ACTUAL", headers=auth["mr1"])).json()
|
|
assert [r["id"] for r in plans] == [plan_id]
|
|
assert [r["id"] for r in actuals] == [actual["id"]]
|
|
|
|
|
|
class TestEndOfDayNeedsAnApprovedPlan:
|
|
async def test_without_a_plan_it_is_refused(self, client, auth, org):
|
|
resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "ACTUAL"})
|
|
assert resp.status_code == 422
|
|
assert "Daily Call Report (Plan)" in resp.json()["error"]["message"]
|
|
|
|
async def test_while_the_plan_awaits_approval_it_is_refused(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
await _plan(client, auth, doctors, [], approve=False)
|
|
resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "ACTUAL"})
|
|
assert resp.status_code == 422
|
|
assert "approved by the Area Manager" in resp.json()["error"]["message"]
|
|
|
|
async def test_once_approved_it_is_linked_to_the_plan(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
plan_id = await _plan(client, auth, doctors, [])
|
|
actual = await _actual(client, auth)
|
|
assert actual["plan_id"] == plan_id
|
|
plan = (await client.get(f"{API}/dcr/{plan_id}", headers=auth["mr1"])).json()
|
|
assert plan["actual_report_id"] == actual["id"]
|
|
|
|
|
|
class TestPlanVersusActual:
|
|
async def test_every_planned_visit_must_be_made_or_explained(self, client, auth, org):
|
|
doctors, chemists = await _create_masters(client, auth, org, n_doctors=3, n_chemists=1)
|
|
await _plan(client, auth, doctors[:2], chemists)
|
|
dcr = await _actual(client, auth)
|
|
assert {p["status"] for p in dcr["planned_visits"]} == {"PENDING"}
|
|
assert len(dcr["planned_visits"]) == 3
|
|
|
|
# P5 — an unplanned visit is allowed; it is not on the checklist.
|
|
assert (await _visit_doctor(client, auth, dcr["id"], doctors[2])).status_code == 201
|
|
body = (await _visit_doctor(client, auth, dcr["id"], doctors[0])).json()
|
|
by_name = {p["name"]: p["status"] for p in body["planned_visits"]}
|
|
assert by_name[doctors[0]["name"]] == "VISITED"
|
|
|
|
# P3 — the doctor and chemist still pending block the submit, by name.
|
|
blocked = await client.post(f"{API}/dcr/{dcr['id']}/submit", headers=auth["mr1"])
|
|
assert blocked.status_code == 422
|
|
assert set(blocked.json()["error"]["details"]["pending"]) == {doctors[1]["name"], chemists[0]["name"]}
|
|
|
|
await client.put(f"{API}/dcr/{dcr['id']}/missed", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[1]["id"], "reason": "Clinic closed for the day"})
|
|
explained = await client.put(f"{API}/dcr/{dcr['id']}/missed", headers=auth["mr1"],
|
|
json={"chemist_id": chemists[0]["id"], "reason": "Shop shut"})
|
|
assert explained.status_code == 200, explained.text
|
|
missed = [p for p in explained.json()["planned_visits"] if p["status"] == "MISSED"]
|
|
assert {p["missed_reason"] for p in missed} == {"Clinic closed for the day", "Shop shut"}
|
|
|
|
submit = await client.post(f"{API}/dcr/{dcr['id']}/submit", headers=auth["mr1"])
|
|
assert submit.status_code == 200, submit.text
|
|
approve = await client.post(f"{API}/dcr/{dcr['id']}/approve", headers=auth["am1"], json={})
|
|
assert approve.status_code == 200, approve.text
|
|
pdf = await client.get(f"{API}/dcr/{dcr['id']}/pdf", headers=auth["mr1"])
|
|
assert pdf.status_code == 200 and pdf.content.startswith(b"%PDF-")
|
|
|
|
async def test_recording_the_visit_later_replaces_the_missed_note(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
await _plan(client, auth, doctors, [])
|
|
dcr = await _actual(client, auth)
|
|
await client.put(f"{API}/dcr/{dcr['id']}/missed", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "reason": "Doctor on leave"})
|
|
body = (await _visit_doctor(client, auth, dcr["id"], doctors[0])).json()
|
|
assert body["planned_visits"][0]["status"] == "VISITED"
|
|
assert body["planned_visits"][0]["missed_reason"] is None
|
|
# ...and removing the visit brings the line back to pending, not to the old note.
|
|
call_id = body["doctor_calls"][0]["id"]
|
|
removed = (await client.delete(f"{API}/dcr/{dcr['id']}/calls/{call_id}", headers=auth["mr1"])).json()
|
|
assert removed["planned_visits"][0]["status"] == "PENDING"
|
|
|
|
async def test_missed_notes_are_validated(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1)
|
|
plan_id = await _plan(client, auth, doctors[:1], [])
|
|
dcr = await _actual(client, auth)
|
|
url = f"{API}/dcr/{dcr['id']}/missed"
|
|
# Not on the plan.
|
|
off_plan = await client.put(url, headers=auth["mr1"], json={"doctor_id": doctors[1]["id"], "reason": "Busy day"})
|
|
assert off_plan.status_code == 422
|
|
# Exactly one target, and a real reason.
|
|
assert (await client.put(url, headers=auth["mr1"], json={"reason": "Busy day"})).status_code == 422
|
|
assert (await client.put(url, headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "reason": " "})).status_code == 422
|
|
# Already visited.
|
|
await _visit_doctor(client, auth, dcr["id"], doctors[0])
|
|
visited = await client.put(url, headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "reason": "Busy day"})
|
|
assert visited.status_code == 409
|
|
# Not on a plan at all.
|
|
plan_note = await client.put(f"{API}/dcr/{plan_id}/missed", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "reason": "Busy day"})
|
|
assert plan_note.status_code in (409, 422)
|
|
|
|
async def test_a_note_can_be_reworded_and_cleared(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
await _plan(client, auth, doctors, [])
|
|
dcr = await _actual(client, auth)
|
|
url = f"{API}/dcr/{dcr['id']}/missed"
|
|
await client.put(url, headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "reason": "Doctor on leave"})
|
|
body = (await client.put(url, headers=auth["mr1"], json={"doctor_id": doctors[0]["id"], "reason": "On leave till Monday"})).json()
|
|
line = body["planned_visits"][0]
|
|
assert line["missed_reason"] == "On leave till Monday"
|
|
cleared = await client.delete(f"{url}/{line['missed_visit_id']}", headers=auth["mr1"])
|
|
assert cleared.status_code == 200 and cleared.json()["planned_visits"][0]["status"] == "PENDING"
|
|
|
|
async def test_an_end_of_day_visit_needs_an_outcome(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
await _plan(client, auth, doctors, [])
|
|
dcr = await _actual(client, auth)
|
|
resp = await client.post(
|
|
f"{API}/dcr/{dcr['id']}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00"},
|
|
)
|
|
assert resp.status_code == 422
|
|
assert "call_outcome" in resp.json()["error"]["message"]
|
|
|
|
async def test_only_the_owning_mr_writes_missed_notes(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
await _plan(client, auth, doctors, [])
|
|
dcr = await _actual(client, auth)
|
|
body = {"doctor_id": doctors[0]["id"], "reason": "Doctor on leave"}
|
|
assert (await client.put(f"{API}/dcr/{dcr['id']}/missed", headers=auth["am1"], json=body)).status_code == 403
|
|
assert (await client.put(f"{API}/dcr/{dcr['id']}/missed", headers=auth["mr2"], json=body)).status_code == 403
|
|
|
|
|
|
class TestPlanPdf:
|
|
async def test_an_approved_plan_downloads_as_a_daily_call_plan(self, client, auth, org, db):
|
|
from app.pdf.service import PDFService
|
|
from app.reports.repository import DCRRepository
|
|
|
|
doctors, chemists = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
plan_id = await _plan(client, auth, doctors, chemists)
|
|
pdf = await client.get(f"{API}/dcr/{plan_id}/pdf", headers=auth["mr1"])
|
|
assert pdf.status_code == 200 and pdf.content.startswith(b"%PDF-")
|
|
assert 'filename="PLAN-MR001-' in pdf.headers["content-disposition"]
|
|
|
|
import uuid
|
|
|
|
context = await PDFService(db)._context(await DCRRepository(db).get_full(uuid.UUID(plan_id)))
|
|
assert context["title"] == "DAILY CALL REPORT (PLAN)"
|
|
assert "PLANNED TIME" in context["grids"]["doctors"]["headers"]
|
|
assert context["grids"]["doctors"]["rows"][0]["cells"][3] == "23:00"
|
|
|
|
|
|
class TestPlanningAhead:
|
|
async def test_a_plan_may_be_made_up_to_two_days_ahead_but_a_done_report_never(self, client, auth, org):
|
|
from datetime import timedelta
|
|
|
|
from app.common.utils import local_today
|
|
|
|
day = lambda n: (local_today() + timedelta(days=n)).isoformat() # noqa: E731
|
|
made = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": day(2), "report_type": "PLAN"})
|
|
assert made.status_code == 201, made.text
|
|
too_far = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": day(3), "report_type": "PLAN"})
|
|
assert too_far.status_code == 422
|
|
done = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": day(1), "report_type": "ACTUAL"})
|
|
assert done.status_code == 422
|