**Prodigi EMR Lite External Integration API Contract**

Base path:

```http
/api/integrations/v1
```

All API responses use:

```json
{
  "Result": "Success",
  "Data": {},
  "Message": "..."
}
```

## 1. Login

```http
POST /api/integrations/v1/login/
```

Authenticates the external integration client and returns a JWT Bearer token.

### Request

```json
{
  "username": "piramal",
  "password": "********"
}
```

### Success Response

```json
{
  "Result": "Success",
  "Data": {
    "accessToken": "eyJhbGciOiJIUzI1NiIs...",
    "refreshToken": "eyJhbGciOiJIUzI1NiIs...",
    "tokenType": "Bearer",
    "expiresIn": 3600
  },
  "Message": "Login successful"
}
```

Use the token in all other requests:

```http
Authorization: Bearer <accessToken>
Content-Type: application/json
```

## 2. Push Order

```http
POST /api/integrations/v1/orders/
```

Creates or updates patient, visit, and order data in Prodigi EMR Lite.

### Supported `orderType`

```text
XRAY_CHEST
MTB
MTB_PLUS
MTB_RIF
```

When `XRAY_CHEST` is sent, Prodigi EMR Lite automatically runs CAD internally. Piramal does not need to create a separate CAD order.

### Request

```json
{
  "externalPatientId": "PIR-PAT-001",
  "externalVisitId": "PIR-VIS-001",
  "externalOrderId": "PIR-ORD-001",
  "orderType": "XRAY_CHEST",
  "orderedAt": "2026-06-25T10:30:00+05:30",
  "patient": {
    "firstName": "John",
    "age": "45",
    "sex": "Male"
  }
}
```

### Required Fields

```text
externalPatientId
externalVisitId
externalOrderId
orderType
orderedAt
patient.firstName
patient.age
patient.sex
```

### Success Response

```json
{
  "Result": "Success",
  "Data": {
    "externalPatientId": "PIR-PAT-001",
    "externalVisitId": "PIR-VIS-001",
    "externalOrderId": "PIR-ORD-001",
    "patientId": 12,
    "visitId": 31,
    "orderId": 88,
    "orderType": "XRAY_CHEST",
    "status": "PENDING"
  },
  "Message": "Order accepted"
}
```

The endpoint is idempotent. If the same `externalOrderId` is sent again by the same integration client, Prodigi EMR Lite returns the existing mapped order instead of creating a duplicate.

## 3. Pull Order Result

```http
POST /api/integrations/v1/orders/result/
```


### Request

```json
{
    "externalOrderId": "PIR-ORD-001",
    "includeAssets": "base64"
}
```

Allowed `includeAssets`:
  base64
  none

Fetches the result for an order previously pushed by the external app.

For `XRAY_CHEST`, this returns the CAD result generated internally by Prodigi EMR Lite.

### Pending Response

```json
{
  "Result": "Success",
  "Data": {
    "externalOrderId": "PIR-ORD-001",
    "orderType": "XRAY_CHEST",
    "status": "IN_PROGRESS",
    "components": {
      "xray": {
        "status": "PENDING"
      },
      "cad": {
        "status": "PENDING"
      }
    },
    "result": null
  },
  "Message": "Result is not available yet"
}
```

### X-ray + CAD Completed Response

```json
{
  "Result": "Success",
  "Data": {
    "externalOrderId": "PIR-ORD-001",
    "orderType": "XRAY_CHEST",
    "status": "COMPLETED",
    "components": {
      "xray": {
        "status": "COMPLETED"
      },
      "cad": {
        "status": "COMPLETED",
        "provider": ""
      }
    },
    "result": {
      "summary": "TB Presumptive",
      "rawJson": {},
      "reportedAt": "2026-06-25T11:30:00+05:30",
      "assets": [
        {
          "type": "REPORT",
          "contentType": "application/pdf",
          "fileName": "cad-report.pdf",
          "base64": "JVBERi0x..."
        },
        {
          "type": "SECONDARY_CAPTURE",
          "contentType": "image/png",
          "fileName": "sc-image-1.png",
          "base64": "iVBORw0KGgo..."
        }
      ]
    }
  },
  "Message": "Result fetched successfully"
}
```

### TrueNat Completed Response

```json
{
  "Result": "Success",
  "Data": {
    "externalOrderId": "PIR-TN-001",
    "orderType": "MTB_PLUS",
    "status": "COMPLETED",
    "components": {
      "mtb_plus": {
        "status": "COMPLETED",
        "provider": "TrueNAT",
    },
    "result": {
      "summary": "TB Positive",
      "rawJson": {},
      "reportedAt": "2026-06-25T12:15:00+05:30",
      "assets": [
        {
          "type": "REPORT",
          "contentType": "application/pdf",
          "fileName": "truenat-report.pdf",
        }
      ]
    }
  },
  "Message": "Result fetched successfully"
}
```

## Status Values

```text
PENDING
IN_PROGRESS
COMPLETED
FAILED
```

## Error Response

```json
{
  "Result": "Failure",
  "Data": {
    "code": "ORDER_TYPE_NOT_SUPPORTED",
    "field": "orderType"
  },
  "Message": "Order type is not supported"
}
```

## Notes

For `XRAY_CHEST`, Prodigi EMR Lite internally creates and tracks a CAD order separately because CAD results are mapped to their own internal order ID. This internal mapping is not exposed as a separate external order.

Piramal should always use the original `externalOrderId` sent during order creation when pulling results.