Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user activity info #59

Merged
merged 11 commits into from
Oct 8, 2024
Merged
5 changes: 5 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
// false negatives
"import/namespace": ["off"],
"no-empty-pattern": "off",
"@typescript-eslint/no-empty-object-type": [
"error",
// `interface SpecificVariantProps extends VariantProps {}` is fine
{ "allowInterfaces": "with-single-extends" }
],
// make sure to `await` inside try…catch
"@typescript-eslint/return-await": ["error", "in-try-catch"],
"@typescript-eslint/no-confusing-void-expression": [
Expand Down
7 changes: 7 additions & 0 deletions modules/firebase/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
type fhirElementConverter,
type fhirMedicationRequestConverter,
type fhirObservationConverter,
type fhirQuestionnaireResponseConverter,
type fhirResourceConverter,
type InferEncoded,
type invitationConverter,
Expand Down Expand Up @@ -67,4 +68,10 @@ export type UserMessage = InferEncoded<typeof userMessageConverter> & {
id: string
}

export type QuestionnaireResponse = InferEncoded<
typeof fhirQuestionnaireResponseConverter
> & {
id: string
}

export type LocalizedText = string | Record<string, string>
43 changes: 25 additions & 18 deletions modules/firebase/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
type Invitation,
type MedicationClass,
type Organization,
type QuestionnaireResponse,
type User,
type UserMessage,
} from '@/modules/firebase/models'
Expand All @@ -58,6 +59,7 @@
eGfrObservations: 'eGfrObservations',
potassiumObservations: 'potassiumObservations',
messages: 'messages',
questionnaireResponses: 'questionnaireResponses',

Check warning on line 62 in modules/firebase/utils.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/utils.ts#L62

Added line #L62 was not covered by tests
}

export type ResourceType = 'invitation' | 'user'
Expand All @@ -71,22 +73,16 @@
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',

Check warning on line 83 in modules/firebase/utils.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/utils.ts#L76-L83

Added lines #L76 - L83 were not covered by tests
}

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 @@ -155,17 +151,28 @@
}: {
userId: string
resourceType: ResourceType
observationType: ObservationType
observationType: UserObservationCollection
}) =>
collection(
db,
`/${userPath(resourceType)}/${userId}/${observationPath(observationType)}`,
`/${userPath(resourceType)}/${userId}/${observationType}`,

Check warning on line 158 in modules/firebase/utils.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/utils.ts#L158

Added line #L158 was not covered by tests
) as CollectionReference<FHIRObservation>,
userMessages: ({ userId }: { userId: string }) =>
collection(
db,
`/${collectionNames.users}/${userId}/${collectionNames.messages}`,
) as CollectionReference<UserMessage>,
questionnaireResponses: ({
userId,
resourceType,
}: {

Check warning on line 168 in modules/firebase/utils.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/utils.ts#L165-L168

Added lines #L165 - L168 were not covered by tests
userId: string
resourceType: ResourceType
}) =>
collection(
db,
`/${userPath(resourceType)}/${userId}/${collectionNames.questionnaireResponses}`,
) as CollectionReference<QuestionnaireResponse>,

Check warning on line 175 in modules/firebase/utils.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/utils.ts#L172-L175

Added lines #L172 - L175 were not covered by tests
})

