diff --git a/app/Http/Controllers/Care/ConsultationController.php b/app/Http/Controllers/Care/ConsultationController.php index 1ece089..a24837d 100644 --- a/app/Http/Controllers/Care/ConsultationController.php +++ b/app/Http/Controllers/Care/ConsultationController.php @@ -164,7 +164,7 @@ class ConsultationController extends Controller ->orderBy('name') ->get(); - $investigationTypes = InvestigationType::owned($this->ownerRef($request)) + $investigationTypes = InvestigationType::query() ->where('organization_id', $organization->id) ->where('is_active', true) ->orderBy('name') diff --git a/app/Http/Controllers/Care/InvestigationTypeController.php b/app/Http/Controllers/Care/InvestigationTypeController.php index bce2e24..0b3b112 100644 --- a/app/Http/Controllers/Care/InvestigationTypeController.php +++ b/app/Http/Controllers/Care/InvestigationTypeController.php @@ -23,7 +23,7 @@ class InvestigationTypeController extends Controller $this->authorizeAbility($request, 'lab.manage'); $organization = $this->organization($request); - $types = InvestigationType::owned($this->ownerRef($request)) + $types = InvestigationType::query() ->where('organization_id', $organization->id) ->orderBy('category') ->orderBy('name') @@ -83,7 +83,6 @@ class InvestigationTypeController extends Controller protected function authorizeType(Request $request, InvestigationType $type): void { - $this->authorizeOwner($request, $type); abort_unless($type->organization_id === $this->organization($request)->id, 404); } diff --git a/app/Models/Practitioner.php b/app/Models/Practitioner.php index 77ba725..af89a2b 100644 --- a/app/Models/Practitioner.php +++ b/app/Models/Practitioner.php @@ -85,6 +85,13 @@ class Practitioner extends Model */ public function syncAssignedBranches(array $branchIds): void { + if (! \Illuminate\Support\Facades\Schema::hasTable('care_practitioner_branch')) { + $ids = array_values(array_unique(array_filter(array_map('intval', $branchIds)))); + $this->forceFill(['branch_id' => $ids[0] ?? null])->save(); + + return; + } + $ids = array_values(array_unique(array_filter(array_map('intval', $branchIds)))); $this->branches()->sync($ids); $this->forceFill([ diff --git a/app/Services/Care/DemoTenantSeeder.php b/app/Services/Care/DemoTenantSeeder.php index 9cb99b5..e0284ca 100644 --- a/app/Services/Care/DemoTenantSeeder.php +++ b/app/Services/Care/DemoTenantSeeder.php @@ -107,6 +107,10 @@ class DemoTenantSeeder $this->upsertOwnerMember($organization, $ownerRef); $branches = $this->seedBranches($organization, $ownerRef, $volumes); + // Lab catalog early — survives if later clinical seeding fails mid-run. + $investigationTypes = $volumes['paid_modules'] + ? $this->seedInvestigationCatalog($organization, $ownerRef) + : []; $this->seedStaffMembers($organization, $ownerRef, $plan, $branches); $departments = $this->seedDepartments($branches, $ownerRef, $volumes); if (in_array($plan, ['pro', 'enterprise'], true)) { @@ -138,7 +142,11 @@ class DemoTenantSeeder $this->assignAllDemoWaiters($organization, $ownerRef, $branches, $practitioners); $this->renumberWaitingQueuePositions($organization, $ownerRef, $branches); + // Catalog first so a later lab-request failure never leaves Lab → Catalog empty. if ($volumes['paid_modules']) { + if ($investigationTypes === []) { + $investigationTypes = $this->seedInvestigationCatalog($organization, $ownerRef); + } $this->seedPaidModules( $organization, $branches, @@ -146,6 +154,7 @@ class DemoTenantSeeder $patients, $ownerRef, $volumes, + $investigationTypes, ); $this->seedDevices($organization, $branches, $ownerRef); } @@ -434,6 +443,12 @@ class DemoTenantSeeder DB::table('care_patients')->where('owner_ref', $ownerRef)->delete(); DB::table('care_practitioner_schedules')->where('owner_ref', $ownerRef)->delete(); + if (Schema::hasTable('care_practitioner_branch')) { + $practitionerIds = DB::table('care_practitioners')->where('owner_ref', $ownerRef)->pluck('id'); + if ($practitionerIds->isNotEmpty()) { + DB::table('care_practitioner_branch')->whereIn('practitioner_id', $practitionerIds)->delete(); + } + } DB::table('care_practitioners')->where('owner_ref', $ownerRef)->delete(); // Keep the owner member + organization so SSO redirects never see @@ -1214,21 +1229,26 @@ class DemoTenantSeeder } } - private function seedPaidModules( - Organization $organization, - array $branches, - array $practitioners, - array $patients, - string $ownerRef, - array $volumes, - ): void { + /** + * @return list + */ + private function seedInvestigationCatalog(Organization $organization, string $ownerRef): array + { $types = []; foreach ([ ['name' => 'Fasting Blood Glucose', 'code' => 'FBG', 'category' => 'blood', 'unit' => 'mmol/L', 'low' => 3.9, 'high' => 6.1, 'price' => 2500], ['name' => 'Full Blood Count', 'code' => 'FBC', 'category' => 'blood', 'unit' => null, 'low' => null, 'high' => null, 'price' => 4500], ['name' => 'Malaria Parasite', 'code' => 'MP', 'category' => 'blood', 'unit' => null, 'low' => null, 'high' => null, 'price' => 1500], ['name' => 'Urinalysis', 'code' => 'UA', 'category' => 'urine', 'unit' => null, 'low' => null, 'high' => null, 'price' => 2000], - ] as $i => $spec) { + ['name' => 'HbA1c', 'code' => 'HBA1C', 'category' => 'blood', 'unit' => '%', 'low' => null, 'high' => 5.7, 'price' => 6000], + ['name' => 'Lipid Profile', 'code' => 'LIPID', 'category' => 'blood', 'unit' => null, 'low' => null, 'high' => null, 'price' => 5500], + ['name' => 'Liver Function Test', 'code' => 'LFT', 'category' => 'blood', 'unit' => null, 'low' => null, 'high' => null, 'price' => 5000], + ['name' => 'Kidney Function Test', 'code' => 'KFT', 'category' => 'blood', 'unit' => null, 'low' => null, 'high' => null, 'price' => 5000], + ['name' => 'Blood Group & Rh', 'code' => 'BGRH', 'category' => 'blood', 'unit' => null, 'low' => null, 'high' => null, 'price' => 1800], + ['name' => 'Pregnancy Test (Urine)', 'code' => 'HCG', 'category' => 'urine', 'unit' => null, 'low' => null, 'high' => null, 'price' => 1200], + ['name' => 'Stool Routine Examination', 'code' => 'SRE', 'category' => 'stool', 'unit' => null, 'low' => null, 'high' => null, 'price' => 2200], + ['name' => 'Chest X-Ray', 'code' => 'CXR', 'category' => 'xray', 'unit' => null, 'low' => null, 'high' => null, 'price' => 8000], + ] as $spec) { $types[] = InvestigationType::withTrashed()->updateOrCreate( [ 'organization_id' => $organization->id, @@ -1248,6 +1268,28 @@ class DemoTenantSeeder ); } + return $types; + } + + /** + * @param list $branches + * @param list $practitioners + * @param list $patients + * @param list $types + */ + private function seedPaidModules( + Organization $organization, + array $branches, + array $practitioners, + array $patients, + string $ownerRef, + array $volumes, + array $types = [], + ): void { + if ($types === []) { + $types = $this->seedInvestigationCatalog($organization, $ownerRef); + } + $drugs = []; foreach ([ ['name' => 'Amoxicillin 500mg', 'unit' => 'capsule', 'price' => 1500], @@ -1288,6 +1330,10 @@ class DemoTenantSeeder $drugs[] = $drug; } + if ($patients === [] || $practitioners === [] || $types === [] || $branches === []) { + return; + } + for ($i = 0; $i < $volumes['lab_requests']; $i++) { $patient = $patients[$i % count($patients)]; $branch = $branches[$i % count($branches)]; diff --git a/tests/Feature/DemoSeedCommandTest.php b/tests/Feature/DemoSeedCommandTest.php index ade561d..a9739de 100644 --- a/tests/Feature/DemoSeedCommandTest.php +++ b/tests/Feature/DemoSeedCommandTest.php @@ -79,12 +79,20 @@ class DemoSeedCommandTest extends TestCase $this->assertSame(3, Branch::query()->where('owner_ref', $user->public_id)->count()); $this->assertTrue(app(PlanService::class)->canUseProModules($org)); - $this->assertGreaterThan(0, InvestigationType::query()->where('owner_ref', $user->public_id)->count()); + $this->assertGreaterThanOrEqual(12, InvestigationType::query()->where('organization_id', $org->id)->count()); $this->assertGreaterThan(0, InvestigationRequest::query()->where('owner_ref', $user->public_id)->count()); $this->assertGreaterThan(0, Drug::query()->where('owner_ref', $user->public_id)->count()); $this->assertGreaterThan(0, Prescription::query()->where('owner_ref', $user->public_id)->count()); $this->assertGreaterThan(0, Bill::query()->where('owner_ref', $user->public_id)->count()); + $this->actingAs($user) + ->withoutMiddleware(\App\Http\Middleware\EnsurePlatformSession::class) + ->get(route('care.lab.catalog.index')) + ->assertOk() + ->assertSee('Full Blood Count') + ->assertSee('Chest X-Ray') + ->assertDontSee('No tests in catalog.'); + $catalog = array_keys(config('care.specialty_modules', [])); $this->assertNotEmpty($catalog); foreach ($catalog as $key) {