brackt/app/lib/owner-map.ts

30 lines
989 B
TypeScript
Raw Normal View History

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;
}