# DTHospital mobile API (v1)

JSON over HTTPS. Base URL: `https://<your-server>/api/v1`. Every request should send
`Accept: application/json` (the server forces JSON either way).

## Authentication

```
POST /auth/login      {"login":"reception","password":"...","device_name":"Pixel 8"}
→ 200 {"token_type":"Bearer","access_token":"12|k3J...","user":{...,"role":"Receptionist","permissions":["patient_view",...]},"hospital":{"name":"..."}}
```

Send `Authorization: Bearer <access_token>` on every other call. Tokens last 90 days, are
stored hashed on the server, and are per device: `POST /auth/logout` revokes only the
token it is called with. Login is rate limited (10/min) and is refused with 403 while the
server has OTP switched on, because the app has no second-factor step yet.

| Status | Meaning |
|---|---|
| 401 | Missing, wrong, expired or revoked token (or bad login) |
| 403 | Signed in, but the user's role lacks the permission (`permission` field says which) |
| 404 | No such record |
| 422 | Validation failed: `{"message":..., "errors":{"field":["..."]}}` |
| 429 | Rate limited |

**Use `user.permissions` from login / `GET /auth/me` to decide which screens to show.**
They are the same role rules as the web app (Admin → Permissions).

## Endpoints

| Method & path | Permission | Notes |
|---|---|---|
| `GET /ping` | none | Health check |
| `POST /auth/login`, `GET /auth/me`, `POST /auth/logout` | – | |
| `GET /dashboard` | – | Counts, bed occupancy, today's appointments; `finance` only with `view_payment_reports` |
| `GET /patients?q=&per_page=` | patient_view | Search by name, MR number, CNIC, phone, email. Laravel pagination envelope |
| `POST /patients` | patient_add | Same rules as the web form (CNIC format and uniqueness, MR number allocation) |
| `GET /patients/{id}` | patient_view | Demographics + recent appointments, prescriptions, labs, history, invoices |
| `PUT /patients/{id}` | patient_update | |
| `DELETE /patients/{id}` | patient_delete | 204 |
| `GET /appointments?date=&doctor=&status=&patient=` | appointment_view | Defaults to today; rows include `patient_name`, `doctor_name`; `statuses` lists allowed values |
| `POST /appointments` | appointment_add | `patient, doctor, date, start (HH:MM), minutes?, status, remarks?, video?` — doctor clashes and doctor hours are enforced (422 on `start`) |
| `GET/PUT/DELETE /appointments/{id}` | appointment_view / _update / _cancel | |
| `GET /doctors?q=` | doctor_view | |
| `GET /departments` | department_view | |
| `GET /medicines?q=` | medicine_view | |
| `GET /prescriptions?patient=`, `/prescriptions/{id}` | prescription_view | with items |
| `GET /lab?patient=&status=`, `/lab/{id}` | lab_test_view | with tests |
| `GET /invoices?patient=`, `/invoices/{id}` | invoice_view | detail adds `paid`, `balance` |
| `GET /beds` | bed_view | |
| `GET /admissions?current=1` | admission_view | `current=1` = not yet discharged |

## Quick check

```bash
curl -s -X POST $BASE/api/v1/auth/login -H 'Content-Type: application/json' \
     -d '{"login":"admin","password":"..."}'
curl -s $BASE/api/v1/patients?q=ali -H "Authorization: Bearer $TOKEN"
```

## Notes for the app developer

- Branch-restricted users (users with a branch) only see their branch, as on the web. Users
  without a branch see all branches; there is no branch switcher in the API yet.
- Native apps need no CORS configuration. A browser-based client (PWA) served from a
  different origin will, so add the origin to `config/cors.php`.
- Always use HTTPS in production; the bearer token is a credential.
- Passport is installed in the project, but its tables in this database are from an older
  version than the installed package, so the mobile API uses its own small token table
  (`api_tokens`, migration `2026_11_30_000001`). Run `php artisan migrate` on deploy.
- Tests: `tests/Feature/MobileApiTest.php`.

## Mobile web app (PWA)

A ready-made installable app lives in `public/app/` and is served at `/app/` (no build step).
Open it on a phone and use "Add to Home screen". It signs in through this API and shows only
the screens the user's role allows. Staff can also reach it from the web app's account menu.
Static files only: `index.html`, `app.js`, `app.css`, `sw.js` (caches the app shell, never API data),
`manifest.webmanifest`. Bump the version in `sw.js` and the `?v=` query strings when you change them.
