{
  "info": {
    "name": "Octapower - Water Invoice",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    {
      "key": "base_url",
      "value": "http://localhost/octapower"
    },
    {
      "key": "username",
      "value": "collector_user"
    },
    {
      "key": "password",
      "value": "123456"
    },
    {
      "key": "device_os",
      "value": "android"
    },
    {
      "key": "app_version",
      "value": ""
    },
    {
      "key": "token",
      "value": ""
    },
    {
      "key": "collector_id",
      "value": ""
    },
    {
      "key": "user_active_id",
      "value": ""
    },
    {
      "key": "account_position_id",
      "value": "1"
    }
  ],
  "item": [
    {
      "name": "1 - Login",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('Login OK', () => pm.response.to.have.status(200));",
              "",
              "// Token is returned in the response header, not the body",
              "const auth = pm.response.headers.get('Authorization') || '';",
              "const token = auth.replace(/^Bearer\\s+/i, '').trim();",
              "pm.collectionVariables.set('token', token);",
              "console.log('token:', token);",
              "",
              "const body = pm.response.json();",
              "if (body.user_active_id) pm.collectionVariables.set('user_active_id', body.user_active_id);"
            ]
          }
        }
      ],
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"username\": \"{{username}}\",\n  \"password\": \"{{password}}\",\n  \"device_id\": \"postman-device\",\n  \"device_os\": \"{{device_os}}\",\n  \"app_version\": \"{{app_version}}\"\n}"
        },
        "url": "{{base_url}}/api_2/login.php"
      }
    },
    {
      "name": "2 - Add Water Invoices (2 invoices)",
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get('token')) throw new Error('Run \"1 - Login\" first');",
              "pm.collectionVariables.set('now', new Date().toISOString().slice(0, 19).replace('T', ' '));"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test('Status 200', () => pm.response.to.have.status(200));",
              "const r = pm.response.json();",
              "pm.test('API status = 1', () => pm.expect(r.status).to.eql(1));",
              "console.log('success_no:', r.success_no, 'error_no:', r.error_no, r.error_val);"
            ]
          }
        }
      ],
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "Authorization",
            "value": "Bearer {{token}}"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"action\": \"add_inv_water\",\n  \"lang_code\": \"ar\",\n  \"app_collector_id\": \"{{collector_id}}\",\n  \"app_user_active_id\": \"{{user_active_id}}\",\n  \"ident\": \"postman-{{$timestamp}}\",\n  \"data\": [\n    {\n      \"app_subscriber_id\": 101,\n      \"date\": \"{{now}}\",\n      \"app_currency_id\": 1,\n      \"app_currency_rate\": 1,\n      \"app_account_position_id\": \"{{account_position_id}}\",\n      \"qty\": 1,\n      \"description\": \"Water invoice 1\",\n      \"in\": 20,\n      \"out\": 5,\n      \"price\": 1.5,\n      \"total_price\": 30\n    },\n    {\n      \"app_subscriber_id\": 102,\n      \"date\": \"{{now}}\",\n      \"app_currency_id\": 1,\n      \"app_currency_rate\": 1,\n      \"app_account_position_id\": \"{{account_position_id}}\",\n      \"qty\": 1,\n      \"description\": \"Water invoice 2\",\n      \"in\": 10,\n      \"out\": 0,\n      \"price\": 1.5,\n      \"total_price\": 15\n    }\n  ]\n}"
        },
        "url": "{{base_url}}/api_2/action.php"
      }
    }
  ]
}