{
  "name": "Logyca_Daily_Sales_Summary",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 1 * * *"
            }
          ]
        }
      },
      "id": "7f9e1a2b-3c4d-5e6f-7a8b-9c0d1e2f3a4b",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT COUNT(*) AS pending_count FROM jobs WHERE status IN ('PENDING', 'PROCESSING') AND created_at >= CURRENT_DATE - INTERVAL '1 day';"
      },
      "id": "3a4b5c6d-7e8f-9a0b-1c2d-3e4f5a6b7c8d",
      "name": "Check_Pending_Jobs",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        500,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.pending_count }}",
              "operation": "equal",
              "value2": 0
            }
          ]
        }
      },
      "id": "4b5c6d7e-8f9a-0b1c-2d3e-4f5a6b7c8d9e",
      "name": "Gate_If_No_Pending",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        750,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO sales_daily_summary (summary_date, total_sales_count, total_revenue)\nSELECT \n    sale_date, \n    COUNT(*) as total_sales_count, \n    SUM(total) as total_revenue\nFROM sales\nWHERE sale_date = CURRENT_DATE - INTERVAL '1 day'\nGROUP BY sale_date\nON CONFLICT (summary_date) \nDO UPDATE SET \n    total_sales_count = EXCLUDED.total_sales_count,\n    total_revenue = EXCLUDED.total_revenue,\n    processed_at = NOW()\nRETURNING summary_date, total_sales_count, total_revenue;"
      },
      "id": "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d",
      "name": "Postgres_Aggregation",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        1000,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "content": "### \u26a0\ufe0f Jobs Pendientes Detectados\nSe detectaron jobs PENDING o PROCESSING del d\u00eda anterior.\nEl resumen NO fue generado para evitar datos incompletos.\n**pending_count:** {{ $json.pending_count }}"
      },
      "id": "5c6d7e8f-9a0b-1c2d-3e4f-5a6b7c8d9e0f",
      "name": "Log_Skip",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1000,
        400
      ]
    },
    {
      "parameters": {
        "content": "### \u2705 Resumen de Ventas Generado\n**Fecha:** {{ $json.summary_date }}\n**Registros:** {{ $json.total_sales_count }}\n**Total Revenue:** ${{ $json.total_revenue }}"
      },
      "id": "2b3c4d5e-6f7a-8b9c-0d1e-2f3a4b5c6d7e",
      "name": "Log_Success",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1250,
        200
      ]
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Check_Pending_Jobs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check_Pending_Jobs": {
      "main": [
        [
          {
            "node": "Gate_If_No_Pending",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gate_If_No_Pending": {
      "main": [
        [
          {
            "node": "Postgres_Aggregation",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Log_Skip",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres_Aggregation": {
      "main": [
        [
          {
            "node": "Log_Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "hash": "abc123def456"
}