Skip to content

Commit

Permalink
Fix default user for the user (#546)
Browse files Browse the repository at this point in the history
# Description

The column was moved from `Team` to `UserTeam`, because one team can be
the default for a specific user, but not to all of them. This is
addressing the logic in dashboard
  • Loading branch information
jakubno authored Jan 20, 2025
2 parents 69025a7 + 1c1a681 commit 61cc00a
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions apps/web/src/utils/useUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ export type Team = {
apiKeys: string[]
}

interface APIKey { api_key: string; }
interface UserTeam {
id: string;
name: string;
is_default: boolean;
tier: string;
email: string;
team_api_keys: { api_key: string; }[];
teams: {
tier: string;
email: string;
team_api_keys: { api_key: string; }[];
id: string;
name: string;
}
}

export type E2BUser = (User & {
Expand Down Expand Up @@ -110,20 +113,30 @@ export const CustomUserContextProvider = (props) => {
if (!session) return
if (!session.user.id) return

// @ts-ignore
const { data: userTeams, teamsError } = await supabase
const { data: userTeams, error: teamsError } = await supabase
.from('users_teams')
.select('teams (id, name, is_default, tier, email, team_api_keys (api_key))')
.select('is_default, teams (id, name, tier, email, team_api_keys (api_key))')
.eq('user_id', session?.user.id) // Due to RLS, we could also safely just fetch all, but let's be explicit for sure

if (teamsError) Sentry.captureException(teamsError)
// TODO: Adjust when user can be part of multiple teams
// @ts-ignore
const teams = userTeams?.map(userTeam => userTeam.teams).map((team: UserTeam) => ({
...team,
apiKeys: team.team_api_keys.map(apiKey => apiKey.api_key)
} as Team))

if (userTeams === undefined || userTeams === null) {
console.log('No user teams found')
Sentry.captureEvent({ message: 'No user teams found' })
return
}

const typedUserTeams = userTeams as unknown as UserTeam[]
const teams: Team[] = typedUserTeams.map((userTeam: UserTeam): Team => {
return {
id: userTeam.teams.id,
name: userTeam.teams.name,
tier: userTeam.teams.tier,
is_default: userTeam.is_default,
email: userTeam.teams.email,
apiKeys: userTeam.teams.team_api_keys.map((apiKey: APIKey) => apiKey.api_key),
}
})
const defaultTeam = teams?.find(team => team.is_default)

if (!defaultTeam) {
Expand Down

0 comments on commit 61cc00a

Please sign in to comment.