|
| 1 | +import { Encounter } from 'fhir/r4b'; |
| 2 | + |
| 3 | +import { useService } from 'aidbox-react/lib/hooks/service'; |
| 4 | +import { success } from 'aidbox-react/lib/libs/remoteData'; |
| 5 | +import { extractBundleResources, getFHIRResources as getAidboxResources, WithId } from 'aidbox-react/lib/services/fhir'; |
| 6 | +import { mapSuccess, service } from 'aidbox-react/lib/services/service'; |
| 7 | + |
| 8 | +import { Client } from '@beda.software/aidbox-types'; |
| 9 | +import { matchCurrentUserRole, Role } from '@beda.software/emr/dist/utils/role'; |
| 10 | +import config from '@beda.software/emr-config'; |
| 11 | + |
| 12 | +export function useSmartApps(encounter?: Encounter) { |
| 13 | + const [appsRemoteData] = useService(async () => { |
| 14 | + let clientType = matchCurrentUserRole<string>({ |
| 15 | + [Role.Patient]: () => 'smart-on-fhir-patient', |
| 16 | + [Role.Admin]: () => 'smart-on-fhir', |
| 17 | + [Role.Practitioner]: () => 'smart-on-fhir-practitioner', |
| 18 | + [Role.Receptionist]: () => 'smart-on-fhir-practitioner', |
| 19 | + }); |
| 20 | + if (encounter) { |
| 21 | + if (clientType === 'smart-on-fhir-practitioner') { |
| 22 | + clientType = 'smart-on-fhir-encounter'; |
| 23 | + } else { |
| 24 | + const mockResonse: Array<WithId<Client>> = []; |
| 25 | + return success(mockResonse); |
| 26 | + } |
| 27 | + } |
| 28 | + return mapSuccess( |
| 29 | + await getAidboxResources<Client>('Client', { ['.type']: clientType }), |
| 30 | + (b) => extractBundleResources(b).Client, |
| 31 | + ); |
| 32 | + }); |
| 33 | + return { appsRemoteData }; |
| 34 | +} |
| 35 | + |
| 36 | +export interface LaunchProps { |
| 37 | + user: string; |
| 38 | + client: string; |
| 39 | + patient: string; |
| 40 | + practitioner?: string; |
| 41 | + encounter?: string; |
| 42 | +} |
| 43 | + |
| 44 | +interface LaunchRPCResult { |
| 45 | + result: { |
| 46 | + uri: string; |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +export async function getLaunchURI({ user, client, patient, practitioner, encounter }: LaunchProps) { |
| 51 | + return await service<LaunchRPCResult>({ |
| 52 | + url: '/rpc', |
| 53 | + method: 'POST', |
| 54 | + data: { |
| 55 | + method: 'aidbox.smart/get-launch-uri', |
| 56 | + params: { |
| 57 | + user, |
| 58 | + iss: encodeURIComponent(`${config.baseURL}/fhir`), |
| 59 | + client, |
| 60 | + ctx: { |
| 61 | + patient, |
| 62 | + ...(practitioner ? { practitioner } : {}), |
| 63 | + ...(encounter ? { encounter } : {}), |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }); |
| 68 | +} |
0 commit comments