2026-05-20 19:55:48 -07:00
|
|
|
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;
|
2026-05-29 16:24:22 +00:00
|
|
|
pickInRound: number;
|
2026-05-20 19:55:48 -07:00
|
|
|
isDraftComplete: boolean;
|
2026-05-29 16:24:22 +00:00
|
|
|
nextTeamName?: string;
|
|
|
|
|
nextTeamOwnerId?: string | null;
|
2026-05-20 19:55:48 -07:00
|
|
|
db: ReturnType<typeof database>;
|
|
|
|
|
}): Promise<void> {
|
|
|
|
|
const {
|
|
|
|
|
seasonId,
|
|
|
|
|
leagueId,
|
|
|
|
|
pickedTeamName,
|
|
|
|
|
participantName,
|
|
|
|
|
sportName,
|
|
|
|
|
pickNumber,
|
|
|
|
|
round,
|
2026-05-29 16:24:22 +00:00
|
|
|
pickInRound,
|
2026-05-20 19:55:48 -07:00
|
|
|
isDraftComplete,
|
2026-05-29 16:24:22 +00:00
|
|
|
nextTeamName,
|
|
|
|
|
nextTeamOwnerId,
|
2026-05-20 19:55:48 -07:00
|
|
|
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;
|
2026-05-29 16:24:22 +00:00
|
|
|
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);
|
2026-05-20 19:55:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await sendPickAnnouncementNotification({
|
|
|
|
|
webhookUrl: league.discordWebhookUrl,
|
|
|
|
|
draftUrl,
|
|
|
|
|
pickNumber,
|
|
|
|
|
round,
|
2026-05-29 16:24:22 +00:00
|
|
|
pickInRound,
|
2026-05-20 19:55:48 -07:00
|
|
|
pickedTeamName,
|
|
|
|
|
participantName,
|
|
|
|
|
sportName,
|
|
|
|
|
nextTeamName,
|
|
|
|
|
nextOwnerDiscordId,
|
|
|
|
|
isDraftComplete,
|
|
|
|
|
});
|
|
|
|
|
}
|