{
  "name": "Daily Rep Activity Leaderboard",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 8,
              "triggerAtMinute": 0
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "conditions": {
          "conditions": [
            {
              "leftValue": "={{ new Date().getDay() >= 1 && new Date().getDay() <= 5 }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "weekday-check",
      "name": "Weekday?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "yesterday_start_ms",
              "name": "yesterday_start_ms",
              "value": "={{ new Date(new Date().setHours(0,0,0,0) - 86400000).getTime().toString() }}",
              "type": "string"
            },
            {
              "id": "today_start_ms",
              "name": "today_start_ms",
              "value": "={{ new Date(new Date().setHours(0,0,0,0)).getTime().toString() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "set-dates",
      "name": "Set Dates",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [440, 0]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://api.hubapi.com/crm/v3/owners",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "hubspotApi",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "limit",
              "value": "100"
            }
          ]
        },
        "options": {}
      },
      "id": "fetch-owners",
      "name": "Fetch Owners",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [660, 0],
      "credentials": {
        "hubspotApi": {
          "id": "credential-id",
          "name": "HubSpot API"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.hubapi.com/crm/v3/objects/calls/search",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "hubspotApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={ \"filterGroups\": [{ \"filters\": [{ \"propertyName\": \"hs_timestamp\", \"operator\": \"GTE\", \"value\": \"{{ $('Set Dates').first().json.yesterday_start_ms }}\" }, { \"propertyName\": \"hs_timestamp\", \"operator\": \"LT\", \"value\": \"{{ $('Set Dates').first().json.today_start_ms }}\" }] }], \"properties\": [\"hs_timestamp\", \"hubspot_owner_id\"], \"limit\": 100 }",
        "options": {}
      },
      "id": "fetch-calls",
      "name": "Fetch Calls",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [880, -120],
      "credentials": {
        "hubspotApi": {
          "id": "credential-id",
          "name": "HubSpot API"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.hubapi.com/crm/v3/objects/emails/search",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "hubspotApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={ \"filterGroups\": [{ \"filters\": [{ \"propertyName\": \"hs_timestamp\", \"operator\": \"GTE\", \"value\": \"{{ $('Set Dates').first().json.yesterday_start_ms }}\" }, { \"propertyName\": \"hs_timestamp\", \"operator\": \"LT\", \"value\": \"{{ $('Set Dates').first().json.today_start_ms }}\" }] }], \"properties\": [\"hs_timestamp\", \"hubspot_owner_id\"], \"limit\": 100 }",
        "options": {}
      },
      "id": "fetch-emails",
      "name": "Fetch Emails",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [880, 0],
      "credentials": {
        "hubspotApi": {
          "id": "credential-id",
          "name": "HubSpot API"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.hubapi.com/crm/v3/objects/meetings/search",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "hubspotApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={ \"filterGroups\": [{ \"filters\": [{ \"propertyName\": \"hs_timestamp\", \"operator\": \"GTE\", \"value\": \"{{ $('Set Dates').first().json.yesterday_start_ms }}\" }, { \"propertyName\": \"hs_timestamp\", \"operator\": \"LT\", \"value\": \"{{ $('Set Dates').first().json.today_start_ms }}\" }] }], \"properties\": [\"hs_timestamp\", \"hubspot_owner_id\"], \"limit\": 100 }",
        "options": {}
      },
      "id": "fetch-meetings",
      "name": "Fetch Meetings",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [880, 120],
      "credentials": {
        "hubspotApi": {
          "id": "credential-id",
          "name": "HubSpot API"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const owners = $('Fetch Owners').first().json.results;\nconst calls = $('Fetch Calls').first().json.results || [];\nconst emails = $('Fetch Emails').first().json.results || [];\nconst meetings = $('Fetch Meetings').first().json.results || [];\n\n// Build owner name lookup\nconst ownerMap = {};\nfor (const o of owners) {\n  ownerMap[o.id] = `${o.firstName} ${o.lastName}`.trim() || o.email;\n}\n\n// Count activities per owner\nconst reps = {};\nfunction count(items, type) {\n  for (const item of items) {\n    const ownerId = item.properties.hubspot_owner_id;\n    if (!ownerId) continue;\n    if (!reps[ownerId]) reps[ownerId] = { calls: 0, emails: 0, meetings: 0, total: 0 };\n    reps[ownerId][type]++;\n    reps[ownerId].total++;\n  }\n}\n\ncount(calls, 'calls');\ncount(emails, 'emails');\ncount(meetings, 'meetings');\n\n// Rank by total activity\nconst ranked = Object.entries(reps)\n  .map(([id, counts]) => ({\n    name: ownerMap[id] || `Owner ${id}`,\n    ...counts\n  }))\n  .sort((a, b) => b.total - a.total);\n\n// Assign medals\nconst medals = ['\ud83e\udd47', '\ud83e\udd48', '\ud83e\udd49'];\nconst leaderboard = ranked.map((rep, i) => ({\n  rank: i + 1,\n  medal: medals[i] || `${i + 1}.`,\n  ...rep\n}));\n\nreturn [{ json: { leaderboard, date: new Date(Date.now() - 86400000).toLocaleDateString('en-US', { weekday: 'long', month: 'short', day: 'numeric' }) } }];"
      },
      "id": "rank-reps",
      "name": "Rank Reps",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1100, 0]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "sales-activity",
          "mode": "name"
        },
        "messageType": "block",
        "blocksUi": "={\n  \"blocks\": [\n    {\n      \"type\": \"header\",\n      \"text\": {\n        \"type\": \"plain_text\",\n        \"text\": \"\ud83c\udfc6 Rep Activity Leaderboard\"\n      }\n    },\n    {\n      \"type\": \"context\",\n      \"elements\": [\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"Activity for \" + $json.date\n        }\n      ]\n    },\n    {\n      \"type\": \"divider\"\n    },\n    {\n      \"type\": \"section\",\n      \"text\": {\n        \"type\": \"mrkdwn\",\n        \"text\": $json.leaderboard.map(r => `${r.medal} *${r.name}* \u2014 ${r.total} activities (${r.calls}C ${r.emails}E ${r.meetings}M)`).join('\\n')\n      }\n    },\n    {\n      \"type\": \"context\",\n      \"elements\": [\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"C = Calls | E = Emails | M = Meetings\"\n        }\n      ]\n    }\n  ]\n}",
        "otherOptions": {
          "unfurl_links": false
        }
      },
      "id": "notify-slack",
      "name": "Post Leaderboard",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [1320, 0],
      "credentials": {
        "slackApi": {
          "id": "credential-id",
          "name": "Slack API"
        }
      }
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Weekday?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Weekday?": {
      "main": [
        [
          {
            "node": "Set Dates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Dates": {
      "main": [
        [
          {
            "node": "Fetch Owners",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Owners": {
      "main": [
        [
          {
            "node": "Fetch Calls",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Emails",
            "type": "main",
            "index": 0
          },
          {
            "node": "Fetch Meetings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Calls": {
      "main": [
        [
          {
            "node": "Rank Reps",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Emails": {
      "main": [
        [
          {
            "node": "Rank Reps",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Meetings": {
      "main": [
        [
          {
            "node": "Rank Reps",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rank Reps": {
      "main": [
        [
          {
            "node": "Post Leaderboard",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "active": false,
  "meta": {
    "instanceId": "",
    "templateId": "revi-rep-leaderboard"
  }
}
