import { getAuth } from "@clerk/react-router/server"; import { eq, and } from "drizzle-orm"; import { database } from "~/database/context"; import * as schema from "~/database/schema"; export async function action(args: any) { const { request } = args; const auth = await getAuth(args); const userId = (auth as any).userId as string | null; if (!userId) { return Response.json({ error: "Unauthorized" }, { status: 401 }); } const formData = await request.formData(); const seasonId = formData.get("seasonId") as string; if (!seasonId) { return Response.json({ error: "Season ID is required" }, { status: 400 }); } const db = database(); // Get season const season = await db.query.seasons.findFirst({ where: eq(schema.seasons.id, seasonId), with: { league: true, }, }); if (!season) { return Response.json({ error: "Season not found" }, { status: 404 }); } // Verify user is commissioner const isCommissioner = await db.query.commissioners.findFirst({ where: and( eq(schema.commissioners.leagueId, season.leagueId), eq(schema.commissioners.userId, userId) ), }); if (!isCommissioner) { return Response.json( { error: "Only commissioners can pause the draft" }, { status: 403 } ); } // Check if draft is active if (season.status !== "draft") { return Response.json({ error: "Draft is not active" }, { status: 400 }); } // Pause the draft await db .update(schema.seasons) .set({ draftPaused: true }) .where(eq(schema.seasons.id, seasonId)); // Emit socket event try { const io = (global as any).__socketIO; io.to(`draft-${seasonId}`).emit("draft-paused", { seasonId, paused: true, }); } catch (error) { console.error("Socket.IO error:", error); } return Response.json({ success: true, paused: true }); }