Skip to content

Commit

Permalink
add telemed location handling for demo visits
Browse files Browse the repository at this point in the history
  • Loading branch information
AykhanAhmadli committed Nov 7, 2024
1 parent a270f13 commit 2ec42f1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
44 changes: 35 additions & 9 deletions packages/telemed-ehr/app/src/helpers/create-sample-appointments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -43,6 +47,7 @@ export const createSampleAppointments = async (
fhirClient: FhirClient | undefined,
authToken: string,
phoneNumber: string,
user: User | undefined,
): Promise<void> => {
try {
if (!fhirClient) {
Expand All @@ -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`, {
Expand All @@ -83,18 +88,22 @@ export const createSampleAppointments = async (
const generateRandomPatientInfo = async (
fhirClient: FhirClient,
visitService: 'in-person' | 'telemedicine',
user: User | undefined,
phoneNumber?: string,
): Promise<CreateAppointmentParams> => {
const firstNames = ['Alice', 'Bob', 'Charlie', 'Diana', 'Ethan', 'Fatima', 'Gabriel', 'Hannah', 'Ibrahim', 'Jake'];
const lastNames = ['Smith', 'Johnson', 'Williams', 'Jones', 'Brown', 'Clark', 'Davis', 'Elliott', 'Foster', 'Garcia'];
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: [
Expand All @@ -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 {
Expand All @@ -137,7 +158,7 @@ const generateRandomPatientInfo = async (
timezone: 'UTC',
isDemo: true,
phoneNumber: phoneNumber,
locationID: randomLocationId,
locationID: telemedLocationId,
};
}

Expand All @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,6 +29,7 @@ const CreateDemoVisits = ({ schedulePage }: CreateDemoVisitsProps): ReactElement
});
const { fhirClient } = useApiClients();
const { getAccessTokenSilently } = useAuth0();
const user = useOttehrUser();

const handleCreateSampleAppointments = async (
event: React.MouseEvent<HTMLButtonElement> | React.FormEvent<HTMLFormElement>,
Expand All @@ -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!',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '');
Expand Down

0 comments on commit 2ec42f1

Please sign in to comment.