TshieldWebhookController.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\TshieldWebhookModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. class TshieldWebhookController
  7. {
  8. private TshieldWebhookModel $model;
  9. public function __construct()
  10. {
  11. $this->model = new TshieldWebhookModel();
  12. }
  13. public function __invoke(ServerRequestInterface $request)
  14. {
  15. $body = json_decode((string)$request->getBody(), true);
  16. if (!is_array($body)) {
  17. return ResponseLib::sendFail('Invalid JSON payload.', [], 'E_VALIDATE')->withStatus(400);
  18. }
  19. error_log('[TShieldWebhook] payload=' . json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
  20. $statusDescription = $body['status']['status']
  21. ?? $body['status']
  22. ?? ($body['data']['status']['status'] ?? null);
  23. $statusNormalized = is_string($statusDescription) ? mb_strtolower(trim($statusDescription)) : '';
  24. $isApproved = $statusNormalized === 'aprovado'
  25. || $statusNormalized === 'aprovada'
  26. || (is_string($statusNormalized) && str_contains($statusNormalized, 'aprov'));
  27. if (!$isApproved) {
  28. return ResponseLib::sendOk([
  29. 'processed' => false,
  30. 'reason' => 'Status not approved. No action taken.',
  31. 'status' => $statusDescription,
  32. ], 'S_IGNORED');
  33. }
  34. $externalIds = [
  35. $body['number'] ?? null,
  36. $body['token'] ?? null,
  37. $body['data']['number'] ?? null,
  38. $body['data']['token'] ?? null,
  39. $body['analysis']['number'] ?? null,
  40. $body['analysis']['token'] ?? null,
  41. $body['query']['number'] ?? null,
  42. $body['query']['token'] ?? null,
  43. ];
  44. $externalNumber = null;
  45. foreach ($externalIds as $candidate) {
  46. if (is_numeric($candidate)) {
  47. $candidate = (string)$candidate;
  48. }
  49. if (!is_string($candidate)) {
  50. continue;
  51. }
  52. $candidate = trim($candidate);
  53. if ($candidate === '') {
  54. continue;
  55. }
  56. $externalNumber = $candidate;
  57. break;
  58. }
  59. if ($externalNumber === null) {
  60. return ResponseLib::sendFail('Missing analysis number/token in payload.', [], 'E_VALIDATE')->withStatus(400);
  61. }
  62. error_log('[TShieldWebhook] approved candidates=' . json_encode($externalIds, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
  63. error_log('[TShieldWebhook] approved number=' . $externalNumber);
  64. $updated = $this->model->approveByExternalIds($externalIds);
  65. if (!$updated) {
  66. return ResponseLib::sendFail(
  67. 'No user found for provided analysis number.',
  68. ['number' => $externalNumber, 'candidates' => $externalIds],
  69. 'E_NOT_FOUND'
  70. )->withStatus(404);
  71. }
  72. return ResponseLib::sendOk([
  73. 'processed' => true,
  74. 'number' => $externalNumber,
  75. ]);
  76. }
  77. }