Explorar el Código

migration token changes

ljoaquim hace 2 semanas
padre
commit
1c792098f3
Se han modificado 6 ficheros con 97 adiciones y 5 borrados
  1. 4 0
      .env.example
  2. 7 2
      migrations/migrations_v1.sql
  3. 3 2
      package.json
  4. 16 0
      test/b3_credential_test.js
  5. 66 0
      test/b3_credentials_check.sh
  6. 1 1
      test/cpr_create.sh

+ 4 - 0
.env.example

@@ -10,6 +10,10 @@ ACCOUNT1_KEY=
 ACCOUNT1_NAME=
 DOCUMENT_NUMBER=
 
+#================= B3 ======================
+B3_CLIENT_ID=
+B3_CLIENT_SECRET=
+
 #================= DATABASE (PostgreSQL) =================
 DB_HOST=
 DB_PORT=

+ 7 - 2
migrations/migrations_v1.sql

@@ -219,16 +219,21 @@ CREATE TABLE "token" (
     "token_id" SERIAL PRIMARY KEY,
     "token_external_id" TEXT NOT NULL,
     "token_commodities_amount" INTEGER NOT NULL,
-    "token_flag" TEXT NOT NULL,
     "token_commodities_value" INTEGER NOT NULL,
+    "token_uf" TEXT NOT NULL,
+    "token_city" TEXT NOT NULL,
+    "token_content" TEXT NOT NULL, -- financial instrument
+    "token_flag" TEXT NOT NULL,
     "wallet_id" INTEGER NOT NULL,
     "chain_id" INTEGER NOT NULL,
     "commodities_id" INTEGER NOT NULL,
     "cpr_id" INTEGER NOT NULL,
+    "user_id" INTEGER NOT NULL,
     FOREIGN KEY ("wallet_id") REFERENCES "wallet" ("wallet_id"),
     FOREIGN KEY ("chain_id") REFERENCES "chain" ("chain_id"),
     FOREIGN KEY ("commodities_id") REFERENCES "commodities" ("commodities_id"),
-    FOREIGN KEY ("cpr_id") REFERENCES "cpr" ("cpr_id")
+    FOREIGN KEY ("cpr_id") REFERENCES "cpr" ("cpr_id"),
+    FOREIGN KEY ("user_id") REFERENCES "user" ("user_id")
 );
 
 CREATE TABLE "tx_coin" (

+ 3 - 2
package.json

@@ -12,6 +12,7 @@
   "dependencies": {
     "crypto": "^1.0.1",
     "decimal.js": "^10.6.0",
-    "dotenv": "^17.2.3"
+    "dotenv": "^17.2.3",
+    "request": "^2.88.2"
   }
-}
+}

+ 16 - 0
test/b3_credential_test.js

@@ -0,0 +1,16 @@
+// This is an example you can use in your Node.js app
+const request = require('request');
+const options = {
+	url: 'https://developers.b3.com.br:8065/aapi/oauth/token',
+	method: 'POST',
+	headers: {
+		'accept': 'application/json',
+		'Content-Type': 'application/x-www-form-urlencoded'
+	},
+	body: 'client_id=1df059ec-3090-4429-a229-176388b174ed&client_secret=6c20c9b8-97d3-4362-bc35-4dd0c85dbe89&grant_type=client_credentials',
+	gzip: true,
+	json: true
+};
+request(options, (err, response, body) => {
+	console.log('success:', body);
+});

+ 66 - 0
test/b3_credentials_check.sh

@@ -0,0 +1,66 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+usage() {
+    cat <<'USAGE'
+Usage:
+  Add B3_CLIENT_ID and B3_CLIENT_SECRET to the project .env file, then run:
+    ./test/b3_credentials_check.sh
+
+The script:
+  1. Loads credentials from ../.env
+  2. Requests an OAuth token via Client Credentials (prod URL)
+  3. Calls the prod healthcheck endpoint with the token
+
+Both endpoints are hardcoded to the production environment.
+USAGE
+}
+
+SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
+PROJECT_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
+ENV_FILE="$PROJECT_ROOT/.env"
+
+parse_env_value() {
+    local key="$1"
+    if [[ ! -f "$ENV_FILE" ]]; then
+        return
+    fi
+    sed -n "s/^${key}=//p" "$ENV_FILE" | tail -n1 | tr -d '\r'
+}
+
+B3_CLIENT_ID=$(parse_env_value "B3_CLIENT_ID")
+B3_CLIENT_SECRET=$(parse_env_value "B3_CLIENT_SECRET")
+
+if [[ -z "$B3_CLIENT_ID" || -z "$B3_CLIENT_SECRET" ]]; then
+    echo "[error] B3_CLIENT_ID and B3_CLIENT_SECRET must be defined in .env" >&2
+    usage
+    exit 1
+fi
+
+B3_AUTH_URL="https://apib3i.b3.com.br/auth/oauth/v2/token"
+B3_HEALTHCHECK_URL="https://apib3i.b3.com.br/api/healthcheck/{token}"
+
+echo "[info] Requesting OAuth token from $B3_AUTH_URL"
+TOKEN_RESPONSE=$(curl --silent --show-error --fail \
+    -u "$B3_CLIENT_ID:$B3_CLIENT_SECRET" \
+    -H "Content-Type: application/x-www-form-urlencoded" \
+    -d 'grant_type=client_credentials' \
+    "$B3_AUTH_URL")
+
+TOKEN=$(echo "$TOKEN_RESPONSE" | jq -r '.access_token // empty')
+
+if [[ -z "$TOKEN" ]]; then
+    echo "[error] Failed to extract access_token from response:" >&2
+    echo "$TOKEN_RESPONSE" >&2
+    exit 1
+fi
+
+echo "[info] Token retrieved successfully"
+
+HEALTH_URL=${B3_HEALTHCHECK_URL//\{token\}/$TOKEN}
+echo "[info] Calling healthcheck at $HEALTH_URL"
+
+HEALTH_RESPONSE=$(curl --silent --show-error --fail "$HEALTH_URL")
+
+echo "[info] Healthcheck response:"
+echo "$HEALTH_RESPONSE" | jq .

+ 1 - 1
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:-eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImVtYWlsIjoiYWRtaW5AbHVteW9uLnRlY2giLCJpYXQiOjE3NjQ3MDkxNzMsImV4cCI6MTc2NDcxMjc3M30.2ZUmjLPxe3RIQoY7LgzrrwKKjj4FuKKOaQIZoaG8QPo}"
+JWT_TOKEN="${JWT_TOKEN:-eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImVtYWlsIjoiYWRtaW5AbHVteW9uLnRlY2giLCJpYXQiOjE3NjQ3MjE0NDgsImV4cCI6MTc2NDcyNTA0OH0.VSmjjp8eJKNR-TU7-e3rjMpP2wlJPnQ2mgwYtoXieyA}"
 PAYLOAD_FILE="${1:-}"
 
 usage() {