{
  "id": "sub006-trends-ingestion-0000000",
  "name": "SUB-006-trends-ingestion",
  "nodes": [
    {
      "parameters": {
        "inputSource": "passthrough"
      },
      "id": "c1000000-0006-0006-0006-000000000001",
      "name": "Trigger",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        500
      ]
    },
    {
      "parameters": {
        "url": "https://trends.google.com/trending/rss?geo=US",
        "options": {}
      },
      "id": "c1000000-0006-0006-0006-000000000002",
      "name": "Fetch Trends RSS",
      "type": "n8n-nodes-base.rssFeedRead",
      "typeVersion": 1.1,
      "position": [
        460,
        500
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "={{ `SELECT COALESCE((SELECT id FROM raw_research WHERE source_url = '${($json.link||'').replace(/'/g,\"''\")}' LIMIT 1), 0) AS existing_id, '${($json.link||'').replace(/'/g,\"''\")}' AS trend_url, '${($json.title||'').replace(/'/g,\"''\")}' AS title, '${($json.contentSnippet||$json.title||'').replace(/'/g,\"''\")}' AS content_text` }}",
        "options": {}
      },
      "id": "c1000000-0006-0006-0006-000000000003",
      "name": "Check Duplicate",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        680,
        500
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "c1000000-0006-0006-0006-000000000040",
              "leftValue": "={{ $json.existing_id }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "c1000000-0006-0006-0006-000000000004",
      "name": "Is New?",
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.2,
      "position": [
        900,
        500
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "={{ `INSERT INTO raw_research (channel_id, source_type, source_url, source_name, content_title, content_text, status, created_at) VALUES (${$('Trigger').item.json.channel_id}, 'google_trends', '${($json.trend_url||'').replace(/'/g,\"''\")}', 'Google Trends', '${($json.title||'').replace(/'/g,\"''\")}', '${($json.content_text||'').replace(/'/g,\"''\")}', 'ingested', NOW()) RETURNING id` }}",
        "options": {}
      },
      "id": "c1000000-0006-0006-0006-000000000005",
      "name": "Insert Raw Research",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1120,
        500
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "inserted_items",
        "options": {}
      },
      "id": "c1000000-0006-0006-0006-000000000006",
      "name": "Aggregate Results",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1340,
        500
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "c1000000-0006-0006-0006-000000000050",
              "name": "items_inserted",
              "value": "={{ $json.inserted_items.length }}",
              "type": "number"
            },
            {
              "id": "c1000000-0006-0006-0006-000000000051",
              "name": "keyword",
              "value": "={{ $('Trigger').item.json.keyword }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "c1000000-0006-0006-0006-000000000007",
      "name": "Return",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1560,
        500
      ]
    }
  ],
  "connections": {
    "Trigger": {
      "main": [
        [
          {
            "node": "Fetch Trends RSS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Trends RSS": {
      "main": [
        [
          {
            "node": "Check Duplicate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Duplicate": {
      "main": [
        [
          {
            "node": "Is New?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is New?": {
      "main": [
        [
          {
            "node": "Insert Raw Research",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert Raw Research": {
      "main": [
        [
          {
            "node": "Aggregate Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Results": {
      "main": [
        [
          {
            "node": "Return",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}