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

Fix user id processing #85

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions modules/user/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
getDocData,
getDocDataOrThrow,
getDocsData,
type ResourceType,
type UserAuthenticationInformation,
} from '@/modules/firebase/utils'
import { queryClient } from '@/modules/query/queryClient'
Expand Down Expand Up @@ -109,16 +110,25 @@
}
}

const invitationPrefix = 'invitation-'

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

View check run for this annotation

Codecov / codecov/patch

modules/user/queries.tsx#L113

Added line #L113 was not covered by tests

/**
* Gets user or invitation data
* @param userId Starts with `invitation-` if user is an invitation.
* It's necessary to prefix invitation id, because user id and invitation id are not guaranteed to be distinct
* */
export const getUserData = async (userId: string) => {
const prefix = 'invitation-'
if (userId.startsWith(prefix)) {
const id = userId.slice(prefix.length)
return getUserInvitationData(id)
}
return getUserAuthData(userId)
}
export const parseUserId = (userId: string) =>
userId.startsWith(invitationPrefix) ?
{
userId: userId.slice(invitationPrefix.length),
resourceType: 'invitation' as const,
}
: { userId, resourceType: 'user' as const }

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

View check run for this annotation

Codecov / codecov/patch

modules/user/queries.tsx#L120-L126

Added lines #L120 - L126 were not covered by tests

export const getUserData = async (
userId: string,
resourceType: ResourceType,

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

View check run for this annotation

Codecov / codecov/patch

modules/user/queries.tsx#L128-L130

Added lines #L128 - L130 were not covered by tests
) =>
resourceType === 'invitation' ?
getUserInvitationData(userId)
: getUserAuthData(userId)

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

View check run for this annotation

Codecov / codecov/patch

modules/user/queries.tsx#L132-L134

Added lines #L132 - L134 were not covered by tests
11 changes: 6 additions & 5 deletions routes/~_dashboard/~patients/~$id/~index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
getDocsData,
type ResourceType,
} from '@/modules/firebase/utils'
import { getUserData } from '@/modules/user/queries'
import { getUserData, parseUserId } from '@/modules/user/queries'

Check warning on line 24 in routes/~_dashboard/~patients/~$id/~index.tsx

View check run for this annotation

Codecov / codecov/patch

routes/~_dashboard/~patients/~$id/~index.tsx#L24

Added line #L24 was not covered by tests
import {
Tabs,
TabsContent,
Expand Down Expand Up @@ -84,9 +84,9 @@

const PatientPage = () => {
const router = useRouter()
const { id: userId } = Route.useParams()
const { tab } = Route.useSearch()
const {
userId,

Check warning on line 89 in routes/~_dashboard/~patients/~$id/~index.tsx

View check run for this annotation

Codecov / codecov/patch

routes/~_dashboard/~patients/~$id/~index.tsx#L89

Added line #L89 was not covered by tests
medications,
formProps,
userMedications,
Expand Down Expand Up @@ -246,13 +246,14 @@
tab: z.nativeEnum(PatientPageTab).optional().catch(undefined),
}),
loader: async ({ params }) => {
const userId = params.id
const userData = await getUserData(userId)
const { resourceType, user, authUser } = userData
const { userId, resourceType } = parseUserId(params.id)
const userData = await getUserData(userId, resourceType)
const { user, authUser } = userData

Check warning on line 251 in routes/~_dashboard/~patients/~$id/~index.tsx

View check run for this annotation

Codecov / codecov/patch

routes/~_dashboard/~patients/~$id/~index.tsx#L249-L251

Added lines #L249 - L251 were not covered by tests
if (user.type !== UserType.patient) throw notFound()

return {
user,
userId,

Check warning on line 256 in routes/~_dashboard/~patients/~$id/~index.tsx

View check run for this annotation

Codecov / codecov/patch

routes/~_dashboard/~patients/~$id/~index.tsx#L256

Added line #L256 was not covered by tests
authUser,
resourceType,
medications: await getMedicationsData(),
Expand Down
10 changes: 6 additions & 4 deletions routes/~_dashboard/~users/~$id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { queryClient } from '@/modules/query/queryClient'
import {
getUserData,
parseUserId,
userOrganizationQueryOptions,
} from '@/modules/user/queries'
import { getUserName } from '@/packages/design-system/src/modules/auth/user'
Expand All @@ -27,8 +28,8 @@

const UserPage = () => {
const router = useRouter()
const { id: userId } = Route.useParams()
const { authUser, user, resourceType, organizations } = Route.useLoaderData()
const { authUser, user, resourceType, organizations, userId } =
Route.useLoaderData()

Check warning on line 32 in routes/~_dashboard/~users/~$id.tsx

View check run for this annotation

Codecov / codecov/patch

routes/~_dashboard/~users/~$id.tsx#L31-L32

Added lines #L31 - L32 were not covered by tests

const updateUser = async (form: UserFormSchema) => {
const authData = {
Expand Down Expand Up @@ -88,12 +89,13 @@
component: UserPage,
beforeLoad: () => ensureType([UserType.admin, UserType.owner]),
loader: async ({ params }) => {
const userId = params.id
const { resourceType, user, authUser } = await getUserData(userId)
const { resourceType, userId } = parseUserId(params.id)
const { user, authUser } = await getUserData(userId, resourceType)

Check warning on line 93 in routes/~_dashboard/~users/~$id.tsx

View check run for this annotation

Codecov / codecov/patch

routes/~_dashboard/~users/~$id.tsx#L92-L93

Added lines #L92 - L93 were not covered by tests
if (user.type === UserType.patient) throw notFound()

return {
user,
userId,

Check warning on line 98 in routes/~_dashboard/~users/~$id.tsx

View check run for this annotation

Codecov / codecov/patch

routes/~_dashboard/~users/~$id.tsx#L98

Added line #L98 was not covered by tests
authUser,
resourceType,
organizations: await queryClient.ensureQueryData(
Expand Down
Loading