Skip to content

Commit

Permalink
Copy UserObservationCollection from firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszbachorski committed Oct 7, 2024
1 parent c956710 commit 55b5a66
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 80 deletions.
31 changes: 13 additions & 18 deletions modules/firebase/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
type Invitation,
type MedicationClass,
type Organization,
type QuestionnaireResponse,
type User,
type UserMessage,
} from '@/modules/firebase/models'
Expand Down Expand Up @@ -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<User>,
Expand Down Expand Up @@ -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<FHIRObservation>,
userMessages: ({ userId }: { userId: string }) =>
collection(
Expand Down Expand Up @@ -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<FHIRObservation>,
userMessage: ({ userId, messageId }: { userId: string; messageId: string }) =>
doc(
Expand Down
116 changes: 62 additions & 54 deletions routes/~_dashboard/~patients/clientUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 6 additions & 8 deletions routes/~_dashboard/~patients/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down

0 comments on commit 55b5a66

Please sign in to comment.