# 09 — Prescriptions

← [Back to index](README.md)

Doctor-only. A prescription is created `active` (there is **no `dispensed`** state).

---

## Prescription object
```json
{
  "id": "rx1",
  "patientId": "p1",
  "patientName": "محمد علي حسن",
  "doctorId": "u_123",
  "doctorName": "د. أحمد",
  "items": [
    {
      "id": "ri1",
      "medicationName": "كونكور 5مج",
      "activeIngredient": "Bisoprolol",
      "dosage": "5mg",
      "frequency": "مرة يومياً",
      "durationDays": 30,
      "route": null,
      "instructions": "بعد الأكل",
      "quantity": 30
    }
  ],
  "status": "active",
  "createdAt": "2026-05-22T10:40:00Z",
  "visitId": "v1",
  "diagnosis": "ارتفاع ضغط الدم",
  "notes": "...",
  "validUntil": "2026-06-21T10:40:00Z"
}
```
- `status`: [RxStatus](11-enums.md) — `draft`/`active`/`expired`/`cancelled` only.
- `items[]` = `RxItem`. `activeIngredient` is required by the model; when created from a
  visit the app sets it = `medicationName`.
- `validUntil`: server may compute `createdAt + max(items.durationDays)` days.

---

## Endpoints
| Endpoint | Notes |
|---|---|
| `POST /prescriptions` | create (see body below) → `201` Prescription |
| `GET /prescriptions` | **all** prescriptions, most recent first (the tab view) |
| `GET /prescriptions/{id}` | single Prescription |
| `GET /patients/{patientId}/prescriptions` | one patient's history |
| `PATCH /prescriptions/{id}/cancel` | sets `status: cancelled` → `200` Prescription |
| `POST /prescriptions/check-allergy-conflicts` | drug-allergy safety check (below) |

### `POST /prescriptions` body
```json
{
  "patientId": "p1",
  "patientName": "محمد علي حسن",
  "items": [ { "id": "", "medicationName": "...", "activeIngredient": "...",
               "dosage": "...", "frequency": "...", "durationDays": 30,
               "route": null, "instructions": null, "quantity": null } ],
  "visitId": "v1",
  "diagnosis": "ارتفاع ضغط الدم",
  "notes": "..."
}
```
- `items[].id` may be `""` for new items — assign ids server-side.
- Set `status: active`, `createdAt`; compute `validUntil`.

---

## `POST /prescriptions/check-allergy-conflicts`
Called as the doctor edits items.

**Request**
```json
{
  "items": [ { "...RxItem" } ],
  "patientAllergies": [ { "substance": "Penicillin", "severity": "severe" } ]
}
```
(`patientAllergies` come from the patient's [medical record](06-medical-records.md).)

**Response `200`** — array of **DrugAlert** (may be empty):
```json
[
  {
    "type": "allergyConflict",
    "severity": "critical",
    "title": "تعارض حساسية",
    "description": "المريض لديه حساسية من ...",
    "medicationName": "كونكور 5مج",
    "conflictingSubstance": "Penicillin"
  }
]
```
- `type`: [DrugAlertType](11-enums.md). `severity`: [DrugAlertSeverity](11-enums.md)
  (`critical` when the matched allergy is severe, else `warning`).
- Matching today is case-insensitive substring on `activeIngredient` ↔ `substance`; a real
  drug DB is welcome. **Alerts inform but don't block** submission.

---

## Field contracts

### `RxItem`
`id`, `medicationName` (req), `activeIngredient` (req), `dosage` (req), `frequency` (req),
`durationDays` (int, req), `route?`, `instructions?`, `quantity?` (int).

### `DrugAlert`
`type` ([DrugAlertType](11-enums.md)), `severity` ([DrugAlertSeverity](11-enums.md)),
`title` (str), `description` (str), `medicationName` (str), `conflictingSubstance?` (str).
