> ## Documentation Index
> Fetch the complete documentation index at: https://developer.eka.care/llms.txt
> Use this file to discover all available pages before exploring further.

# Cases

> Organise medical records into cases (folders) with the Medical Records Web SDK

Cases act as **folders** for records. A record can belong to multiple cases.

Every case method needs two IDs:

* `bid` — your business ID, provided by Eka Care when your account is set up.
* `patientId` — the OID of the logged-in user whose cases you are working with.

```typescript theme={null}
const BID = 'your-business-id';
const OID = 'logged-in-user-oid';
```

## List Cases

`listCases` is **cache-first**: fires `onStale` immediately with IndexedDB data, then delta-syncs with the server and resolves the promise with fresh results.

```typescript theme={null}
// Simple — wait for the synced list
const cases = await sdk.listCases({ bid: BID, patientId: OID });

// Stale-while-revalidate — render cached instantly, update when fresh arrives
const cases = await sdk.listCases({
  bid: BID, patientId: OID,
  onStale: (cached) => setCases(cached),
});
setCases(cases);
```

To list the documents inside a case:

```typescript theme={null}
const docs = await sdk.listLocalDocuments({
  bid: BID, patientId: OID,
  filter: { caseId: 'case-uuid' },
});
```

## Create a Case

```typescript theme={null}
await sdk.createCase({
  bid: BID, patientId: OID,
  data: {
    id:           crypto.randomUUID(),
    display_name: 'Follow-up visit',
    type:         'OP',                             // EM | IP | OP | HH
    occurred_at:  Math.floor(Date.now() / 1000),    // required
  },
});
```

<Note>
  **Offline:** the case is saved locally (`isRemoteCreated: false`) and created
  on the server on reconnect.
</Note>

## Update a Case

```typescript theme={null}
await sdk.updateCase({
  bid: BID, patientId: OID,
  caseId: 'case-uuid',
  data: { display_name: 'New name', type: 'IP', occurred_at: 1748000000 },
});
```

## Delete a Case

Deletes the case only — the records inside it are **not** deleted. They just lose the link to this case (the case ID is removed from each record's `caseIDs`) and remain available in the record list.

```typescript theme={null}
await sdk.deleteCase({ bid: BID, patientId: OID, caseId: 'case-uuid' });
```

## Assigning Records to Cases

The case–record link lives **on the document, not the case** — every record carries a `caseIDs: string[]` array, and there is no case-side "add documents" method. Both directions are handled through the record APIs.

### Assign at upload time

Pass `cases` in the batch item of `addDocument`:

```typescript theme={null}
await sdk.addDocument({
  bid: BID, patientId: OID,
  batchRequests: [{
    documentType: 'lr',
    documentDate: Math.floor(Date.now() / 1000),
    cases: ['case-uuid-1', 'case-uuid-2'],   // linked from the moment it's created
    files: [{ contentType: 'application/pdf', file_size: file.size }],
  }],
  files: [[file]],
  filenames: [[file.name]],
});
```

### Assign / unassign an existing document

Use `editDocument` with `data.cases`. This is local-first — the DB is patched immediately and synced to the server in the background.

<Warning>
  `data.cases` **replaces** the document's full case list. To add a case
  without dropping existing links, read the record's current `caseIDs` and
  append.
</Warning>

```typescript theme={null}
// Add to a case (preserving existing links)
await sdk.editDocument({
  bid: BID, patientId: OID,
  documentId: doc.documentId,
  data: { cases: [...doc.caseIDs, 'new-case-uuid'] },
});

// Remove from a case
await sdk.editDocument({
  bid: BID, patientId: OID,
  documentId: doc.documentId,
  data: { cases: doc.caseIDs.filter(id => id !== 'case-to-remove') },
});

// Unlink from all cases
await sdk.editDocument({
  bid: BID, patientId: OID,
  documentId: doc.documentId,
  data: { cases: [] },
});
```

### Assign multiple documents to a case

There is no `case.addDocuments()` — loop `editDocument` over the documents:

```typescript theme={null}
for (const doc of selectedDocs) {
  await sdk.editDocument({
    bid: BID, patientId: OID,
    documentId: doc.documentId,
    data: { cases: [...doc.caseIDs, caseId] },
  });
}
```

### Reading the relationship

```typescript theme={null}
// Documents in a case
const docs = await sdk.listLocalDocuments({ bid: BID, patientId: OID, filter: { caseId } });

// Cases a document belongs to
doc.caseIDs  // string[] on every record object
```

Deletion side effects are handled automatically: `deleteCase` strips that case ID from all linked records' `caseIDs`, and `deleteDocument` removes the document from any linked cases.
