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 * as schema from "~/database/schema";
@ -71,23 +71,34 @@ export async function findLeaguesWithActiveSeasonsByUserId(
): Promise<League[]> {
const db = database();
// Query to find all leagues where user has a team in the current season
const leagues = await db
.selectDistinct({
id: schema.leagues.id,
name: schema.leagues.name,
createdBy: schema.leagues.createdBy,
currentSeasonId: schema.leagues.currentSeasonId,
isPublicDraftBoard: schema.leagues.isPublicDraftBoard,
createdAt: schema.leagues.createdAt,
updatedAt: schema.leagues.updatedAt,
})
const leagueFields = {
id: schema.leagues.id,
name: schema.leagues.name,
createdBy: schema.leagues.createdBy,
currentSeasonId: schema.leagues.currentSeasonId,
isPublicDraftBoard: schema.leagues.isPublicDraftBoard,
createdAt: schema.leagues.createdAt,
updatedAt: schema.leagues.updatedAt,
};
// Leagues where user has a team in the current season
const leaguesWithTeam = db
.select(leagueFields)
.from(schema.leagues)
.innerJoin(schema.teams, eq(schema.teams.seasonId, schema.leagues.currentSeasonId))
.where(eq(schema.teams.ownerId, userId))
.orderBy(desc(schema.leagues.createdAt));
.where(eq(schema.teams.ownerId, userId));
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>
<CardTitle>No Active Leagues</CardTitle>
<CardDescription>
You don't have any teams in leagues with active seasons yet
You aren't a member of any leagues yet
</CardDescription>
</CardHeader>
<CardContent>