{
  "name": "INGEST Agent Workflow",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -1536,
        -288
      ],
      "id": "manual_trigger_node",
      "name": "Manual Trigger"
    },
    {
      "parameters": {
        "operation": "getAll",
        "limit": 10,
        "filters": {
          "q": "has:attachment newer_than:1d"
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        -1344,
        -288
      ],
      "id": "YOUR_ID",
      "name": "Gmail \u2014 Get many messages",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "get",
        "messageId": "={{$json.id}}",
        "simple": false,
        "options": {
          "downloadAttachments": true
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        -1120,
        -288
      ],
      "id": "YOUR_ID",
      "name": "Gmail \u2014 (Get Message + Download Attachments)",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "$binary",
        "include": "allOtherFields",
        "options": {}
      },
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        -864,
        -288
      ],
      "id": "YOUR_ID",
      "name": "Split Out \u2014 (1 attachment = 1 item)"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "YOUR_ID",
              "leftValue": "={{ Object.keys($binary ?? {}).length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -624,
        -288
      ],
      "id": "YOUR_ID",
      "name": "IF Node \u2014 Condition: Item has $binary"
    },
    {
      "parameters": {
        "inputDataFieldName": "={{ Object.keys($binary)[0] }}",
        "name": "={{ $binary[Object.keys($binary)[0]].fileName }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "YOUR_GOOGLE_DRIVE_FOLDER_ID",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -336,
        -384
      ],
      "id": "YOUR_ID",
      "name": "Google Drive \u2014 Upload file to RAW folder",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "list",
          "cachedResultName": "AI_Factory_Control",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1G4x50V3uHSy-jCFt_pf-ZDpvlJN9HPX018-ju8PHdNY/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "YOUR_SHEET_ID",
          "mode": "list",
          "cachedResultName": "DATA_CATALOG",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1G4x50V3uHSy-jCFt_pf-ZDpvlJN9HPX018-ju8PHdNY/edit#gid=0"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "source": "gmail",
            "status": "raw",
            "added_at": "={{$now}}",
            "filename": "={{ $json[\"originalFilename\"] }}",
            "mime": "={{ $json[\"mimeType\"] }}",
            "raw_path": "={{ $node[\"Google Drive \u2014 Upload file to RAW folder\"].json[\"webViewLink\"] }}",
            "size": "={{ (Number($json[\"size\"]) / 1024).toFixed(2) }} KB",
            "email_id": "={{ $node[\"Split Out \u2014 (1 attachment = 1 item)\"].json[\"messageId\"] }}",
            "id": "={{ $json[\"id\"] }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "source",
              "displayName": "source",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "email_id",
              "displayName": "email_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "filename",
              "displayName": "filename",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "mime",
              "displayName": "mime",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "size",
              "displayName": "size",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "raw_path",
              "displayName": "raw_path",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "clean_path",
              "displayName": "clean_path",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "gold_path",
              "displayName": "gold_path",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "added_at",
              "displayName": "added_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -112,
        -384
      ],
      "id": "YOUR_ID",
      "name": "Google Sheets \u2014 Append (DATA_CATALOG)",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "list",
          "cachedResultName": "AI_Factory_Control",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1G4x50V3uHSy-jCFt_pf-ZDpvlJN9HPX018-ju8PHdNY/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "YOUR_SHEET_ID",
          "mode": "list",
          "cachedResultName": "EVENT_LOG",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1G4x50V3uHSy-jCFt_pf-ZDpvlJN9HPX018-ju8PHdNY/edit#gid=2072862873"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "ts": "={{ $now }}",
            "agent": "ingest",
            "action": "save_to_raw_folder",
            "notes": "Attachment saved to RAW folder",
            "input": "={{ $node[\"Split Out \u2014 (1 attachment = 1 item)\"].json[\"messageId\"] }}",
            "output": "={{ $node[\"Google Drive \u2014 Upload file to RAW folder\"].json[\"webViewLink\"] }}",
            "status": "={{ $node[\"Google Sheets \u2014 Append (DATA_CATALOG)\"].json ? \"success\" : \"error\" }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "ts",
              "displayName": "ts",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "agent",
              "displayName": "agent",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "action",
              "displayName": "action",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "input",
              "displayName": "input",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "output",
              "displayName": "output",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "notes",
              "displayName": "notes",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        112,
        -384
      ],
      "id": "YOUR_ID",
      "name": "Google Sheets \u2014 Append (EVENT_LOG)",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "content": "INGEST Agent",
        "height": 576,
        "width": 1920,
        "color": 6
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1616,
        -544
      ],
      "typeVersion": 1,
      "id": "YOUR_ID",
      "name": "Sticky Note - INGEST"
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Gmail \u2014 Get many messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail \u2014 Get many messages": {
      "main": [
        [
          {
            "node": "Gmail \u2014 (Get Message + Download Attachments)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail \u2014 (Get Message + Download Attachments)": {
      "main": [
        [
          {
            "node": "Split Out \u2014 (1 attachment = 1 item)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out \u2014 (1 attachment = 1 item)": {
      "main": [
        [
          {
            "node": "IF Node \u2014 Condition: Item has $binary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Node \u2014 Condition: Item has $binary": {
      "main": [
        [
          {
            "node": "Google Drive \u2014 Upload file to RAW folder",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Google Drive \u2014 Upload file to RAW folder": {
      "main": [
        [
          {
            "node": "Google Sheets \u2014 Append (DATA_CATALOG)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets \u2014 Append (DATA_CATALOG)": {
      "main": [
        [
          {
            "node": "Google Sheets \u2014 Append (EVENT_LOG)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "YOUR_ID",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "ingest_agent_manual_trigger",
  "tags": []
}