
=====================
# Updated endpoints
=====================

## `/api/prescriptions`

`GET / ` - Added nhi and prescription_id
Now returns:
```js
{
    "first_name": "John",
    "last_name": "Smith",
    "address": "100 Great South Road",
    "phone_number": "021 123 4567",
    "nhi": "ABC1234",
    "created_at": "2025-10-07T20:46:14.689Z",
    "prescription_id": "9fe845dd-b7c8-41eb-9528-c4a918bccf55",
    "medications": [
      "Ibuprofen 200 mg Cap"
    ]
}
```

`POST /updatePrescription and POST /collectPrescription`
Now expects (before encryption):
```js
{
    "prescription_id": "15033777-b995-49b0-be3d-af062307cbc7"
}
```

## `/api/calls`

`GET /` - Added ai_summary and user_messages:
```js
  {
    id: '5699f9d8-d474-485d-9bb0-28f2f5646e58',
    patient_name: 'UNKNOWN',
    patient_id: null,
    call_sid: 'CAa068ca41601130489975b8821ca26a3e',
    phone_number: '0275556799',
    status: 'HANGUP',
    created_at: '2025-10-13T20:02:01.543Z',
    ended_at: '2025-10-13T20:02:15.358Z',
    ai_summary: 'No call transcript provided',
    user_messages: null
  }
```

=================
# New endpoints
=================

## `/api/publicKey`

**Expects JWT in auth header** (Bearer {JWT})

`GET /` - gets the public key used by the system encoded in base64. This can be used to encrypt data to send to the system. Does not require a public key in header

## `/api/prescriptions`
`POST /addRepeats` - Adds medicines in repeats array as repeats. 

Expects (before encryption):
```js
{
    "first_name": "Aria-Rose",
    "last_name": "Tainui",
    "address": "43A Papakura-Clevedon Road, Papakura, Auckland 2110",
    "medications": [
        "Omeprazole 20mg Cap",
        "Rosuvastatin 10mg Tab"
    ],
    "repeats": [
        "Omeprazole 20mg Cap",
        "Rosuvastatin 10mg Tab"
    ]
}
```

## `/api/calls`

**Expects JWT in auth header** (Bearer {JWT})

`GET /getRecording` - requires a valid public key in the header as Client-Public-Key and callSid in header. Gets the wav file of the call and sends it compressed in a zip folder

One recording stored currently:
callSid: `CAbda8ff181eef8ba0d25a3fb033ecf85d`

Example of decrypting wav file

```js
import crypto from "crypto";
import concat from "concat-stream";
import AdmZip from "adm-zip";

// Javascript example
function decryptWavWithPrivate(privateKey, encryptedPayload) {
    const { encryptedKey, iv, authTag, encryptedData } = encryptedPayload;

    // Step 1: Decrypt AES key with RSA private key
    const aesKey = crypto.privateDecrypt(
        {
            key: privateKey,
            padding: constants.RSA_PKCS1_OAEP_PADDING,
            oaepHash: "sha256",
        },
        Buffer.from(encryptedKey, "base64")
    );

    // Step 2: AES-GCM decrypt ZIP data
    const decipher = crypto.createDecipheriv(
        "aes-256-gcm",
        aesKey,
        Buffer.from(iv, "base64")
    );
    decipher.setAuthTag(Buffer.from(authTag, "base64"));
    const encryptedBuffer = Buffer.from(encryptedData, "base64");
    const decryptedBuffer = Buffer.concat([decipher.update(encryptedBuffer), decipher.final()]);

    // Step 3: Unzip decrypted ZIP
    const zip = new AdmZip(decryptedBuffer); 
    const zipEntries = zip.getEntries(); // List of entries in the zip folder
}

```



## `/api/calls/verify`
Create a verified patient and number relation

METHOD: POST
AUTH: Bearer Token
HEADER: 
- Client-Public-Key

BODY:
```js
{
    "patientId": "c8547fdd-f915-46b7-8cfd-cc3a79df2b28",
    "callId": "f646174b-5d9f-45f0-9e36-19dc387edfbb",
    "type": "patient", // patient | medical | unverify
}
```

type:
- patient: number belongs to verified patient
- medical: number belongs to verified medical centre
- unverify: remove verification between number and patient


## `/api/calls/`

New variable:
- verification
Values:
- NO_VERIFICATION: caller request doesn't need any verification (e.g. calling about opening hours)
- UNVERIFIED: no verification between number and patient
- VERIFIED: Existing verification between number and patient
- VERIFIED_MEDICAL: existing verification between medical centre number and patient
- UNVERIFIED_MEDICAL: number has been verified as medical centre with another patient but current patient has not been verified with number