Show leagues where user is a member regardless of team assignment

The homepage now uses a union query to show leagues where the user
either has a team in the current season OR is a commissioner. Previously,
commissioners without a team could not see their leagues on the homepage.

https://claude.ai/code/session_01E3ugKTfatkEc5TcDGXvHiW
This commit is contained in:
Claude 2026-02-20 18:13:27 +00:00
parent eb7aa42cef
commit f496dfd59c
No known key found for this signature in database
2 changed files with 27 additions and 16 deletions

View file

@ -1,4 +1,4 @@
import { eq, desc, and } from "drizzle-orm"; import { eq, desc, and, union } from "drizzle-orm";
import { database } from "~/database/context"; import { database } from "~/database/context";
import * as schema from "~/database/schema"; import * as schema from "~/database/schema";
@ -71,9 +71,7 @@ export async function findLeaguesWithActiveSeasonsByUserId(
): Promise<League[]> { ): Promise<League[]> {
const db = database(); const db = database();
// Query to find all leagues where user has a team in the current season const leagueFields = {
const leagues = await db
.selectDistinct({
id: schema.leagues.id, id: schema.leagues.id,
name: schema.leagues.name, name: schema.leagues.name,
createdBy: schema.leagues.createdBy, createdBy: schema.leagues.createdBy,
@ -81,13 +79,26 @@ export async function findLeaguesWithActiveSeasonsByUserId(
isPublicDraftBoard: schema.leagues.isPublicDraftBoard, isPublicDraftBoard: schema.leagues.isPublicDraftBoard,
createdAt: schema.leagues.createdAt, createdAt: schema.leagues.createdAt,
updatedAt: schema.leagues.updatedAt, updatedAt: schema.leagues.updatedAt,
}) };
// Leagues where user has a team in the current season
const leaguesWithTeam = db
.select(leagueFields)
.from(schema.leagues) .from(schema.leagues)
.innerJoin(schema.teams, eq(schema.teams.seasonId, schema.leagues.currentSeasonId)) .innerJoin(schema.teams, eq(schema.teams.seasonId, schema.leagues.currentSeasonId))
.where(eq(schema.teams.ownerId, userId)) .where(eq(schema.teams.ownerId, userId));
.orderBy(desc(schema.leagues.createdAt));
return leagues; // Leagues where user is a commissioner (with or without a team)
const leaguesAsCommissioner = db
.select(leagueFields)
.from(schema.leagues)
.innerJoin(schema.commissioners, eq(schema.commissioners.leagueId, schema.leagues.id))
.where(eq(schema.commissioners.userId, userId));
// Union deduplicates results in case user is both a commissioner and has a team
return await union(leaguesWithTeam, leaguesAsCommissioner).orderBy(
desc(schema.leagues.createdAt)
);
} }
/** /**

View file

@ -94,7 +94,7 @@ export default function Home({ loaderData }: Route.ComponentProps) {
<CardHeader> <CardHeader>
<CardTitle>No Active Leagues</CardTitle> <CardTitle>No Active Leagues</CardTitle>
<CardDescription> <CardDescription>
You don't have any teams in leagues with active seasons yet You aren't a member of any leagues yet
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>
<CardContent> <CardContent>