{
  "info": {
    "_postman_id": "api-gateway-analytics-v2",
    "name": "API Gateway - Analytics Integration",
    "description": "Production API Gateway with combined KrakenD + JWT Proxy container for Analytics SQL API v2 integration",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "item": [
    {
      "name": "Health",
      "item": [
        {
          "name": "Gateway Health",
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "X-API-Key",
                "value": "{{api_key}}"
              }
            ],
            "url": {
              "raw": "{{gateway_url}}/__health",
              "host": [
                "{{gateway_url}}"
              ],
              "path": [
                "__health"
              ]
            },
            "description": "Check KrakenD API Gateway health status"
          }
        }
      ]
    },
    {
      "name": "Analytics Query Workflow",
      "item": [
        {
          "name": "1. Submit Customer Query",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "// Store query ID for next requests",
                  "var jsonData = pm.response.json();",
                  "if (jsonData && jsonData.queryId) {",
                  "    pm.collectionVariables.set('query_id', jsonData.queryId);",
                  "    console.log('Query ID stored:', jsonData.queryId);",
                  "    console.log('You can now run the status and results requests');",
                  "} else {",
                  "    console.error('Failed to get query ID from response');",
                  "}",
                  "",
                  "// Validate response",
                  "pm.test(\"Query submitted successfully\", function () {",
                  "    pm.response.to.have.status(200);",
                  "    pm.expect(jsonData.success).to.be.true;",
                  "    pm.expect(jsonData.queryId).to.exist;",
                  "    pm.expect(jsonData.status).to.equal('RUNNING');",
                  "});",
                  "",
                  "// Log response time",
                  "console.log('Response time:', pm.response.responseTime, 'ms');"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json"
              },
              {
                "key": "X-API-Key",
                "value": "{{api_key}}"
              }
            ],
            "body": {
              "mode": "raw",
              "raw": "{}"
            },
            "url": {
              "raw": "{{gateway_url}}/api/data/v1/analytics/customer-vibes",
              "host": [
                "{{gateway_url}}"
              ],
              "path": [
                "api",
                "data",
                "v1",
                "analytics",
                "customer-vibes"
              ]
            },
            "description": "Submit async query to retrieve customer data from Analytics. Returns immediately with query ID."
          }
        },
        {
          "name": "1b. Submit Customer Query (24h Delta)",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "// Store query ID for next requests",
                  "var jsonData = pm.response.json();",
                  "if (jsonData && jsonData.queryId) {",
                  "    pm.collectionVariables.set('query_id', jsonData.queryId);",
                  "    console.log('Delta Query ID stored:', jsonData.queryId);",
                  "    console.log('You can now run the status and results requests');",
                  "} else {",
                  "    console.error('Failed to get query ID from response');",
                  "}",
                  "",
                  "// Validate response",
                  "pm.test(\"Delta query submitted successfully\", function () {",
                  "    pm.response.to.have.status(200);",
                  "    pm.expect(jsonData.success).to.be.true;",
                  "    pm.expect(jsonData.queryId).to.exist;",
                  "    pm.expect(jsonData.status).to.equal('RUNNING');",
                  "});",
                  "",
                  "// Log response time",
                  "console.log('Response time:', pm.response.responseTime, 'ms');"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json"
              },
              {
                "key": "X-API-Key",
                "value": "{{api_key}}"
              }
            ],
            "body": {
              "mode": "raw",
              "raw": "{}"
            },
            "url": {
              "raw": "{{gateway_url}}/api/data/v1/analytics/customer-vibes-delta-24h",
              "host": [
                "{{gateway_url}}"
              ],
              "path": [
                "api",
                "data",
                "v1",
                "analytics",
                "customer-vibes-delta-24h"
              ]
            },
            "description": "Submit async query to retrieve customer data updated within last 24 hours from Analytics. Returns immediately with query ID."
          }
        },
        {
          "name": "1c. Get Business360 Hierarchy",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "// Store query ID for next requests",
                  "var jsonData = pm.response.json();",
                  "if (jsonData && jsonData.queryId) {",
                  "    pm.collectionVariables.set('query_id', jsonData.queryId);",
                  "    console.log('Hierarchy Query ID stored:', jsonData.queryId);",
                  "} else {",
                  "    console.error('Failed to get query ID from response');",
                  "}",
                  "",
                  "// Validate response",
                  "pm.test(\"Hierarchy query submitted successfully\", function () {",
                  "    pm.response.to.have.status(200);",
                  "    pm.expect(jsonData.success).to.be.true;",
                  "    pm.expect(jsonData.queryId).to.exist;",
                  "});"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json"
              },
              {
                "key": "X-API-Key",
                "value": "{{api_key}}"
              }
            ],
            "body": {
              "mode": "raw",
              "raw": "{}"
            },
            "url": {
              "raw": "{{gateway_url}}/api/data/v1/analytics/business360-hierarchy",
              "host": [
                "{{gateway_url}}"
              ],
              "path": [
                "api",
                "data",
                "v1",
                "analytics",
                "business360-hierarchy"
              ]
            },
            "description": "Get brand/tenant/shop organizational hierarchy for authorized brands. Returns all relationships."
          }
        },
        {
          "name": "1d. Get Brand Insights",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "// Store query ID for next requests",
                  "var jsonData = pm.response.json();",
                  "if (jsonData && jsonData.queryId) {",
                  "    pm.collectionVariables.set('query_id', jsonData.queryId);",
                  "    console.log('Brand Insights Query ID stored:', jsonData.queryId);",
                  "    console.log('Brand ID used:', pm.collectionVariables.get('brand_id'));",
                  "} else {",
                  "    console.error('Failed to get query ID from response');",
                  "}",
                  "",
                  "// Validate response",
                  "pm.test(\"Brand insights query submitted successfully\", function () {",
                  "    pm.response.to.have.status(200);",
                  "    pm.expect(jsonData.success).to.be.true;",
                  "    pm.expect(jsonData.queryId).to.exist;",
                  "    pm.expect(jsonData.parameters.brand_id).to.exist;",
                  "});"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json"
              },
              {
                "key": "X-API-Key",
                "value": "{{api_key}}"
              }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"brand_id\": \"{{brand_id}}\"\n}"
            },
            "url": {
              "raw": "{{gateway_url}}/api/data/v1/analytics/business360-insights-brand",
              "host": [
                "{{gateway_url}}"
              ],
              "path": [
                "api",
                "data",
                "v1",
                "analytics",
                "business360-insights-brand"
              ]
            },
            "description": "Get business insights aggregated by brand. Requires brand_id in request body. Returns time-series data from 2024-09-01 to current date."
          }
        },
        {
          "name": "1e. Get Tenant Insights",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "// Store query ID for next requests",
                  "var jsonData = pm.response.json();",
                  "if (jsonData && jsonData.queryId) {",
                  "    pm.collectionVariables.set('query_id', jsonData.queryId);",
                  "    console.log('Tenant Insights Query ID stored:', jsonData.queryId);",
                  "    console.log('Tenant ID used:', pm.collectionVariables.get('tenant_id'));",
                  "} else {",
                  "    console.error('Failed to get query ID from response');",
                  "}",
                  "",
                  "// Validate response",
                  "pm.test(\"Tenant insights query submitted successfully\", function () {",
                  "    pm.response.to.have.status(200);",
                  "    pm.expect(jsonData.success).to.be.true;",
                  "    pm.expect(jsonData.queryId).to.exist;",
                  "    pm.expect(jsonData.parameters.tenant_id).to.exist;",
                  "});"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json"
              },
              {
                "key": "X-API-Key",
                "value": "{{api_key}}"
              }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"tenant_id\": \"{{tenant_id}}\"\n}"
            },
            "url": {
              "raw": "{{gateway_url}}/api/data/v1/analytics/business360-insights-tenant",
              "host": [
                "{{gateway_url}}"
              ],
              "path": [
                "api",
                "data",
                "v1",
                "analytics",
                "business360-insights-tenant"
              ]
            },
            "description": "Get business insights aggregated by tenant. Requires tenant_id in request body. Returns time-series data from 2024-09-01 to current date."
          }
        },
        {
          "name": "1f. Get Shop Insights",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "// Store query ID for next requests",
                  "var jsonData = pm.response.json();",
                  "if (jsonData && jsonData.queryId) {",
                  "    pm.collectionVariables.set('query_id', jsonData.queryId);",
                  "    console.log('Shop Insights Query ID stored:', jsonData.queryId);",
                  "    console.log('Shop ID used:', pm.collectionVariables.get('shop_id'));",
                  "} else {",
                  "    console.error('Failed to get query ID from response');",
                  "}",
                  "",
                  "// Validate response",
                  "pm.test(\"Shop insights query submitted successfully\", function () {",
                  "    pm.response.to.have.status(200);",
                  "    pm.expect(jsonData.success).to.be.true;",
                  "    pm.expect(jsonData.queryId).to.exist;",
                  "    pm.expect(jsonData.parameters.shop_id).to.exist;",
                  "});"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "POST",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json"
              },
              {
                "key": "X-API-Key",
                "value": "{{api_key}}"
              }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"shop_id\": \"{{shop_id}}\"\n}"
            },
            "url": {
              "raw": "{{gateway_url}}/api/data/v1/analytics/business360-insights-shop",
              "host": [
                "{{gateway_url}}"
              ],
              "path": [
                "api",
                "data",
                "v1",
                "analytics",
                "business360-insights-shop"
              ]
            },
            "description": "Get business insights aggregated by shop. Requires shop_id in request body. Returns time-series data from 2024-09-01 to current date."
          }
        },
        {
          "name": "2. Check Query Status",
          "event": [
            {
              "listen": "prerequest",
              "script": {
                "exec": [
                  "// Check if query_id exists",
                  "var queryId = pm.collectionVariables.get('query_id');",
                  "if (!queryId || queryId === '') {",
                  "    console.error('❌ ERROR: query_id is not set!');",
                  "    console.log('');",
                  "    console.log('To fix this issue:');",
                  "    console.log('1. Run \"1. Submit Customer Query\" first to get a query_id');",
                  "    console.log('2. Wait for it to complete (you should see \"Query ID stored: ...\" in console)');",
                  "    console.log('3. Then run this request');",
                  "    console.log('');",
                  "    console.log('Make sure you are using:');",
                  "    console.log('- Gateway URL: https://public-api.steercrm.com');",
                  "    console.log('- Environment: API Gateway - Analytics');",
                  "    console.log('');",
                  "    throw new Error('query_id not set. Please run \"1. Submit Customer Query\" first.');",
                  "}"
                ],
                "type": "text/javascript"
              }
            },
            {
              "listen": "test",
              "script": {
                "exec": [
                  "pm.test(\"Status check successful\", function () {",
                  "    pm.response.to.have.status(200);",
                  "    var jsonData = pm.response.json();",
                  "    pm.expect(jsonData.queryId).to.equal(pm.collectionVariables.get('query_id'));",
                  "    pm.expect(jsonData.status).to.be.oneOf(['RUNNING', 'SUCCESS', 'FAILED']);",
                  "});",
                  "",
                  "var jsonData = pm.response.json();",
                  "console.log('Query status:', jsonData.status);",
                  "",
                  "// If successful, store partition info",
                  "if (jsonData.status === 'SUCCESS' && jsonData.resultSetMetaData) {",
                  "    var partitions = jsonData.resultSetMetaData.partitionInfo;",
                  "    if (partitions) {",
                  "        pm.collectionVariables.set('total_partitions', partitions.length);",
                  "        console.log('Total partitions:', partitions.length);",
                  "        console.log('Total rows:', jsonData.resultSetMetaData.numRows);",
                  "    }",
                  "}"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "X-API-Key",
                "value": "{{api_key}}"
              }
            ],
            "url": {
              "raw": "{{gateway_url}}/api/data/v1/analytics/{{query_id}}/status",
              "host": [
                "{{gateway_url}}"
              ],
              "path": [
                "api",
                "data",
                "v1",
                "analytics",
                "{{query_id}}",
                "status"
              ]
            },
            "description": "Check the status of submitted query. Returns SUCCESS when data is ready for retrieval."
          }
        },
        {
          "name": "3. Get Results - Partition 0",
          "event": [
            {
              "listen": "prerequest",
              "script": {
                "exec": [
                  "// Check if query_id exists",
                  "var queryId = pm.collectionVariables.get('query_id');",
                  "if (!queryId || queryId === '') {",
                  "    console.error('❌ ERROR: query_id is not set!');",
                  "    console.log('');",
                  "    console.log('To fix this issue:');",
                  "    console.log('1. Run \"1. Submit Customer Query\" first to get a query_id');",
                  "    console.log('2. Wait for it to complete');",
                  "    console.log('3. Run \"2. Check Query Status\" to verify it is SUCCESS');",
                  "    console.log('4. Then run this request');",
                  "    console.log('');",
                  "    console.log('Make sure you are using:');",
                  "    console.log('- Gateway URL: https://public-api.steercrm.com');",
                  "    console.log('- Environment: API Gateway - Analytics');",
                  "    console.log('');",
                  "    throw new Error('query_id not set. Please run \"1. Submit Customer Query\" first.');",
                  "}"
                ],
                "type": "text/javascript"
              }
            },
            {
              "listen": "test",
              "script": {
                "exec": [
                  "pm.test(\"Results retrieved successfully\", function () {",
                  "    pm.response.to.have.status(200);",
                  "    var jsonData = pm.response.json();",
                  "    pm.expect(jsonData.success).to.be.true;",
                  "    pm.expect(jsonData.data).to.be.an('array');",
                  "    pm.expect(jsonData.partition).to.equal(0);",
                  "});",
                  "",
                  "var jsonData = pm.response.json();",
                  "console.log('Partition 0 rows:', jsonData.rowCount);",
                  "console.log('Response time:', pm.response.responseTime, 'ms');",
                  "",
                  "// Store response time for cache comparison",
                  "pm.collectionVariables.set('uncached_time', pm.response.responseTime);"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "X-API-Key",
                "value": "{{api_key}}"
              }
            ],
            "url": {
              "raw": "{{gateway_url}}/api/data/v1/analytics/{{query_id}}/results?partition=0",
              "host": [
                "{{gateway_url}}"
              ],
              "path": [
                "api",
                "data",
                "v1",
                "analytics",
                "{{query_id}}",
                "results"
              ],
              "query": [
                {
                  "key": "partition",
                  "value": "0",
                  "description": "Partition number (0-based)"
                }
              ]
            },
            "description": "Retrieve results from partition 0. First call fetches from Analytics, subsequent calls use Redis cache."
          }
        }
      ]
    },
    {
      "name": "Query Types",
      "item": [
        {
          "name": "Available Query Types",
          "request": {
            "method": "GET",
            "header": [
              {
                "key": "X-API-Key",
                "value": "{{api_key}}"
              }
            ],
            "url": {
              "raw": "{{gateway_url}}/api/data/v1/analytics/types",
              "host": [
                "{{gateway_url}}"
              ],
              "path": [
                "api",
                "data",
                "v1",
                "analytics",
                "types"
              ]
            },
            "description": "Get list of available predefined query types (customer-vibes)"
          }
        }
      ]
    }
  ],
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// Initialize variables if not set",
          "if (!pm.collectionVariables.get('partition_number')) {",
          "    pm.collectionVariables.set('partition_number', '1');",
          "}",
          "",
          "// Check if this is the first run",
          "if (!pm.collectionVariables.get('query_id') || pm.collectionVariables.get('query_id') === '') {",
          "    console.log('');",
          "    console.log('🚀 Welcome to API Gateway Testing!');",
          "    console.log('');",
          "    console.log('To get started, run requests in this order:');",
          "    console.log('1. Health → Gateway Health');",
          "    console.log('2. Analytics Query Workflow → Choose a query:');",
          "    console.log('   - 1. Submit Customer Query (full dataset)');",
          "    console.log('   - 1b. Submit Customer Query (24h delta)');",
          "    console.log('   - 1c. Get Business360 Hierarchy');",
          "    console.log('   - 1d. Get Brand Insights (requires brand_id)');",
          "    console.log('   - 1e. Get Tenant Insights (requires tenant_id)');",
          "    console.log('   - 1f. Get Shop Insights (requires shop_id)');",
          "    console.log('3. Analytics Query Workflow → 2. Check Query Status');",
          "    console.log('4. Analytics Query Workflow → 3. Get Results - Partition 0');",
          "    console.log('');",
          "}"
        ]
      }
    }
  ],
  "variable": [
    {
      "key": "gateway_url",
      "value": "https://public-api.steercrm.com",
      "type": "string",
      "description": "API Gateway URL (Production)"
    },
    {
      "key": "api_key",
      "value": "your-api-key-here",
      "type": "string",
      "description": "API Key for authentication - replace with your actual key"
    },
    {
      "key": "query_id",
      "value": "",
      "type": "string",
      "description": "Dynamically set after query submission"
    },
    {
      "key": "total_partitions",
      "value": "",
      "type": "string",
      "description": "Total number of partitions in result set"
    },
    {
      "key": "brand_id",
      "value": "b0462aa4-1a6b-4c16-ab17-6e4fd5967f94",
      "type": "string",
      "description": "Brand ID for Business360 insights queries"
    },
    {
      "key": "tenant_id",
      "value": "",
      "type": "string",
      "description": "Tenant ID for Business360 insights queries - set after running hierarchy"
    },
    {
      "key": "shop_id",
      "value": "",
      "type": "string",
      "description": "Shop ID for Business360 insights queries - set after running hierarchy"
    }
  ]
}
