diff --git a/modules/firebase/utils.ts b/modules/firebase/utils.ts index ee337331..70676fcf 100644 --- a/modules/firebase/utils.ts +++ b/modules/firebase/utils.ts @@ -39,6 +39,7 @@ import { type Invitation, type MedicationClass, type Organization, + type QuestionnaireResponse, type User, type UserMessage, } from '@/modules/firebase/models' @@ -72,22 +73,16 @@ export const userPath = (resourceType: ResourceType) => resourceType, ) -export enum ObservationType { - creatinine = 'creatinine', - eGFR = 'eGFR', - potassium = 'potassium', +export enum UserObservationCollection { + bodyWeight = 'bodyWeightObservations', + bloodPressure = 'bloodPressureObservations', + creatinine = 'creatinineObservations', + dryWeight = 'dryWeightObservations', + eGfr = 'eGfrObservations', + heartRate = 'heartRateObservations', + potassium = 'potassiumObservations', } -export const observationPath = (type: ObservationType) => - strategy( - { - [ObservationType.creatinine]: collectionNames.creatinineObservations, - [ObservationType.eGFR]: collectionNames.eGfrObservations, - [ObservationType.potassium]: collectionNames.potassiumObservations, - }, - type, - ) - export const getCollectionRefs = (db: Firestore) => ({ users: () => collection(db, collectionNames.users) as CollectionReference, @@ -156,11 +151,11 @@ export const getCollectionRefs = (db: Firestore) => ({ }: { userId: string resourceType: ResourceType - observationType: ObservationType + observationType: UserObservationCollection }) => collection( db, - `/${userPath(resourceType)}/${userId}/${observationPath(observationType)}`, + `/${userPath(resourceType)}/${userId}/${observationType}`, ) as CollectionReference, userMessages: ({ userId }: { userId: string }) => collection( @@ -244,12 +239,12 @@ export const getDocumentsRefs = (db: Firestore) => ({ }: { userId: string resourceType: ResourceType - observationType: ObservationType + observationType: UserObservationCollection observationId: string }) => doc( db, - `/${userPath(resourceType)}/${userId}/${observationPath(observationType)}/${observationId}`, + `/${userPath(resourceType)}/${userId}/${observationType}/${observationId}`, ) as DocumentReference, userMessage: ({ userId, messageId }: { userId: string; messageId: string }) => doc( diff --git a/routes/~_dashboard/~patients/clientUtils.ts b/routes/~_dashboard/~patients/clientUtils.ts index db1e7221..6994b08b 100644 --- a/routes/~_dashboard/~patients/clientUtils.ts +++ b/routes/~_dashboard/~patients/clientUtils.ts @@ -7,68 +7,76 @@ // import { useMemo } from 'react' import { basicFhirCoding } from '@/modules/firebase/models' -import { ObservationType } from '@/modules/firebase/utils' -import { strategy } from '@/packages/design-system/src/utils/misc' +import { UserObservationCollection } from '@/modules/firebase/utils' import { type MedicationsData } from '@/routes/~_dashboard/~patients/utils' -export const getObservationTypeUnits = (observationType: ObservationType) => - strategy( - { - [ObservationType.eGFR]: [ - { - unit: 'mL/min/1.73m2', - code: 'mL/min/{1.73_m2}', - coding: [ - { - ...basicFhirCoding, - system: 'http://loinc.org', - code: '98979-8', - display: - 'Glomerular filtration rate/1.73 sq M.predicted [Volume Rate/Area] in Serum, Plasma or Blood by Creatinine-based formula (CKD-EPI 2021)', - }, - ], - }, - ], - [ObservationType.potassium]: [ - { - unit: 'mEq/L', - code: 'meq/L', - coding: [ - { - ...basicFhirCoding, - system: 'http://loinc.org', - code: '6298-4', - display: 'Potassium [Moles/volume] in Blood', - }, - ], - }, - ], - [ObservationType.creatinine]: [ - { - unit: 'mg/dL', - code: 'mg/dL', - coding: [ - { - ...basicFhirCoding, - system: 'http://loinc.org', - code: '2160-0', - display: 'Creatinine [Mass/volume] in Serum or Plasma', - }, - ], - }, - ], - }, - observationType, - ) +export const labsObservationCollections = [ + UserObservationCollection.creatinine, + UserObservationCollection.eGfr, + UserObservationCollection.potassium, +] + +export const getObservationTypeUnits = ( + observationType: UserObservationCollection, +) => { + if (observationType === UserObservationCollection.eGfr) + return [ + { + unit: 'mL/min/1.73m2', + code: 'mL/min/{1.73_m2}', + coding: [ + { + ...basicFhirCoding, + system: 'http://loinc.org', + code: '98979-8', + display: + 'Glomerular filtration rate/1.73 sq M.predicted [Volume Rate/Area] in Serum, Plasma or Blood by Creatinine-based formula (CKD-EPI 2021)', + }, + ], + }, + ] + if (observationType === UserObservationCollection.potassium) + return [ + { + unit: 'mEq/L', + code: 'meq/L', + coding: [ + { + ...basicFhirCoding, + system: 'http://loinc.org', + code: '6298-4', + display: 'Potassium [Moles/volume] in Blood', + }, + ], + }, + ] + if (observationType === UserObservationCollection.creatinine) + return [ + { + unit: 'mg/dL', + code: 'mg/dL', + coding: [ + { + ...basicFhirCoding, + system: 'http://loinc.org', + code: '2160-0', + display: 'Creatinine [Mass/volume] in Serum or Plasma', + }, + ], + }, + ] +} export const getUnitOfObservationType = ( - type: ObservationType, + type: UserObservationCollection, currentUnit?: string, ) => { const newUnits = getObservationTypeUnits(type) const existingUnit = - currentUnit ? newUnits.find((unit) => unit.unit === currentUnit) : undefined - const newUnit = existingUnit ?? newUnits.at(0) + currentUnit ? + newUnits?.find((unit) => unit.unit === currentUnit) + : undefined + const newUnit = existingUnit ?? newUnits?.at(0) if (!newUnit) throw new Error('Observation units cannot be empty') return newUnit } diff --git a/routes/~_dashboard/~patients/utils.ts b/routes/~_dashboard/~patients/utils.ts index 4e099857..8c6b36f4 100644 --- a/routes/~_dashboard/~patients/utils.ts +++ b/routes/~_dashboard/~patients/utils.ts @@ -145,14 +145,12 @@ export const getLabsData = async ({ resourceType: ResourceType }) => { const rawObservations = await Promise.all( - Object.values(ObservationType).map(async (type) => { - return { - type, - data: await getDocsData( - refs.userObservation({ userId, resourceType, observationType: type }), - ), - } - }), + labsObservationCollections.map(async (type) => ({ + type, + data: await getDocsData( + refs.userObservation({ userId, resourceType, observationType: type }), + ), + })), ) const observations = rawObservations.flatMap((observations) =>