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> { 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 && entry[1] !== undefined) ); const ownerMap: Record = {}; 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; }