{
 "name": "Insider buying digest (Edgrapi)",
 "nodes": [
  {
   "parameters": {
    "rule": {
     "interval": [
      {
       "field": "cronExpression",
       "expression": "0 7 * * 1-5"
      }
     ]
    }
   },
   "name": "Weekdays at 7am",
   "type": "n8n-nodes-base.scheduleTrigger",
   "typeVersion": 1.2,
   "position": [
    0,
    0
   ]
  },
  {
   "parameters": {
    "url": "https://api.edgrapi.com/v1/insider/latest",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "sendQuery": true,
    "queryParameters": {
     "parameters": [
      {
       "name": "min_value",
       "value": "50000"
      },
      {
       "name": "limit",
       "value": "50"
      }
     ]
    },
    "options": {}
   },
   "name": "Latest insider buys",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    220,
    0
   ],
   "credentials": {
    "httpHeaderAuth": {
     "id": "",
     "name": "Edgrapi API key"
    }
   }
  },
  {
   "parameters": {
    "url": "https://api.edgrapi.com/v1/insider/clusters",
    "authentication": "genericCredentialType",
    "genericAuthType": "httpHeaderAuth",
    "sendQuery": true,
    "queryParameters": {
     "parameters": [
      {
       "name": "days",
       "value": "15"
      },
      {
       "name": "min_insiders",
       "value": "3"
      }
     ]
    },
    "options": {}
   },
   "name": "Cluster buys",
   "type": "n8n-nodes-base.httpRequest",
   "typeVersion": 4.2,
   "position": [
    440,
    0
   ],
   "credentials": {
    "httpHeaderAuth": {
     "id": "",
     "name": "Edgrapi API key"
    }
   }
  },
  {
   "parameters": {
    "jsCode": "// n8n Code node: \"Build digest\". Mode: Run Once for All Items.\n// Reads the two HTTP Request nodes and returns one item with the email subject + HTML,\n// or no items at all when there's nothing worth sending (so the Send Email node doesn't run).\nconst MIN_VALUE = 100000;\nconst buys = $('Latest insider buys').first().json.buys || [];\nconst clusters = $('Cluster buys').first().json.clusters || [];\n\nconst esc = s => String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');\nconst usd = n => '$' + Math.round(n).toLocaleString('en-US');\n\n// Officers and directors only: drop buyers whose only role is \"10% owner\" (mostly funds)\nconst keep = buys.filter(b => b.buy_value >= MIN_VALUE &&\n  !((b.relationship || []).length === 1 && b.relationship[0] === '10% owner'));\n\nif (keep.length === 0 && clusters.length === 0) return [];\n\nconst rows = keep.map(b => `<tr>\n  <td><b>${esc(b.ticker)}</b><br><span style=\"color:#666\">${esc(b.company)}</span></td>\n  <td>${esc(b.owner)}<br><span style=\"color:#666\">${esc(b.officer_title || (b.relationship || []).join(', '))}</span></td>\n  <td style=\"text-align:right\">${usd(b.buy_value)}</td>\n  <td><a href=\"${esc(b.filed_url)}\">Form 4</a></td></tr>`).join('');\n\nconst clusterRows = clusters.slice(0, 10).map(c => `<li><b>${esc(c.ticker)}</b> ${esc(c.company)}:\n  ${c.insider_count} insiders, ${usd(c.total_value)}</li>`).join('');\n\nconst html = `<h2 style=\"font-family:sans-serif\">Insider buys, last 24 hours</h2>\n${keep.length ? `<table cellpadding=\"6\" style=\"border-collapse:collapse;font-family:sans-serif;font-size:14px\">\n<tr style=\"background:#eee\"><th align=\"left\">Company</th><th align=\"left\">Insider</th><th>Bought</th><th></th></tr>\n${rows}</table>` : '<p>No open-market buys over $100,000 today.</p>'}\n<h3 style=\"font-family:sans-serif\">Cluster buys, last 15 days</h3>\n<ul style=\"font-family:sans-serif;font-size:14px\">${clusterRows || '<li>None.</li>'}</ul>\n<p style=\"color:#888;font-size:12px;font-family:sans-serif\">Open-market purchases (Form 4, code P) from SEC EDGAR.\nNot investment advice.</p>`;\n\nconst top = keep[0];\nconst subject = keep.length\n  ? `${keep.length} insider buy${keep.length > 1 ? 's' : ''}: ${top.ticker} ${usd(top.buy_value)} leads`\n  : `${clusters.length} cluster buys this fortnight`;\n\nreturn [{ json: { subject, html, buys: keep.length, clusters: clusters.length } }];\n"
   },
   "name": "Build digest",
   "type": "n8n-nodes-base.code",
   "typeVersion": 2,
   "position": [
    660,
    0
   ]
  },
  {
   "parameters": {
    "fromEmail": "digest@example.com",
    "toEmail": "you@example.com",
    "subject": "={{ $json.subject }}",
    "emailFormat": "html",
    "html": "={{ $json.html }}",
    "options": {
     "appendAttribution": false
    }
   },
   "name": "Email digest",
   "type": "n8n-nodes-base.emailSend",
   "typeVersion": 2.1,
   "position": [
    880,
    0
   ],
   "credentials": {
    "smtp": {
     "id": "",
     "name": "SMTP"
    }
   }
  },
  {
   "parameters": {},
   "name": "Test now",
   "type": "n8n-nodes-base.manualTrigger",
   "typeVersion": 1,
   "position": [
    0,
    200
   ]
  }
 ],
 "connections": {
  "Weekdays at 7am": {
   "main": [
    [
     {
      "node": "Latest insider buys",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Latest insider buys": {
   "main": [
    [
     {
      "node": "Cluster buys",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Cluster buys": {
   "main": [
    [
     {
      "node": "Build digest",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Build digest": {
   "main": [
    [
     {
      "node": "Email digest",
      "type": "main",
      "index": 0
     }
    ]
   ]
  },
  "Test now": {
   "main": [
    [
     {
      "node": "Latest insider buys",
      "type": "main",
      "index": 0
     }
    ]
   ]
  }
 },
 "settings": {
  "executionOrder": "v1",
  "timezone": "America/New_York"
 },
 "active": false
}