- Replace 30-line Map+Set queue with a 4-line per-league promise chain in discord.ts — picks still arrive in order, errors don't break the chain - Remove the internal DB re-queries from notifyPickMadeOnDiscord; callers now pre-resolve nextTeamName and nextTeamOwnerId from the draftSlots they already hold, eliminating the redundant team/owner lookups and the pickInRoundFor circular-import workaround - Add the missing Discord notification to draft.force-manual-pick.ts (was the only pick path that never notified Discord) - Update tests to match the simplified function signature https://claude.ai/code/session_01GCkguG2muQwnh3WTvENrwJ
68 lines
1.8 KiB
TypeScript
68 lines
1.8 KiB
TypeScript
import { eq } from "drizzle-orm";
|
|
import * as schema from "~/database/schema";
|
|
import type { database } from "~/database/context";
|
|
import { findDiscordIdsByUserIds } from "~/models/account";
|
|
import { sendPickAnnouncementNotification } from "~/services/discord";
|
|
|
|
export async function notifyPickMadeOnDiscord(params: {
|
|
seasonId: string;
|
|
leagueId: string;
|
|
pickedTeamName: string;
|
|
participantName: string;
|
|
sportName: string;
|
|
pickNumber: number;
|
|
round: number;
|
|
pickInRound: number;
|
|
isDraftComplete: boolean;
|
|
nextTeamName?: string;
|
|
nextTeamOwnerId?: string | null;
|
|
db: ReturnType<typeof database>;
|
|
}): Promise<void> {
|
|
const {
|
|
seasonId,
|
|
leagueId,
|
|
pickedTeamName,
|
|
participantName,
|
|
sportName,
|
|
pickNumber,
|
|
round,
|
|
pickInRound,
|
|
isDraftComplete,
|
|
nextTeamName,
|
|
nextTeamOwnerId,
|
|
db,
|
|
} = params;
|
|
|
|
const league = await db.query.leagues.findFirst({
|
|
where: eq(schema.leagues.id, leagueId),
|
|
});
|
|
if (!league?.discordWebhookUrl || !league.discordPicksAnnouncementEnabled) return;
|
|
|
|
const appUrl = process.env.APP_URL ?? "https://brackt.com";
|
|
const draftUrl = `${appUrl}/leagues/${leagueId}/draft/${seasonId}`;
|
|
|
|
let nextOwnerDiscordId: string | undefined;
|
|
if (!isDraftComplete && nextTeamOwnerId) {
|
|
const owner = await db.query.users.findFirst({
|
|
where: eq(schema.users.id, nextTeamOwnerId),
|
|
});
|
|
if (owner?.discordPingEnabled) {
|
|
const discordIds = await findDiscordIdsByUserIds([owner.id]);
|
|
nextOwnerDiscordId = discordIds.get(owner.id);
|
|
}
|
|
}
|
|
|
|
await sendPickAnnouncementNotification({
|
|
webhookUrl: league.discordWebhookUrl,
|
|
draftUrl,
|
|
pickNumber,
|
|
round,
|
|
pickInRound,
|
|
pickedTeamName,
|
|
participantName,
|
|
sportName,
|
|
nextTeamName,
|
|
nextOwnerDiscordId,
|
|
isDraftComplete,
|
|
});
|
|
}
|