Files
ladill-care/app/Http/Controllers/Care/SpecialtyModuleController.php
T
isaaccladandCursor bb4dbee81a
Deploy Ladill Care / deploy (push) Successful in 1m10s
Restrict specialty modules to doctors assigned to that specialty.
Nav and pages require a linked practitioner desk in the module department; demo seeds specialty doctors and stops overwriting their assigned waiters.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-18 08:37:06 +00:00

130 lines
4.9 KiB
PHP

<?php
namespace App\Http\Controllers\Care;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
use App\Models\Appointment;
use App\Models\Department;
use App\Services\Care\CareQueueBridge;
use App\Services\Care\OrganizationResolver;
use App\Services\Care\SpecialtyModuleService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class SpecialtyModuleController extends Controller
{
use ScopesToAccount;
public function show(
Request $request,
string $module,
SpecialtyModuleService $modules,
CareQueueBridge $queueBridge,
): View {
$definition = $modules->definition($module);
abort_unless($definition, 404);
$organization = $this->organization($request);
$member = $this->member($request);
abort_unless($modules->memberCanAccess($organization, $member, $module), 403);
$owner = $this->ownerRef($request);
$resolver = app(OrganizationResolver::class);
$branchScope = $resolver->branchScope($member);
$practitionerScope = $resolver->practitionerScope($organization, $member);
$departments = Department::owned($owner)
->where('type', $definition['department_type'] ?? 'general')
->where('is_active', true)
->when($branchScope, fn ($q) => $q->where('branch_id', $branchScope))
->with('branch')
->orderBy('name')
->get();
$departmentIds = $departments->pluck('id')->all();
$waiting = Appointment::owned($owner)
->where('organization_id', $organization->id)
->whereIn('status', [Appointment::STATUS_WAITING, Appointment::STATUS_CHECKED_IN])
->when($departmentIds !== [], fn ($q) => $q->whereIn('department_id', $departmentIds))
->when($departmentIds === [], fn ($q) => $q->whereRaw('1 = 0'))
->when($branchScope, fn ($q) => $q->where('branch_id', $branchScope))
->when(
$practitionerScope !== null,
fn ($q) => $q->whereIn('practitioner_id', $practitionerScope ?: [0]),
)
->with(['patient', 'practitioner', 'department'])
->orderBy('queue_position')
->orderBy('checked_in_at')
->limit(25)
->get();
$branchId = (int) ($branchScope ?: $departments->first()?->branch_id);
if ($practitionerScope !== null && $practitionerScope !== []) {
$pracBranch = (int) \App\Models\Practitioner::owned($owner)
->whereKey($practitionerScope[0])
->value('branch_id');
if ($pracBranch > 0) {
$branchId = $pracBranch;
}
}
return view('care.specialty.show', [
'organization' => $organization,
'moduleKey' => $module,
'definition' => $definition,
'departments' => $departments,
'waiting' => $waiting,
'queueStubs' => $modules->queueStubsFor($organization, $module),
'branchId' => $branchId,
'queueIntegration' => $branchId
? $queueBridge->listMeta($organization, $module, $branchId)
: ['enabled' => false],
]);
}
public function callNext(
Request $request,
string $module,
SpecialtyModuleService $modules,
CareQueueBridge $queueBridge,
): RedirectResponse {
$definition = $modules->definition($module);
abort_unless($definition, 404);
$organization = $this->organization($request);
$member = $this->member($request);
abort_unless($modules->memberCanAccess($organization, $member, $module), 403);
abort_unless($queueBridge->isEnabled($organization), 404);
$resolver = app(OrganizationResolver::class);
$branchScope = $resolver->branchScope($member);
$practitionerScope = $resolver->practitionerScope($organization, $member);
$branchId = (int) ($request->input('branch_id') ?: $branchScope);
$practitionerId = null;
if ($practitionerScope !== null) {
abort_unless($practitionerScope !== [], 422);
$practitionerId = $practitionerScope[0];
$pracBranch = (int) \App\Models\Practitioner::owned($this->ownerRef($request))
->whereKey($practitionerId)
->value('branch_id');
if ($pracBranch > 0) {
$branchId = $pracBranch;
}
}
abort_unless($branchId > 0, 422);
$result = $queueBridge->callNext($organization, $module, $branchId, $member, $practitionerId);
$ticket = $result['ticket'] ?? null;
if (! $ticket) {
return back()->with('info', 'No patients waiting in this specialty queue.');
}
return back()->with('success', 'Called '.$ticket['ticket_number'].'.');
}
}