{
  "name": "[DEMO] 스마트스토어 주문 CSV → 일일 매출 요약 + 재고 알림",
  "nodes": [
    {
      "id": "1",
      "name": "매일 09:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        200
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "triggerAtHour": 9
            }
          ]
        }
      }
    },
    {
      "id": "2",
      "name": "주문 CSV 읽기",
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1,
      "position": [
        220,
        200
      ],
      "parameters": {
        "operation": "read",
        "fileSelector": "/data/sample_orders.csv"
      }
    },
    {
      "id": "3",
      "name": "CSV 파싱",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        440,
        200
      ],
      "parameters": {
        "operation": "csv",
        "options": {}
      }
    },
    {
      "id": "4",
      "name": "집계 + 재고 비교",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        200
      ],
      "parameters": {
        "jsCode": "// DEMO: 취소/환불 제외 후 최근 날짜 매출 집계 + 재고 비교\nconst EXCLUDE = ['취소','반품','환불'];\nconst stock = { '온결당 한과 정 세트':[12,5], '온결당 한과 덕담 세트':[30,10], '온결당 한과 복 세트':[8,5], '온결당 약과 단품 10입':[25,10] };\nconst rows = $input.all().map(i => i.json).filter(r => r['상품명'] && !EXCLUDE.includes(String(r['주문상태']).trim()));\nconst dates = rows.map(r => String(r['주문일시']).substring(0,10)).sort();\nconst target = dates[dates.length-1];\nlet total = 0, count = 0; const byItem = {}; const sold = {};\nfor (const r of rows) {\n  const q = Number(r['수량'])||0, p = Number(r['상품가격'])||0, name = r['상품명'];\n  sold[name] = (sold[name]||0) + q;\n  if (String(r['주문일시']).substring(0,10) !== target) continue;\n  total += q*p; count++;\n  byItem[name] = (byItem[name]||0) + q;\n}\nconst low = Object.entries(stock).map(([n,[s,m]]) => ({n, remain: s-(sold[n]||0), m})).filter(x => x.remain <= x.m);\nconst won = n => n.toLocaleString('ko-KR') + '원';\nconst lines = [`${target} 매출 요약`, `주문 ${count}건 / 매출 ${won(total)}`, ''].concat(Object.entries(byItem).map(([k,v]) => `- ${k}: ${v}개`));\nlines.push('', '* 포트폴리오 데모 메일입니다. 가상 데이터 기반입니다.');\nreturn [{ json: { date: target, total, count, summaryText: lines.join('\\n'), lowCount: low.length, lowText: low.map(x => `- ${x.n}: 잔여 ${x.remain}개 (기준 ${x.m}개)`).join('\\n') } }];"
      }
    },
    {
      "id": "5",
      "name": "매출 요약 초안",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        900,
        100
      ],
      "parameters": {
        "resource": "draft",
        "operation": "create",
        "subject": "=[일일 매출] {{ $json.date }}",
        "message": "={{ $json.summaryText }}",
        "options": {}
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "REPLACE_ME",
          "name": "Gmail account (연결 필요)"
        }
      }
    },
    {
      "id": "6",
      "name": "재고 부족?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        900,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ $json.lowCount }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        }
      }
    },
    {
      "id": "7",
      "name": "재고 알림 초안",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1140,
        280
      ],
      "parameters": {
        "resource": "draft",
        "operation": "create",
        "subject": "=[재고 부족 알림] {{ $json.lowCount }}개 상품",
        "message": "={{ $json.lowText }}",
        "options": {}
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "REPLACE_ME",
          "name": "Gmail account (연결 필요)"
        }
      }
    },
    {
      "id": "8",
      "name": "DEMO 안내",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "parameters": {
        "content": "## DEMO 워크플로\n포트폴리오용 가상 데이터 샘플입니다. 실제 스마트스토어 API와 연동되지 않습니다.\n- CSV 경로와 Gmail 인증을 본인 환경에 맞게 설정\n- 재고표는 Code 노드 상단 객체에서 수정",
        "height": 150,
        "width": 420
      }
    }
  ],
  "connections": {
    "매일 09:00": {
      "main": [
        [
          {
            "node": "주문 CSV 읽기",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "주문 CSV 읽기": {
      "main": [
        [
          {
            "node": "CSV 파싱",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "CSV 파싱": {
      "main": [
        [
          {
            "node": "집계 + 재고 비교",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "집계 + 재고 비교": {
      "main": [
        [
          {
            "node": "매출 요약 초안",
            "type": "main",
            "index": 0
          },
          {
            "node": "재고 부족?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "재고 부족?": {
      "main": [
        [
          {
            "node": "재고 알림 초안",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}