brackt/app/lib/owner-map.ts
Claude 4ebcc061bb
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
2026-03-18 23:42:37 +00:00

29 lines
989 B
TypeScript

import { findUsersByClerkIds, getUserDisplayName } from "~/models/user";
/**
* Builds a map of teamId -> manager username (or displayName fallback).
* Accepts any array of objects that have a `team` with `id` and `ownerId`.
*/
export async function buildOwnerMap(
slots: Array<{ team: { id: string; ownerId: string | null } }>
): Promise<Record<string, string>> {
const uniqueOwnerIds = [...new Set(
slots.map((s) => s.team.ownerId).filter((id): id is string => id !== null)
)];
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<string, string> = {};
for (const slot of slots) {
const name = slot.team.ownerId ? userByClerkId.get(slot.team.ownerId) : undefined;
if (name) {
ownerMap[slot.team.id] = name;
}
}
return ownerMap;
}