{
  "createdAt": "2025-09-08T07:23:41.194Z",
  "updatedAt": "2025-10-12T14:49:40.061Z",
  "id": "CGQJGxafMl4J2Kwg",
  "name": "workflow_1_webhook",
  "active": false,
  "isArchived": false,
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "workflow-1-webhook",
        "options": {}
      },
      "id": "75527304-d9d2-454c-b48a-33cf8bbbd7bb",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -864,
        32
      ],
      "typeVersion": 1,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "jsCode": "// Asumsi input dari webhook adalah: { \"date\": \"2025-04-04\" } atau { \"date\": \"20250404\" }\nconst tanggal = $('Webhook').first().json.body[0].date; // Mengambil data tanggal, contoh: \"2025-04-04\"\n\n// Menghapus tanda hubung (-) dari string tanggal jika ada.\n// Jika tidak ada tanda hubung, string tidak akan berubah.\nconst tanggalFormatted = tanggal.replace(/-/g, ''); // Hasilnya akan menjadi \"20250404\"\n\n// Membuat nama file yang akan dicari di Google Drive\nconst namaFile = `daily_sales_${tanggalFormatted}.csv`;\u00a0\n\n// Mengembalikan nama file untuk digunakan di node selanjutnya\nreturn { namaFile };"
      },
      "id": "ad116d81-6e07-4af6-9703-b8154ac73bd7",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        -416,
        -64
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "jsCode": "const salesData = $input.all().map(item => item.json);\n\n// 1. Validate the incoming data\nif (!Array.isArray(salesData) || salesData.length === 0) {\n  // If there's no valid data, stop the workflow by returning an empty array.\n  return [];\n}\n\n// 2. Transform the sales data into a SQL VALUES list.\nconst valuesList = salesData\n  .map(sale => {\n    const productIndex = parseInt(sale.index, 10);\n    const quantitySold = parseInt(sale.quantity_sold, 10);\n\n    // Basic validation for each item\n    if (isNaN(productIndex) || isNaN(quantitySold)) {\n      return null;\n    }\n    return `(${productIndex}, ${quantitySold})`;\n  })\n  .filter(Boolean) // Remove any null entries from invalid sales items\n  .join(', ');\n\n// If after filtering there are no valid values, stop.\nif (!valuesList) {\n  return [];\n}\n\n\n// 3. Construct the final, powerful UPDATE query.\nconst updateQuery = `\nUPDATE public.amazon_dataset AS ad\nSET\n    stock = ad.stock - v.quantity_sold\nFROM (\n    VALUES ${valuesList}\n) AS v(product_index, quantity_sold)\nWHERE ad.index = v.product_index;\n`;\n\n// 4. Output the generated query for the next node to use.\nreturn [{ json: { query: updateQuery } }];"
      },
      "id": "c4769db6-db16-4bd2-9d4f-324944759aea",
      "name": "Prepare Bulk Update Query",
      "type": "n8n-nodes-base.code",
      "position": [
        256,
        -160
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        32,
        -160
      ],
      "id": "c27e7ea6-aba2-49f8-8358-da3762619e57",
      "name": "Extract from File"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "{{ $json.query }}",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        480,
        -160
      ],
      "id": "895ff878-867f-48e4-9acf-421d48401ec1",
      "name": "Execute a SQL query",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "bucketName": "skripsi",
        "fileKey": "=daily_sales/{{ $('Code').item.json.namaFile }}"
      },
      "type": "n8n-nodes-base.s3",
      "typeVersion": 1,
      "position": [
        -192,
        -64
      ],
      "id": "03bec1d0-2fd7-4786-b2ee-d460c4c4fe55",
      "name": "Download a file",
      "credentials": {
        "s3": {
          "name": "<your credential>"
        }
      },
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://192.168.1.5:5000/api/workflow/update",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "task_id",
              "value": "={{ $json.body[0].flask_task_id }}"
            },
            {
              "name": "step_id",
              "value": "75527304-d9d2-454c-b48a-33cf8bbbd7bb"
            },
            {
              "name": "status",
              "value": "failed"
            },
            {
              "name": "message",
              "value": "Proses gagal. Menghentikan alur kerja"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -640,
        128
      ],
      "id": "41fad355-9106-411e-90d7-3038187abfc9",
      "name": "HTTP Request1"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://192.168.1.5:5000/api/workflow/update",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "task_id",
              "value": "={{ $('Webhook').item.json.body[0].flask_task_id }}"
            },
            {
              "name": "step_id",
              "value": "03bec1d0-2fd7-4786-b2ee-d460c4c4fe55"
            },
            {
              "name": "status",
              "value": "failed"
            },
            {
              "name": "wokflow_type",
              "value": "={{ $('Webhook').item.json.body[0].workflow_type }}"
            },
            {
              "name": "message",
              "value": "gagal mendownload"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        32,
        32
      ],
      "id": "ef0aeebc-6be0-42c7-b6d2-ff1e094cad93",
      "name": "HTTP Request2"
    },
    {
      "parameters": {
        "workflowId": {
          "__rl": true,
          "value": "32JWFem2u7Nkz5IJ",
          "mode": "list",
          "cachedResultUrl": "/workflow/32JWFem2u7Nkz5IJ",
          "cachedResultName": "workflow_1_info"
        },
        "workflowInputs": {
          "mappingMode": "defineBelow",
          "value": {}
        },
        "options": {}
      },
      "type": "n8n-nodes-base.executeWorkflow",
      "typeVersion": 1.3,
      "position": [
        -640,
        -64
      ],
      "id": "3f3a6e01-2154-4f36-8d73-ea2a86c85d4d",
      "name": "Call 'workflow_1_info'"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Call 'workflow_1_info'",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Download a file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Bulk Update Query": {
      "main": [
        [
          {
            "node": "Execute a SQL query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from File": {
      "main": [
        [
          {
            "node": "Prepare Bulk Update Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download a file": {
      "main": [
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query": {
      "main": [
        []
      ]
    },
    "Call 'workflow_1_info'": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "versionId": "5cb49a1c-7631-4aca-a27d-528a74e3233c",
  "triggerCount": 1,
  "shared": [
    {
      "createdAt": "2025-10-13T10:29:43.656Z",
      "updatedAt": "2025-10-13T10:29:43.656Z",
      "role": "workflow:owner",
      "workflowId": "CGQJGxafMl4J2Kwg",
      "projectId": "FX62idw1VcxEmrbL"
    }
  ],
  "tags": []
}