Files
ladill-care/tests/Feature/CareOutpatientCensusTest.php
T
isaaccladandCursor a23604570e
Deploy Ladill Care / deploy (push) Successful in 52s
Allow MAR visit charts for nurses on duty outside their home branch.
Station could list clinic patients on a covered unit while authorizeBranch 404'd the MAR link; duty-unit census now grants access too.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-20 17:43:35 +00:00

379 lines
13 KiB
PHP

<?php
namespace Tests\Feature;
use App\Http\Middleware\EnsurePlatformSession;
use App\Models\Appointment;
use App\Models\Branch;
use App\Models\CareUnit;
use App\Models\Department;
use App\Models\Member;
use App\Models\Organization;
use App\Models\Patient;
use App\Models\User;
use App\Models\Visit;
use App\Services\Care\CareFeatures;
use App\Services\Care\CareUnitCensusService;
use App\Services\Care\RosterService;
use Database\Seeders\AssessmentTemplateSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;
class CareOutpatientCensusTest extends TestCase
{
use RefreshDatabase;
protected User $owner;
protected Organization $organization;
protected Branch $branch;
protected Department $department;
protected CareUnit $opd;
protected function setUp(): void
{
parent::setUp();
$this->withoutMiddleware(EnsurePlatformSession::class);
$this->owner = User::create([
'public_id' => 'opd-census-owner',
'name' => 'Owner',
'email' => 'opd-census-owner@example.com',
]);
$this->organization = Organization::create([
'owner_ref' => $this->owner->public_id,
'name' => 'OPD Clinic',
'slug' => 'opd-clinic',
'settings' => [
'onboarded' => true,
'facility_type' => 'hospital',
'plan' => 'pro',
'plan_expires_at' => now()->addMonth()->toIso8601String(),
'rollout' => [CareFeatures::ASSESSMENTS_ENGINE => true],
],
]);
Member::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'user_ref' => $this->owner->public_id,
'role' => 'hospital_admin',
]);
$this->branch = Branch::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'name' => 'Tema Harbour Clinic',
'is_active' => true,
]);
$this->department = Department::create([
'owner_ref' => $this->owner->public_id,
'branch_id' => $this->branch->id,
'name' => 'Outpatient',
'type' => 'outpatient',
'is_active' => true,
]);
$this->opd = CareUnit::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'department_id' => $this->department->id,
'name' => 'OPD',
'kind' => 'outpatient',
'code' => 'OP',
'is_active' => true,
]);
}
public function test_outpatient_board_includes_checked_in_department_visits_without_manual_place(): void
{
$patient = Patient::create([
'uuid' => (string) Str::uuid(),
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'patient_number' => 'P-OPD-1',
'first_name' => 'Yaw',
'last_name' => 'Adjei',
]);
$visit = Visit::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'patient_id' => $patient->id,
'status' => Visit::STATUS_IN_PROGRESS,
'checked_in_at' => now(),
]);
Appointment::create([
'uuid' => (string) Str::uuid(),
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'department_id' => $this->department->id,
'patient_id' => $patient->id,
'visit_id' => $visit->id,
'type' => Appointment::TYPE_WALK_IN,
'status' => Appointment::STATUS_WAITING,
'checked_in_at' => now(),
]);
$otherDept = Department::create([
'owner_ref' => $this->owner->public_id,
'branch_id' => $this->branch->id,
'name' => 'Dental',
'type' => 'dentistry',
'is_active' => true,
]);
$otherPatient = Patient::create([
'uuid' => (string) Str::uuid(),
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'patient_number' => 'P-OPD-2',
'first_name' => 'Ama',
'last_name' => 'Other',
]);
$otherVisit = Visit::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'patient_id' => $otherPatient->id,
'status' => Visit::STATUS_OPEN,
'checked_in_at' => now(),
]);
Appointment::create([
'uuid' => (string) Str::uuid(),
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'department_id' => $otherDept->id,
'patient_id' => $otherPatient->id,
'visit_id' => $otherVisit->id,
'type' => Appointment::TYPE_WALK_IN,
'status' => Appointment::STATUS_WAITING,
'checked_in_at' => now(),
]);
$census = app(CareUnitCensusService::class);
$visits = $census->visitsForUnit($this->opd, $this->owner->public_id);
$this->assertTrue($census->isClinicUnit($this->opd));
$this->assertCount(1, $visits);
$this->assertTrue($visits->contains('id', $visit->id));
$this->assertFalse($visits->contains('id', $otherVisit->id));
$user = User::create([
'public_id' => 'opd-nurse',
'name' => 'OPD Nurse',
'email' => 'opd-nurse@example.com',
]);
$member = Member::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'user_ref' => $user->public_id,
'role' => 'nurse',
'branch_id' => $this->branch->id,
]);
$roster = app(RosterService::class);
$day = $roster->ensureDefaultShifts($this->organization, $this->owner->public_id)->firstWhere('code', 'day');
$roster->assign(
$this->opd,
$day,
$member,
now()->toDateString(),
$this->owner->public_id,
$this->owner->public_id,
);
$this->actingAs($user)
->get(route('care.nursing.station', ['unit' => $this->opd->id]))
->assertOk()
->assertSee('Yaw Adjei')
->assertSee('In clinic')
->assertDontSee('Ama Other');
$this->actingAs($user)
->get(route('care.care-units.show', $this->opd))
->assertOk()
->assertSee('Yaw Adjei')
->assertSee('in clinic')
->assertSee('Clinic');
}
public function test_nursing_action_auto_places_clinic_visit_onto_opd(): void
{
$this->seed(AssessmentTemplateSeeder::class);
$patient = Patient::create([
'uuid' => (string) Str::uuid(),
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'patient_number' => 'P-OPD-3',
'first_name' => 'Kofi',
'last_name' => 'Mensah',
]);
$visit = Visit::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'patient_id' => $patient->id,
'status' => Visit::STATUS_OPEN,
'checked_in_at' => now(),
]);
Appointment::create([
'uuid' => (string) Str::uuid(),
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'department_id' => $this->department->id,
'patient_id' => $patient->id,
'visit_id' => $visit->id,
'type' => Appointment::TYPE_WALK_IN,
'status' => Appointment::STATUS_CHECKED_IN,
'checked_in_at' => now(),
]);
$this->actingAs($this->owner)
->post(route('care.care-units.vitals.store', [$this->opd, $visit]), [
'pulse' => 78,
'spo2' => 98,
])
->assertRedirect(route('care.care-units.assessments', $this->opd));
$visit->refresh();
$this->assertSame($this->opd->id, (int) $visit->care_unit_id);
$this->assertNotNull($visit->placed_at);
}
public function test_inpatient_unit_still_requires_placement(): void
{
$ward = CareUnit::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'department_id' => $this->department->id,
'name' => 'Medical Ward',
'kind' => 'inpatient',
'is_active' => true,
]);
$patient = Patient::create([
'uuid' => (string) Str::uuid(),
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'patient_number' => 'P-WARD-X',
'first_name' => 'Efua',
'last_name' => 'Boateng',
]);
$visit = Visit::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'patient_id' => $patient->id,
'status' => Visit::STATUS_OPEN,
'checked_in_at' => now(),
]);
Appointment::create([
'uuid' => (string) Str::uuid(),
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'department_id' => $this->department->id,
'patient_id' => $patient->id,
'visit_id' => $visit->id,
'type' => Appointment::TYPE_WALK_IN,
'status' => Appointment::STATUS_WAITING,
'checked_in_at' => now(),
]);
$visits = app(CareUnitCensusService::class)->visitsForUnit($ward, $this->owner->public_id);
$this->assertCount(0, $visits);
}
public function test_nurse_rostered_on_other_branch_unit_can_open_mar(): void
{
$homeBranch = Branch::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'name' => 'Home Branch',
'is_active' => true,
]);
$user = User::create([
'public_id' => 'opd-cover-nurse',
'name' => 'Cover Nurse',
'email' => 'opd-cover-nurse@example.com',
]);
$member = Member::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'user_ref' => $user->public_id,
'role' => 'nurse',
'branch_id' => $homeBranch->id,
]);
$roster = app(RosterService::class);
$day = $roster->ensureDefaultShifts($this->organization, $this->owner->public_id)->firstWhere('code', 'day');
$roster->assign(
$this->opd,
$day,
$member,
now()->toDateString(),
$this->owner->public_id,
$this->owner->public_id,
);
$patient = Patient::create([
'uuid' => (string) Str::uuid(),
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'patient_number' => 'P-OPD-MAR',
'first_name' => 'Mansa',
'last_name' => 'Addo',
]);
$visit = Visit::create([
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'patient_id' => $patient->id,
'status' => Visit::STATUS_IN_PROGRESS,
'checked_in_at' => now(),
]);
Appointment::create([
'uuid' => (string) Str::uuid(),
'owner_ref' => $this->owner->public_id,
'organization_id' => $this->organization->id,
'branch_id' => $this->branch->id,
'department_id' => $this->department->id,
'patient_id' => $patient->id,
'visit_id' => $visit->id,
'type' => Appointment::TYPE_WALK_IN,
'status' => Appointment::STATUS_WAITING,
'checked_in_at' => now(),
]);
// Home-branch gate alone would 404; duty on OPD must allow the chart.
$this->actingAs($user)
->get(route('care.visits.mar', $visit))
->assertOk()
->assertSee('Mansa Addo');
}
}