{
  "name": "گالری ۱۲: برنامه‌ریز هوشمند کار (Gmail + Sheets + Calendar)",
  "nodes": [
    {
      "parameters": {
        "content": "## برنامه‌ریز هوشمند\n۱) ایمیل با برچسب «برنامه‌ریزی» (کار تازه از خودت یا همکار)\n۲) استخراج کارها: عنوان، مدت، مهلت\n۳) اهداف کاری از Sheets ← اولویت بر اساس هدف و مهلت\n۴) زمان آزاد ۵ روز کاری آینده از تقویم (۹ تا ۱۷)\n۵) بدون تکرار: کاری که قبلاً زمان‌بندی شده (کلید در توضیح رویداد) دوباره ساخته نمی‌شود\n۶) ساخت رویداد، ثبت در Sheets و یک ایمیل خلاصه\n\n⚠️ نمونه آموزشی loOkiYar؛ غیرفعال. گره‌های بنفش (سنجاق‌دار) داده نمایشی ساختگی دارند و در اجرای دستی به سرویس واقعی وصل نمی‌شوند.",
        "height": 338,
        "width": 540,
        "color": 4
      },
      "id": "5bdb950f-7e4e-5d00-99ba-c077fec207d1",
      "name": "یادداشت 1",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -60,
        -538
      ]
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyX",
              "value": 5,
              "unit": "minutes"
            }
          ]
        },
        "simple": false,
        "filters": {
          "readStatus": "unread",
          "labelIds": [
            "REPLACE_WITH_LABEL_ID_planning"
          ]
        },
        "options": {}
      },
      "id": "9656e6ac-c73a-543a-8ca1-c5b6c164d3ba",
      "name": "ایمیل برنامه‌ریزی",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        120
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "e6161cc65c9b5504",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "text": "=موضوع: {{ $json.subject }}\nمتن:\n{{ ($json.text || '').slice(0, 6000) }}",
        "schemaType": "fromJson",
        "jsonSchemaExample": "{\"tasks\": [{\"title\": \"تهیه گزارش فروش\", \"minutes\": 90, \"deadline\": \"2026-10-24\"}]}",
        "options": {
          "systemPromptTemplate": "Extract concrete work tasks from the email. minutes = estimated effort (30-240). deadline = YYYY-MM-DD only if written. Do not invent tasks."
        }
      },
      "id": "d7d4c32e-fd70-5fdc-a082-d3d0f6012b39",
      "name": "استخراج کارها",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "typeVersion": 1.2,
      "position": [
        240,
        120
      ]
    },
    {
      "parameters": {
        "model": "openai/gpt-5-mini",
        "options": {
          "temperature": 0
        }
      },
      "id": "dc8e9230-f80e-5b24-9967-df273ae6734a",
      "name": "مدل (OpenRouter)",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "typeVersion": 1,
      "position": [
        240,
        360
      ],
      "credentials": {
        "openRouterApi": {
          "id": "fce14e07dac85574",
          "name": "OpenRouter account"
        }
      }
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "REPLACE_WITH_SPREADSHEET_ID"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Goals"
        },
        "options": {}
      },
      "id": "39e6a812-d428-5f7c-9a71-2cbebc9164d0",
      "name": "اهداف کاری",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        480,
        -60
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "6fd5b108914d5fc7",
          "name": "Google Sheets account"
        }
      },
      "executeOnce": true
    },
    {
      "parameters": {
        "operation": "getAll",
        "calendar": {
          "__rl": true,
          "mode": "id",
          "value": "me@example.com"
        },
        "returnAll": true,
        "timeMin": "={{ $today.plus({ days: 1 }) }}",
        "timeMax": "={{ $today.plus({ days: 8 }) }}",
        "options": {
          "singleEvents": true,
          "orderBy": "startTime"
        }
      },
      "id": "2b16cc01-c8a3-5f1c-9478-23405e9e203b",
      "name": "زمان‌های اشغال",
      "type": "n8n-nodes-base.googleCalendar",
      "typeVersion": 1.3,
      "position": [
        480,
        300
      ],
      "credentials": {
        "googleCalendarOAuth2Api": {
          "id": "0491059a394554db",
          "name": "Google Calendar account"
        }
      },
      "executeOnce": true
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "goals",
        "options": {}
      },
      "id": "7791a15e-a209-5d95-a03f-a7e5e31af787",
      "name": "بسته اهداف",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        700,
        -60
      ]
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "events",
        "options": {}
      },
      "id": "fe152834-2bdc-5242-a2ee-6f8e2ddec746",
      "name": "بسته تقویم",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    },
    {
      "parameters": {
        "mode": "combine",
        "combineBy": "combineByPosition",
        "numberInputs": 3,
        "options": {}
      },
      "id": "3d563d83-db16-578b-9da9-716b908a8608",
      "name": "همه ورودی‌ها",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.1,
      "position": [
        940,
        120
      ]
    },
    {
      "parameters": {
        "jsCode": "// Score each task against goals, then place it in the first free slot (9:00-17:00, Sat-Wed) before its deadline.\nconst { output, goals = [], events = [] } = $json;\nconst tasks = (output?.tasks || []).slice(0, 10);\nconst busy = events.filter(e => e.start?.dateTime).map(e => [new Date(e.start.dateTime), new Date(e.end.dateTime)]);\nconst planned = new Set(events.map(e => (e.description || '').match(/plan-key:(\\S+)/)?.[1]).filter(Boolean));\nconst tz = '+03:30', workDays = [6, 0, 1, 2, 3]; // Sat..Wed\nconst keyOf = t => (t.title + '|' + (t.deadline || '')).toLowerCase().replace(/\\s+/g, '-');\nconst score = t => {\n  const g = goals.find(g => (g.keywords || '').split(',').some(k => k.trim() && t.title.includes(k.trim())));\n  const days = t.deadline ? (new Date(t.deadline) - new Date()) / 864e5 : 30;\n  return { goal: g?.goal || '—', weight: Number(g?.weight || 1), score: Number(g?.weight || 1) * 10 - Math.min(days, 30) };\n};\nconst free = (s, e) => !busy.some(([a, b]) => s < b && e > a);\nconst out = [];\nconst sorted = tasks.map(t => ({ ...t, ...score(t), key: keyOf(t) })).sort((a, b) => b.score - a.score);\nfor (const t of sorted) {\n  if (planned.has(t.key)) { out.push({ json: { ...t, status: 'already_planned' } }); continue; }\n  let slot = null;\n  for (let d = 1; d <= 7 && !slot; d++) {\n    const day = new Date(Date.now() + d * 864e5);\n    if (!workDays.includes(day.getDay())) continue;\n    const ymd = day.toISOString().slice(0, 10);\n    if (t.deadline && ymd > t.deadline) break;\n    for (let h = 9; h + t.minutes / 60 <= 17 && !slot; h += 0.5) {\n      const s = new Date(`${ymd}T${String(Math.floor(h)).padStart(2, '0')}:${h % 1 ? '30' : '00'}:00${tz}`);\n      const e = new Date(s.getTime() + t.minutes * 6e4);\n      if (free(s, e)) slot = [s, e];\n    }\n  }\n  if (slot) { busy.push(slot); out.push({ json: { ...t, status: 'to_create', start: slot[0].toISOString(), end: slot[1].toISOString() } }); }\n  else out.push({ json: { ...t, status: 'no_slot' } });\n}\nreturn out;"
      },
      "id": "0088ac8b-d7b0-5a4a-9f00-13ed315109b3",
      "name": "اولویت و جای خالی",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1160,
        120
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "9f2e21b1-5bb8-5b01-82ab-a561e7280f8a",
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "to_create",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "ساخت رویداد"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "1595192b-8b19-511c-8e07-c11d747e8d20",
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "already_planned",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "قبلاً زمان‌بندی شده"
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "id": "61c25927-31ff-51ff-8289-5a787ba5f9d6",
      "name": "وضعیت کار",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        1380,
        120
      ]
    },
    {
      "parameters": {
        "calendar": {
          "__rl": true,
          "mode": "id",
          "value": "me@example.com"
        },
        "start": "={{ $json.start }}",
        "end": "={{ $json.end }}",
        "additionalFields": {
          "summary": "=🎯 {{ $json.title }}",
          "description": "=هدف: {{ $json.goal }}\nمهلت: {{ $json.deadline || '-' }}\nplan-key:{{ $json.key }}"
        }
      },
      "id": "4d4b1482-d743-5082-92d4-32c579def04c",
      "name": "ساخت رویداد",
      "type": "n8n-nodes-base.googleCalendar",
      "typeVersion": 1.3,
      "position": [
        1620,
        0
      ],
      "credentials": {
        "googleCalendarOAuth2Api": {
          "id": "0491059a394554db",
          "name": "Google Calendar account"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "REPLACE_WITH_SPREADSHEET_ID"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Plan"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "title": "={{ $('وضعیت کار').item.json.title }}",
            "goal": "={{ $('وضعیت کار').item.json.goal }}",
            "start": "={{ $('وضعیت کار').item.json.start }}",
            "event_id": "={{ $json.id }}",
            "key": "={{ $('وضعیت کار').item.json.key }}"
          },
          "schema": [
            {
              "id": "title",
              "displayName": "title",
              "type": "string"
            },
            {
              "id": "goal",
              "displayName": "goal",
              "type": "string"
            },
            {
              "id": "start",
              "displayName": "start",
              "type": "string"
            },
            {
              "id": "event_id",
              "displayName": "event_id",
              "type": "string"
            },
            {
              "id": "key",
              "displayName": "key",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "d0da6f5d-b578-5b22-9fd8-637d6e4ce038",
      "name": "ثبت برنامه",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1840,
        0
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "6fd5b108914d5fc7",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "numberInputs": 3
      },
      "id": "4962d7c9-a2d0-5f1e-9a2f-bbbad2375bf2",
      "name": "نتیجه همه کارها",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.1,
      "position": [
        2060,
        140
      ]
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "rows",
        "options": {}
      },
      "id": "6595cdd2-e7f1-5463-8ce3-1816fd01aced",
      "name": "یک گزارش",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        2260,
        140
      ]
    },
    {
      "parameters": {
        "operation": "reply",
        "messageId": "={{ $('ایمیل برنامه‌ریزی').first().json.id }}",
        "emailType": "text",
        "message": "={{ $json.rows.map(r => (r.status === 'already_planned' ? '↺ قبلاً برنامه‌ریزی شده: ' : r.status === 'no_slot' ? '⚠️ جای خالی پیدا نشد: ' : '✅ ') + (r.title || r.summary) + (r.start ? ' — ' + DateTime.fromISO(r.start).setZone('Asia/Tehran').toFormat('ccc dd LLL HH:mm') : '')).join('\\n') }}",
        "options": {
          "appendAttribution": false,
          "replyToSenderOnly": true
        }
      },
      "id": "e5b7f4c3-8949-5947-b2c6-44b4c11e32e3",
      "name": "ایمیل خلاصه برنامه",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        2460,
        140
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "e6161cc65c9b5504",
          "name": "Gmail account"
        }
      }
    }
  ],
  "connections": {
    "مدل (OpenRouter)": {
      "ai_languageModel": [
        [
          {
            "node": "استخراج کارها",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "ایمیل برنامه‌ریزی": {
      "main": [
        [
          {
            "node": "استخراج کارها",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "استخراج کارها": {
      "main": [
        [
          {
            "node": "اهداف کاری",
            "type": "main",
            "index": 0
          },
          {
            "node": "زمان‌های اشغال",
            "type": "main",
            "index": 0
          },
          {
            "node": "همه ورودی‌ها",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "اهداف کاری": {
      "main": [
        [
          {
            "node": "بسته اهداف",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "زمان‌های اشغال": {
      "main": [
        [
          {
            "node": "بسته تقویم",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "بسته اهداف": {
      "main": [
        [
          {
            "node": "همه ورودی‌ها",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "بسته تقویم": {
      "main": [
        [
          {
            "node": "همه ورودی‌ها",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "همه ورودی‌ها": {
      "main": [
        [
          {
            "node": "اولویت و جای خالی",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "اولویت و جای خالی": {
      "main": [
        [
          {
            "node": "وضعیت کار",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "وضعیت کار": {
      "main": [
        [
          {
            "node": "ساخت رویداد",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "نتیجه همه کارها",
            "type": "main",
            "index": 1
          }
        ],
        [
          {
            "node": "نتیجه همه کارها",
            "type": "main",
            "index": 2
          }
        ]
      ]
    },
    "ساخت رویداد": {
      "main": [
        [
          {
            "node": "ثبت برنامه",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ثبت برنامه": {
      "main": [
        [
          {
            "node": "نتیجه همه کارها",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "نتیجه همه کارها": {
      "main": [
        [
          {
            "node": "یک گزارش",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "یک گزارش": {
      "main": [
        [
          {
            "node": "ایمیل خلاصه برنامه",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "id": "LkyGal12Planner",
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": "lookiyar-academy-demo"
  },
  "tags": [
    {
      "name": "loOkiYar Academy"
    },
    {
      "name": "gallery"
    }
  ],
  "pinData": {
    "ایمیل برنامه‌ریزی": [
      {
        "json": {
          "id": "demo-plan-1",
          "subject": "کارهای این هفته",
          "text": "سلام، این هفته باید گزارش فروش مهر را آماده کنم (حدود ۹۰ دقیقه، تا ۲۴ مهر)، با ناشر جدید تماس بگیرم (۳۰ دقیقه) و فهرست کتاب‌های کودک را مرور کنم (۶۰ دقیقه، تا ۲۶ مهر).",
          "from": {
            "value": [
              {
                "address": "me@example.com"
              }
            ]
          },
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "استخراج کارها": [
      {
        "json": {
          "output": {
            "tasks": [
              {
                "title": "تهیه گزارش فروش مهر",
                "minutes": 90,
                "deadline": "2026-10-24"
              },
              {
                "title": "تماس با ناشر جدید",
                "minutes": 30
              },
              {
                "title": "مرور فهرست کتاب‌های کودک",
                "minutes": 60,
                "deadline": "2026-10-26"
              }
            ]
          },
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "اهداف کاری": [
      {
        "json": {
          "goal": "افزایش فروش فصل",
          "keywords": "فروش,گزارش",
          "weight": 3,
          "_demo": "داده نمایشی ساختگی"
        }
      },
      {
        "json": {
          "goal": "تأمین‌کننده تازه",
          "keywords": "ناشر",
          "weight": 2,
          "_demo": "داده نمایشی ساختگی"
        }
      },
      {
        "json": {
          "goal": "بخش کودک",
          "keywords": "کودک",
          "weight": 2,
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "زمان‌های اشغال": [
      {
        "json": {
          "summary": "جلسه هفتگی تیم",
          "start": {
            "dateTime": "2026-10-22T09:00:00+03:30"
          },
          "end": {
            "dateTime": "2026-10-22T11:00:00+03:30"
          },
          "description": "",
          "_demo": "داده نمایشی ساختگی"
        }
      },
      {
        "json": {
          "summary": "🎯 مرور فهرست کتاب‌های کودک",
          "start": {
            "dateTime": "2026-10-25T09:00:00+03:30"
          },
          "end": {
            "dateTime": "2026-10-25T10:00:00+03:30"
          },
          "description": "plan-key:مرور-فهرست-کتاب‌های-کودک|2026-10-26",
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "ساخت رویداد": [
      {
        "json": {
          "id": "demo-event-a",
          "status": "confirmed",
          "_demo": "داده نمایشی ساختگی"
        }
      },
      {
        "json": {
          "id": "demo-event-b",
          "status": "confirmed",
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "ثبت برنامه": [
      {
        "json": {
          "title": "تهیه گزارش فروش مهر",
          "event_id": "demo-event-a",
          "_demo": "داده نمایشی ساختگی"
        }
      },
      {
        "json": {
          "title": "تماس با ناشر جدید",
          "event_id": "demo-event-b",
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ],
    "ایمیل خلاصه برنامه": [
      {
        "json": {
          "id": "demo-sent-plan",
          "labelIds": [
            "SENT"
          ],
          "_demo": "داده نمایشی ساختگی"
        }
      }
    ]
  }
}