# GetEmployees: stored procedure replaced by a MySQL query (2026-11-27)

## What changed

`MantriAPIController::GetEmployees` no longer calls `EXEC TPP_GetEmployees empid, locid`, a SQL Server procedure that does not exist in MySQL. It now runs the specified query as a prepared statement:

```sql
SELECT EMI_Id AS employeeid, CONCAT(EMI_Code, ' | ', EMI_Name) AS employeename
FROM pdv_emi
WHERE 1 = 1 AND EMI_Id > 1 AND EMI_DOR IS NULL AND EMI_CliId = ?   -- locid
ORDER BY employeename
```

`pdv_emi` itself only returns rows with `EMI_Deleted = 0`.

The rest of the C# behaviour is unchanged:

- **Credential check:** `credentialsMatch()`. On failure: `ListDetails: []` with `notgranted` / `Failure` / `Credentials not matching`.
- **"All" row:** `{"employeeid":0,"employeename":"All"}` is inserted first only when the query returns **more than one** row **and** `allreq` is exactly `"Y"`. The comparison is case-sensitive, as in C#.
- **Success:** `setGranted()`, i.e. `granted` / `Success` / `Permission Granted Successfully !`.
- **Request:** `apikey`, `secretkey`, `empid`, `locid`, `allreq`, bound by the existing rules. Keys are case-insensitive; JSON or form bodies are accepted; a non-numeric `locid` becomes 0.
- **Route:** POST only (`[HttpPost]`).

## Points that need your decision

1. **`empid` is accepted but not used.**
   - The C# code passed `empid` to `TPP_GetEmployees`. That procedure's source is not in the project or in MySQL; it exists only on the original SQL Server.
   - The specified query has no `empid` condition, so `empid` currently has no effect. The tests confirm that `empid` 1, 999 and missing all give identical results.
   - The original procedure may have used it to restrict the list, for example to the employee's own team or to users with location rights. **Please send the `TPP_GetEmployees` source from SQL Server** so its `empid` logic can be added if needed.
2. **`EMI_Id > 1` excludes employee 1.**
   - Employee 1 ("VISHAL SHAHAJI MADANE", location 1002) is a real, active employee, and is excluded by this condition.
   - Every other employee query in this database uses `EMI_Id > 0`, which excludes only the system user `-1`. Implemented as specified; changing it is a one-character edit.
3. **NULL employee code.**
   - Employee 391 has no `EMI_Code`, so `CONCAT` returns NULL.
   - Their `employeename` is serialized as `{}` (the API-wide NULL convention) and sorts first.
   - `CONCAT_WS` or `IFNULL(EMI_Code, '')` would avoid this, but either would change the specified query.

## Test results

All tests ran through Apache with the real `DbConnect::connect()`.

**GetEmployees tests: 23 / 23 pass.**

| Check | Result |
|---|---|
| Rows identical to the same SQL run directly, in the same order | Pass: location 1000 (333 rows), 1001 (179), 1002 (198), 1003 (5) |
| `allreq = "Y"` | "All" row first, followed by the same rows |
| `allreq` `"N"`, `"y"` or missing | No "All" row |
| No rows (`locid` -9) with `allreq` `"Y"` | `[]`, no "All" row |
| `employeeid` is a number | Pass |
| Envelope order unchanged | Pass |
| Resigned employees (`EMI_DOR` set) | Excluded |
| Employee 1 | Excluded, as specified |
| Employee 391 | `employeename` is `{}` |
| Form body and mixed-case keys | Bind identically to lowercase JSON |
| `locid` `"abc"` or `"1000 OR 1=1"` | Bound as 0, gives `[]`; nothing is executed |
| Invalid apikey or secretkey | `notgranted` |
| GET | 404 |

**Regression: all suites pass.**

| Suite | Result |
|---|---|
| Unit | 45 / 45 |
| Migration HTTP | 29 / 29. The old check "GetEmployees: procedure missing gives 500" now expects `granted`, as intended. |
| Route sweep | 70 / 70 |
| Employee APIs | 30 / 30 HTTP and 40 / 40 transactional |

Doc 02 no longer lists `TPP_GetEmployees` as required, and doc 03 now marks GetEmployees as V1.
