From f496dfd59c245246acb86e6ab340c1e5ef422f11 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Feb 2026 18:13:27 +0000 Subject: [PATCH] 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 --- app/models/league.ts | 41 ++++++++++++++++++++++++++--------------- app/routes/home.tsx | 2 +- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/app/models/league.ts b/app/models/league.ts index 7f92e8b..8d4e9e5 100644 --- a/app/models/league.ts +++ b/app/models/league.ts @@ -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 { 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) + ); } /** diff --git a/app/routes/home.tsx b/app/routes/home.tsx index e2dac3b..eecba0c 100644 --- a/app/routes/home.tsx +++ b/app/routes/home.tsx @@ -94,7 +94,7 @@ export default function Home({ loaderData }: Route.ComponentProps) { No Active Leagues - You don't have any teams in leagues with active seasons yet + You aren't a member of any leagues yet