{
  "name": "Weekly price-watch email",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "weeksInterval": 1,
              "triggerAtDay": [
                1
              ],
              "triggerAtHour": 7
            }
          ]
        }
      },
      "id": "b2000000-0000-4000-8000-000000000001",
      "name": "Every Monday",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        200
      ]
    },
    {
      "parameters": {
        "resource": "scrap",
        "operation": "run",
        "scrapId": {
          "__rl": true,
          "mode": "id",
          "value": "PASTE_THE_BACKCOUNTRY_SCRAP_ID"
        },
        "waitForResult": true,
        "parameters": "{}",
        "output": "rows"
      },
      "id": "b2000000-0000-4000-8000-000000000002",
      "name": "Backcountry",
      "type": "@trawlme/n8n-nodes-trawl.trawl",
      "typeVersion": 1,
      "position": [
        360,
        40
      ],
      "alwaysOutputData": true,
      "credentials": {
        "trawlApi": {
          "id": "trawl-account",
          "name": "Trawl account"
        }
      }
    },
    {
      "parameters": {
        "resource": "scrap",
        "operation": "run",
        "scrapId": {
          "__rl": true,
          "mode": "id",
          "value": "PASTE_THE_FREERIDE_SCRAP_ID"
        },
        "waitForResult": true,
        "parameters": "{}",
        "output": "rows"
      },
      "id": "b2000000-0000-4000-8000-000000000003",
      "name": "Freeride",
      "type": "@trawlme/n8n-nodes-trawl.trawl",
      "typeVersion": 1,
      "position": [
        360,
        200
      ],
      "alwaysOutputData": true,
      "credentials": {
        "trawlApi": {
          "id": "trawl-account",
          "name": "Trawl account"
        }
      }
    },
    {
      "parameters": {
        "resource": "scrap",
        "operation": "run",
        "scrapId": {
          "__rl": true,
          "mode": "id",
          "value": "PASTE_THE_WINTER_SCRAP_ID"
        },
        "waitForResult": true,
        "parameters": "{}",
        "output": "rows"
      },
      "id": "b2000000-0000-4000-8000-000000000004",
      "name": "Winter",
      "type": "@trawlme/n8n-nodes-trawl.trawl",
      "typeVersion": 1,
      "position": [
        360,
        360
      ],
      "alwaysOutputData": true,
      "credentials": {
        "trawlApi": {
          "id": "trawl-account",
          "name": "Trawl account"
        }
      }
    },
    {
      "parameters": {
        "numberInputs": 3
      },
      "id": "b2000000-0000-4000-8000-000000000005",
      "name": "Wait for all three",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        620,
        200
      ]
    },
    {
      "parameters": {
        "jsCode": "const pages = ['Backcountry', 'Freeride', 'Winter'];\nconst money = n => '$' + Number(n).toFixed(2);\nconst td = (value, extra = '') =>\n  '<td style=\"padding:8px 12px;' + extra + '\">' + value + '</td>';\n\nconst stats = pages.map(page => {\n  const rows = $(page).all()\n    .map(item => item.json)\n    .filter(row => row.name && row.price_usd != null);\n  if (!rows.length) return { page, count: 0 };\n  const cheapest = rows.reduce((a, b) => (b.price_usd < a.price_usd ? b : a));\n  const avg = rows.reduce((sum, r) => sum + r.price_usd, 0) / rows.length;\n  return { page, count: rows.length, cheapest, avg };\n});\n\nconst found = stats.filter(s => s.count);\nconst lowest = found.length\n  ? Math.min(...found.map(s => s.cheapest.price_usd))\n  : null;\nconst green = 'background:#d1fae5;font-weight:bold;';\n\nconst lines = stats.map(s => {\n  if (!s.count) return '<tr>' + td(s.page) + td('No rows') + '</tr>';\n  const best = s.cheapest.price_usd === lowest ? green : '';\n  return '<tr>' + td(s.page) + td(s.count)\n    + td(money(s.cheapest.price_usd), best) + td(s.cheapest.name)\n    + td(money(s.avg)) + '</tr>';\n}).join('');\n\nconst head = ['Page', 'Listings', 'Lowest price', 'Cheapest item', 'Average']\n  .map(h => '<th style=\"padding:8px 12px;\">' + h + '</th>').join('');\nconst html = '<h2>Weekly price watch</h2><table>'\n  + '<tr style=\"text-align:left\">' + head + '</tr>' + lines + '</table>';\n\nconst subject = lowest === null\n  ? 'Weekly price watch: no prices this week'\n  : 'Weekly price watch: lowest price ' + money(lowest);\n\nreturn [{ json: { html, subject } }];\n"
      },
      "id": "b2000000-0000-4000-8000-000000000006",
      "name": "Build table",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        860,
        200
      ]
    },
    {
      "parameters": {
        "fromEmail": "price-watch@example.com",
        "toEmail": "you@example.com",
        "subject": "={{ $json.subject }}",
        "emailFormat": "html",
        "html": "={{ $json.html }}",
        "options": {}
      },
      "id": "b2000000-0000-4000-8000-000000000007",
      "name": "Send email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1100,
        200
      ],
      "credentials": {
        "smtp": {
          "id": "smtp-account",
          "name": "SMTP account"
        }
      }
    }
  ],
  "connections": {
    "Every Monday": {
      "main": [
        [
          {
            "node": "Backcountry",
            "type": "main",
            "index": 0
          },
          {
            "node": "Freeride",
            "type": "main",
            "index": 0
          },
          {
            "node": "Winter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Backcountry": {
      "main": [
        [
          {
            "node": "Wait for all three",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Freeride": {
      "main": [
        [
          {
            "node": "Wait for all three",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Winter": {
      "main": [
        [
          {
            "node": "Wait for all three",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "Wait for all three": {
      "main": [
        [
          {
            "node": "Build table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build table": {
      "main": [
        [
          {
            "node": "Send email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}