Skip to content

Commit

Permalink
add better error logs + new prop
Browse files Browse the repository at this point in the history
  • Loading branch information
sharlotta93 committed Sep 16, 2024
1 parent 24c8fc9 commit 8ee5d7d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,31 @@ export const IntakeScheduling: FC<IntakeSchedulingProps> = ({
const [provider, setProvider] = useState<string | undefined>(undefined)
const [date, setDate] = useState<Date | undefined>(undefined)
const [slot, setSlot] = useState<SlotType | undefined>(undefined)
const [providerPreferences, setProviderPreferences] = useState<{
age?: string
gender?: string | undefined
ethnicity?: string | undefined
clinicalFocus?: string[] | undefined
deliveryMethod?: string | undefined
locationState?: string | undefined
therapeuticModality?: string | undefined
}>({
age: undefined,
gender: undefined,
ethnicity: undefined,
clinicalFocus: undefined,
deliveryMethod: undefined,
locationState: undefined,
therapeuticModality: undefined,
})

const { activity_id, fields } = activityDetails
const { onSubmit } = useIntakeScheduling()

const {
providerId,
patientName,
agePreference,
age,
genderPreference,
ethnicityPreference,
clinicalFocusPreference,
Expand All @@ -51,12 +68,41 @@ export const IntakeScheduling: FC<IntakeSchedulingProps> = ({
// Reset to default mode on unmount
resetLayoutMode()
}
}, [])
})

useEffect(() => {
// Set the initial provider preferences from the mapped fields
setProviderPreferences({
age: age ? String(age) : undefined,
gender: genderPreference,
ethnicity: ethnicityPreference,
clinicalFocus: clinicalFocusPreference?.split(','),
therapeuticModality: therapeuticModalityPreference,
locationState: locationStatePreference,
})
}, [
age,
genderPreference,
ethnicityPreference,
clinicalFocusPreference,
therapeuticModalityPreference,
locationStatePreference,
])

const handleProviderPreferencesChange = useCallback(
(key: string, value: any) => {
setProviderPreferences((prev) => ({
...prev,
[key]: value,
}))
},
[]
)

const fetchProvidersFn = useCallback(
() =>
fetchProviders({
age: agePreference ? String(agePreference) : undefined,
age: age ? String(age) : undefined,
gender: genderPreference,
ethnicity: ethnicityPreference,
therapeuticModality: therapeuticModalityPreference,
Expand Down Expand Up @@ -90,7 +136,7 @@ export const IntakeScheduling: FC<IntakeSchedulingProps> = ({
},
}),
[
agePreference,
age,
genderPreference,
ethnicityPreference,
clinicalFocusPreference,
Expand Down Expand Up @@ -144,6 +190,8 @@ export const IntakeScheduling: FC<IntakeSchedulingProps> = ({
opts={{
allowSchedulingInThePast: false,
}}
providerPreferences={providerPreferences}
onProviderPreferencesChange={handleProviderPreferencesChange}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
GetAvailabilitiesResponseSchema,
GetProvidersResponseSchema,
} from '@awell-health/sol-scheduling'
import { SolApiResponseError } from './helpers/error'
import { log } from '../../../../../utils/logging'

export const fetchProviders = async (
Expand All @@ -25,30 +26,32 @@ export const fetchProviders = async (
})

if (!response.ok) {
const errorMessage = `Failed to fetch providers: ${response.statusText}`
log(
{
message: `${basicMessage}: failed`,
message: `${basicMessage}: ${errorMessage}`,
data: { input, response },
},
'ERROR',
''
)
throw new Error('Failed to fetch providers')
throw new SolApiResponseError(errorMessage)
}

const jsonRes = await response.json()
const result = GetProvidersResponseSchema.safeParse(jsonRes)

if (!result.success) {
const errorMessage = 'SOL API response does not match expected schema'
log(
{
message: `${basicMessage}: zod parsing error`,
message: `${basicMessage}: ${errorMessage}`,
data: { input, response, result },
},
'ERROR',
''
)
throw new Error('Zod error', result.error)
throw new SolApiResponseError(errorMessage, result.error.issues)
}
log(
{
Expand Down Expand Up @@ -85,32 +88,32 @@ export const fetchAvailability = async (
)

if (!response.ok) {
const errorMessage = `Failed to fetch availability for provider ${input.providerId[0]}: ${response.statusText}`
log(
{
message: `${basicMessage}: failed`,
message: `${basicMessage}: ${errorMessage}`,
data: { input, response },
},
'ERROR',
''
)
throw new Error(
`Failed to fetch availability for provider ${input.providerId[0]}`
)
throw new SolApiResponseError(errorMessage)
}

const jsonRes = await response.json()
const result = GetAvailabilitiesResponseSchema.safeParse(jsonRes)

if (!result.success) {
const errorMessage = 'SOL API response does not match expected schema'
log(
{
message: `${basicMessage}: failed with zod error`,
message: `${basicMessage}: ${errorMessage}`,
data: { input, result },
},
'ERROR',
''
)
throw new Error('Zod error', result.error)
throw new SolApiResponseError(errorMessage, result.error.issues)
}

log(
Expand Down Expand Up @@ -144,12 +147,13 @@ export const bookAppointment = async (
body: JSON.stringify(input),
})
if (!response.ok) {
const errorMessage = `Failed to book appointment: ${response.statusText}`
log(
{ message: `${basicMessage}: failed`, data: { input, response } },
'ERROR',
'Failed to book appointment'
errorMessage
)
throw new Error(`Failed to book appointment`)
throw new SolApiResponseError(errorMessage)
}

const jsonRes = (await response.json()) as BookAppointmentResponseType
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ZodError } from '@awell-health/sol-scheduling'

export class SolApiResponseError extends Error {
public issues: ZodError['issues']

constructor(message: string, issues: ZodError['issues'] = []) {
super(message)
this.name = 'SolApiResponseError'
this.issues = issues
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export {
export type ActionFields = {
providerId?: string
patientName: string
agePreference?: number
age?: string
genderPreference?: 'M' | 'F' | 'Non-binary/non-conforming'
ethnicityPreference?:
| 'Asian'
Expand Down

0 comments on commit 8ee5d7d

Please sign in to comment.