Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attachments resources to esm-api and esm-react-utils (supporting O3-2618) #860

Merged
merged 3 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/framework/esm-api/src/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/** @module @category API */
import type { UploadedFile } from './types';
import { openmrsFetch } from './openmrs-fetch';

export const attachmentUrl = '/ws/rest/v1/attachment';

export function getAttachmentByUuid(attachmentUuid: string, abortController: AbortController) {
return openmrsFetch(`${attachmentUrl}/${attachmentUuid}`, {
signal: abortController.signal,
});
}

export function getAttachments(patientUuid: string, includeEncounterless: boolean, abortController: AbortController) {
return openmrsFetch(`${attachmentUrl}?patient=${patientUuid}&includeEncounterless=${includeEncounterless}`, {
signal: abortController.signal,
});
}

export async function createAttachment(patientUuid: string, fileToUpload: UploadedFile) {
const formData = new FormData();

formData.append('fileCaption', fileToUpload.fileName);
formData.append('patient', patientUuid);

if (fileToUpload.file) {
formData.append('file', fileToUpload.file);
} else {
formData.append('file', new File([''], fileToUpload.fileName), fileToUpload.fileName);
formData.append('base64Content', fileToUpload.base64Content);
}
return openmrsFetch(`${attachmentUrl}`, {
method: 'POST',
body: formData,
});
}

export function deleteAttachmentPermanently(attachmentUuid: string, abortController: AbortController) {
return openmrsFetch(`${attachmentUrl}/${attachmentUuid}`, {
method: 'DELETE',
signal: abortController.signal,
});
}
1 change: 1 addition & 0 deletions packages/framework/esm-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './types';
export * from './attachments';
export * from './openmrs-fetch';
export * from './setup';

Expand Down
1 change: 1 addition & 0 deletions packages/framework/esm-api/src/public.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './types';
export * from './openmrs-fetch';
export * from './attachments';

export * from './shared-api-objects/current-user';
export * from './shared-api-objects/current-patient';
Expand Down
25 changes: 25 additions & 0 deletions packages/framework/esm-api/src/types/attachments-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export interface UploadedFile {
file?: File;
base64Content: string;
fileName: string;
fileType: string;
fileDescription: string;
status?: 'uploading' | 'complete';
}

export interface Attachment {
id: string;
src: string;
title: string;
description: string;
dateTime: string;
bytesMimeType: string;
bytesContentFamily: string;
}
export interface AttachmentResponse {
bytesContentFamily: string;
bytesMimeType: string;
comment: string;
dateTime: string;
uuid: string;
}
1 change: 1 addition & 0 deletions packages/framework/esm-api/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './attachments-types';
export * from './fetch';
export * from './fhir-resource';
export * from './openmrs-resource';
Expand Down
129 changes: 129 additions & 0 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
### API Functions

