{
  "name": "GCP → UAT 資料同步",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 2 * * *"
            }
          ]
        }
      },
      "id": "schedule",
      "name": "每日 02:00 執行",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "command": "/home/wooo/momo_pro_system/scripts/sync_gcp_to_uat.sh 2>&1",
        "cwd": "/home/wooo/momo_pro_system"
      },
      "id": "execute-sync",
      "name": "執行同步腳本",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [460, 300],
      "continueOnFail": true
    },
    {
      "parameters": {
        "jsCode": "const output = $input.first().json.stdout || '';\nconst stderr = $input.first().json.stderr || '';\nconst exitCode = $input.first().json.exitCode;\n\nconst now = new Date().toLocaleString('zh-TW', { timeZone: 'Asia/Taipei' });\n\nlet success = exitCode === 0;\nlet message = '';\n\nif (success) {\n  // 從輸出中提取統計資訊\n  const statsMatch = output.match(/UAT 資料表統計:[\\s\\S]*?同步完成/);\n  const stats = statsMatch ? statsMatch[0] : '';\n  \n  message = `✅ <b>GCP → UAT 資料同步完成</b>\\n\\n`;\n  message += `📊 同步成功\\n`;\n  message += `⏰ 時間: ${now}`;\n} else {\n  message = `🔴 <b>GCP → UAT 資料同步失敗</b>\\n\\n`;\n  message += `❌ 錯誤: ${stderr || output.slice(-500)}\\n`;\n  message += `⏰ 時間: ${now}`;\n}\n\nreturn [{\n  json: {\n    success,\n    message,\n    exitCode,\n    output: output.slice(-1000),\n    timestamp: now\n  }\n}];"
      },
      "id": "process-result",
      "name": "處理結果",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [680, 300]
    },
    {
      "parameters": {
        "url": "https://api.telegram.org/bot8075645931:AAH-EGKMo8ZC4QJs-Nc1_0s92xHrGdQvdpg/sendMessage",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"chat_id\": \"5619078117\",\n  \"parse_mode\": \"HTML\",\n  \"text\": \"{{ $json.message }}\"\n}",
        "options": {}
      },
      "id": "telegram",
      "name": "Telegram 通知",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [900, 300]
    }
  ],
  "connections": {
    "每日 02:00 執行": {
      "main": [
        [{ "node": "執行同步腳本", "type": "main", "index": 0 }]
      ]
    },
    "執行同步腳本": {
      "main": [
        [{ "node": "處理結果", "type": "main", "index": 0 }]
      ]
    },
    "處理結果": {
      "main": [
        [{ "node": "Telegram 通知", "type": "main", "index": 0 }]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "updatedAt": "2026-02-07T15:30:00.000Z",
  "versionId": "1"
}
