{
  "name": "CrudApiDelete",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "DELETE",
        "path": "api/messages/:id",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -80,
        0
      ],
      "id": "4adf4004-711c-4367-a8e0-f8eeff388f3b",
      "name": "Webhook"
    },
    {
      "parameters": {
        "queue": "Crud_Delete",
        "options": {}
      },
      "type": "n8n-nodes-base.rabbitmq",
      "typeVersion": 1.1,
      "position": [
        912,
        0
      ],
      "id": "0e4483f4-cad7-4929-8d8e-d1324b068b6c",
      "name": "RabbitMQ",
      "credentials": {
        "rabbitmq": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"type\": \"https://example.com/probs/internal-error\",\n  \"title\": \"Internal Server Error\",\n  \"status\": 500,\n  \"detail\": \"Ocorreu um erro interno inesperado ao processar sua solicita\u00e7\u00e3o.\",\n  \"instance\": \"/api/messages\",\n  \"request_id\": \"req_xyz987\"\n}",
        "options": {
          "responseCode": 500
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        272,
        144
      ],
      "id": "32f7db19-63df-44dd-bcbf-b27c6f94f811",
      "name": "error_500"
    },
    {
      "parameters": {
        "jsCode": "const item = $input.item;\n\nif(item.json === undefined || item.json === null){\n  item.json = {};\n}\n\n\nconst crypto = require('crypto');\n\nconst secretKey = 'seu-segredo-secreto';\n\nfunction base64UrlDecode(data) {\n  let base64 = data.replace(/-/g, '+').replace(/_/g, '/');\n  \n  while (base64.length % 4) {\n    base64 += '=';\n  }\n  return Buffer.from(base64, 'base64').toString('utf8');\n}\n\n\ntry {\n  \n  const authHeader = $json.headers['authorization'];\n\n  if (!authHeader || !authHeader.startsWith('Bearer ')) {\n    throw new Error('Authorization header missing or invalid');\n  }\n\n \n  const token = authHeader.split(' ')[1];\n\n  \n  const [encodedHeader, encodedPayload, signature] = token.split('.');\n\n  if (!encodedHeader || !encodedPayload || !signature) {\n    throw new Error('Invalid token format');\n  }\n\n \n  const dataToSign = `${encodedHeader}.${encodedPayload}`;\n  const expectedSignature = crypto\n    .createHmac('sha256', secretKey)\n    .update(dataToSign)\n    .digest('base64url');\n\n  \n  const sigBuffer = Buffer.from(signature, 'base64url');\n  const expectedSigBuffer = Buffer.from(expectedSignature, 'base64url');\n\n  if (sigBuffer.length !== expectedSigBuffer.length || !crypto.timingSafeEqual(sigBuffer, expectedSigBuffer)) {\n    throw new Error('Invalid signature');\n  }\n\n  \n  const payload = JSON.parse(base64UrlDecode(encodedPayload));\n  const nowInSeconds = Math.floor(Date.now() / 1000);\n\n  if (payload.exp < nowInSeconds) {\n    throw new Error('Token has expired');\n  }\n\n \n  item.json.auth_user = payload;\n  item.json.isValid = true;\n\n} catch (Error) {\n\n  item.json.isValid = false;\n  item.json.errorDetails = Error.message;\n}\n\nreturn item;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        96,
        0
      ],
      "id": "0ca41e55-2854-4cea-9ab0-f6265873df59",
      "name": "token_Validation",
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "6ee1deb8-ade6-4e8c-98e3-ff69028b1eb1",
              "leftValue": "={{ $json.isValid }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        272,
        0
      ],
      "id": "18d0a073-aa9b-4cb1-99c1-babf110a3dd7",
      "name": "token_Is_Valid"
    },
    {
      "parameters": {
        "jsCode": "\nconst uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;\n\nconst returnItems = [];\n\nfor (const item of $input.all()) {\n  try {\n    \n    if (item.json === undefined || item.json === null) {\n      item.json = {};\n    }\n    \n    \n    const id = item.json.params.id;\n    \n    \n    if (!id || !uuidRegex.test(id)) {\n      item.json.id_validation_error = {\n          type: \"https://example.com/probs/invalid-parameter\",\n          title: \"Invalid Parameter\",\n          status: 400,\n          detail: \"The 'id' parameter in the URL is missing or not a valid UUID.\",\n          instance: item.json.webhookUrl || 'webhookUrl_not_found'\n      };\n    } else {\n      item.json.id_validation_error = null;\n    }\n    \n  } catch (error) {\n    item.json.id_validation_error = {\n        type: \"https://example.com/probs/internal-error\",\n        title: \"Internal Server Error\",\n        status: 500,\n        detail: \"An unexpected error occurred during ID validation: \" + error.message\n    };\n  }\n\n  returnItems.push(item);\n}\n\n\nreturn returnItems;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        496,
        0
      ],
      "id": "a3eac238-856e-47b5-8f55-fd494fefdaf5",
      "name": "id_Validation",
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "bb2fa213-a725-4dbc-9e0e-c142d71a68af",
              "leftValue": "={{ $json.id_validation_error }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        704,
        0
      ],
      "id": "086de467-e047-491f-88c3-357d587689db",
      "name": "id_Validation_Error_Is_Not_Empty"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n\"type\": \"https://example.com/probs/invalid-input\",\n  \"title\": \"Invalid id\",\n  \"status\": 400,\n  \"detail\": \"One or more fields failed validation\",\n  \"instance\": \"/api/messages/:id\"}",
        "options": {
          "responseCode": 400
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        912,
        144
      ],
      "id": "54eca7dd-4a16-4330-8537-9ff262f4ffb9",
      "name": "error_400"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"title\": \"unauthorized\",\n  \"status\": 401,\n  \"detail\": \"invalid or expired token\"\n}",
        "options": {
          "responseCode": 401
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        496,
        144
      ],
      "id": "1ed43445-15f7-49f4-b330-91e55a694678",
      "name": "error_401"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"type\": \"https://example.com/probs/internal-error\",\n  \"title\": \"Internal Server Error\",\n  \"status\": 500,\n  \"detail\": \"Ocorreu um erro interno inesperado ao processar sua solicita\u00e7\u00e3o.\",\n  \"instance\": \"/api/messages\",\n  \"request_id\": \"req_xyz987\"\n}",
        "options": {
          "responseCode": 500
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        704,
        144
      ],
      "id": "79313418-d4c6-423b-bec2-2d180598fa51",
      "name": "error_500_"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"status\": 202,\n  \"title\": \"accepted\"\n}",
        "options": {
          "responseCode": 202
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1104,
        0
      ],
      "id": "7a1d4561-538d-4a46-9abb-93ac200119d2",
      "name": "accepted_202"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "token_Validation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "RabbitMQ": {
      "main": [
        [
          {
            "node": "accepted_202",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "token_Validation": {
      "main": [
        [
          {
            "node": "token_Is_Valid",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "error_500",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "token_Is_Valid": {
      "main": [
        [
          {
            "node": "id_Validation",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "error_401",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "id_Validation": {
      "main": [
        [
          {
            "node": "id_Validation_Error_Is_Not_Empty",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "error_500_",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "id_Validation_Error_Is_Not_Empty": {
      "main": [
        [
          {
            "node": "error_400",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "RabbitMQ",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "d55fb305-7a1c-4a3f-8dce-3dc0f8899e4f",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "OqEKBj7ov7ZGebfW",
  "tags": []
}