Allow explicit multi-branch practitioner assignment for telemedicine.
Deploy Ladill Care / deploy (push) Successful in 1m29s

Replace optional “All branches” with required branch checkboxes; doctors may switch only among assigned sites, never org-wide by default.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-07-18 08:50:03 +00:00
co-authored by Cursor
parent d643687335
commit 899b08179e
17 changed files with 478 additions and 103 deletions
@@ -11,6 +11,7 @@ use App\Models\Practitioner;
use App\Services\Care\AuditLogger;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
class PractitionerController extends Controller
@@ -25,7 +26,7 @@ class PractitionerController extends Controller
$practitioners = Practitioner::owned($owner)
->where('organization_id', $organization->id)
->with(['branch', 'department', 'member'])
->with(['branch', 'branches', 'department', 'member'])
->orderBy('name')
->get();
@@ -66,10 +67,12 @@ class PractitionerController extends Controller
? Member::owned($owner)->where('organization_id', $organization->id)->findOrFail($validated['member_id'])
: null;
$branchIds = array_map('intval', $validated['branch_ids']);
$practitioner = Practitioner::create([
'owner_ref' => $owner,
'organization_id' => $organization->id,
'branch_id' => $validated['branch_id'] ?? null,
'branch_id' => $branchIds[0],
'department_id' => $validated['department_id'] ?? null,
'member_id' => $member?->id,
'user_ref' => $member?->user_ref,
@@ -77,6 +80,7 @@ class PractitionerController extends Controller
'specialty' => $validated['specialty'] ?? null,
'is_active' => true,
]);
$practitioner->syncAssignedBranches($branchIds);
AuditLogger::record($owner, 'practitioner.created', $organization->id, $owner, Practitioner::class, $practitioner->id);
@@ -90,6 +94,8 @@ class PractitionerController extends Controller
$organization = $this->organization($request);
$owner = $this->ownerRef($request);
$practitioner->load('branches');
return view('care.admin.practitioners.edit', [
'practitioner' => $practitioner,
'branches' => $this->activeBranches($owner, $organization->id),
@@ -111,8 +117,10 @@ class PractitionerController extends Controller
? Member::owned($owner)->where('organization_id', $organization->id)->findOrFail($validated['member_id'])
: null;
$branchIds = array_map('intval', $validated['branch_ids']);
$practitioner->update([
'branch_id' => $validated['branch_id'] ?? null,
'branch_id' => $branchIds[0],
'department_id' => $validated['department_id'] ?? null,
'member_id' => $member?->id,
'user_ref' => $member?->user_ref,
@@ -120,6 +128,7 @@ class PractitionerController extends Controller
'specialty' => $validated['specialty'] ?? null,
'is_active' => $request->boolean('is_active', true),
]);
$practitioner->syncAssignedBranches($branchIds);
AuditLogger::record($owner, 'practitioner.updated', $organization->id, $owner, Practitioner::class, $practitioner->id);
@@ -141,23 +150,22 @@ class PractitionerController extends Controller
}
/**
* @return array{name: string, specialty?: string|null, branch_id?: int|null, department_id?: int|null, member_id?: int|null}
* @return array{name: string, specialty?: string|null, branch_ids: list<int>, department_id?: int|null, member_id?: int|null}
*/
protected function validated(Request $request, string $owner, int $organizationId): array
{
$validated = $request->validate([
'name' => ['required', 'string', 'max:255'],
'specialty' => ['nullable', 'string', 'max:255'],
'branch_id' => ['nullable', 'integer', 'exists:care_branches,id'],
'branch_ids' => ['required', 'array', 'min:1'],
'branch_ids.*' => [
'integer',
Rule::exists('care_branches', 'id')->where(fn ($q) => $q->where('organization_id', $organizationId)->where('owner_ref', $owner)),
],
'department_id' => ['nullable', 'integer', 'exists:care_departments,id'],
'member_id' => ['nullable', 'integer', 'exists:care_members,id'],
]);
if (! empty($validated['branch_id'])) {
$branch = Branch::owned($owner)->findOrFail($validated['branch_id']);
abort_unless($branch->organization_id === $organizationId, 404);
}
if (! empty($validated['department_id'])) {
$department = Department::owned($owner)->with('branch')->findOrFail($validated['department_id']);
abort_unless($department->branch?->organization_id === $organizationId, 404);