From 4ebcc061bbbb1b38081569e86aed0c27d53f6686 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Mar 2026 23:42:37 +0000 Subject: [PATCH] Fix N+1 user queries in buildOwnerMap Replace the Promise.all+findUserByClerkId loop with a single findUsersByClerkIds batch query, consistent with the league loader and settings loader fixes. https://claude.ai/code/session_01VAkeDDVZMYS1DweQnUrRnH --- app/lib/owner-map.ts | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/app/lib/owner-map.ts b/app/lib/owner-map.ts index c041424..e7d2656 100644 --- a/app/lib/owner-map.ts +++ b/app/lib/owner-map.ts @@ -1,4 +1,4 @@ -import { findUserByClerkId, getUserDisplayName } from "~/models/user"; +import { findUsersByClerkIds, getUserDisplayName } from "~/models/user"; /** * Builds a map of teamId -> manager username (or displayName fallback). @@ -7,30 +7,20 @@ import { findUserByClerkId, getUserDisplayName } from "~/models/user"; export async function buildOwnerMap( slots: Array<{ team: { id: string; ownerId: string | null } }> ): Promise> { - const ownerIds = slots - .map((s) => s.team.ownerId) - .filter((id): id is string => id !== null); - const uniqueOwnerIds = [...new Set(ownerIds)]; + const uniqueOwnerIds = [...new Set( + slots.map((s) => s.team.ownerId).filter((id): id is string => id !== null) + )]; - const ownerEntries = await Promise.all( - uniqueOwnerIds.map(async (ownerId) => { - const user = await findUserByClerkId(ownerId); - const name = user ? getUserDisplayName(user) : null; - return name ? { ownerId, name } : null; - }) - ); - - const ownerByClerkId = new Map( - ownerEntries - .filter((e): e is NonNullable => e !== null) - .map((e) => [e.ownerId, e.name]) + const userRows = await findUsersByClerkIds(uniqueOwnerIds); + const userByClerkId = new Map( + userRows + .map((u) => [u.clerkId, getUserDisplayName(u)] as const) + .filter((entry): entry is [string, string] => entry[1] != null) ); const ownerMap: Record = {}; for (const slot of slots) { - const name = slot.team.ownerId - ? ownerByClerkId.get(slot.team.ownerId) - : undefined; + const name = slot.team.ownerId ? userByClerkId.get(slot.team.ownerId) : undefined; if (name) { ownerMap[slot.team.id] = name; }