|
@@ -23,24 +23,62 @@ class TshieldWebhookController
|
|
|
return ResponseLib::sendFail('Invalid JSON payload.', [], 'E_VALIDATE')->withStatus(400);
|
|
return ResponseLib::sendFail('Invalid JSON payload.', [], 'E_VALIDATE')->withStatus(400);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $statusDescription = $body['status']['status'] ?? null;
|
|
|
|
|
- if ($statusDescription !== 'Aprovado') {
|
|
|
|
|
|
|
+ error_log('[TShieldWebhook] payload=' . json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
|
|
|
|
+
|
|
|
|
|
+ $statusDescription = $body['status']['status']
|
|
|
|
|
+ ?? $body['status']
|
|
|
|
|
+ ?? ($body['data']['status']['status'] ?? null);
|
|
|
|
|
+ $statusNormalized = is_string($statusDescription) ? mb_strtolower(trim($statusDescription)) : '';
|
|
|
|
|
+ $isApproved = $statusNormalized === 'aprovado'
|
|
|
|
|
+ || $statusNormalized === 'aprovada'
|
|
|
|
|
+ || (is_string($statusNormalized) && str_contains($statusNormalized, 'aprov'));
|
|
|
|
|
+ if (!$isApproved) {
|
|
|
return ResponseLib::sendOk([
|
|
return ResponseLib::sendOk([
|
|
|
'processed' => false,
|
|
'processed' => false,
|
|
|
'reason' => 'Status not approved. No action taken.',
|
|
'reason' => 'Status not approved. No action taken.',
|
|
|
|
|
+ 'status' => $statusDescription,
|
|
|
], 'S_IGNORED');
|
|
], 'S_IGNORED');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $externalNumber = $body['number'] ?? $body['token'] ?? null;
|
|
|
|
|
- if (!is_string($externalNumber) || trim($externalNumber) === '') {
|
|
|
|
|
|
|
+ $externalIds = [
|
|
|
|
|
+ $body['number'] ?? null,
|
|
|
|
|
+ $body['token'] ?? null,
|
|
|
|
|
+ $body['data']['number'] ?? null,
|
|
|
|
|
+ $body['data']['token'] ?? null,
|
|
|
|
|
+ $body['analysis']['number'] ?? null,
|
|
|
|
|
+ $body['analysis']['token'] ?? null,
|
|
|
|
|
+ $body['query']['number'] ?? null,
|
|
|
|
|
+ $body['query']['token'] ?? null,
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $externalNumber = null;
|
|
|
|
|
+ foreach ($externalIds as $candidate) {
|
|
|
|
|
+ if (is_numeric($candidate)) {
|
|
|
|
|
+ $candidate = (string)$candidate;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!is_string($candidate)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ $candidate = trim($candidate);
|
|
|
|
|
+ if ($candidate === '') {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ $externalNumber = $candidate;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($externalNumber === null) {
|
|
|
return ResponseLib::sendFail('Missing analysis number/token in payload.', [], 'E_VALIDATE')->withStatus(400);
|
|
return ResponseLib::sendFail('Missing analysis number/token in payload.', [], 'E_VALIDATE')->withStatus(400);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $updated = $this->model->approveByExternalId($externalNumber);
|
|
|
|
|
|
|
+ error_log('[TShieldWebhook] approved candidates=' . json_encode($externalIds, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
|
|
|
|
+ error_log('[TShieldWebhook] approved number=' . $externalNumber);
|
|
|
|
|
+
|
|
|
|
|
+ $updated = $this->model->approveByExternalIds($externalIds);
|
|
|
if (!$updated) {
|
|
if (!$updated) {
|
|
|
return ResponseLib::sendFail(
|
|
return ResponseLib::sendFail(
|
|
|
'No user found for provided analysis number.',
|
|
'No user found for provided analysis number.',
|
|
|
- ['number' => $externalNumber],
|
|
|
|
|
|
|
+ ['number' => $externalNumber, 'candidates' => $externalIds],
|
|
|
'E_NOT_FOUND'
|
|
'E_NOT_FOUND'
|
|
|
)->withStatus(404);
|
|
)->withStatus(404);
|
|
|
}
|
|
}
|