{
  "info": {
    "_postman_id": "c9d2f1a4-2222-4b3c-8d4e-000000000002",
    "name": "Yasmina SME Medical Insurance (Sandbox)",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
    "description": "The SME Medical Insurance journey against https://sandbox.yasmina.ai: company, employees, prices from every provider, issue from a quote, health declarations, payment.\n\nSet client_id and client_secret in the collection variables to your sandbox credentials from the portal. Run it top to bottom with the Collection Runner and a 7000 ms delay: sandbox allows 10 requests a minute per IP."
  },
  "auth": {
    "type": "bearer",
    "bearer": [
      {
        "key": "token",
        "value": "{{token}}"
      }
    ]
  },
  "item": [
    {
      "name": "0. Get access token",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/oauth/token",
        "description": "OAuth 2.0 client credentials. Saves access_token into the collection variable `token`, which every other request uses.",
        "auth": {
          "type": "basic",
          "basic": [
            {
              "key": "username",
              "value": "{{client_id}}"
            },
            {
              "key": "password",
              "value": "{{client_secret}}"
            }
          ]
        },
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            {
              "key": "grant_type",
              "value": "client_credentials"
            }
          ]
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200 with access_token\", () => { pm.response.to.have.status(200); pm.expect(pm.response.json().access_token).to.be.a(\"string\"); });",
              "pm.collectionVariables.set(\"token\", pm.response.json().access_token);"
            ]
          }
        }
      ]
    },
    {
      "name": "1. Create company",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/companies",
        "description": "",
        "body": {
          "mode": "raw",
          "raw": "{\n  \"name\": \"Postman Test Co {{suffix}}\",\n  \"name_ar\": \"شركة اختبار\",\n  \"sponsor_number\": \"1000000001\",\n  \"email_address\": \"sme-test-{{suffix}}@example.com\",\n  \"phone_number\": \"+966512345678\",\n  \"commercial_registration_number\": \"70101{{suffix}}\",\n  \"unified_national_number\": \"7000000001\"\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.collectionVariables.set(\"suffix\", String(Date.now()).slice(-5));"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"201 with id\", () => { pm.response.to.have.status(201); pm.expect(pm.response.json().id).to.be.a(\"number\"); });",
              "pm.test(\"unified_national_number is echoed back\", () => pm.expect(pm.response.json().unified_national_number).to.eql(\"7000000001\"));",
              "pm.collectionVariables.set(\"company_id\", pm.response.json().id);"
            ]
          }
        }
      ]
    },
    {
      "name": "1b. Create company - bad numbers (expect 422)",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/companies",
        "description": "sponsor_number must be 10 digits, commercial_registration_number must be 10 digits starting with 7, unified_national_number is required.",
        "body": {
          "mode": "raw",
          "raw": "{\n  \"name\": \"Bad Co\",\n  \"name_ar\": \"شركة\",\n  \"sponsor_number\": \"789\",\n  \"email_address\": \"bad@example.com\",\n  \"phone_number\": \"+966512345678\",\n  \"commercial_registration_number\": \"1000000001\"\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"422 on all three fields\", () => { pm.response.to.have.status(422); const d = pm.response.json().details;",
              "  pm.expect(d.sponsor_number, \"sponsor_number must be 10 digits\").to.exist;",
              "  pm.expect(d.commercial_registration_number, \"CR must start with 7\").to.exist;",
              "  pm.expect(d.unified_national_number, \"unified_national_number is required\").to.exist; });"
            ]
          }
        }
      ]
    },
    {
      "name": "2a. List packages - medgulf",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/products/categories?insurance_provider=medgulf"
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200\", () => pm.response.to.have.status(200));",
              "const b = pm.response.json();",
              "pm.test(\"provider is medgulf\", () => pm.expect(b.insurance_provider).to.eql(\"medgulf\"));",
              "pm.test(\"categories present and self-describing\", () => { const cats = b.categories; pm.expect(Object.keys(cats)).to.not.be.empty; for (const [k,v] of Object.entries(cats)) { pm.expect(v.category).to.eql(k); pm.expect(v.insurance_provider).to.eql(b.insurance_provider); } });",
              "const keys = Object.keys(b.categories);",
              "pm.collectionVariables.set(\"medgulf_category_first\", keys[0]);",
              "pm.collectionVariables.set(\"medgulf_category_last\", keys[keys.length-1]);",
              "pm.test(\"every package carries a price\", () => { for (const v of Object.values(b.categories)) pm.expect(v.price, \"price\").to.be.a(\"number\"); });",
              "pm.test(\"the four shared tiers\", () => pm.expect(Object.keys(b.categories)).to.eql([\"basic\",\"standard\",\"premium\",\"executive\"]));"
            ]
          }
        }
      ]
    },
    {
      "name": "2b. List packages - walaa",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/products/categories?insurance_provider=walaa"
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200\", () => pm.response.to.have.status(200));",
              "const b = pm.response.json();",
              "pm.test(\"provider is walaa\", () => pm.expect(b.insurance_provider).to.eql(\"walaa\"));",
              "pm.test(\"categories present and self-describing\", () => { const cats = b.categories; pm.expect(Object.keys(cats)).to.not.be.empty; for (const [k,v] of Object.entries(cats)) { pm.expect(v.category).to.eql(k); pm.expect(v.insurance_provider).to.eql(b.insurance_provider); } });"
            ]
          }
        }
      ]
    },
    {
      "name": "2c. List packages - Al-Rajhi Takaful",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/products/categories?insurance_provider=alrajhi"
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200\", () => pm.response.to.have.status(200));",
              "const b = pm.response.json();",
              "pm.test(\"provider is alrajhi\", () => pm.expect(b.insurance_provider).to.eql(\"alrajhi\"));",
              "pm.test(\"categories present and self-describing\", () => { const cats = b.categories; pm.expect(Object.keys(cats)).to.not.be.empty; for (const [k,v] of Object.entries(cats)) { pm.expect(v.category).to.eql(k); pm.expect(v.insurance_provider).to.eql(b.insurance_provider); } });"
            ]
          }
        }
      ]
    },
    {
      "name": "2d. List packages - tawuniya",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/products/categories?insurance_provider=tawuniya"
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200\", () => pm.response.to.have.status(200));",
              "const b = pm.response.json();",
              "pm.test(\"provider is tawuniya\", () => pm.expect(b.insurance_provider).to.eql(\"tawuniya\"));",
              "pm.test(\"categories present and self-describing\", () => { const cats = b.categories; pm.expect(Object.keys(cats)).to.not.be.empty; for (const [k,v] of Object.entries(cats)) { pm.expect(v.category).to.eql(k); pm.expect(v.insurance_provider).to.eql(b.insurance_provider); } });"
            ]
          }
        }
      ]
    },
    {
      "name": "2e. List packages - malath",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/products/categories?insurance_provider=malath"
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200\", () => pm.response.to.have.status(200));",
              "const b = pm.response.json();",
              "pm.test(\"provider is malath\", () => pm.expect(b.insurance_provider).to.eql(\"malath\"));",
              "pm.test(\"categories present and self-describing\", () => { const cats = b.categories; pm.expect(Object.keys(cats)).to.not.be.empty; for (const [k,v] of Object.entries(cats)) { pm.expect(v.category).to.eql(k); pm.expect(v.insurance_provider).to.eql(b.insurance_provider); } });"
            ]
          }
        }
      ]
    },
    {
      "name": "2f. List packages - no provider defaults to medgulf",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/products/categories",
        "description": "Omitting insurance_provider falls back to MedGulf."
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"defaults to medgulf\", () => { pm.response.to.have.status(200); pm.expect(pm.response.json().insurance_provider).to.eql(\"medgulf\"); });"
            ]
          }
        }
      ]
    },
    {
      "name": "2g. List packages - bupa is gone (expect 422)",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/products/categories?insurance_provider=bupa"
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"422 - bupa is no longer offered\", () => { pm.response.to.have.status(422); pm.expect(pm.response.json().details.insurance_provider).to.exist; });"
            ]
          }
        }
      ]
    },
    {
      "name": "3. Save the company's employees",
      "request": {
        "method": "PUT",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/companies/{{company_id}}/employees",
        "description": "The full employee list, replaced on every call. Each employee picks one of four tiers: basic, standard, premium, executive. No prices here.",
        "body": {
          "mode": "raw",
          "raw": "{\n  \"employees\": [\n    {\n      \"name\": \"John Doe\",\n      \"nationality_iso\": \"SA\",\n      \"category\": \"basic\",\n      \"nationality_id\": \"1111111111\",\n      \"mobile_number\": \"0500000001\",\n      \"date_of_birth\": \"1409-09-14\",\n      \"dependents\": [\n        {\n          \"name\": \"Jane Doe\",\n          \"relationship\": \"spouse\",\n          \"nationality_iso\": \"SA\",\n          \"nationality_id\": \"1111111112\",\n          \"date_of_birth\": \"1411-01-05\"\n        },\n        {\n          \"name\": \"Jack Doe\",\n          \"relationship\": \"child\",\n          \"nationality_iso\": \"SA\",\n          \"nationality_id\": \"1111111113\",\n          \"date_of_birth\": \"1437-04-22\"\n        }\n      ]\n    },\n    {\n      \"name\": \"Richard Roe\",\n      \"nationality_iso\": \"JO\",\n      \"category\": \"executive\",\n      \"iqama_id\": \"2222222222\",\n      \"mobile_number\": \"0500000002\",\n      \"date_of_birth\": \"1988-04-20\",\n      \"dependents\": []\n    }\n  ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200 with the two employees\", () => { pm.response.to.have.status(200); pm.expect(pm.response.json().employees).to.have.lengthOf(2); });",
              "pm.test(\"tiers and dependents kept\", () => { const e = pm.response.json().employees; pm.expect(e[0].category).to.eql(\"basic\"); pm.expect(e[1].category).to.eql(\"executive\"); pm.expect(e[0].dependents).to.have.lengthOf(2); });"
            ]
          }
        }
      ]
    },
    {
      "name": "3b. Save employees - bad tier, 9-digit iqama, no mobile (expect 422)",
      "request": {
        "method": "PUT",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/companies/{{company_id}}/employees",
        "description": "",
        "body": {
          "mode": "raw",
          "raw": "{\n  \"employees\": [\n    {\n      \"name\": \"John Doe\",\n      \"nationality_iso\": \"SA\",\n      \"category\": \"gold\",\n      \"nationality_id\": \"1111111111\",\n      \"mobile_number\": \"0500000001\",\n      \"date_of_birth\": \"1409-09-14\",\n      \"dependents\": [\n        {\n          \"name\": \"Jane Doe\",\n          \"relationship\": \"spouse\",\n          \"nationality_iso\": \"SA\",\n          \"nationality_id\": \"1111111112\",\n          \"date_of_birth\": \"1411-01-05\"\n        },\n        {\n          \"name\": \"Jack Doe\",\n          \"relationship\": \"child\",\n          \"nationality_iso\": \"SA\",\n          \"nationality_id\": \"1111111113\",\n          \"date_of_birth\": \"1437-04-22\"\n        }\n      ]\n    },\n    {\n      \"name\": \"Richard Roe\",\n      \"nationality_iso\": \"JO\",\n      \"category\": \"executive\",\n      \"iqama_id\": \"234567890\",\n      \"date_of_birth\": \"1988-04-20\",\n      \"dependents\": []\n    }\n  ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"422 naming each fault\", () => { pm.response.to.have.status(422); const d = pm.response.json().details; pm.expect(d[\"employees.0.category\"]).to.exist; pm.expect(d[\"employees.1.iqama_id\"]).to.exist; pm.expect(d[\"employees.1.mobile_number\"]).to.exist; });"
            ]
          }
        }
      ]
    },
    {
      "name": "3c. Read the employees back",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/companies/{{company_id}}/employees",
        "description": ""
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200 with two employees\", () => { pm.response.to.have.status(200); pm.expect(pm.response.json().employees).to.have.lengthOf(2); });"
            ]
          }
        }
      ]
    },
    {
      "name": "3d. Get prices from every provider",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/quote-requests",
        "description": "One response, one quote per provider, priced from the package tables for the employees saved above. The cheapest is picked for the next step.",
        "body": {
          "mode": "raw",
          "raw": "{\n  \"company_id\": \"{{company_id}}\"\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"201 with one quote per provider\", () => { pm.response.to.have.status(201); pm.expect(pm.response.json().quotes).to.have.lengthOf(5); });",
              "const q = pm.response.json();",
              "pm.test(\"every quote has a uuid, a provider and a total\", () => { for (const x of q.quotes) { pm.expect(x.quote_price_id).to.match(/^[0-9a-f-]{36}$/); pm.expect(x.insurance_provider).to.be.a(\"string\"); pm.expect(x.total_price).to.be.a(\"number\"); } });",
              "pm.test(\"totals are the sum of the per-employee lines\", () => { for (const x of q.quotes) pm.expect(x.employees.reduce((s,l)=>s+l.price,0)).to.eql(x.total_price); });",
              "pm.test(\"providers price differently\", () => pm.expect(new Set(q.quotes.map(x=>x.total_price)).size).to.be.above(1));",
              "pm.collectionVariables.set(\"quote_request_id\", q.id);",
              "const pick = q.quotes.reduce((a,b) => a.total_price <= b.total_price ? a : b);",
              "pm.collectionVariables.set(\"quote_price_id\", pick.quote_price_id);",
              "pm.collectionVariables.set(\"chosen_provider\", pick.insurance_provider);",
              "pm.collectionVariables.set(\"total_due\", pick.total_price);"
            ]
          }
        }
      ]
    },
    {
      "name": "3e. Read the quote request back",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/quote-requests/{{quote_request_id}}",
        "description": ""
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200 and same id\", () => { pm.response.to.have.status(200); pm.expect(pm.response.json().id).to.eql(pm.collectionVariables.get(\"quote_request_id\")); });"
            ]
          }
        }
      ]
    },
    {
      "name": "3f. Issue the policy from the chosen quote",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/policies",
        "description": "",
        "body": {
          "mode": "raw",
          "raw": "{\n  \"quote_request_id\": \"{{quote_request_id}}\",\n  \"quote_price_id\": \"{{quote_price_id}}\",\n  \"redirect_url\": \"https://www.example.com?yasmina_policy_id=policyID\"\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"201 with one policy for the company\", () => { pm.response.to.have.status(201); pm.expect(pm.response.json().id).to.be.a(\"number\"); });",
              "const p = pm.response.json();",
              "pm.test(\"it carries the chosen provider and the quoted total\", () => { pm.expect(p.meta_data.insurance_provider).to.eql(pm.collectionVariables.get(\"chosen_provider\")); pm.expect(Number(p.price)).to.eql(Number(pm.collectionVariables.get(\"total_due\"))); });",
              "pm.test(\"both employees are members on it\", () => pm.expect(p.meta_data.insured).to.have.lengthOf(2));",
              "pm.test(\"policyID was substituted in the redirect\", () => pm.expect(p.redirect_url).to.eql(\"https://www.example.com?yasmina_policy_id=\" + p.id));",
              "pm.collectionVariables.set(\"policy_id_1\", p.id);"
            ]
          }
        }
      ]
    },
    {
      "name": "3g. Issue with a quote id from nowhere (expect 422)",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/policies",
        "description": "",
        "body": {
          "mode": "raw",
          "raw": "{\n  \"quote_request_id\": \"{{quote_request_id}}\",\n  \"quote_price_id\": \"00000000-0000-4000-8000-000000000000\"\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"422\", () => pm.response.to.have.status(422));"
            ]
          }
        }
      ]
    },
    {
      "name": "4a. List policies for the company",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/policies?company_id={{company_id}}",
        "description": ""
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200\", () => pm.response.to.have.status(200));",
              "pm.test(\"one policy for this company\", () => { const p = pm.response.json(); pm.expect(p).to.have.lengthOf(1); pm.expect(p[0].id).to.eql(Number(pm.collectionVariables.get(\"policy_id_1\"))); });"
            ]
          }
        }
      ]
    },
    {
      "name": "4b. List policies (no filter)",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/policies",
        "description": "Only SME medical policies, never the client's motor or property book."
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200\", () => pm.response.to.have.status(200));",
              "const p = pm.response.json();",
              "pm.test(\"every policy is a medical one\", () => { const medical = p.find(x => x.id == pm.collectionVariables.get(\"policy_id_1\")).product_id;",
              "  const foreign = p.filter(x => x.product_id !== medical);",
              "  pm.expect(foreign.length, `${foreign.length} non-medical policies leaked`).to.eql(0); });"
            ]
          }
        }
      ]
    },
    {
      "name": "5a. Get the declaration questions",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/medical-forms",
        "description": ""
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200 with question groups\", () => { pm.response.to.have.status(200); pm.expect(pm.response.json().general_health_information).to.exist; });"
            ]
          }
        }
      ]
    },
    {
      "name": "5a2. Payment link before declaring (expect 422)",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/payment-link?company_id={{company_id}}",
        "description": "No payment link until every employee has declared their medical history."
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"422 while employees still have to declare\", () => pm.response.to.have.status(422));",
              "pm.test(\"it names who is missing\", () => pm.expect(pm.response.json().message).to.include(\"declare\"));"
            ]
          }
        }
      ]
    },
    {
      "name": "5b. Download the declaration form (PDF)",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/pdf"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/medical-forms/download?company_id={{company_id}}",
        "description": "A printable health declaration, one page per enrolled employee with their name, id and package already filled in. Use Postman's \"Send and Download\" to save it. Omit company_id for a single blank form."
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200 PDF\", () => { pm.response.to.have.status(200); pm.expect(pm.response.headers.get(\"Content-Type\")).to.include(\"application/pdf\"); });",
              "pm.test(\"comes back as a download named after the company\", () => pm.expect(pm.response.headers.get(\"Content-Disposition\")).to.include(\"health-declaration-\" + pm.collectionVariables.get(\"company_id\")));",
              "pm.test(\"it is a real PDF\", () => pm.expect(pm.response.stream.toString().slice(0, 4)).to.eql(\"%PDF\"));"
            ]
          }
        }
      ]
    },
    {
      "name": "5c. Fill the declarations for the employees",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/medical-forms/fill",
        "description": "A company admin answers on the employees' behalf. The answers are stored on each employee's policy.",
        "body": {
          "mode": "raw",
          "raw": "{\n  \"company_id\": \"{{company_id}}\",\n  \"all_employees\": {\n    \"allergies\": \"No\",\n    \"current_medications\": \"No\",\n    \"hospitalizations\": \"No\",\n    \"pre_existing_conditions\": \"No\",\n    \"past_surgeries\": \"No\",\n    \"surgery_complications\": \"No\",\n    \"chest_pain_or_breathlessness\": \"No\",\n    \"heart_procedures\": \"No\",\n    \"smoking\": \"No\",\n    \"alcohol_consumption\": \"No\",\n    \"physical_activity\": \"No\",\n    \"family_history\": \"No\",\n    \"chronic_disease\": [\n      \"None of the above\"\n    ]\n  },\n  \"ids_or_emails\": [\n    {\n      \"id_or_email\": \"1111111111\",\n      \"answers\": {\n        \"allergies\": \"No\",\n        \"current_medications\": \"No\",\n        \"hospitalizations\": \"No\",\n        \"pre_existing_conditions\": \"No\",\n        \"past_surgeries\": \"Yes\",\n        \"surgery_complications\": \"No\",\n        \"chest_pain_or_breathlessness\": \"No\",\n        \"heart_procedures\": \"No\",\n        \"smoking\": \"No\",\n        \"alcohol_consumption\": \"No\",\n        \"physical_activity\": \"No\",\n        \"family_history\": \"No\",\n        \"chronic_disease\": [\n          \"Liver Disease\"\n        ]\n      }\n    }\n  ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200 with a count of who was declared for\", () => { pm.response.to.have.status(200); pm.expect(pm.response.json().declared).to.eql(2); pm.expect(pm.response.json().remaining).to.eql(0); });"
            ]
          }
        }
      ]
    },
    {
      "name": "5d. Fill with a bogus chronic disease (expect 422)",
      "request": {
        "method": "POST",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/medical-forms/fill",
        "body": {
          "mode": "raw",
          "raw": "{\n  \"company_id\": \"{{company_id}}\",\n  \"all_employees\": {\n    \"allergies\": \"No\",\n    \"current_medications\": \"No\",\n    \"hospitalizations\": \"No\",\n    \"pre_existing_conditions\": \"No\",\n    \"past_surgeries\": \"No\",\n    \"surgery_complications\": \"No\",\n    \"chest_pain_or_breathlessness\": \"No\",\n    \"heart_procedures\": \"No\",\n    \"smoking\": \"No\",\n    \"alcohol_consumption\": \"No\",\n    \"physical_activity\": \"No\",\n    \"family_history\": \"No\",\n    \"chronic_disease\": [\n      \"None of the above\"\n    ]\n  },\n  \"ids_or_emails\": [\n    {\n      \"id_or_email\": \"1111111111\",\n      \"answers\": {\n        \"allergies\": \"No\",\n        \"current_medications\": \"No\",\n        \"hospitalizations\": \"No\",\n        \"pre_existing_conditions\": \"No\",\n        \"past_surgeries\": \"No\",\n        \"surgery_complications\": \"No\",\n        \"chest_pain_or_breathlessness\": \"No\",\n        \"heart_procedures\": \"No\",\n        \"smoking\": \"No\",\n        \"alcohol_consumption\": \"No\",\n        \"physical_activity\": \"No\",\n        \"family_history\": \"No\",\n        \"chronic_disease\": [\n          \"Bogus\"\n        ]\n      }\n    }\n  ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        }
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"422 on the chronic disease list\", () => { pm.response.to.have.status(422); pm.expect(pm.response.json().details[\"ids_or_emails.0.answers.chronic_disease.0\"]).to.exist; });"
            ]
          }
        }
      ]
    },
    {
      "name": "6a. Payment due",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/payment-due?company_id={{company_id}}",
        "description": "The sum of the premiums of this company's unissued policies."
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200\", () => pm.response.to.have.status(200));",
              "pm.test(\"equals the two premiums just issued\", () => pm.expect(Number(pm.response.json().payment)).to.eql(Number(pm.collectionVariables.get(\"total_due\"))));"
            ]
          }
        }
      ]
    },
    {
      "name": "6a2. Payment due without a company (expect 422)",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/payment-due"
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"422 - company_id is required\", () => { pm.response.to.have.status(422); pm.expect(pm.response.json().details.company_id).to.exist; });"
            ]
          }
        }
      ]
    },
    {
      "name": "6b. Payment link",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/payment-link?company_id={{company_id}}",
        "description": ""
      },
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "type": "text/javascript",
            "exec": [
              "if (!pm.collectionVariables.get(\"company_id\")) {",
              "  throw new Error(\"company_id is empty - run request 1 (Create company) first, or use the Collection Runner from the top.\");",
              "}"
            ]
          }
        },
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200 with a signed Payfort link\", () => { pm.response.to.have.status(200); const l = pm.response.json().payment;",
              "  pm.expect(l).to.include(\"payfort-payment-gateway\"); pm.expect(l).to.include(\"signature=\"); });",
              "pm.collectionVariables.set(\"payment_link\", pm.response.json().payment);"
            ]
          }
        }
      ]
    },
    {
      "name": "6c. Open the payment page",
      "request": {
        "method": "GET",
        "header": [],
        "url": "{{payment_link}}",
        "description": "The Payfort gateway page, the same one property and travel use. Shows the company's total and takes card details.",
        "auth": {
          "type": "noauth"
        }
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"the payment page renders\", () => pm.response.to.have.status(200));",
              "pm.test(\"it shows this company's total\", () => { const t = Number(pm.collectionVariables.get(\"total_due\")).toLocaleString(\"en-US\", {minimumFractionDigits: 2}); pm.expect(pm.response.text()).to.include(t); });"
            ]
          }
        }
      ]
    },
    {
      "name": "7. List companies",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Accept",
            "value": "application/json"
          }
        ],
        "url": "{{base_url}}/api/v1/medical/companies",
        "description": ""
      },
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "pm.test(\"200 and contains the new company\", () => { pm.response.to.have.status(200); pm.expect(pm.response.json().map(c=>c.id)).to.include(Number(pm.collectionVariables.get(\"company_id\"))); });"
            ]
          }
        }
      ]
    }
  ],
  "variable": [
    {
      "key": "base_url",
      "value": "https://sandbox.yasmina.ai"
    },
    {
      "key": "client_id",
      "value": ""
    },
    {
      "key": "client_secret",
      "value": ""
    },
    {
      "key": "token",
      "value": ""
    },
    {
      "key": "suffix",
      "value": ""
    },
    {
      "key": "company_id",
      "value": ""
    },
    {
      "key": "medgulf_category_first",
      "value": ""
    },
    {
      "key": "medgulf_category_last",
      "value": ""
    },
    {
      "key": "policy_id_1",
      "value": ""
    },
    {
      "key": "policy_id_2",
      "value": ""
    },
    {
      "key": "payment_link",
      "value": ""
    },
    {
      "key": "quote_request_id",
      "value": ""
    },
    {
      "key": "quote_price_id",
      "value": ""
    },
    {
      "key": "chosen_provider",
      "value": ""
    }
  ]
}