* Add Discord draft pick announcements Posts a message to the league Discord webhook each time a pick is made, announcing the picked participant and pinging the next team on the clock if their owner has opted into Discord notifications. Enabled via a separate toggle in league settings (independent from standings update notifications). https://claude.ai/code/session_01Tvwsv3LfL9JUqxoLct8dTn * Address code review feedback on Discord pick announcements - Fix "on the clock" timing: read currentPickNumber fresh from DB post-autodraft-chain (matches sendOnTheClockEmail guarantee) - Remove outer try/catch from notifyPickMadeOnDiscord so callers' .catch() is not dead code - Add missing draft-discord.server.test.ts with 12 tests covering all early-exit and happy paths - Fix silent empty-string fallback for missing pickedSlot: warn and skip instead - Eliminate sequential season→league DB queries by accepting leagueId as a direct param - Show "save webhook URL to configure options" hint when URL is typed but not yet saved - Remove block-scope braces at both call sites (plain const declarations) - Remove redundant "Round N, Pick M" description line (title already carries this info) - Inline pickInRoundFor helper to avoid circular import with draft-utils https://claude.ai/code/session_01Tvwsv3LfL9JUqxoLct8dTn * Fix lint errors from review fixes - Remove unused logger import (no longer needed after removing try/catch) - Remove unused OWNER_ID constant in test fixture - Use toSorted() instead of sort() in sendDraftOrderNotification https://claude.ai/code/session_01Tvwsv3LfL9JUqxoLct8dTn --------- Co-authored-by: Claude <noreply@anthropic.com>
101 lines
3.1 KiB
TypeScript
101 lines
3.1 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";
|
|
|
|
type DraftSlot = { teamId: string; draftOrder: number };
|
|
|
|
// Inline snake-draft pick calculation — mirrors calculatePickInfo in draft-utils.ts
|
|
// without creating a circular import (draft-utils imports this module).
|
|
function pickInRoundFor(pickNumber: number, teamCount: number): number {
|
|
const round = Math.ceil(pickNumber / teamCount);
|
|
const rawPickInRound = ((pickNumber - 1) % teamCount) + 1;
|
|
const isOddRound = round % 2 === 1;
|
|
const teamIndex = isOddRound ? rawPickInRound - 1 : teamCount - rawPickInRound;
|
|
return teamIndex + 1;
|
|
}
|
|
|
|
export async function notifyPickMadeOnDiscord(params: {
|
|
seasonId: string;
|
|
leagueId: string;
|
|
pickedTeamName: string;
|
|
participantName: string;
|
|
sportName: string;
|
|
pickNumber: number;
|
|
round: number;
|
|
pickInRound: number;
|
|
isDraftComplete: boolean;
|
|
totalTeams: number;
|
|
draftSlots: DraftSlot[];
|
|
db: ReturnType<typeof database>;
|
|
}): Promise<void> {
|
|
const {
|
|
seasonId,
|
|
leagueId,
|
|
pickedTeamName,
|
|
participantName,
|
|
sportName,
|
|
pickNumber,
|
|
round,
|
|
pickInRound,
|
|
isDraftComplete,
|
|
totalTeams,
|
|
draftSlots,
|
|
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 nextTeamName: string | undefined;
|
|
let nextOwnerDiscordId: string | undefined;
|
|
|
|
if (!isDraftComplete) {
|
|
// Read currentPickNumber fresh from DB so we see the post-autodraft-chain state,
|
|
// matching the same guarantee sendOnTheClockEmail provides.
|
|
const freshSeason = await db.query.seasons.findFirst({
|
|
where: eq(schema.seasons.id, seasonId),
|
|
});
|
|
if (freshSeason) {
|
|
const nextPickInRound = pickInRoundFor(freshSeason.currentPickNumber ?? 1, totalTeams);
|
|
const nextSlot = draftSlots.find((s) => s.draftOrder === nextPickInRound);
|
|
if (nextSlot) {
|
|
const nextTeam = await db.query.teams.findFirst({
|
|
where: eq(schema.teams.id, nextSlot.teamId),
|
|
});
|
|
if (nextTeam) {
|
|
nextTeamName = nextTeam.name;
|
|
if (nextTeam.ownerId) {
|
|
const owner = await db.query.users.findFirst({
|
|
where: eq(schema.users.id, nextTeam.ownerId),
|
|
});
|
|
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,
|
|
});
|
|
}
|