{
  "name": "Weekly Account Enrichment Report",
  "nodes": [
    {
      "id": "a1b2c3d4-0001-0001-0001-000000000001",
      "name": "Every Monday 8am UTC",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 8 * * 1"
            }
          ]
        }
      }
    },
    {
      "id": "a1b2c3d4-0002-0002-0002-000000000002",
      "name": "Get Active SF Accounts",
      "type": "n8n-nodes-base.googleBigQuery",
      "typeVersion": 2.1,
      "position": [
        220,
        300
      ],
      "parameters": {
        "authentication": "serviceAccount",
        "operation": "executeQuery",
        "projectId": {
          "mode": "id",
          "value": "guesty-data"
        },
        "sqlQuery": "SELECT\n  sf_account_id,\n  account_name,\n  account_id AS guesty_account_id,\n  account_segmentation,\n  avg_mrr,\n  website\nFROM `guesty-data.guesty_analytics.dim_accounts`\nWHERE account_active = TRUE\nORDER BY account_name ASC",
        "options": {}
      },
      "credentials": {
        "googleApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "a1b2c3d4-0003-0003-0003-000000000003",
      "name": "Search HubSpot Contact",
      "type": "n8n-nodes-base.hubspot",
      "typeVersion": 2.2,
      "position": [
        440,
        300
      ],
      "onError": "continueRegularOutput",
      "parameters": {
        "authentication": "oAuth2",
        "resource": "contact",
        "operation": "search",
        "returnAll": false,
        "limit": 1,
        "filterGroupsUi": {
          "filterGroupsValues": [
            {
              "filtersUi": {
                "filterValues": [
                  {
                    "propertyName": "salesforce_accountid",
                    "operator": "EQ",
                    "value": "={{ $json.sf_account_id }}"
                  }
                ]
              }
            }
          ]
        },
        "additionalFields": {
          "properties": [
            "email",
            "lifecyclestage",
            "hs_last_activity_date"
          ]
        }
      },
      "credentials": {
        "hubspotOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "a1b2c3d4-0004-0004-0004-000000000004",
      "name": "Merge Account + HubSpot Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        660,
        300
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "f1",
              "name": "sf_account_id",
              "value": "={{ $('Get Active SF Accounts').item.json.sf_account_id }}",
              "type": "string"
            },
            {
              "id": "f2",
              "name": "account_name",
              "value": "={{ $('Get Active SF Accounts').item.json.account_name }}",
              "type": "string"
            },
            {
              "id": "f3",
              "name": "guesty_account_id",
              "value": "={{ $('Get Active SF Accounts').item.json.guesty_account_id }}",
              "type": "string"
            },
            {
              "id": "f4",
              "name": "account_segmentation",
              "value": "={{ $('Get Active SF Accounts').item.json.account_segmentation }}",
              "type": "string"
            },
            {
              "id": "f5",
              "name": "avg_mrr",
              "value": "={{ $('Get Active SF Accounts').item.json.avg_mrr }}",
              "type": "number"
            },
            {
              "id": "f6",
              "name": "website",
              "value": "={{ $('Get Active SF Accounts').item.json.website }}",
              "type": "string"
            },
            {
              "id": "f7",
              "name": "hs_email",
              "value": "={{ $json.properties ? $json.properties.email : '' }}",
              "type": "string"
            },
            {
              "id": "f8",
              "name": "hs_lifecycle_stage",
              "value": "={{ $json.properties ? $json.properties.lifecyclestage : '' }}",
              "type": "string"
            },
            {
              "id": "f9",
              "name": "hs_last_activity_date",
              "value": "={{ $json.properties ? $json.properties.hs_last_activity_date : '' }}",
              "type": "string"
            }
          ]
        }
      }
    },
    {
      "id": "a1b2c3d4-0005-0005-0005-000000000005",
      "name": "Aggregate All Rows",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        880,
        300
      ],
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "rows",
        "include": "allFields"
      }
    },
    {
      "id": "a1b2c3d4-0006-0006-0006-000000000006",
      "name": "Create Google Doc",
      "type": "n8n-nodes-base.googleDocs",
      "typeVersion": 2,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "authentication": "oAuth2",
        "resource": "document",
        "operation": "create",
        "driveId": "myDrive",
        "folderId": "root",
        "title": "=Account Enrichment - {{ $now.toFormat('yyyy-MM-dd') }}"
      },
      "credentials": {
        "googleDocsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "a1b2c3d4-0007-0007-0007-000000000007",
      "name": "Write Report Content",
      "type": "n8n-nodes-base.googleDocs",
      "typeVersion": 2,
      "position": [
        1320,
        300
      ],
      "parameters": {
        "authentication": "oAuth2",
        "resource": "document",
        "operation": "update",
        "documentURL": "={{ $json.id }}",
        "actionsUi": {
          "actionFields": [
            {
              "object": "text",
              "action": "insert",
              "locationChoice": "endOfSegmentLocation",
              "insertSegment": "body",
              "text": "=Account Enrichment Report - {{ $now.toFormat('yyyy-MM-dd') }}\nGenerated every Monday at 8:00 AM UTC\n\nACCOUNT NAME | SF ACCOUNT ID | GUESTY ID | SEGMENTATION | AVG MRR | WEBSITE | HS EMAIL | LIFECYCLE STAGE | LAST ACTIVITY\n{{ $('Aggregate All Rows').item.json.rows.map(function(r) { return [r.account_name, r.sf_account_id, r.guesty_account_id, r.account_segmentation, r.avg_mrr, r.website, r.hs_email, r.hs_lifecycle_stage, r.hs_last_activity_date].join(' | '); }).join('\\n') }}"
            }
          ]
        }
      },
      "credentials": {
        "googleDocsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Every Monday 8am UTC": {
      "main": [
        [
          {
            "node": "Get Active SF Accounts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Active SF Accounts": {
      "main": [
        [
          {
            "node": "Search HubSpot Contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search HubSpot Contact": {
      "main": [
        [
          {
            "node": "Merge Account + HubSpot Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Account + HubSpot Data": {
      "main": [
        [
          {
            "node": "Aggregate All Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate All Rows": {
      "main": [
        [
          {
            "node": "Create Google Doc",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Google Doc": {
      "main": [
        [
          {
            "node": "Write Report Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}