OrderbookCprMonitoringListController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\CprMonitoringModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. use Respect\Validation\Exceptions\ValidationException;
  7. use Respect\Validation\Validator as val;
  8. class OrderbookCprMonitoringListController
  9. {
  10. private \PDO $pdo;
  11. private CprMonitoringModel $monitoringModel;
  12. public function __construct()
  13. {
  14. if (!isset($GLOBALS['pdo']) || !$GLOBALS['pdo'] instanceof \PDO) {
  15. throw new \RuntimeException('Global PDO connection not initialized');
  16. }
  17. $this->pdo = $GLOBALS['pdo'];
  18. $this->monitoringModel = new CprMonitoringModel();
  19. }
  20. public function __invoke(ServerRequestInterface $request)
  21. {
  22. $userId = (int)($request->getAttribute('api_user_id') ?? 0);
  23. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  24. if ($userId <= 0 || $companyId <= 0) {
  25. return ResponseLib::sendFail('Unauthorized', [], 'E_VALIDATE')->withStatus(401);
  26. }
  27. $body = json_decode((string)$request->getBody(), true) ?? [];
  28. try {
  29. val::key('orderbook_id', val::intType()->positive())
  30. ->assert($body);
  31. } catch (ValidationException $e) {
  32. return ResponseLib::sendFail('Validation failed: ' . $e->getFullMessage(), [], 'E_VALIDATE')->withStatus(400);
  33. }
  34. $orderbookId = (int)$body['orderbook_id'];
  35. try {
  36. $stmt = $this->pdo->prepare(
  37. 'SELECT
  38. t.cpr_id,
  39. w.company_id AS wallet_company_id
  40. FROM "orderbook" o
  41. LEFT JOIN "token" t ON t.token_id = o.token_id
  42. LEFT JOIN "wallet" w ON w.wallet_id = o.wallet_id
  43. WHERE o.orderbook_id = :orderbook_id
  44. LIMIT 1'
  45. );
  46. $stmt->execute(['orderbook_id' => $orderbookId]);
  47. $row = $stmt->fetch(\PDO::FETCH_ASSOC) ?: null;
  48. $cprId = (int)($row['cpr_id'] ?? 0);
  49. $walletCompanyId = (int)($row['wallet_company_id'] ?? 0);
  50. if ($cprId <= 0) {
  51. return ResponseLib::sendFail('CPR not found for orderbook', ['orderbook_id' => $orderbookId], 'E_NOT_FOUND')->withStatus(404);
  52. }
  53. if ($walletCompanyId <= 0) {
  54. return ResponseLib::sendFail('Wallet not found for orderbook', ['orderbook_id' => $orderbookId], 'E_NOT_FOUND')->withStatus(404);
  55. }
  56. if ($companyId !== 1 && $walletCompanyId !== $companyId) {
  57. return ResponseLib::sendFail('Forbidden', [], 'E_FORBIDDEN')->withStatus(403);
  58. }
  59. $rows = $this->monitoringModel->listByCprId($cprId);
  60. } catch (\Throwable $e) {
  61. return ResponseLib::sendFail('Failed to list cpr monitoring: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  62. }
  63. return $rows
  64. ? ResponseLib::sendOk($rows)
  65. : ResponseLib::sendFail('Cpr monitoring not found', [], 'E_DATABASE')->withStatus(204);
  66. }
  67. }