service = new CprMonitoringService(); } public function __invoke(ServerRequestInterface $request) { $userId = (int)($request->getAttribute('api_user_id') ?? 0); $companyId = (int)($request->getAttribute('api_company_id') ?? 0); if ($userId <= 0 || $companyId <= 0) { return ResponseLib::sendFail('Unauthorized', [], 'E_VALIDATE')->withStatus(401); } $body = json_decode((string)$request->getBody(), true) ?? []; try { val::key('cpr_id', val::intType()->positive()) ->assert($body); } catch (ValidationException $e) { return ResponseLib::sendFail('Validation failed: ' . $e->getFullMessage(), [], 'E_VALIDATE')->withStatus(400); } $cprId = (int)$body['cpr_id']; try { $rows = $this->service->listByCprId($cprId); } catch (\InvalidArgumentException $e) { return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400); } catch (\Throwable $e) { return ResponseLib::sendFail('Failed to list cpr monitoring: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500); } return $rows ? ResponseLib::sendOk($rows) : ResponseLib::sendFail('Cpr monitoring not found', [], 'E_DATABASE')->withStatus(204); } }