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:
parent
eb7aa42cef
commit
f496dfd59c
2 changed files with 27 additions and 16 deletions
|
|
@ -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,23 +71,34 @@ 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
|
id: schema.leagues.id,
|
||||||
.selectDistinct({
|
name: schema.leagues.name,
|
||||||
id: schema.leagues.id,
|
createdBy: schema.leagues.createdBy,
|
||||||
name: schema.leagues.name,
|
currentSeasonId: schema.leagues.currentSeasonId,
|
||||||
createdBy: schema.leagues.createdBy,
|
isPublicDraftBoard: schema.leagues.isPublicDraftBoard,
|
||||||
currentSeasonId: schema.leagues.currentSeasonId,
|
createdAt: schema.leagues.createdAt,
|
||||||
isPublicDraftBoard: schema.leagues.isPublicDraftBoard,
|
updatedAt: schema.leagues.updatedAt,
|
||||||
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)
|
.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)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue