From 2ec42f1b30b480ff5693506ef1b8d94f9921e4f1 Mon Sep 17 00:00:00 2001 From: aykhanahmadli Date: Thu, 7 Nov 2024 22:59:13 +0100 Subject: [PATCH] add telemed location handling for demo visits --- .../src/helpers/create-sample-appointments.ts | 44 +++++++++++++++---- .../tracking-board/CreateDemoVisits.tsx | 4 +- .../features/tracking-board/StateSelect.tsx | 1 - 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/packages/telemed-ehr/app/src/helpers/create-sample-appointments.ts b/packages/telemed-ehr/app/src/helpers/create-sample-appointments.ts index 8de241fc..51ffa4ce 100644 --- a/packages/telemed-ehr/app/src/helpers/create-sample-appointments.ts +++ b/packages/telemed-ehr/app/src/helpers/create-sample-appointments.ts @@ -3,6 +3,10 @@ import { DateTime } from 'luxon'; import { FhirClient, SearchParam } from '@zapehr/sdk'; import { PersonSex } from '../../../app/src/types/types'; import { Patient, Practitioner } from 'fhir/r4'; +import { getSelectors } from '../shared/store/getSelectors'; +import { useTrackingBoardStore } from '../telemed'; +import { allLicensesForPractitioner, User } from 'ehr-utils'; +import useOttehrUser from '../hooks/useOttehrUser'; type UserType = 'Patient' | 'Parent/Guardian'; @@ -43,6 +47,7 @@ export const createSampleAppointments = async ( fhirClient: FhirClient | undefined, authToken: string, phoneNumber: string, + user: User | undefined, ): Promise => { try { if (!fhirClient) { @@ -56,9 +61,9 @@ export const createSampleAppointments = async ( const appointmentTypes = ['telemedicine', 'in-person'] as const; - for (let i = 0; i < 1; i++) { + for (let i = 0; i < 10; i++) { const visitService = appointmentTypes[i % 2]; - const randomPatientInfo = await generateRandomPatientInfo(fhirClient, visitService, phoneNumber); + const randomPatientInfo = await generateRandomPatientInfo(fhirClient, visitService, user, phoneNumber); const inputBody = JSON.stringify(randomPatientInfo); const response = await fetch(`${intakeZambdaUrl}/zambda/${createAppointmentZambdaId}/execute`, { @@ -83,6 +88,7 @@ export const createSampleAppointments = async ( const generateRandomPatientInfo = async ( fhirClient: FhirClient, visitService: 'in-person' | 'telemedicine', + user: User | undefined, phoneNumber?: string, ): Promise => { const firstNames = ['Alice', 'Bob', 'Charlie', 'Diana', 'Ethan', 'Fatima', 'Gabriel', 'Hannah', 'Ibrahim', 'Jake']; @@ -90,11 +96,14 @@ const generateRandomPatientInfo = async ( const sexes: PersonSex[] = [PersonSex.Male, PersonSex.Female, PersonSex.Intersex]; const searchParams: SearchParam[] = [{ name: 'status', value: 'active' }]; - const availableLocations: any[] = await fhirClient?.searchResources({ + + const allOffices: any[] = await fhirClient?.searchResources({ resourceType: 'Location', - searchParams: searchParams, + searchParams: [{ name: '_count', value: '1000' }], }); + const activeOffices = allOffices.filter((item) => item.status === 'active'); + const practitionersTemp: Practitioner[] = await fhirClient.searchResources({ resourceType: 'Practitioner', searchParams: [ @@ -110,14 +119,26 @@ const generateRandomPatientInfo = async ( .minus({ years: 7 + Math.floor(Math.random() * 16) }) .toISODate(); const randomSex = sexes[Math.floor(Math.random() * sexes.length)]; - const randomLocationIndex = Math.floor(Math.random() * availableLocations.length); - const randomLocationId = availableLocations[randomLocationIndex].id; + const randomLocationIndex = Math.floor(Math.random() * activeOffices.length); + const randomLocationId = activeOffices[randomLocationIndex].id; const randomProviderId = practitionersTemp[Math.floor(Math.random() * practitionersTemp.length)].id; const selectedInPersonLocationID = localStorage.getItem('selectedInPersonLocationID'); const selectedState = localStorage.getItem('selectedState'); - console.log('selectedState', selectedState); - console.log('availableLocations', availableLocations); + + const availableStates = + user?.profileResource && + allLicensesForPractitioner(user.profileResource).map((item) => { + return item.state; + }); + + const randomState = availableStates?.[Math.floor(Math.random() * availableStates.length)] || ''; + + const locationId = getLocationIdFromState(selectedState || randomState, allOffices); + + const isLocationActive = activeOffices.some((office) => office.id === locationId); + + const telemedLocationId = isLocationActive ? locationId : randomLocationId; if (visitService === 'telemedicine') { return { @@ -137,7 +158,7 @@ const generateRandomPatientInfo = async ( timezone: 'UTC', isDemo: true, phoneNumber: phoneNumber, - locationID: randomLocationId, + locationID: telemedLocationId, }; } @@ -164,3 +185,8 @@ const generateRandomPatientInfo = async ( phoneNumber: phoneNumber, }; }; + +const getLocationIdFromState = (state: string, allLocations: any[]): string | undefined => { + const location = allLocations.find((item) => item.address?.state === state); + return location?.id; +}; diff --git a/packages/telemed-ehr/app/src/telemed/features/tracking-board/CreateDemoVisits.tsx b/packages/telemed-ehr/app/src/telemed/features/tracking-board/CreateDemoVisits.tsx index 44cc78a2..22190a64 100644 --- a/packages/telemed-ehr/app/src/telemed/features/tracking-board/CreateDemoVisits.tsx +++ b/packages/telemed-ehr/app/src/telemed/features/tracking-board/CreateDemoVisits.tsx @@ -8,6 +8,7 @@ import { useAuth0 } from '@auth0/auth0-react'; import { otherColors } from '../../../CustomThemeProvider'; import { LoadingButton } from '@mui/lab'; import { Box } from '@mui/system'; +import useOttehrUser from '../../../hooks/useOttehrUser'; interface CreateDemoVisitsProps { schedulePage?: 'telemedicine' | 'in-person'; @@ -28,6 +29,7 @@ const CreateDemoVisits = ({ schedulePage }: CreateDemoVisitsProps): ReactElement }); const { fhirClient } = useApiClients(); const { getAccessTokenSilently } = useAuth0(); + const user = useOttehrUser(); const handleCreateSampleAppointments = async ( event: React.MouseEvent | React.FormEvent, @@ -44,7 +46,7 @@ const CreateDemoVisits = ({ schedulePage }: CreateDemoVisitsProps): ReactElement setLoading(true); setInputError(false); const authToken = await getAccessTokenSilently(); - const response = await createSampleAppointments(fhirClient, authToken, formattedPhoneNumber); + const response = await createSampleAppointments(fhirClient, authToken, formattedPhoneNumber, user); setSnackbar({ open: true, message: 'Appointments created successfully!', diff --git a/packages/telemed-ehr/app/src/telemed/features/tracking-board/StateSelect.tsx b/packages/telemed-ehr/app/src/telemed/features/tracking-board/StateSelect.tsx index d35758ca..403bf8d1 100644 --- a/packages/telemed-ehr/app/src/telemed/features/tracking-board/StateSelect.tsx +++ b/packages/telemed-ehr/app/src/telemed/features/tracking-board/StateSelect.tsx @@ -10,7 +10,6 @@ export function StateSelect(): ReactElement { const options = [EMPTY_STATE, ...availableStates.map((state) => ({ label: state, value: state }))]; const randomState = availableStates[Math.floor(Math.random() * availableStates.length)]; - localStorage.setItem('selectedState', randomState); const handleStateChange = (_e: any, { value }: { label: string | null; value: string | null }): void => { localStorage.setItem('selectedState', value || '');