Parcourir la source

add more data on cpr

gdias il y a 1 semaine
Parent
commit
e5e9adbc53

+ 4 - 0
controllers/B3CprRegisterController.php

@@ -95,6 +95,10 @@ class B3CprRegisterController
             return ResponseLib::sendFail($e->getMessage(), [], 'E_VALIDATE')->withStatus(400);
         }
 
+        if (!array_key_exists('cpr_ticker', $cpr)) {
+            $cpr['cpr_ticker'] = '';
+        }
+
         $userId = (int)($request->getAttribute('api_user_id') ?? 0);
         if ($userId <= 0) {
             return ResponseLib::sendFail('Authenticated user not found', [], 'E_VALIDATE')->withStatus(401);

+ 6 - 1
controllers/PaymentConfirmController.php

@@ -83,6 +83,8 @@ class PaymentConfirmController
             return ResponseLib::sendFail('cURL error during B3 CPR request', ['error' => $result['error']], 'E_EXTERNAL')->withStatus(502);
         }
 
+        $tickerSymbol = $result['json']['data']['tickerSymbol'] ?? null;
+
         try {
             $tokenResult = $this->createTokenFromCpr($cpr);
         } catch (\Throwable $e) {
@@ -95,9 +97,12 @@ class PaymentConfirmController
 
         try {
             $this->cprModel->updateTokenId((int)$cpr['cpr_id'], (int)$tokenResult['token_id']);
+            if ($tickerSymbol !== null) {
+                $this->cprModel->updateTicker((int)$cpr['cpr_id'], (string)$tickerSymbol);
+            }
         } catch (\Throwable $e) {
             return ResponseLib::sendFail(
-                'Falha ao vincular token à CPR: ' . $e->getMessage(),
+                'Falha ao atualizar CPR: ' . $e->getMessage(),
                 [],
                 'E_CPR_UPDATE'
             )->withStatus(500);

+ 2 - 0
migrations/20260126_add_cpr_ticker_column.sql

@@ -0,0 +1,2 @@
+ALTER TABLE "cpr"
+ADD COLUMN IF NOT EXISTS "cpr_ticker" TEXT;

+ 22 - 0
models/CprModel.php

@@ -108,6 +108,13 @@ class CprModel
             }
 
             if (!array_key_exists($column, $data)) {
+                if ($column === 'cpr_ticker') {
+                    $columns[] = '"' . $column . '"';
+                    $placeholders[] = ':' . $column;
+                    $params[$column] = '';
+                    continue;
+                }
+
                 if ($info['nullable']) {
                     $columns[] = '"' . $column . '"';
                     $placeholders[] = ':' . $column;
@@ -195,6 +202,21 @@ class CprModel
         }
     }
 
+    public function updateTicker(int $cprId, string $ticker): void
+    {
+        if ($cprId <= 0) {
+            throw new \InvalidArgumentException('Invalid CPR id provided');
+        }
+
+        $normalizedTicker = trim($ticker);
+
+        $stmt = $this->pdo->prepare('UPDATE "cpr" SET cpr_ticker = :cpr_ticker WHERE cpr_id = :cpr_id');
+        $stmt->execute([
+            'cpr_ticker' => $normalizedTicker,
+            'cpr_id' => $cprId,
+        ]);
+    }
+
     private function normalizeChildrenCodes($value): string
     {
         if (is_array($value)) {

+ 2 - 1
models/CprQueryModel.php

@@ -46,7 +46,8 @@ class CprQueryModel
                 cpr_product_class_name,
                 cpr_issue_date,
                 cpr_issuer_name,
-                cpr_issue_financial_value
+                cpr_issue_financial_value,
+                cpr_ticker
             FROM cpr
             ORDER BY cpr_issue_date DESC
         ';