{
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "afiliados-shopee",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -304,
        -16
      ],
      "id": "852f9cbc-5449-42a0-9b05-c44dd5cd4824",
      "name": "Webhook"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        816,
        -112
      ],
      "id": "264b3503-5470-4550-8c07-a30281de8615",
      "name": "Respond to Webhook"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "570007f6-01ff-4705-97a9-f72da9b21756",
              "leftValue": "={{ $json.status }}",
              "rightValue": 200,
              "operator": {
                "type": "number",
                "operation": "notEquals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        816,
        80
      ],
      "id": "e0f9c7e5-2c0e-4a26-8e8c-61f862be15bf",
      "name": "If"
    },
    {
      "parameters": {
        "sendTo": "teste@test.com",
        "subject": "Erro na execu\u00e7\u00e3o",
        "message": "Houve um erro na execu\u00e7\u00e3o da gera\u00e7\u00e3o do link de afiliados",
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        1040,
        80
      ],
      "id": "a3379021-73bc-4cea-baa1-e61ccf1b656c",
      "name": "Send a message",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://open-api.affiliate.shopee.com.br/graphql",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Authorization",
              "value": "=SHA256 Credential={{ $('Preparar Request Shopee').item.json.appId }},Timestamp={{ $('Preparar Request Shopee').item.json.timestamp }},Signature={{ $json.signature }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json.rawBody }}",
        "options": {
          "allowUnauthorizedCerts": false
        }
      },
      "id": "7a5a46d2-be0a-4ee9-acf0-68bc0f8b8e29",
      "name": "Gerar Link Afiliado",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        592,
        -16
      ]
    },
    {
      "parameters": {
        "jsCode": "// CONFIGURA\u00c7\u00c3O B\u00c1SICA\nconst appId = $input.first().json.shopee_app_id;\nconst appSecret = $input.first().json.shopee_app_secret;\nconst timestamp = $input.first().json.timestamp;\nconst linkOriginal = $input.first().json.url;\n\n// Usar mutation GraphQL com input conforme schema da API\n// subIds \u00e9 opcional e pode ser omitido ou passar array vazio\nconst graphqlQuery = `mutation {\n    generateShortLink(input: { originUrl: \"${linkOriginal}\" }) {\n        shortLink\n    }\n}`;\n\nconst payload = {\n    query: graphqlQuery\n};\n\nconst payloadString = JSON.stringify(payload);\n\nreturn {\n  appId,\n  appSecret,\n  timestamp,\n  body: payload,\n  rawBody: payloadString\n};"
      },
      "id": "d0f24385-27c9-4d76-9d98-c01e0c1cb64c",
      "name": "Preparar Request Shopee",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        144,
        -16
      ]
    },
    {
      "parameters": {
        "type": "SHA256",
        "value": "={{ $json.appId + $json.timestamp + $json.rawBody + $('variaveis').item.json.shopee_app_secret }}",
        "dataPropertyName": "signature"
      },
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 1,
      "position": [
        368,
        -16
      ],
      "id": "e5cd6614-9a96-4e31-86cb-d28698eb69c7",
      "name": "Crypto"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a1ff29c8-d405-4655-9860-b8913dab4e98",
              "name": "url",
              "value": "={{ $('Webhook').item.json.body.url }}",
              "type": "string"
            },
            {
              "id": "0586320b-74f6-4605-97f9-54a539ae62e3",
              "name": "timestamp",
              "value": "={{ $now.toFormat('X') }}",
              "type": "string"
            },
            {
              "id": "8dd164a0-4e92-4028-a207-a1dbfa87b362",
              "name": "shopee_app_id",
              "value": "18332030606",
              "type": "string"
            },
            {
              "id": "748fb8bf-c7b7-4f24-8da9-36a6c3c1c1ff",
              "name": "shopee_app_secret",
              "value": "MJS67QHU7HMCRX5AHI75YI2FO4M2AIXP",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -80,
        -16
      ],
      "id": "84cda82b-16ad-48e8-af0e-dcd781b63759",
      "name": "variaveis"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "variaveis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gerar Link Afiliado": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          },
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Preparar Request Shopee": {
      "main": [
        [
          {
            "node": "Crypto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Crypto": {
      "main": [
        [
          {
            "node": "Gerar Link Afiliado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "variaveis": {
      "main": [
        [
          {
            "node": "Preparar Request Shopee",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": true
  }
}