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 default user for the user #546

Merged
merged 1 commit into from
Jan 20, 2025
Merged
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
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
Loading