authorizeAbility($request, 'admin.members.view'); $organization = $this->organization($request); $members = Member::owned($this->ownerRef($request)) ->where('organization_id', $organization->id) ->with('branch') ->orderBy('created_at') ->get(); return view('qms.admin.members.index', [ 'members' => $members, 'organization' => $organization, 'roles' => config('qms.roles'), ]); } public function create(Request $request): View { $this->authorizeAbility($request, 'admin.members.manage'); $organization = $this->organization($request); $branches = Branch::owned($this->ownerRef($request)) ->where('organization_id', $organization->id) ->where('is_active', true) ->orderBy('name') ->get(); return view('qms.admin.members.create', [ 'organization' => $organization, 'branches' => $branches, 'roles' => config('qms.roles'), ]); } public function store(Request $request): RedirectResponse { $this->authorizeAbility($request, 'admin.members.manage'); $organization = $this->organization($request); $owner = $this->ownerRef($request); $validated = $request->validate([ 'user_ref' => ['required', 'string', 'max:255'], 'role' => ['required', 'string', 'in:'.implode(',', array_keys(config('qms.roles')))], 'branch_id' => ['nullable', 'integer', 'exists:queue_branches,id'], ]); $member = Member::updateOrCreate( [ 'organization_id' => $organization->id, 'user_ref' => $validated['user_ref'], ], [ 'owner_ref' => $owner, 'role' => $validated['role'], 'branch_id' => $validated['branch_id'] ?? null, ], ); AuditLogger::record($owner, 'member.created', $organization->id, $owner, Member::class, $member->id); return redirect()->route('qms.members.index')->with('success', 'Member saved.'); } public function destroy(Request $request, Member $member): RedirectResponse { $this->authorizeAbility($request, 'admin.members.manage'); $this->authorizeOwner($request, $member); abort_if($member->user_ref === $this->ownerRef($request), 422, 'You cannot remove yourself.'); $memberId = $member->id; $organizationId = $member->organization_id; $member->delete(); AuditLogger::record($this->ownerRef($request), 'member.deleted', $organizationId, $this->ownerRef($request), Member::class, $memberId); return redirect()->route('qms.members.index')->with('success', 'Member removed.'); } }