findOrFail((int) $payload['parent_id']); $code = trim((string) $payload['code']); $name = trim((string) $payload['name']); $status = (int) ($payload['status'] ?? 1); if ($code === '' || $name === '') { throw ValidationException::withMessages([ 'code' => ['required'], 'name' => ['required'], ]); } if (AgentNode::query()->where('admin_site_id', $parent->admin_site_id)->where('code', $code)->exists()) { throw ValidationException::withMessages([ 'code' => ['unique'], ]); } return DB::transaction(function () use ($actor, $parent, $code, $name, $status): AgentNode { $node = AgentNode::query()->create([ 'admin_site_id' => $parent->admin_site_id, 'parent_id' => $parent->id, 'path' => '/', 'depth' => (int) $parent->depth + 1, 'code' => $code, 'name' => $name, 'status' => $status === 0 ? 0 : 1, 'created_by' => $actor->id, 'extra_json' => null, ]); $node->path = (string) $parent->path.$node->id.'/'; $node->save(); return $node->fresh(['adminSite']); }); } /** * @param array{name?: string, status?: int} $payload */ public function update(AgentNode $node, array $payload): AgentNode { if (array_key_exists('name', $payload)) { $name = trim((string) $payload['name']); if ($name !== '') { $node->name = $name; } } if (array_key_exists('status', $payload)) { $node->status = (int) $payload['status'] === 0 ? 0 : 1; } $node->save(); return $node->fresh(['adminSite']); } public function destroy(AgentNode $node): void { DB::transaction(static function () use ($node): void { AdminRole::query() ->where('owner_agent_id', $node->id) ->whereNotNull('delegated_from_role_id') ->each(static fn (AdminRole $role): bool => (bool) $role->delete()); $node->delete(); }); } public function hasBlockingCustomRoles(AgentNode $node): bool { return AdminRole::query() ->where('owner_agent_id', $node->id) ->whereNull('delegated_from_role_id') ->exists(); } }