export const getDocumentsRefs = (db: Firestore) => ({
Expand Down Expand Up @@ -232,12 +239,12 @@
}: {
userId: string
resourceType: ResourceType
observationType: ObservationType
observationType: UserObservationCollection
observationId: string
}) =>
doc(
db,
`/${userPath(resourceType)}/${userId}/${observationPath(observationType)}/${observationId}`,
`/${userPath(resourceType)}/${userId}/${observationType}/${observationId}`,

Check warning on line 247 in modules/firebase/utils.ts

View check run for this annotation

Codecov / codecov/patch

modules/firebase/utils.ts#L247

Added line #L247 was not covered by tests
) as DocumentReference<FHIRObservation>,
userMessage: ({ userId, messageId }: { userId: string; messageId: string }) =>
doc(
Expand Down
30 changes: 14 additions & 16 deletions modules/user/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//
import { UserType } from '@stanfordbdhg/engagehf-models'
import { queryOptions } from '@tanstack/react-query'
import { notFound } from '@tanstack/react-router'

Check warning on line 10 in modules/user/queries.tsx

View check run for this annotation

Codecov / codecov/patch

modules/user/queries.tsx#L10

Added line #L10 was not covered by tests
import { query, where } from 'firebase/firestore'
import { docRefs, getCurrentUser, refs } from '@/modules/firebase/app'
import { type Invitation, type Organization } from '@/modules/firebase/models'
Expand All @@ -32,7 +33,6 @@
) => ({
resourceId: invitation.id,
resourceType: 'invitation' as const,
uid: invitation.userId,
email: invitation.auth?.email,
displayName: invitation.auth?.displayName,
organization: organizationMap.get(invitation.user.organization ?? ''),
Expand Down Expand Up @@ -93,25 +93,23 @@
}),
)
const authUser = allAuthData.at(0)
if (!authUser) throw new Error('Incomplete data')

Check warning on line 96 in modules/user/queries.tsx

View check run for this annotation

Codecov / codecov/patch

modules/user/queries.tsx#L96

Added line #L96 was not covered by tests
return { user, authUser, resourceType: 'user' as const }
}
const invitation = await getDocData(docRefs.invitation(userId))
if (!invitation) throw notFound()
if (!invitation.auth) throw new Error('Incomplete data')

Check warning on line 101 in modules/user/queries.tsx

View check run for this annotation

Codecov / codecov/patch

modules/user/queries.tsx#L100-L101

Added lines #L100 - L101 were not covered by tests
return {
user:
invitation?.user ?
{
...invitation.user,
invitationCode: invitation.id,
}
: undefined,
authUser:
invitation?.auth ?
{
uid: userId,
email: invitation.auth.email ?? null,
displayName: invitation.auth.displayName ?? null,
}
: undefined,
user: {
...invitation.user,
invitationCode: invitation.id,
lastActiveDate: null,
},
authUser: {
uid: userId,
email: invitation.auth.email,
displayName: invitation.auth.displayName,
},

Check warning on line 112 in modules/user/queries.tsx

View check run for this annotation

Codecov / codecov/patch

modules/user/queries.tsx#L103-L112

Added lines #L103 - L112 were not covered by tests
resourceType: 'invitation' as const,
}
}
9 changes: 5 additions & 4 deletions modules/user/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

interface SharedUser {
resourceType: 'invitation' | 'user'
uid: Nil<string>
resourceId: string
displayName: Nil<string>
email: Nil<string>
organization: Nil<{ name: string }>
Expand All @@ -27,7 +27,8 @@
const columnHelper = createColumnHelper<User>()
return {
id: columnHelper.accessor(
(user) => (user.resourceType === 'invitation' ? 'invitation' : user.uid),
(user) =>
user.resourceType === 'invitation' ? 'invitation' : user.resourceId,

Check warning on line 31 in modules/user/table.tsx

View check run for this annotation

Codecov / codecov/patch

modules/user/table.tsx#L30-L31

Added lines #L30 - L31 were not covered by tests
{
header: 'Id',
cell: (props) => {
Expand All @@ -40,8 +41,8 @@
Invitation
</div>
</Tooltip>
: user.uid ?
<CopyText className="max-w-[7rem]">{user.uid}</CopyText>
: user.resourceId ?
<CopyText className="max-w-[7rem]">{user.resourceId}</CopyText>

Check warning on line 45 in modules/user/table.tsx

View check run for this annotation

Codecov / codecov/patch

modules/user/table.tsx#L44-L45

Added lines #L44 - L45 were not covered by tests
: '-'
)
},
Expand Down
Loading
Loading