brackt/app/routes/api/draft.resume.ts

77 lines
1.9 KiB
TypeScript

import { getAuth } from "@clerk/react-router/ssr.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 resume the draft" },
{ status: 403 }
);
}
// Check if draft is active
if (season.status !== "draft") {
return Response.json(
{ error: "Draft is not active" },
{ status: 400 }
);
}
// Resume the draft
await db
.update(schema.seasons)
.set({ draftPaused: false })
.where(eq(schema.seasons.id, seasonId));
// Emit socket event
try {
const io = (global as any).__socketIO;
io.to(`draft-${seasonId}`).emit("draft-resumed", {
seasonId,
paused: false,
});
} catch (error) {
console.error("Socket.IO error:", error);
}
return Response.json({ success: true, paused: false });
}