Skip to content

Commit

Permalink
Add user activity info (#59)
Browse files Browse the repository at this point in the history
# Add user activity info

### Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md).
  • Loading branch information
arkadiuszbachorski authored Oct 8, 2024
1 parent 0bf8c52 commit bdcc14c
Show file tree
Hide file tree
Showing 18 changed files with 982 additions and 744 deletions.
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 @@ import {
type Invitation,
type MedicationClass,
type Organization,
type QuestionnaireResponse,
type User,
type UserMessage,
} from '@/modules/firebase/models'
Expand All @@ -58,6 +59,7 @@ export const collectionNames = {
eGfrObservations: 'eGfrObservations',
potassiumObservations: 'potassiumObservations',
messages: 'messages',
questionnaireResponses: 'questionnaireResponses',
}

export type ResourceType = 'invitation' | 'user'
Expand All @@ -71,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 @@ -155,17 +151,28 @@ 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(
db,
`/${collectionNames.users}/${userId}/${collectionNames.messages}`,
) as CollectionReference<UserMessage>,
questionnaireResponses: ({
userId,
resourceType,
}: {
userId: string
resourceType: ResourceType
}) =>
collection(
db,
`/${userPath(resourceType)}/${userId}/${collectionNames.questionnaireResponses}`,
) as CollectionReference<QuestionnaireResponse>,
})

export const getDocumentsRefs = (db: Firestore) => ({
Expand Down Expand Up @@ -232,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
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'
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 @@ export const parseInvitationToUser = (
) => ({
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 @@ export const getUserData = async (userId: string) => {
}),
)
const authUser = allAuthData.at(0)
if (!authUser) throw new Error('Incomplete data')
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')
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,
},
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 @@ import { type Nil } from '@/packages/design-system/src/utils/misc'

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 @@ export const createSharedUserColumns = <User extends SharedUser>() => {
const columnHelper = createColumnHelper<User>()
return {
id: columnHelper.accessor(
(user) => (user.resourceType === 'invitation' ? 'invitation' : user.uid),
(user) =>
user.resourceType === 'invitation' ? 'invitation' : user.resourceId,
{
header: 'Id',
cell: (props) => {
Expand All @@ -40,8 +41,8 @@ export const createSharedUserColumns = <User extends SharedUser>() => {
Invitation
</div>
</Tooltip>
: user.uid ?
<CopyText className="max-w-[7rem]">{user.uid}</CopyText>
: user.resourceId ?
<CopyText className="max-w-[7rem]">{user.resourceId}</CopyText>
: '-'
)
},
Expand Down
Loading

0 comments on commit bdcc14c

Please sign in to comment.