{
  "name": "Marketa \u2014 revise blog (webhook from monday stage change)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "marketa-revise",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook: /marketa-revise",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.monday.com/v2",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "API-Version",
              "value": "2024-01"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"query\": \"query ($id: [ID!]!) { items(ids: $id) { name column_values { id type text value } } }\",\n  \"variables\": { \"id\": [{{ JSON.stringify(String($json.body.pulseId)) }}] }\n}",
        "options": {}
      },
      "id": "monday-fetch",
      "name": "monday: fetch item",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        420,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const item = $input.first().json.data?.items?.[0];\nif (!item) throw new Error('item not found');\nconst col = (id) => item.column_values.find(c => c.id === id);\nconst text = (c) => c?.text?.trim() || '';\nconst pulseId = $('Webhook: /marketa-revise').first().json.body.pulseId;\nconst draft = text(col('long_text_mm3gj0s8'));\nconst notes = text(col('long_text_mm3g2bp9'));\nif (!draft) throw new Error('no existing draft to revise');\nif (!notes) throw new Error('no edit notes provided');\nreturn [{\n  json: {\n    pulseId: String(pulseId),\n    title: item.name,\n    draft,\n    notes\n  }\n}];"
      },
      "id": "normalize",
      "name": "Normalize item",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        640,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT body FROM {{ SUPABASE_BRAIN_TABLE }} WHERE source_type = 'voice_guide' LIMIT 1;"
      },
      "id": "voice-guide",
      "name": "Fetch voice guide",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        860,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.anthropic.com/v1/messages",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "anthropicApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "anthropic-version",
              "value": "2023-06-01"
            },
            {
              "name": "content-type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"claude-opus-4-7\",\n  \"max_tokens\": 4000,\n  \"system\": {{ JSON.stringify('You are Marketa revising a draft per reviewer feedback. Apply the requested changes precisely without rewriting unaffected sections. Preserve voice and cite markers. Follow voice guide.\\n\\nVOICE GUIDE:\\n' + $('Fetch voice guide').first().json.body) }},\n  \"messages\": [{\n    \"role\": \"user\",\n    \"content\": {{ JSON.stringify('Title: ' + $('Normalize item').first().json.title + '\\n\\nCurrent draft:\\n' + $('Normalize item').first().json.draft + '\\n\\nReviewer edit notes:\\n' + $('Normalize item').first().json.notes + '\\n\\nReturn the full revised draft in markdown. Do not include any preamble.') }}\n  }]\n}",
        "options": {}
      },
      "id": "claude-revise",
      "name": "Claude: revise draft",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1080,
        300
      ],
      "credentials": {
        "anthropicApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const revised = $input.first().json.content?.[0]?.text || '';\nconst pulseId = $('Normalize item').first().json.pulseId;\nreturn [{ json: { pulseId, revised } }];"
      },
      "id": "extract-revised",
      "name": "Extract revised text",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1300,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.monday.com/v2",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "API-Version",
              "value": "2024-01"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"query\": \"mutation ($board: ID!, $item: ID!, $vals: JSON!) { change_multiple_column_values(board_id: $board, item_id: $item, column_values: $vals) { id } }\",\n  \"variables\": {\n    \"board\": \"5028637584\",\n    \"item\": {{ JSON.stringify($json.pulseId) }},\n    \"vals\": {{ JSON.stringify(JSON.stringify({\n      \"long_text_mm3gj0s8\": { \"text\": $json.revised },\n      \"long_text_mm3g2bp9\": { \"text\": \"\" },\n      \"dropdown_mm3jh58b\": { \"labels\": [\"Draft ready\"] }\n    })) }}\n  }\n}",
        "options": {}
      },
      "id": "monday-patch",
      "name": "monday: patch revised + clear notes",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1520,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"ok\": true, \"pulseId\": {{ JSON.stringify($('Extract revised text').first().json.pulseId) }} }",
        "options": {}
      },
      "id": "respond",
      "name": "Respond 200",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1740,
        300
      ]
    }
  ],
  "connections": {
    "Webhook: /marketa-revise": {
      "main": [
        [
          {
            "node": "monday: fetch item",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "monday: fetch item": {
      "main": [
        [
          {
            "node": "Normalize item",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize item": {
      "main": [
        [
          {
            "node": "Fetch voice guide",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch voice guide": {
      "main": [
        [
          {
            "node": "Claude: revise draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude: revise draft": {
      "main": [
        [
          {
            "node": "Extract revised text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract revised text": {
      "main": [
        [
          {
            "node": "monday: patch revised + clear notes",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "monday: patch revised + clear notes": {
      "main": [
        [
          {
            "node": "Respond 200",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  }
}