brackt/app/services/draft-discord.server.ts
chrisp ef29c1fcd9
All checks were successful
🚀 Deploy / 🧪 Test (push) Successful in 1m36s
🚀 Deploy / ʦ TypeScript (push) Successful in 1m23s
🚀 Deploy / 🔍 Lint (push) Successful in 50s
🚀 Deploy / 🐳 Build (push) Successful in 14m19s
🚀 Deploy / 🚀 Deploy (push) Successful in 13s
claude/discord-pick-notification-refactor-FeusJ (#58)
Co-authored-by: Claude <noreply@anthropic.com>
Reviewed-on: #58
2026-05-29 16:24:22 +00:00

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