brackt/app/lib/owner-map.ts
Claude 3ba9d15f9e
Extract getUserDisplayName helper and use consistently throughout
Add a single getUserDisplayName(user) function to app/models/user.ts that
encapsulates the username → displayName fallback logic. Replace 9 scattered
inline expressions across the codebase (owner-map, scoring-calculator,
league routes, settings, invite flow, draft API, Clerk webhook) with calls
to the shared helper.

No behaviour change — all existing logic preserved, just centralised.

https://claude.ai/code/session_01VAkeDDVZMYS1DweQnUrRnH
2026-03-18 23:15:01 +00:00

39 lines
1.2 KiB
TypeScript

import { findUserByClerkId, 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 ownerIds = slots
.map((s) => s.team.ownerId)
.filter((id): id is string => id !== null);
const uniqueOwnerIds = [...new Set(ownerIds)];
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<typeof e> => e !== null)
.map((e) => [e.ownerId, e.name])
);
const ownerMap: Record<string, string> = {};
for (const slot of slots) {
const name = slot.team.ownerId
? ownerByClerkId.get(slot.team.ownerId)
: undefined;
if (name) {
ownerMap[slot.team.id] = name;
}
}
return ownerMap;
}