{
  "id": "AutomationNoteContentPipeline01",
  "name": "Synthetic Content Editorial Pipeline",
  "description": null,
  "active": false,
  "isArchived": false,
  "nodes": [
    {
      "id": "0f9a9e2b-4ed2-47c7-86bb-08132026c001",
      "webhookId": "a1b2c3d4-2026-4813-8e13-content000001",
      "name": "Receive Content",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "synthetic-content-editorial",
        "authentication": "none",
        "responseMode": "responseNode"
      }
    },
    {
      "name": "Discovery",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        300
      ],
      "parameters": {
        "jsCode": "const input = $json.body ?? $json;\nreturn [{ json: { content_id: input.content_id, title: input.title, body: input.body, approved: input.approved === true, simulate_newsletter_failure: input.simulate_newsletter_failure === true, source: 'synthetic-fixture' } }];"
      },
      "id": "320d014e-21c7-4879-976b-d9e0350fd915"
    },
    {
      "name": "Normalize",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        300
      ],
      "parameters": {
        "jsCode": "const item = $json;\nconst title = typeof item.title === 'string' ? item.title.trim().replace(/\\s+/g, ' ') : '';\nconst body = typeof item.body === 'string' ? item.body.trim().replace(/\\s+/g, ' ') : '';\nconst invalid_fields = [];\nif (!/^content-[0-9]{3}$/.test(item.content_id ?? '')) invalid_fields.push('content_id');\nif (title.length < 5) invalid_fields.push('title');\nif (body.length < 20) invalid_fields.push('body');\nreturn [{ json: { ...item, title, body, valid: invalid_fields.length === 0, invalid_fields } }];"
      },
      "id": "7a2e306f-12d4-433e-b841-39c45add58e1"
    },
    {
      "name": "Is Input Valid",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        660,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true
          },
          "conditions": [
            {
              "leftValue": "={{ $json.valid }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "9a77cfb9-359a-4739-b4f3-99ca5891053d"
    },
    {
      "name": "Deduplicate",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        200
      ],
      "parameters": {
        "jsCode": "const fs = require('fs');\nconst path = '/opt/automation-note/state/pipeline-state.json';\nconst state = fs.existsSync(path) ? JSON.parse(fs.readFileSync(path, 'utf8')) : { items: {}, calls: [] };\nconst record = state.items[$json.content_id] ?? null;\nconst duplicate = record?.status === 'published';\nreturn [{ json: { ...$json, duplicate, existing_draft_id: record?.draft_id ?? null, existing_channels: record?.channels ?? null, audit: { stored_records: Object.keys(state.items).length, publish_attempts: record?.attempts ?? { website: 0, newsletter: 0 } } } }];"
      },
      "id": "350cf4e4-dc08-4b15-abfb-5924e2701928"
    },
    {
      "name": "Is Duplicate",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1100,
        200
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true
          },
          "conditions": [
            {
              "leftValue": "={{ $json.duplicate }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "b699e4d3-f8a0-4ebe-889f-5990f28e517c"
    },
    {
      "name": "Editorial Queue",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1320,
        280
      ],
      "parameters": {
        "jsCode": "return [{ json: { ...$json, queue_status: 'PENDING_HUMAN_APPROVAL' } }];"
      },
      "id": "50e737fa-5c92-4b41-83a2-ea06f7bde4f5"
    },
    {
      "name": "Human Approval gate",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1540,
        280
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true
          },
          "conditions": [
            {
              "leftValue": "={{ $json.approved }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "6e8326c5-7e45-4430-9a22-a5b635a10bbb"
    },
    {
      "name": "Create Draft",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1760,
        180
      ],
      "parameters": {
        "jsCode": "const fs = require('fs');\nconst path = '/opt/automation-note/state/pipeline-state.json';\nconst state = fs.existsSync(path) ? JSON.parse(fs.readFileSync(path, 'utf8')) : { items: {}, calls: [] };\nconst record = state.items[$json.content_id] ?? null;\nconst draft_id = record?.draft_id ?? `draft-${$json.content_id}`;\nreturn [{ json: { ...$json, draft_id, draft_reused: Boolean(record?.draft_id), previous_channels: record?.channels ?? {}, previous_attempts: record?.attempts ?? { website: 0, newsletter: 0 } } }];"
      },
      "id": "166e4cfb-cc8e-40c4-a64f-16b8d1802ff2"
    },
    {
      "name": "Review",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1980,
        180
      ],
      "parameters": {
        "jsCode": "return [{ json: { ...$json, review_status: 'APPROVED', review_notes: 'synthetic editorial review passed' } }];"
      },
      "id": "f217d8c1-9e67-4a26-bd6e-52be9a4b90d7"
    },
    {
      "name": "Publish",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2200,
        180
      ],
      "parameters": {
        "jsCode": "const fs = require('fs');\nconst path = '/opt/automation-note/state/pipeline-state.json';\nconst state = fs.existsSync(path) ? JSON.parse(fs.readFileSync(path, 'utf8')) : { items: {}, calls: [] };\nconst previous = state.items[$json.content_id] ?? { attempts: { website: 0, newsletter: 0 }, channels: {} };\nconst attempts = { ...previous.attempts };\nconst channels = { ...previous.channels };\nconst attempted_channels = [];\nfor (const channel of ['website', 'newsletter']) {\n  if (channels[channel] === 'published') continue;\n  attempted_channels.push(channel);\n  attempts[channel] = (attempts[channel] ?? 0) + 1;\n  const result = channel === 'newsletter' && $json.simulate_newsletter_failure === true ? 'retryable_failure' : 'published';\n  channels[channel] = result;\n  state.calls.push({ content_id: $json.content_id, channel, attempt: attempts[channel], result });\n}\nconst publish_status = Object.values(channels).every((status) => status === 'published') ? 'published' : 'partial_failure';\nstate.items[$json.content_id] = { draft_id: $json.draft_id, channels, attempts, status: publish_status };\nfs.writeFileSync(path, JSON.stringify(state, null, 2) + '\\n');\nreturn [{ json: { ...$json, channels, attempted_channels, publish_status, safe_retry: $json.draft_reused && attempted_channels.length < 2, audit: { stored_records: Object.keys(state.items).length, publish_attempts: attempts } } }];"
      },
      "id": "83a6a94c-ef32-459a-8bba-010d39263b9b"
    },
    {
      "name": "Per-channel status tracking",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2420,
        180
      ],
      "parameters": {
        "jsCode": "return [{ json: { ok: true, status: $json.publish_status, content_id: $json.content_id, draft_id: $json.draft_id, queue_status: $json.queue_status, review_status: $json.review_status, channels: $json.channels, attempted_channels: $json.attempted_channels, draft_reused: $json.draft_reused, safe_retry: $json.safe_retry, audit: $json.audit } }];"
      },
      "id": "58f0422d-067d-4a2b-a14d-ed5ebd32e00b"
    },
    {
      "name": "Respond Published",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        2640,
        180
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {
          "responseCode": 201
        }
      },
      "id": "48f240da-4b60-46e7-bfab-6782e9cfac48"
    },
    {
      "name": "Respond Duplicate",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1320,
        80
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { ok: true, status: 'duplicate', content_id: $json.content_id, draft_id: $json.existing_draft_id, audit: $json.audit } }}",
        "options": {
          "responseCode": 200
        }
      },
      "id": "ecf8dd26-e038-43a4-8a38-424dc7f0a4bf"
    },
    {
      "name": "Respond Approval Required",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1760,
        400
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { ok: false, status: 'approval_required', content_id: $json.content_id, queue_status: $json.queue_status, audit: { stored_records: 0, publish_attempts: { website: 0, newsletter: 0 } } } }}",
        "options": {
          "responseCode": 403
        }
      },
      "id": "635edd27-0e8e-4606-9af8-95861a4a1e67"
    },
    {
      "name": "Respond Invalid",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        880,
        460
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { ok: false, status: 'rejected', invalid_fields: $json.invalid_fields, audit: { stored_records: 0, publish_attempts: { website: 0, newsletter: 0 } } } }}",
        "options": {
          "responseCode": 400
        }
      },
      "id": "9f9c0aa0-5115-46ff-a02f-58edb5ae675a"
    }
  ],
  "connections": {
    "Receive Content": {
      "main": [
        [
          {
            "node": "Discovery",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Discovery": {
      "main": [
        [
          {
            "node": "Normalize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize": {
      "main": [
        [
          {
            "node": "Is Input Valid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Input Valid": {
      "main": [
        [
          {
            "node": "Deduplicate",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond Invalid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Deduplicate": {
      "main": [
        [
          {
            "node": "Is Duplicate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Duplicate": {
      "main": [
        [
          {
            "node": "Respond Duplicate",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Editorial Queue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Editorial Queue": {
      "main": [
        [
          {
            "node": "Human Approval gate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Human Approval gate": {
      "main": [
        [
          {
            "node": "Create Draft",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond Approval Required",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Draft": {
      "main": [
        [
          {
            "node": "Review",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Review": {
      "main": [
        [
          {
            "node": "Publish",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Publish": {
      "main": [
        [
          {
            "node": "Per-channel status tracking",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Per-channel status tracking": {
      "main": [
        [
          {
            "node": "Respond Published",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodeGroups": [],
  "pinData": {},
  "versionId": "933c9978-00ef-4412-be23-5ce7f87ad78b",
  "activeVersionId": "933c9978-00ef-4412-be23-5ce7f87ad78b",
  "sourceWorkflowId": null,
  "tags": [],
  "versionMetadata": {
    "name": null,
    "description": null
  }
}
