49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import { getAuth } from "@clerk/react-router/server";
|
|
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
import { eq } from "drizzle-orm";
|
|
import { reorderQueueWithSeason, getTeamQueue } from "~/models/draft-queue";
|
|
|
|
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 teamId = formData.get("teamId");
|
|
const seasonId = formData.get("seasonId");
|
|
const participantIdsJson = formData.get("participantIds");
|
|
|
|
if (!teamId || !seasonId || !participantIdsJson) {
|
|
return Response.json({ error: "Missing required fields" }, { status: 400 });
|
|
}
|
|
|
|
const participantIds = JSON.parse(participantIdsJson as string);
|
|
|
|
const db = database();
|
|
|
|
// Verify user owns this team
|
|
const team = await db.query.teams.findFirst({
|
|
where: eq(schema.teams.id, teamId as string),
|
|
});
|
|
|
|
if (!team || team.ownerId !== userId) {
|
|
return Response.json({ error: "Unauthorized" }, { status: 403 });
|
|
}
|
|
|
|
// Reorder queue
|
|
await reorderQueueWithSeason(seasonId as string, teamId as string, participantIds);
|
|
|
|
// Get updated queue
|
|
const updatedQueue = await getTeamQueue(teamId as string);
|
|
|
|
// TODO: Emit socket event for real-time update
|
|
// const io = getSocketIO();
|
|
// io.to(`draft-${seasonId}`).emit("queue-updated", { teamId, queue: updatedQueue });
|
|
|
|
return Response.json({ success: true, queue: updatedQueue });
|
|
}
|