{
  "createdAt": "2025-07-06T16:25:44.285Z",
  "updatedAt": "2025-10-12T14:31:25.290Z",
  "id": "kzIkLF7hljeez219",
  "name": "workflow_1",
  "active": false,
  "isArchived": false,
  "nodes": [
    {
      "parameters": {
        "jsCode": "// Asumsi input dari webhook adalah: { \"date\": \"2025-04-04\" } atau { \"date\": \"20250404\" }\nconst tanggal = $input.first().json.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": "afecb9f5-92b6-48e5-a24b-64667d5a95a7",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        160,
        80
      ],
      "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": "d8c5b6b9-1670-4be1-823b-1f239b89e898",
      "name": "Prepare Bulk Update Query",
      "type": "n8n-nodes-base.code",
      "position": [
        832,
        80
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        608,
        80
      ],
      "id": "c09b5c2f-db48-4975-868c-5501bca0ce91",
      "name": "Extract from File"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "{{ $json.query }}",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1056,
        80
      ],
      "id": "db9ca7f7-135d-4392-bf0a-36f1f688eb39",
      "name": "Execute a SQL query",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "date"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        -512,
        80
      ],
      "id": "cf3294cc-e7e9-4a05-8c1a-0a2fe8070a3a",
      "name": "When Executed by Another Workflow"
    },
    {
      "parameters": {
        "bucketName": "skripsi",
        "fileKey": "=daily_sales/{{ $json.namaFile }}"
      },
      "type": "n8n-nodes-base.s3",
      "typeVersion": 1,
      "position": [
        384,
        80
      ],
      "id": "3e1f82a7-df2a-44c1-97a2-858769553d31",
      "name": "Download a file",
      "credentials": {
        "s3": {
          "name": "<your credential>"
        }
      }
    },
    {
      "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": [
        -64,
        80
      ],
      "id": "7995fd62-9b04-4c75-900a-229f0a244944",
      "name": "Call 'workflow_1_info'"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://192.168.1.5:5000/api/workflow/start",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "date",
              "value": "={{ $json.date }}"
            },
            {
              "name": "workflow_type",
              "value": "update"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -288,
        80
      ],
      "id": "70f503a3-78db-4536-b628-b32894ab332c",
      "name": "HTTP Request"
    }
  ],
  "connections": {
    "Upload file": {
      "main": [
        [
          {
            "node": "Download file",
            "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
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download a file": {
      "main": [
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call 'workflow_1_info'": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Call 'workflow_1_info'",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query": {
      "main": [
        []
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "versionId": "4c590c8a-ff17-445f-a2b3-94f2d6ba4c64",
  "triggerCount": 0,
  "shared": [
    {
      "createdAt": "2025-10-13T10:29:43.656Z",
      "updatedAt": "2025-10-13T10:29:43.656Z",
      "role": "workflow:owner",
      "workflowId": "kzIkLF7hljeez219",
      "projectId": "FX62idw1VcxEmrbL"
    }
  ],
  "tags": []
}