{
  "name": "v2 Like Minds \u2014 CRM Sync",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 0 6 * * *"
            }
          ]
        }
      },
      "id": "77bbfdf9-7d6d-467b-afe5-ced66076ecc5",
      "name": "Daily 6:00am",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        96
      ]
    },
    {
      "parameters": {
        "method": "DELETE",
        "url": "https://marketing-agent-production-8efa.up.railway.app/api/memory",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ tag: \"[CRM]\" }) }}",
        "options": {}
      },
      "id": "def66e67-815f-4df5-a755-bcb8572337c4",
      "name": "Clear CRM Memory",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        224,
        96
      ],
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "",
          "cachedResultName": "CRM Master Sheet"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Active Projects",
          "cachedResultName": "Active Projects"
        },
        "options": {}
      },
      "id": "9aa6d3cf-8c73-4734-8205-de5da6d26828",
      "name": "Read Active Projects (Sync)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        448,
        0
      ]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "",
          "cachedResultName": "CRM Master Sheet"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Pipeline",
          "cachedResultName": "Pipeline"
        },
        "options": {}
      },
      "id": "ac79c9f6-a4ef-40ce-8353-cfc4f5c3c230",
      "name": "Read Pipeline (Sync)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        448,
        192
      ]
    },
    {
      "parameters": {
        "jsCode": "const out = [];\nfor (const item of $input.all()) {\n  out.push({ json: { ...item.json, _source: \"Active Projects\" } });\n}\nreturn out;"
      },
      "id": "ef7e82c0-571d-48be-b9e3-97a7e28aafe6",
      "name": "Tag Active (Sync)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        672,
        0
      ]
    },
    {
      "parameters": {},
      "id": "569282df-637e-4022-aa7e-5c0d6708dc1b",
      "name": "Combine Rows (Sync)",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        896,
        96
      ]
    },
    {
      "parameters": {
        "jsCode": "const out = [];\nfor (const item of $input.all()) {\n  out.push({ json: { ...item.json, _source: \"Pipeline\" } });\n}\nreturn out;"
      },
      "id": "b023aebd-8412-46c9-9b4b-521f1874f71f",
      "name": "Tag Pipeline (Sync)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        672,
        192
      ]
    },
    {
      "parameters": {
        "jsCode": "const out = [];\nconst headerValues = [\"PROJECT\", \"DEAL / PROJECT\", \"DEAL\", \"NAME\"];\nfor (const item of $input.all()) {\n  const r = item.json;\n  const source = r._source;\n  let name = \"\";\n  let content = \"\";\n  if (source === \"Pipeline\") {\n    name = r['DEAL / PROJECT'] || '';\n    const stage = r['STAGE\\n(dropdown)'] || r['STAGE'] || '';\n    const fee = r['FEE\\n(EX GST)'] || r['FEE'] || '';\n    const nextAction = r['NEXT ACTION'] || '';\n    const contact = r['CONTACT'] || '';\n    const notes = r['NOTES (Gmail verified)'] || '';\n    content = `[CRM] Pipeline: ${name} | Stage: ${stage} | Fee: ${fee} | Contact: ${contact} | Next: ${nextAction} | Notes: ${notes}`;\n  } else {\n    name = r['PROJECT'] || '';\n    const phase = r['DELIVERY PHASE\\n(dropdown)'] || r['DELIVERY PHASE'] || '';\n    const designer = r['DESIGNER\\n(lead)'] || r['DESIGNER'] || '';\n    const fee = r['FEE\\n(EX GST)'] || r['FEE'] || '';\n    const milestone = r['NEXT MILESTONE'] || '';\n    const wip = r[\"THIS WEEK'S WIP\"] || '';\n    const health = r['HEALTH'] || '';\n    const notes = r['NOTES / BLOCKERS'] || '';\n    content = `[CRM] Active Project: ${name} | Phase: ${phase} | Designer: ${designer} | Fee: ${fee} | Health: ${health} | Next: ${milestone} | WIP: ${wip} | Notes: ${notes}`;\n  }\n  const trimmed = String(name).trim();\n  if (!trimmed) continue;\n  if (headerValues.includes(trimmed.toUpperCase())) continue;\n  out.push({ json: { content } });\n}\nreturn out;"
      },
      "id": "05242eb0-1701-47c8-859f-e76e4e49932b",
      "name": "Format Rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        96
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://marketing-agent-production-8efa.up.railway.app/api/memory",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ content: $json.content }) }}",
        "options": {}
      },
      "id": "1592da2e-6062-4a9f-8300-2d9248d05338",
      "name": "POST to Memory",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1344,
        96
      ],
      "credentials": {
        "httpBearerAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "crm-search",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "264e7908-bf50-4c08-9a63-f60f75bd52ba",
      "name": "Search Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        512
      ]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "",
          "cachedResultName": "CRM Master Sheet"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Active Projects",
          "cachedResultName": "Active Projects"
        },
        "options": {}
      },
      "id": "9c6fe548-2192-495d-b9c1-b322c6ccf9a1",
      "name": "Read Active Projects (Search)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        224,
        416
      ]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "",
          "cachedResultName": "CRM Master Sheet"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Pipeline",
          "cachedResultName": "Pipeline"
        },
        "options": {}
      },
      "id": "41023f6e-dac2-4853-8aa8-5e6ed23af554",
      "name": "Read Pipeline (Search)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        224,
        608
      ]
    },
    {
      "parameters": {
        "jsCode": "const out = [];\nfor (const item of $input.all()) {\n  out.push({ json: { ...item.json, _source: \"Active Projects\" } });\n}\nreturn out;"
      },
      "id": "42ace45e-e13c-45bc-a16c-40a50800c9c6",
      "name": "Tag Active (Search)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        448,
        416
      ]
    },
    {
      "parameters": {},
      "id": "42f2afad-fbe4-4087-9db6-7f6adce20265",
      "name": "Combine Rows (Search)",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        672,
        512
      ]
    },
    {
      "parameters": {
        "jsCode": "const out = [];\nfor (const item of $input.all()) {\n  out.push({ json: { ...item.json, _source: \"Pipeline\" } });\n}\nreturn out;"
      },
      "id": "be5c56f1-21ea-4838-83db-bccfd27181b5",
      "name": "Tag Pipeline (Search)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        448,
        608
      ]
    },
    {
      "parameters": {
        "jsCode": "const query = String($('Search Webhook').first().json.body.query ?? '').toLowerCase().trim();\nconst rows = $input.all().map((i) => i.json);\nlet matches = rows;\nif (query) {\n  matches = rows.filter((r) => {\n    for (const key of Object.keys(r)) {\n      const v = r[key];\n      if (v !== null && v !== undefined && String(v).toLowerCase().includes(query)) {\n        return true;\n      }\n    }\n    return false;\n  });\n}\nreturn [{ json: { query, count: matches.length, matches } }];"
      },
      "id": "fe0a8955-bd42-4215-9cba-d14a85c13c8a",
      "name": "Filter Matches",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        896,
        512
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}",
        "options": {}
      },
      "id": "5e91749d-6dac-4920-9620-afebeebe2bd9",
      "name": "Respond Search",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        1120,
        512
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "crm-update",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "31094854-2366-4d20-8f8c-378fbda943e1",
      "name": "Update Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        928
      ]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "",
          "cachedResultName": "CRM Master Sheet"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Active Projects",
          "cachedResultName": "Active Projects"
        },
        "options": {}
      },
      "id": "ccc75216-4d7c-40fb-b882-078512d1ebc4",
      "name": "Read Active Projects (Update)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        224,
        832
      ]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "",
          "cachedResultName": "CRM Master Sheet"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Pipeline",
          "cachedResultName": "Pipeline"
        },
        "options": {}
      },
      "id": "f5c24903-1ba1-4926-857b-bcc2efffa26a",
      "name": "Read Pipeline (Update)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        224,
        1024
      ]
    },
    {
      "parameters": {
        "jsCode": "const out = [];\nfor (const item of $input.all()) {\n  out.push({ json: { ...item.json, _source: \"Active Projects\" } });\n}\nreturn out;"
      },
      "id": "02d5dd12-e72b-45d1-bfa4-109d55695ec0",
      "name": "Tag Active (Update)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        448,
        832
      ]
    },
    {
      "parameters": {},
      "id": "6f6fc93f-401f-4ec2-af47-bef904c9f8ac",
      "name": "Combine Rows (Update)",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        672,
        928
      ]
    },
    {
      "parameters": {
        "jsCode": "const out = [];\nfor (const item of $input.all()) {\n  out.push({ json: { ...item.json, _source: \"Pipeline\" } });\n}\nreturn out;"
      },
      "id": "b17a93da-c234-415f-8963-d12d09a43f42",
      "name": "Tag Pipeline (Update)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        448,
        1024
      ]
    },
    {
      "parameters": {
        "jsCode": "const req = $('Update Webhook').first().json.body || {};\nconst client = String(req.client ?? '').toLowerCase().trim();\nconst field = String(req.field ?? '');\nconst value = req.value;\nconst rows = $input.all().map((i) => i.json);\nconst nameOf = (r) => String((r._source === \"Pipeline\" ? r[\"DEAL / PROJECT\"] : r[\"PROJECT\"]) ?? \"\").toLowerCase().trim();\nconst match = rows.find((r) => nameOf(r) === client);\nif (!match) {\n  return [{ json: { found: false, preview: 'No matching record found for \"' + (req.client ?? '') + '\"', rowIndex: null } }];\n}\nconst displayName = match._source === \"Pipeline\" ? match[\"DEAL / PROJECT\"] : match[\"PROJECT\"];\nconst oldValue = match[field];\nconst preview = '\"' + displayName + '\" (' + match._source + ', row ' + match.row_number + '): ' +\n  field + ' will change from \"' + (oldValue ?? '') + '\" to \"' + (value ?? '') + '\"';\nreturn [{ json: { found: true, preview, rowIndex: match.row_number, source: match._source, name: displayName, field, oldValue: oldValue ?? null, newValue: value ?? null } }];"
      },
      "id": "756e1ede-686e-444d-9996-5ad214866eed",
      "name": "Build Preview",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        896,
        928
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}",
        "options": {}
      },
      "id": "bd64d062-d53e-4ac2-992b-c3be161653ff",
      "name": "Respond Preview",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        1120,
        928
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "crm-update-confirm",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "84946181-2205-4448-8e75-fa4ae8142b70",
      "name": "Confirm Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        1344
      ]
    },
    {
      "parameters": {
        "jsCode": "const req = $('Confirm Webhook').first().json.body || {};\nconst rowIndex = req.rowIndex;\nconst field = req.field;\nconst value = req.value;\nconst source = req.source || 'Pipeline';\nif (rowIndex === undefined || rowIndex === null || !field) {\n  throw new Error('rowIndex and field are required to confirm an update');\n}\nconst row = { row_number: rowIndex, _source: source };\nrow[field] = value;\nreturn [{ json: row }];"
      },
      "id": "e4631a76-7a6d-4cb5-bc26-7077b319a745",
      "name": "Prep Update Row",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        1344
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "source-active",
              "leftValue": "={{ $json._source }}",
              "rightValue": "Active Projects",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "88aabc24-1c69-4b08-97e7-a47391b2d797",
      "name": "Route By Source",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        448,
        1344
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "",
          "cachedResultName": "CRM Master Sheet"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Active Projects",
          "cachedResultName": "Active Projects"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [
            "row_number"
          ],
          "schema": [
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "handlingExtraData": "ignoreIt"
        }
      },
      "id": "4040fe2e-10c3-4468-a8f3-de56ea7e4e48",
      "name": "Update Active Projects Row",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        672,
        1248
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, updated: $json }) }}",
        "options": {}
      },
      "id": "95cc8eb2-3e3b-4048-9a59-e8c1b373b76b",
      "name": "Respond Confirm",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        896,
        1344
      ]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "",
          "cachedResultName": "CRM Master Sheet"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Pipeline",
          "cachedResultName": "Pipeline"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [
            "row_number"
          ],
          "schema": [
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "handlingExtraData": "ignoreIt"
        }
      },
      "id": "30aed242-8a5e-4ab8-a0df-c5cc4d2eaa06",
      "name": "Update Pipeline Row",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        672,
        1440
      ]
    }
  ],
  "connections": {
    "Daily 6:00am": {
      "main": [
        [
          {
            "node": "Clear CRM Memory",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clear CRM Memory": {
      "main": [
        [
          {
            "node": "Read Active Projects (Sync)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Read Pipeline (Sync)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Active Projects (Sync)": {
      "main": [
        [
          {
            "node": "Tag Active (Sync)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Pipeline (Sync)": {
      "main": [
        [
          {
            "node": "Tag Pipeline (Sync)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tag Active (Sync)": {
      "main": [
        [
          {
            "node": "Combine Rows (Sync)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine Rows (Sync)": {
      "main": [
        [
          {
            "node": "Format Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tag Pipeline (Sync)": {
      "main": [
        [
          {
            "node": "Combine Rows (Sync)",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Format Rows": {
      "main": [
        [
          {
            "node": "POST to Memory",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Webhook": {
      "main": [
        [
          {
            "node": "Read Active Projects (Search)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Read Pipeline (Search)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Active Projects (Search)": {
      "main": [
        [
          {
            "node": "Tag Active (Search)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Pipeline (Search)": {
      "main": [
        [
          {
            "node": "Tag Pipeline (Search)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tag Active (Search)": {
      "main": [
        [
          {
            "node": "Combine Rows (Search)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine Rows (Search)": {
      "main": [
        [
          {
            "node": "Filter Matches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tag Pipeline (Search)": {
      "main": [
        [
          {
            "node": "Combine Rows (Search)",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Filter Matches": {
      "main": [
        [
          {
            "node": "Respond Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Webhook": {
      "main": [
        [
          {
            "node": "Read Active Projects (Update)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Read Pipeline (Update)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Active Projects (Update)": {
      "main": [
        [
          {
            "node": "Tag Active (Update)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Pipeline (Update)": {
      "main": [
        [
          {
            "node": "Tag Pipeline (Update)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tag Active (Update)": {
      "main": [
        [
          {
            "node": "Combine Rows (Update)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine Rows (Update)": {
      "main": [
        [
          {
            "node": "Build Preview",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tag Pipeline (Update)": {
      "main": [
        [
          {
            "node": "Combine Rows (Update)",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Build Preview": {
      "main": [
        [
          {
            "node": "Respond Preview",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Confirm Webhook": {
      "main": [
        [
          {
            "node": "Prep Update Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prep Update Row": {
      "main": [
        [
          {
            "node": "Route By Source",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route By Source": {
      "main": [
        [
          {
            "node": "Update Active Projects Row",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Update Pipeline Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Active Projects Row": {
      "main": [
        [
          {
            "node": "Respond Confirm",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Pipeline Row": {
      "main": [
        [
          {
            "node": "Respond Confirm",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "versionId": "e8726c0c-f42a-4ce6-8ce3-4b30f443de00",
  "nodeGroups": [],
  "id": "9kYAzcv3j3KDnixL",
  "tags": []
}