Skip to content

Commit

Permalink
Prefix invitation id (#82)
Browse files Browse the repository at this point in the history
# Prefix invitation id

## ♻️ Current situation & Problem
Invitation id and user id might be the same. 


## ⚙️ Release Notes 
* Prefix invitation id

### 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 Nov 4, 2024
1 parent 176bfad commit f4a02aa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
4 changes: 3 additions & 1 deletion modules/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
// SPDX-License-Identifier: MIT
//

import { type ResourceType } from '@/modules/firebase/utils'
import type { PatientPageTab } from '@/routes/~_dashboard/~patients/~$id/~index'

export const routes = {
home: '/',
notifications: '/notifications',
users: {
index: '/users',
user: (userId: string) => `/users/${userId}`,
user: (userId: string, resourceType: ResourceType) =>
`/users/${resourceType === 'invitation' ? 'invitation-' : ''}${userId}`,
invite: '/users/invite',
},
patients: {
Expand Down
43 changes: 26 additions & 17 deletions modules/user/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,19 @@ export const getUserOrganizationsMap = async () => {
)
}

/**
* Gets user or invitation data
* */
export const getUserData = async (userId: string) => {
const getUserAuthData = async (userId: string) => {
const user = await getDocData(docRefs.user(userId))
if (user) {
const allAuthData = await mapAuthData(
{ userIds: [userId] },
(data, id) => ({
uid: id,
email: data.auth.email,
displayName: data.auth.displayName,
}),
)
const authUser = allAuthData.at(0)
if (!authUser) throw new Error('Incomplete data')
return { user, authUser, resourceType: 'user' as const }
}
const allAuthData = await mapAuthData({ userIds: [userId] }, (data, id) => ({
uid: id,
email: data.auth.email,
displayName: data.auth.displayName,
}))
const authUser = allAuthData.at(0)
if (!authUser || !user) throw notFound()
return { user, authUser, resourceType: 'user' as const }
}

const getUserInvitationData = async (userId: string) => {
const invitation = await getDocData(docRefs.invitation(userId))
if (!invitation) throw notFound()
if (!invitation.auth) throw new Error('Incomplete data')
Expand All @@ -113,3 +108,17 @@ export const getUserData = async (userId: string) => {
resourceType: 'invitation' as const,
}
}

/**
* 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)
}
2 changes: 1 addition & 1 deletion routes/~_dashboard/~users/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const UserMenu = ({ user }: UserMenuProps) => {
/>
<RowDropdownMenu>
<DropdownMenuItem asChild>
<Link to={routes.users.user(user.resourceId)}>
<Link to={routes.users.user(user.resourceId, user.resourceType)}>
<Pencil />
Edit
</Link>
Expand Down
2 changes: 1 addition & 1 deletion routes/~_dashboard/~users/UsersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const UsersTable = ({ data }: UsersDataTableProps) => {
tableView={{
onRowClick: (user) =>
void navigate({
to: routes.users.user(user.resourceId),
to: routes.users.user(user.resourceId, user.resourceType),
}),
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion routes/~_dashboard/~users/~invite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const InviteUserPage = () => {
type: form.type,
},
})
void navigate({ to: routes.users.user(result.data.id) })
void navigate({ to: routes.users.user(result.data.id, 'invitation') })
}

return (
Expand Down

0 comments on commit f4a02aa

Please sign in to comment.