2026-03-18 23:42:37 +00:00
|
|
|
import { findUsersByClerkIds, getUserDisplayName } from "~/models/user";
|
2026-02-22 16:56:07 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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>> {
|
2026-03-18 23:42:37 +00:00
|
|
|
const uniqueOwnerIds = [...new Set(
|
|
|
|
|
slots.map((s) => s.team.ownerId).filter((id): id is string => id !== null)
|
|
|
|
|
)];
|
2026-02-22 16:56:07 -08:00
|
|
|
|
2026-03-18 23:42:37 +00:00
|
|
|
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)
|
2026-02-22 16:56:07 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const ownerMap: Record<string, string> = {};
|
|
|
|
|
for (const slot of slots) {
|
2026-03-18 23:42:37 +00:00
|
|
|
const name = slot.team.ownerId ? userByClerkId.get(slot.team.ownerId) : undefined;
|
2026-02-22 16:56:07 -08:00
|
|
|
if (name) {
|
|
|
|
|
ownerMap[slot.team.id] = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ownerMap;
|
|
|
|
|
}
|