{
  "name": "Daily task report (sample data)",
  "nodes": [
    {
      "id": "cb1fe74e-b588-4d8d-a7f9-ddbd36a6e23f",
      "name": "Read me first",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -985,
        255
      ],
      "parameters": {
        "content": "## Daily task report (sample data)\nThe daily task report on sample tasks, so you can see it before you connect a sheet.\n\n### You need\n- Nothing. It runs on sample data.\n\n### Test it\n- Click Test workflow, then open the last step.",
        "height": 326,
        "width": 560,
        "color": 4
      }
    },
    {
      "parameters": {},
      "id": "696988fb-6f59-4db0-85db-41facba9d3ce",
      "name": "Run demo",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -345,
        255
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const today = $now.startOf('day');\nconst rows = [\n  ['SMB-01', 'Send monthly invoices', 'Completed', 'Sam', '', today.minus({ days: 2 }).toISODate()],\n  ['SMB-02', 'Renew office supplies order', 'In progress', 'Sam', today.toISODate(), ''],\n  ['CRE-01', 'Approve episode captions', 'Blocked', 'Taylor', today.minus({ days: 1 }).toISODate(), ''],\n  ['CRE-02', 'Upload the finished clip', 'Pending', 'Taylor', today.plus({ days: 2 }).toISODate(), ''],\n  ['SHOP-01', 'Confirm restock quantities', 'Done', 'Alex', '', today.minus({ days: 3 }).toISODate()],\n  ['SHOP-02', 'Resolve carrier delay', 'Waiting', 'Alex', today.minus({ days: 2 }).toISODate(), ''],\n  ['SHOP-03', 'Retire last season’s landing page', 'Cancelled', 'Alex', '', ''],\n  ['SHOP-04', 'Check returns queue', 'Pending', '', '', ''],\n];\nconst columns = ['Task ID', 'Task', 'Status', 'Owner', 'Due Date', 'Completed At'];\nreturn rows.map(row => ({ json: Object.fromEntries(columns.map((column, index) => [column, row[index]])) }));"
      },
      "id": "26dfc76f-e2a2-4ed7-a6ba-5d646e7d82a7",
      "name": "Sample tasks",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -95,
        255
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const now = $now;\nconst reportDate = now.toISODate();\nconst periodStart = now.startOf('day').minus({ days: 6 });\nconst maxListed = 25;\nconst dateOnly = /^\\d{4}-\\d{2}-\\d{2}$/;\nconst zonedTimestamp = /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}(?::\\d{2}(?:\\.\\d+)?)?(?:Z|[+-]\\d{2}:\\d{2})$/;\nconst statuses = new Map([\n  ['pending', 'Pending'],\n  ['to do', 'Pending'],\n  ['todo', 'Pending'],\n  ['open', 'Pending'],\n  ['in progress', 'In progress'],\n  ['blocked', 'Blocked'],\n  ['waiting', 'Waiting'],\n  ['completed', 'Completed'],\n  ['complete', 'Completed'],\n  ['done', 'Completed'],\n  ['cancelled', 'Cancelled'],\n  ['canceled', 'Cancelled'],\n]);\nconst text = value => String(value ?? '').trim();\n\nfunction display(value, limit) {\n  const singleLine = value.replace(/\\s+/g, ' ');\n  return singleLine.length > limit ? singleLine.slice(0, limit - 1) + '…' : singleLine;\n}\n\nfunction parseDate(value, allowTime = false) {\n  if (!dateOnly.test(value) && !(allowTime && zonedTimestamp.test(value))) return null;\n  const date = DateTime.fromISO(value, { zone: now.zoneName });\n  return date.isValid ? date : null;\n}\n\nconst rows = $input.all().map(item => item.json).filter(row =>\n  Object.entries(row).some(([column, value]) => column !== 'row_number' && text(value))\n);\nconst idCounts = new Map();\nfor (const row of rows) {\n  const id = text(row['Task ID']);\n  if (id) idCounts.set(id, (idCounts.get(id) ?? 0) + 1);\n}\n\nconst pending = [];\nconst recent = [];\nconst issues = [];\nlet completed = 0;\n\nfor (const row of rows) {\n  const id = text(row['Task ID']);\n  const title = text(row.Task);\n  const status = statuses.get(text(row.Status).toLowerCase().replace(/[_-]+|\\s+/g, ' '));\n  const rowLabel = row.row_number ? 'Sheet row ' + row.row_number : 'Task row';\n  const label = id ? display(id, 60) + ' (' + rowLabel + ')' : rowLabel;\n  const problems = [];\n\n  if (!id) problems.push('missing Task ID');\n  if (idCounts.get(id) > 1) problems.push('duplicate Task ID');\n  if (!title) problems.push('missing task name');\n  if (!status) problems.push('unrecognized status: ' + (display(text(row.Status), 60) || 'blank'));\n  if (problems.length) {\n    issues.push(label + ': ' + problems.join(', '));\n    continue;\n  }\n  if (status === 'Cancelled') continue;\n\n  const task = {\n    id,\n    title: display(title, 140),\n    owner: display(text(row.Owner), 80) || 'Unassigned',\n    status,\n  };\n  if (status === 'Completed') {\n    completed += 1;\n    const finished = parseDate(text(row['Completed At']), true);\n    if (!finished || finished.toMillis() > now.toMillis()) {\n      issues.push(label + ': enter a valid completion date or timestamp that has passed');\n    } else if (finished.toMillis() >= periodStart.toMillis()) {\n      recent.push({ ...task, finished });\n    }\n    continue;\n  }\n\n  const dueValue = text(row['Due Date']);\n  const due = parseDate(dueValue);\n  if (dueValue && !due) issues.push(label + ': Due Date must be a valid YYYY-MM-DD date');\n  pending.push({ ...task, due: due?.toISODate() ?? '' });\n}\n\npending.sort((a, b) => (a.due || '9999-12-31').localeCompare(b.due || '9999-12-31') ||\n  a.id.localeCompare(b.id));\nrecent.sort((a, b) => b.finished.toMillis() - a.finished.toMillis() || a.id.localeCompare(b.id));\n\nconst overdue = pending.filter(task => task.due && task.due < reportDate).length;\nconst dueToday = pending.filter(task => task.due === reportDate).length;\nconst blocked = pending.filter(task => task.status === 'Blocked').length;\nconst taskText = task => '[' + display(task.id, 60) + '] ' + task.title + ': ' + task.owner;\n\nfunction section(heading, entries, format) {\n  const lines = entries.slice(0, maxListed).map(entry => '• ' + format(entry));\n  if (entries.length > maxListed) {\n    lines.push('… ' + (entries.length - maxListed) + ' more. See the Tasks tab.');\n  }\n  return [heading + ' (' + entries.length + ')', ...(lines.length ? lines : ['None'])].join('\\n');\n}\n\nconst report = [\n  'Task status as of ' + now.toFormat('yyyy-MM-dd HH:mm ZZZZ') + ' (' + now.zoneName + ')',\n  'Pending: ' + pending.length + ' | Overdue: ' + overdue + ' | Due today: ' + dueToday +\n    ' | Blocked: ' + blocked,\n  section('Pending tasks', pending, task => {\n    const due = task.due ? 'due ' + task.due : 'no due date';\n    const urgent = task.due && task.due < reportDate ? ': OVERDUE' : '';\n    return taskText(task) + ': ' + task.status + ': ' + due + urgent;\n  }),\n  section('Completed since ' + periodStart.toISODate(), recent,\n    task => taskText(task) + ': ' + task.finished.toISODate()),\n  section('Rows to review', issues, issue => issue),\n].join('\\n\\n');\n\nreturn [{ json: {\n  'Report Date': reportDate,\n  'Period Start': periodStart.toISODate(),\n  'Generated At': now.toISO(),\n  'Completed': completed,\n  'Completed Last 7 Days': recent.length,\n  'Pending': pending.length,\n  'Overdue': overdue,\n  'Due Today': dueToday,\n  'Blocked': blocked,\n  'Rows to Review': issues.length,\n  'Report': report,\n} }];"
      },
      "id": "983b1329-cf4d-47f5-a4ab-5054c7a34658",
      "name": "Generate task report",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        155,
        255
      ]
    }
  ],
  "connections": {
    "Run demo": {
      "main": [
        [
          {
            "node": "Sample tasks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample tasks": {
      "main": [
        [
          {
            "node": "Generate task report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
