diff --git a/routes/~_dashboard/~patients/utils.ts b/routes/~_dashboard/~patients/utils.ts index 8c6b36f4..98bed1c2 100644 --- a/routes/~_dashboard/~patients/utils.ts +++ b/routes/~_dashboard/~patients/utils.ts @@ -11,7 +11,7 @@ import { UserType, } from '@stanfordbdhg/engagehf-models' import { groupBy } from 'es-toolkit' -import { query, where } from 'firebase/firestore' +import { limit, orderBy, query, where } from 'firebase/firestore' import { AllergyType } from '@/modules/firebase/allergy' import { getCurrentUser, refs } from '@/modules/firebase/app' import { @@ -20,13 +20,13 @@ import { } from '@/modules/firebase/appointment' import { type FHIRAllergyIntolerance } from '@/modules/firebase/models' import { mapAuthData } from '@/modules/firebase/user' -import { - getDocsData, - ObservationType, - type ResourceType, -} from '@/modules/firebase/utils' +import { getDocsData, type ResourceType } from '@/modules/firebase/utils' import { queryClient } from '@/modules/query/queryClient' -import { userOrganizationQueryOptions } from '@/modules/user/queries' +import { + type getUserData, + userOrganizationQueryOptions, +} from '@/modules/user/queries' +import { labsObservationCollections } from '@/routes/~_dashboard/~patients/clientUtils' const getUserClinicians = async () => { const { user } = await getCurrentUser() @@ -216,6 +216,24 @@ export const getAppointmentsData = async ({ } } +export const getUserActivity = async ({ + user, + resourceType, + authUser, +}: Awaited>) => { + const latestQuestionnaires = await getDocsData( + query( + refs.questionnaireResponses({ resourceType, userId: authUser.uid }), + orderBy('authored'), + limit(1), + ), + ) + return { + lastActiveDate: user.lastActiveDate, + latestQuestionnaireDate: latestQuestionnaires.at(0)?.authored, + } +} + export type AllergiesData = Awaited> export type Allergy = AllergiesData['allergyIntolerances'][number] export type AppointmentsData = Awaited> diff --git a/routes/~_dashboard/~patients/~$id/LabForm.tsx b/routes/~_dashboard/~patients/~$id/LabForm.tsx index 4be3e8ae..ff87fc15 100644 --- a/routes/~_dashboard/~patients/~$id/LabForm.tsx +++ b/routes/~_dashboard/~patients/~$id/LabForm.tsx @@ -7,7 +7,7 @@ // import { type ComponentProps } from 'react' import { z } from 'zod' -import { ObservationType } from '@/modules/firebase/utils' +import { UserObservationCollection } from '@/modules/firebase/utils' import { Button } from '@/packages/design-system/src/components/Button' import { DatePicker } from '@/packages/design-system/src/components/DatePicker' import { @@ -29,11 +29,12 @@ import { useForm } from '@/packages/design-system/src/forms/useForm' import { getObservationTypeUnits, getUnitOfObservationType, + labsObservationCollections, } from '@/routes/~_dashboard/~patients/clientUtils' import { type Observation } from '@/routes/~_dashboard/~patients/utils' export const labFormSchema = z.object({ - type: z.nativeEnum(ObservationType), + type: z.nativeEnum(UserObservationCollection), effectiveDateTime: z.date(), unit: z.string(), value: z.number(), @@ -48,7 +49,7 @@ interface LabFormProps { export const LabForm = ({ observation, onSubmit }: LabFormProps) => { const isEdit = !!observation - const defaultType = observation?.type ?? ObservationType.potassium + const defaultType = observation?.type ?? UserObservationCollection.potassium const form = useForm({ formSchema: labFormSchema, defaultValues: { @@ -81,8 +82,10 @@ export const LabForm = ({ observation, onSubmit }: LabFormProps) => { field.onChange(type) form.setValue( 'unit', - getUnitOfObservationType(type as ObservationType, formUnit) - .unit, + getUnitOfObservationType( + type as UserObservationCollection, + formUnit, + ).unit, ) }} {...field} @@ -91,7 +94,7 @@ export const LabForm = ({ observation, onSubmit }: LabFormProps) => { - {Object.values(ObservationType).map((type) => ( + {labsObservationCollections.map((type) => ( {type} @@ -110,7 +113,7 @@ export const LabForm = ({ observation, onSubmit }: LabFormProps) => { - {units.map((unit) => ( + {units?.map((unit) => ( {unit.unit} diff --git a/routes/~_dashboard/~patients/~$id/~index.tsx b/routes/~_dashboard/~patients/~$id/~index.tsx index 251d04bb..ce2aaca5 100644 --- a/routes/~_dashboard/~patients/~$id/~index.tsx +++ b/routes/~_dashboard/~patients/~$id/~index.tsx @@ -22,6 +22,11 @@ import { type ResourceType, } from '@/modules/firebase/utils' import { getUserData } from '@/modules/user/queries' +import { + Card, + CardHeader, + CardTitle, +} from '@/packages/design-system/src/components/Card' import { Tabs, TabsContent, @@ -30,6 +35,7 @@ import { } from '@/packages/design-system/src/components/Tabs' import { getUserName } from '@/packages/design-system/src/modules/auth/user' import { PageTitle } from '@/packages/design-system/src/molecules/DashboardLayout' +import { formatISODateTime } from '@/packages/design-system/src/utils/date' import { Medications, type MedicationsFormSchema, @@ -44,6 +50,7 @@ import { getFormProps, getLabsData, getMedicationsData, + getUserActivity, } from '@/routes/~_dashboard/~patients/utils' import { Allergies } from '@/routes/~_dashboard/~patients/~$id/Allergies' import { Appointments } from '@/routes/~_dashboard/~patients/~$id/Appointments' @@ -94,6 +101,7 @@ const PatientPage = () => { user, authUser, resourceType, + activity, } = Route.useLoaderData() const updatePatient = async (form: PatientFormSchema) => { @@ -205,12 +213,35 @@ const PatientPage = () => { - +
+ + + User activity + +
+
    +
  • + latest activity:{' '} + {activity.lastActiveDate ? + formatISODateTime(activity.lastActiveDate) + : '-'} +
  • +
  • + latest questionnaire answer:{' '} + {activity.latestQuestionnaireDate ? + formatISODateTime(activity.latestQuestionnaireDate) + : '-'} +
  • +
+
+
+ +
@@ -259,6 +290,7 @@ export const Route = createFileRoute('/_dashboard/patients/$id/')({ allergiesData: await getAllergiesData({ userId, resourceType }), labsData: await getLabsData({ userId, resourceType }), appointmentsData: await getAppointmentsData({ userId, resourceType }), + activity: await getUserActivity(userData), } }, })