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
@@ -58,12 +58,20 @@ trait ScopesToAccount
protected function scopeToBranch(Request $request, Builder $query, string $column = 'branch_id'): Builder
{
$branchId = app(OrganizationResolver::class)->branchScope($this->member($request));
$allowed = app(OrganizationResolver::class)->allowedBranchIds($this->member($request));
if ($branchId !== null) {
$query->where($column, $branchId);
if ($allowed !== null) {
$query->whereIn($column, $allowed !== [] ? $allowed : [0]);
}
return $query;
}
protected function authorizeBranch(Request $request, ?int $branchId): void
{
abort_unless(
app(OrganizationResolver::class)->mayAccessBranch($this->member($request), $branchId),
404,
);
}
}
+17 -2
View File
@@ -12,6 +12,7 @@ use App\Services\Identity\IdentityTeamClient;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
class MemberController extends Controller
@@ -83,12 +84,22 @@ class MemberController extends Controller
$validated = $request->validate([
'email' => ['required', 'email', 'max:255'],
'role' => ['required', 'string', 'in:'.implode(',', array_keys(config('care.roles')))],
'branch_id' => ['nullable', 'integer', 'exists:care_branches,id'],
'branch_id' => [
Rule::requiredIf(fn () => $request->input('role') === 'doctor'),
'nullable',
'integer',
'exists:care_branches,id',
],
'create_practitioner' => ['sometimes', 'boolean'],
'practitioner_name' => ['nullable', 'string', 'max:255'],
'specialty' => ['nullable', 'string', 'max:255'],
]);
if (! empty($validated['branch_id'])) {
$branch = Branch::owned($owner)->findOrFail($validated['branch_id']);
abort_unless($branch->organization_id === $organization->id, 404);
}
$email = strtolower(trim($validated['email']));
if ($email === strtolower((string) $request->user()->email)) {
@@ -125,7 +136,7 @@ class MemberController extends Controller
$createPractitioner = $request->boolean('create_practitioner', $validated['role'] === 'doctor');
if ($createPractitioner) {
$name = trim((string) ($validated['practitioner_name'] ?? '')) ?: Str::headline(Str::before($email, '@'));
Practitioner::query()->firstOrCreate(
$practitioner = Practitioner::query()->firstOrCreate(
[
'organization_id' => $organization->id,
'member_id' => $member->id,
@@ -139,6 +150,10 @@ class MemberController extends Controller
'is_active' => true,
],
);
if (! empty($validated['branch_id'])) {
$practitioner->syncAssignedBranches([(int) $validated['branch_id']]);
}
}
return redirect()->route('care.members.index')->with('success', 'Invitation sent to '.$email.'.');
@@ -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);
@@ -240,10 +240,6 @@ class QueueController extends Controller
{
$this->authorizeOwner($request, $appointment);
abort_unless($appointment->organization_id === $this->organization($request)->id, 404);
$branchId = app(OrganizationResolver::class)->branchScope($this->member($request));
if ($branchId !== null && $appointment->branch_id !== $branchId) {
abort(404);
}
$this->authorizeBranch($request, (int) $appointment->branch_id);
}
}