brackt/app/services/draft-discord.server.ts
Chris Parsons 1b51cb554b
All checks were successful
🚀 Deploy / 🧪 Test (pull_request) Successful in 1m33s
🚀 Deploy / ʦ TypeScript (pull_request) Successful in 1m16s
🚀 Deploy / 🔍 Lint (pull_request) Successful in 47s
🚀 Deploy / 🐳 Build (pull_request) Has been skipped
🚀 Deploy / 🚀 Deploy (pull_request) Has been skipped
Fix Discord double-ping when autodraft fires immediately after manual pick
When a manual pick triggered an immediate autodraft chain, both Discord
announcements re-read currentPickNumber from the DB, which had already
advanced past the chained pick. Both messages showed the same "On the
clock" person, pinging them twice.

Fix: pass nextPickNumber (the pick immediately following each specific
pick, before any chain) as an explicit param to notifyPickMadeOnDiscord
instead of re-reading from the DB. Also removes a now-unnecessary DB
round-trip on every pick announcement.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-22 20:38:35 -07:00

96 lines
2.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";
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;
nextPickNumber: number;
round: number;
rawPickInRound: number;
isDraftComplete: boolean;
totalTeams: number;
draftSlots: DraftSlot[];
db: ReturnType<typeof database>;
}): Promise<void> {
const {
seasonId,
leagueId,
pickedTeamName,
participantName,
sportName,
pickNumber,
nextPickNumber,
round,
rawPickInRound,
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) {
const nextPickInRound = pickInRoundFor(nextPickNumber, 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: rawPickInRound,
pickedTeamName,
participantName,
sportName,
nextTeamName,
nextOwnerDiscordId,
isDraftComplete,
});
}