Prechádzať zdrojové kódy

debug on dynamic qrcode generation

ljoaquim 2 týždňov pred
rodič
commit
b928243afa
3 zmenil súbory, kde vykonal 81 pridanie a 17 odobranie
  1. 62 11
      controllers/RegisterCprController.php
  2. 16 3
      test/cpr_create.sh
  3. 3 3
      test/fetch_jwt.sh

+ 62 - 11
controllers/RegisterCprController.php

@@ -96,13 +96,28 @@ class RegisterCprController
         $result = BashExecutor::run($command, 60);
 
         if (($result['exitCode'] ?? 1) !== 0) {
+            $this->logCliResult($result, 'genial-cli non-zero exit');
             $message = $result['error'] ?: $result['output'] ?: 'Unknown error';
             throw new \RuntimeException($message);
         }
 
         $output = $result['output'] ?? '';
-        $itemId = $this->extractCliValue('itemId', $output);
-        $qrcodeUrl = $this->extractCliValue('qrcodeURL', $output);
+
+        try {
+            $parsed = $this->decodeCliOutput($output);
+        } catch (\Throwable $e) {
+            $this->logCliResult($result, 'genial-cli parse failure');
+            throw $e;
+        }
+
+        $items = $parsed['data']['items'] ?? null;
+        if (!is_array($items) || empty($items) || !is_array($items[0])) {
+            throw new \RuntimeException('genial-cli output is missing items array');
+        }
+
+        $firstItem = $items[0];
+        $itemId = $firstItem['itemId'] ?? null;
+        $qrcodeUrl = $firstItem['data']['qrcodeURL'] ?? null;
 
         if (!$itemId || !$qrcodeUrl) {
             throw new \RuntimeException('Unable to parse itemId or qrcodeURL from genial-cli output');
@@ -114,17 +129,53 @@ class RegisterCprController
         ];
     }
 
-    private function extractCliValue(string $key, string $content): ?string
+    private function decodeCliOutput(string $content): array
     {
-        $pattern = sprintf('#%s\s*[:=]\s*(?:"([^"]+)"|\' . "'" . '([^\' . "'" . ']+)\' . "'" . '|([^\s,{}]+))#i', preg_quote($key, '#'));
-        if (preg_match($pattern, $content, $matches)) {
-            foreach (array_slice($matches, 1) as $match) {
-                if ($match !== '' && $match !== null) {
-                    return trim($match);
-                }
-            }
+        $clean = trim($content);
+        if ($clean === '') {
+            throw new \RuntimeException('genial-cli returned empty output');
+        }
+
+        // Strip ANSI escape codes, just in case
+        $clean = preg_replace('/\e\[[\d;]*m/', '', $clean);
+
+        // Convert single-quoted strings to JSON-compatible double-quoted ones
+        $clean = preg_replace_callback(
+            "/'([^'\\]*(?:\\.[^'\\]*)*)'/",
+            static function (array $matches): string {
+                $inner = str_replace(['\\', '"'], ['\\\\', '\\"'], $matches[1]);
+                return '"' . $inner . '"';
+            },
+            $clean
+        );
+
+        // Quote object keys so json_decode can understand them
+        $clean = preg_replace(
+            '/([\\{\\[,,]\s*|\n\s*)([A-Za-z_][A-Za-z0-9_]*)\s*:/',
+            '$1"$2":',
+            $clean
+        );
+
+        $decoded = json_decode($clean, true);
+        if (json_last_error() !== JSON_ERROR_NONE) {
+            throw new \RuntimeException('Failed to decode genial-cli output: ' . json_last_error_msg());
         }
 
-        return null;
+        return $decoded;
+    }
+
+    private function logCliResult(array $result, string $context): void
+    {
+        $exitCode = $result['exitCode'] ?? 'null';
+        $stdout = trim($result['output'] ?? '');
+        $stderr = trim($result['error'] ?? '');
+
+        error_log(sprintf(
+            '[RegisterCprController] %s | exitCode: %s | stdout: %s | stderr: %s',
+            $context,
+            (string)$exitCode,
+            $stdout === '' ? '<empty>' : $stdout,
+            $stderr === '' ? '<empty>' : $stderr
+        ));
     }
 }

+ 16 - 3
test/cpr_create.sh

@@ -2,7 +2,7 @@
 set -euo pipefail
 
 BASE_URL="${BASE_URL:-https://api.tooeasy.mixtech.dev.br}"
-JWT_TOKEN="${JWT_TOKEN:-}"
+JWT_TOKEN="${JWT_TOKEN:-eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImVtYWlsIjoiYWRtaW5AbHVteW9uLnRlY2giLCJpYXQiOjE3NjQ3MDkxNzMsImV4cCI6MTc2NDcxMjc3M30.2ZUmjLPxe3RIQoY7LgzrrwKKjj4FuKKOaQIZoaG8QPo}"
 PAYLOAD_FILE="${1:-}"
 
 usage() {
@@ -160,8 +160,21 @@ fi
 
 echo "[info] Sending CPR payload '${PAYLOAD_FILE:-template}' to $API_ENDPOINT"
 
-curl --fail --show-error --silent \
+response_body=$(mktemp)
+trap 'rm -f "$response_body"' EXIT
+
+http_code=$(curl --show-error --silent \
+    -o "$response_body" \
+    -w "%{http_code}" \
     -X POST "$API_ENDPOINT" \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer $JWT_TOKEN" \
-    --data "$PAYLOAD_CONTENT" | jq .
+    --data "$PAYLOAD_CONTENT")
+
+if [[ "$http_code" =~ ^2 ]]; then
+    jq . <"$response_body"
+else
+    echo "[error] Request failed with status $http_code" >&2
+    cat "$response_body" >&2
+    exit 1
+fi

+ 3 - 3
test/fetch_jwt.sh

@@ -1,10 +1,10 @@
 #!/usr/bin/env bash
 set -euo pipefail
 
-BASE_URL="${BASE_URL:-http://localhost:8000}"
+BASE_URL="${BASE_URL:-https://api.tooeasy.mixtech.dev.br}"
 LOGIN_ENDPOINT="$BASE_URL/login"
-EMAIL="${LOGIN_EMAIL:-tester@example.com}"
-PASSWORD="${LOGIN_PASSWORD:-Password123!}"
+EMAIL="${LOGIN_EMAIL:-admin@lumyon.tech}"
+PASSWORD="${LOGIN_PASSWORD:-admin123}"
 
 usage() {
     cat <<'USAGE'