# MantriAPI: master data list APIs

Six read-only list APIs were added to the migrated PHP API on 2026-11-27. They use the same architecture as the existing endpoints:

| Area | Implementation |
|---|---|
| Controller | Methods on `MantriAPIController` |
| Request models | `MantirAPIModel.php`, bound with the MVC model-binding rules |
| Routing | `helpers/route_table.php` |
| Database | `DbConnect::connect()` from `dbConnect.php`, through `MantriDb` prepared statements |
| Authentication | The existing `apikey` / `secretkey` check against `config/api_config.php` |
| Response | The existing `dataConversiontoJson` envelope |

## 1. Endpoints

Base URL: `http://<host>/hrmkirangroupapp/MantriAPI/<name>`. The method is **POST** only; any other verb returns 404, as for every `[HttpPost]` action.

| API | Request fields | SQL (parameters bound with `?`) |
|---|---|---|
| `GetDepartmentLists` | apikey, secretkey, locid | `SELECT DPI_Id AS departmentid, DPI_Name AS departmentname FROM ma_dpi WHERE DPI_Deleted = 0 AND DPI_CliId = locid` |
| `GetCategoryLists` | apikey, secretkey, locid | `SELECT ECI_Id AS categoryid, ECI_Name AS categoryname FROM ma_eci WHERE ECI_Deleted = 0 AND ECI_CliId = locid` |
| `GetDesignationLists` | apikey, secretkey, locid (read, not used in SQL) | `SELECT EMD_Id AS designationid, EMD_Name AS designationname FROM ma_emd WHERE EMD_Deleted = 0` |
| `GetCountryLists` | apikey, secretkey, locid (read, not used in SQL) | `SELECT COI_Id AS countryid, COI_Name AS countryname FROM ma_coi WHERE COI_Deleted = 0` |
| `GetStateLists` | apikey, secretkey, locid (read, not used in SQL), countryid | `SELECT STI_Id AS stateid, STI_Name AS statename FROM ma_sti WHERE STI_Deleted = 0 AND STI_CoiId = countryid` |
| `GetBloodGroupLists` | apikey, secretkey, locid (read, not used in SQL) | `SELECT BLD_Id AS bloodgroupid, BLD_Name AS bloodgroupname FROM ma_bld WHERE BLD_Deleted = 0` |

The short aliases (`GetCategory`, `GetDesignation`, `GetCountry`, `GetState`, `GetBloodGroup`) that were briefly registered have been removed from the route table and now return 404.

**Request binding** follows the existing MVC rules:

- Keys are case-insensitive.
- JSON body, form fields and query string are all accepted.
- A missing or non-numeric `locid` / `countryid` binds as **0**. It is then used in the query like any other value.
- Missing parameters are not reported as an error, which matches every existing endpoint.

## 2. Responses

The envelope is the same as all other endpoints: `ListDetails` comes first, then `cpermission`, `cresponse`, `error`.

**Success.** Example: `GetCountryLists`, real local data.

```json
{"ListDetails":[{"countryid":1,"countryname":"INDIA"}],"cpermission":"granted","cresponse":"Success","error":"Permission Granted Successfully !"}
```

**Success with several rows.** Example: `GetBloodGroupLists`, truncated.

```json
{"ListDetails":[{"bloodgroupid":1,"bloodgroupname":"A+"},{"bloodgroupid":2,"bloodgroupname":"A-"},{"bloodgroupid":3,"bloodgroupname":"B+"}, ...],"cpermission":"granted","cresponse":"Success","error":"Permission Granted Successfully !"}
```

**No matching rows.** Example: `GetDepartmentLists` with `"locid": -7`.

```json
{"ListDetails":[],"cpermission":"granted","cresponse":"Success","error":"Permission Granted Successfully !"}
```

**Invalid apikey or secretkey.**

```json
{"ListDetails":[],"cpermission":"notgranted","cresponse":"Failure","error":"Credentials not matching"}
```

**Database failure.** This follows the `GetUserCompanyAccess` convention for inline-SQL lists: the full details are logged to `logs/api_error.log`, and no credentials or paths are returned.

```json
{"ListDetails":[],"cpermission":"notgranted","cresponse":"Failure","error":"MantriDbConnectException: Unable to connect to the database server."}
```

**Value types.** IDs are JSON numbers and names are JSON strings. A NULL name would be serialized as `{}`, like every other endpoint.

## 3. Postman

`MantriAPI.postman_collection.json` now contains 67 requests, including the six new ones. Set the collection variables `baseUrl`, `apikey` and `secretkey`.

Example body for `GetStateLists`:

```json
{ "apikey": "{{apikey}}", "secretkey": "{{secretkey}}", "locid": 1000, "countryid": 1 }
```

## 4. Test results (2026-11-27, local XAMPP, `pravashr_kirangroup`)

**Overall:** 66 / 66 master-data tests pass. For valid requests, every API result was compared **row-for-row** with the same SQL run directly in MySQL.

| # | Scenario | Result |
|---|---|---|
| 1 | Valid credentials | Pass, all 6 APIs. Rows identical to the direct SQL: departments (locid 1000) 64, categories (locid 1001) 8, designations 17, countries 1, states (countryid 1) 38, blood groups 8. |
| 2 | Invalid apikey | Pass, all 6: `notgranted` / `Failure` / `Credentials not matching`, `ListDetails: []` |
| 3 | Invalid secretkey | Pass, all 6: same response as #2 |
| 4 | Missing parameters | Pass. With no keys, the credentials check fails. A missing `locid` binds as 0, which returns the departments/categories with `CliId = 0`; there are 12 and 1 such rows locally. |
| 5 | Invalid `locid` | Pass. `-7` gives granted with `[]`. `"abc"` binds as 0. |
| 6 | Valid `countryid` | Pass. `countryid 1` returns 38 states, and the result is identical for any `locid`. |
| 7 | Invalid `countryid` | Pass. `99999` gives granted with `[]`. `"1 OR 1=1"` binds as 0 and gives `[]`. |
| 8 | Empty result | Pass. `ListDetails: []` is serialized as an empty array, not `null`. |
| 9 | Multiple records | Pass. Five of the six APIs return several rows; `ma_coi` contains only one country. |
| 10 | DB connection failure | Pass, all 6. Tested with a deliberately wrong password, set in memory only. The response is the generic `Failure` envelope; it contains no user name, file path or secret key, and the secret key is not written to the log either. |

**Additional checks:**

- `locid` does not change the results of the designation, country, state and blood-group APIs.
- GET requests return 404.
- The six APIs were also run through Apache, `.htaccess` and the real `DbConnect::connect()`; the output matched the direct SQL.
- **Regression:** all existing suites still pass. That is 45 unit tests, 29 HTTP tests, and the route sweep, which covered 72 route names at the time (61 migrated, 6 new, 5 aliases). After the aliases were removed and the employee APIs added, the sweep covers 70 routes, all passing (see doc 05).
