authorizeAbility($request, 'displays.view'); $owner = $this->ownerRef($request); $organization = $this->organization($request); $member = app(OrganizationResolver::class)->memberFor($request->user()); $permissions = app(QmsPermissions::class); $devices = Device::owned($owner) ->where('organization_id', $organization->id) ->with('branch') ->orderBy('name') ->paginate(20); return view('qms.devices.index', [ 'devices' => $devices, 'organization' => $organization, 'deviceTypes' => config('qms.device_types'), 'canManage' => $permissions->can($member, 'displays.manage'), ]); } public function create(Request $request): View { $this->authorizeAbility($request, 'displays.manage'); $owner = $this->ownerRef($request); $organization = $this->organization($request); $branches = Branch::owned($owner)->where('organization_id', $organization->id)->orderBy('name')->get(); return view('qms.devices.create', [ 'organization' => $organization, 'branches' => $branches, 'deviceTypes' => config('qms.device_types'), ]); } public function store(Request $request): RedirectResponse { $this->authorizeAbility($request, 'displays.manage'); $owner = $this->ownerRef($request); $organization = $this->organization($request); $validated = $request->validate([ 'name' => ['required', 'string', 'max:255'], 'type' => ['required', 'string', 'in:'.implode(',', array_keys(config('qms.device_types')))], 'branch_id' => ['nullable', 'exists:queue_branches,id'], ]); $device = Device::create([ 'owner_ref' => $owner, 'organization_id' => $organization->id, 'branch_id' => $validated['branch_id'] ?? null, 'name' => $validated['name'], 'type' => $validated['type'], 'status' => 'offline', ]); $token = $this->devices->generateToken($device); AuditLogger::record($owner, 'device.created', $organization->id, $owner, Device::class, $device->id); return redirect()->route('qms.devices.index') ->with('success', 'Device registered. Token: '.$token); } public function regenerateToken(Request $request, Device $device): RedirectResponse { $this->authorizeAbility($request, 'displays.manage'); $this->authorizeOwner($request, $device); $token = $this->devices->generateToken($device); return back()->with('success', 'New device token: '.$token); } }