TshieldWebhookModel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Models;
  3. class TshieldWebhookModel
  4. {
  5. private \PDO $pdo;
  6. public function __construct()
  7. {
  8. if (!isset($GLOBALS['pdo']) || !($GLOBALS['pdo'] instanceof \PDO)) {
  9. throw new \RuntimeException('Database connection not initialized.');
  10. }
  11. $this->pdo = $GLOBALS['pdo'];
  12. }
  13. public function approveByExternalId(string $externalId): bool
  14. {
  15. $stmt = $this->pdo->prepare(
  16. 'UPDATE "user" SET user_kyc = 1 WHERE kyc_external_id = :external_id AND user_flag = \'a\''
  17. );
  18. $stmt->execute(['external_id' => $externalId]);
  19. return $stmt->rowCount() > 0;
  20. }
  21. public function approveByExternalIds(array $externalIds): int
  22. {
  23. $ids = [];
  24. foreach ($externalIds as $id) {
  25. if (!is_string($id)) {
  26. continue;
  27. }
  28. $trimmed = trim($id);
  29. if ($trimmed === '') {
  30. continue;
  31. }
  32. $ids[$trimmed] = true;
  33. }
  34. $ids = array_keys($ids);
  35. if (empty($ids)) {
  36. return 0;
  37. }
  38. $placeholders = [];
  39. $params = [];
  40. foreach ($ids as $idx => $value) {
  41. $key = ':id' . $idx;
  42. $placeholders[] = $key;
  43. $params[$key] = $value;
  44. }
  45. $sql = 'UPDATE "user" SET user_kyc = 1 '
  46. . 'WHERE user_flag = \'a\' AND kyc_external_id IN (' . implode(',', $placeholders) . ')';
  47. $stmt = $this->pdo->prepare($sql);
  48. $stmt->execute($params);
  49. return $stmt->rowCount();
  50. }
  51. public function approveByCpf(string $cpf): int
  52. {
  53. $cpf = preg_replace('/\D+/', '', $cpf);
  54. $cpf = trim((string)$cpf);
  55. if ($cpf === '') {
  56. return 0;
  57. }
  58. $stmt = $this->pdo->prepare(
  59. 'UPDATE "user" '
  60. . 'SET user_kyc = 1 '
  61. . 'WHERE user_flag = \'a\' '
  62. . ' AND regexp_replace(user_cpf, \'\\D\', \'\', \'g\') = :cpf'
  63. );
  64. $stmt->execute(['cpf' => $cpf]);
  65. return $stmt->rowCount();
  66. }
  67. public function approveCompanyOwnerByCnpj(string $cnpj): int
  68. {
  69. $cnpj = preg_replace('/\D+/', '', $cnpj);
  70. $cnpj = trim((string)$cnpj);
  71. if ($cnpj === '') {
  72. return 0;
  73. }
  74. $stmt = $this->pdo->prepare(
  75. 'UPDATE "user" u '
  76. . 'SET user_kyc = 1 '
  77. . 'FROM "company" c '
  78. . 'WHERE u.company_id = c.company_id '
  79. . ' AND regexp_replace(c.company_cnpj, \'\\D\', \'\', \'g\') = :cnpj '
  80. . ' AND u.role_id = 1 '
  81. . ' AND u.user_flag = \'a\''
  82. );
  83. $stmt->execute(['cnpj' => $cnpj]);
  84. return $stmt->rowCount();
  85. }
  86. }