- [clearCurrentUser](API.md#clearcurrentuser)
- [createAttachment](API.md#createattachment)
- [deleteAttachmentPermanently](API.md#deleteattachmentpermanently)
- [fetchCurrentPatient](API.md#fetchcurrentpatient)
- [getAttachmentByUuid](API.md#getattachmentbyuuid)
- [getAttachments](API.md#getattachments)
- [getCurrentUser](API.md#getcurrentuser)
- [getLocations](API.md#getlocations)
- [getLoggedInUser](API.md#getloggedinuser)
Expand Down Expand Up @@ -159,6 +163,7 @@
### Other Functions

- [ExtensionSlot](API.md#extensionslot)
- [useAttachments](API.md#useattachments)

### Store Functions

Expand Down Expand Up @@ -737,6 +742,16 @@ ___

___

### attachmentUrl

• `Const` **attachmentUrl**: ``"/ws/rest/v1/attachment"``

#### Defined in

[packages/framework/esm-api/src/attachments.ts:5](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/attachments.ts#L5)

___

### defaultVisitCustomRepresentation

• `Const` **defaultVisitCustomRepresentation**: `string`
Expand Down Expand Up @@ -935,6 +950,48 @@ ___

___

### createAttachment

▸ **createAttachment**(`patientUuid`, `fileToUpload`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `patientUuid` | `string` |
| `fileToUpload` | [`UploadedFile`](interfaces/UploadedFile.md) |

#### Returns

`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>

#### Defined in

[packages/framework/esm-api/src/attachments.ts:19](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/attachments.ts#L19)

___

### deleteAttachmentPermanently

▸ **deleteAttachmentPermanently**(`attachmentUuid`, `abortController`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `attachmentUuid` | `string` |
| `abortController` | `AbortController` |

#### Returns

`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>

#### Defined in

[packages/framework/esm-api/src/attachments.ts:37](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/attachments.ts#L37)

___

### fetchCurrentPatient

▸ **fetchCurrentPatient**(`patientUuid`, `fetchInit?`, `includeOfflinePatients?`): `Promise`<`fhir.Patient` \| ``null``\>
Expand All @@ -957,6 +1014,49 @@ ___

___

### getAttachmentByUuid

▸ **getAttachmentByUuid**(`attachmentUuid`, `abortController`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `attachmentUuid` | `string` |
| `abortController` | `AbortController` |

#### Returns

`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>

#### Defined in

[packages/framework/esm-api/src/attachments.ts:7](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/attachments.ts#L7)

___

### getAttachments

▸ **getAttachments**(`patientUuid`, `includeEncounterless`, `abortController`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>

#### Parameters

| Name | Type |
| :------ | :------ |
| `patientUuid` | `string` |
| `includeEncounterless` | `boolean` |
| `abortController` | `AbortController` |

#### Returns

`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>

#### Defined in

[packages/framework/esm-api/src/attachments.ts:13](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/attachments.ts#L13)

___

### getCurrentUser

▸ **getCurrentUser**(): `Observable`<[`Session`](interfaces/Session.md)\>
Expand Down Expand Up @@ -3957,6 +4057,35 @@ Passing a function as children

___

### useAttachments

▸ **useAttachments**(`patientUuid`, `includeEncounterless`): `Object`

#### Parameters

| Name | Type |
| :------ | :------ |
| `patientUuid` | `string` |
| `includeEncounterless` | `boolean` |

#### Returns

`Object`

| Name | Type |
| :------ | :------ |
| `data` | [`AttachmentResponse`](interfaces/AttachmentResponse.md)[] |
| `error` | `any` |
| `isLoading` | `boolean` |
| `isValidating` | `boolean` |
| `mutate` | `KeyedMutator`<[`FetchResponse`](interfaces/FetchResponse.md)<{ `results`: [`AttachmentResponse`](interfaces/AttachmentResponse.md)[] }\>\> |

#### Defined in

[packages/framework/esm-react-utils/src/useAttachments.ts:5](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useAttachments.ts#L5)

___

## Store Functions

### createGlobalStore
Expand Down
85 changes: 85 additions & 0 deletions packages/framework/esm-framework/docs/interfaces/Attachment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[@openmrs/esm-framework](../API.md) / Attachment

# Interface: Attachment

## Table of contents

### Properties

- [bytesContentFamily](Attachment.md#bytescontentfamily)
- [bytesMimeType](Attachment.md#bytesmimetype)
- [dateTime](Attachment.md#datetime)
- [description](Attachment.md#description)
- [id](Attachment.md#id)
- [src](Attachment.md#src)
- [title](Attachment.md#title)

## Properties

### bytesContentFamily

• **bytesContentFamily**: `string`

#### Defined in

[packages/framework/esm-api/src/types/attachments-types.ts:17](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L17)

___

### bytesMimeType

• **bytesMimeType**: `string`

#### Defined in

[packages/framework/esm-api/src/types/attachments-types.ts:16](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L16)

___

### dateTime

• **dateTime**: `string`

#### Defined in

[packages/framework/esm-api/src/types/attachments-types.ts:15](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L15)

___

### description

• **description**: `string`

#### Defined in

[packages/framework/esm-api/src/types/attachments-types.ts:14](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L14)

___

### id

• **id**: `string`

#### Defined in

[packages/framework/esm-api/src/types/attachments-types.ts:11](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L11)

___

### src

• **src**: `string`

#### Defined in

[packages/framework/esm-api/src/types/attachments-types.ts:12](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L12)

___

### title

• **title**: `string`

#### Defined in

[packages/framework/esm-api/src/types/attachments-types.ts:13](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L13)
Loading
Loading