2025-10-31 22:13:12 -07:00
|
|
|
import type { Route } from "./+types/admin.sports-seasons.$id.events";
|
|
|
|
|
import { redirect } from "react-router";
|
|
|
|
|
import { findSportsSeasonById } from "~/models/sports-season";
|
|
|
|
|
import {
|
|
|
|
|
getScoringEventsForSportsSeason,
|
|
|
|
|
createScoringEvent,
|
|
|
|
|
deleteScoringEvent,
|
2026-03-07 21:59:29 -08:00
|
|
|
bulkCreateScoringEvents,
|
2025-10-31 22:13:12 -07:00
|
|
|
type CreateScoringEventData,
|
|
|
|
|
} from "~/models/scoring-event";
|
2025-11-11 10:08:25 -08:00
|
|
|
import { getQPStandings } from "~/models/qualifying-points";
|
|
|
|
|
import { finalizeQualifyingPoints } from "~/models/scoring-calculator";
|
2026-03-21 09:44:05 -07:00
|
|
|
|
2025-10-31 22:13:12 -07:00
|
|
|
|
|
|
|
|
export async function loader({ params }: Route.LoaderArgs) {
|
|
|
|
|
const sportsSeason = await findSportsSeasonById(params.id);
|
|
|
|
|
|
|
|
|
|
if (!sportsSeason) {
|
|
|
|
|
throw new Response("Sports season not found", { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const events = await getScoringEventsForSportsSeason(params.id);
|
|
|
|
|
|
2025-11-11 10:08:25 -08:00
|
|
|
// For qualifying sports seasons, get QP standings
|
|
|
|
|
let qpStandings = null;
|
2026-03-21 09:44:05 -07:00
|
|
|
const scoringRules = null;
|
2025-11-11 10:08:25 -08:00
|
|
|
if (sportsSeason.scoringPattern === "qualifying_points") {
|
|
|
|
|
qpStandings = await getQPStandings(params.id);
|
|
|
|
|
|
|
|
|
|
// Get scoring rules from a linked season (if any)
|
|
|
|
|
// For now, we'll use default scoring for projection
|
|
|
|
|
// Note: QP standings are global to the sports season, not league-specific
|
|
|
|
|
// When showing to league members, we would filter by their league's scoring rules
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-31 22:13:12 -07:00
|
|
|
return {
|
|
|
|
|
sportsSeason: sportsSeason as typeof sportsSeason & {
|
|
|
|
|
sport: { id: string; name: string; type: string; slug: string };
|
|
|
|
|
},
|
|
|
|
|
events,
|
2025-11-11 10:08:25 -08:00
|
|
|
qpStandings,
|
|
|
|
|
scoringRules,
|
2025-10-31 22:13:12 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function action({ request, params }: Route.ActionArgs) {
|
|
|
|
|
const formData = await request.formData();
|
|
|
|
|
const intent = formData.get("intent");
|
|
|
|
|
|
2025-11-11 10:08:25 -08:00
|
|
|
if (intent === "finalize-qp") {
|
|
|
|
|
try {
|
|
|
|
|
await finalizeQualifyingPoints(params.id);
|
|
|
|
|
return { success: "Qualifying points finalized and fantasy placements assigned to top 8!" };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error finalizing qualifying points:", error);
|
|
|
|
|
return { error: error instanceof Error ? error.message : "Failed to finalize qualifying points" };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 21:59:29 -08:00
|
|
|
if (intent === "bulk-create") {
|
|
|
|
|
const bulkText = formData.get("bulkEvents");
|
|
|
|
|
const bulkEventType = formData.get("bulkEventType");
|
|
|
|
|
|
|
|
|
|
if (typeof bulkText !== "string" || !bulkText.trim()) {
|
|
|
|
|
return { error: "Event list is required" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const validTypes = ["playoff_game", "major_tournament", "schedule_event"] as const;
|
|
|
|
|
type ValidEventType = typeof validTypes[number];
|
|
|
|
|
const eventType: ValidEventType =
|
|
|
|
|
validTypes.includes(bulkEventType as ValidEventType)
|
|
|
|
|
? (bulkEventType as ValidEventType)
|
|
|
|
|
: "playoff_game";
|
|
|
|
|
|
|
|
|
|
const sportsSeason = await findSportsSeasonById(params.id);
|
|
|
|
|
if (!sportsSeason) return { error: "Sports season not found" };
|
|
|
|
|
|
|
|
|
|
const isQualifyingDefault =
|
|
|
|
|
sportsSeason.scoringPattern === "qualifying_points" &&
|
|
|
|
|
eventType === "major_tournament";
|
|
|
|
|
|
2026-03-15 10:22:42 -07:00
|
|
|
// Parse lines: "Name, YYYY-MM-DD[, HH:MM]" or tab-separated, or just "Name"
|
2026-03-07 21:59:29 -08:00
|
|
|
const lines = bulkText
|
|
|
|
|
.split("\n")
|
|
|
|
|
.map((l) => l.trim())
|
|
|
|
|
.filter(Boolean);
|
|
|
|
|
|
|
|
|
|
const parsedEvents = lines.map((line) => {
|
|
|
|
|
const parts = line.split(/[,\t]/).map((p) => p.trim());
|
|
|
|
|
const name = parts[0];
|
|
|
|
|
const dateStr = parts[1];
|
2026-03-15 10:22:42 -07:00
|
|
|
const timeStr = parts[2];
|
2026-03-07 21:59:29 -08:00
|
|
|
const eventDate =
|
|
|
|
|
dateStr && /^\d{4}-\d{2}-\d{2}$/.test(dateStr)
|
|
|
|
|
? new Date(dateStr)
|
|
|
|
|
: undefined;
|
2026-03-15 10:22:42 -07:00
|
|
|
const eventStartsAt =
|
|
|
|
|
eventDate && dateStr && timeStr && /^\d{2}:\d{2}$/.test(timeStr)
|
|
|
|
|
? new Date(`${dateStr}T${timeStr}:00.000Z`)
|
|
|
|
|
: undefined;
|
|
|
|
|
return { name, eventDate, eventStartsAt, eventType, isQualifyingEvent: isQualifyingDefault };
|
2026-03-07 21:59:29 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const validEvents = parsedEvents.filter((e) => e.name);
|
|
|
|
|
if (validEvents.length === 0) {
|
|
|
|
|
return { error: "No valid events found. Use format: Event Name, YYYY-MM-DD" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await bulkCreateScoringEvents(params.id, validEvents);
|
|
|
|
|
return { success: `Created ${validEvents.length} event${validEvents.length !== 1 ? "s" : ""} successfully` };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error bulk creating events:", error);
|
|
|
|
|
return { error: "Failed to create events. Please try again." };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-31 22:13:12 -07:00
|
|
|
if (intent === "delete-event") {
|
|
|
|
|
const eventId = formData.get("eventId");
|
|
|
|
|
|
|
|
|
|
if (typeof eventId !== "string" || !eventId) {
|
|
|
|
|
return { error: "Event ID is required" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await deleteScoringEvent(eventId);
|
|
|
|
|
return { success: "Event deleted successfully" };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error deleting event:", error);
|
|
|
|
|
return { error: "Failed to delete event" };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const name = formData.get("name");
|
|
|
|
|
const eventType = formData.get("eventType");
|
2026-03-15 10:22:42 -07:00
|
|
|
const eventStartsAtRaw = formData.get("eventStartsAt");
|
2025-10-31 22:13:12 -07:00
|
|
|
|
|
|
|
|
// Validation
|
|
|
|
|
if (typeof name !== "string" || !name.trim()) {
|
|
|
|
|
return { error: "Event name is required" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
eventType !== "playoff_game" &&
|
|
|
|
|
eventType !== "major_tournament" &&
|
2026-03-07 21:59:29 -08:00
|
|
|
eventType !== "schedule_event"
|
2025-10-31 22:13:12 -07:00
|
|
|
) {
|
|
|
|
|
return { error: "Invalid event type" };
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-11 10:08:25 -08:00
|
|
|
// Get the sports season to check if this should be a qualifying event
|
|
|
|
|
const sportsSeason = await findSportsSeasonById(params.id);
|
|
|
|
|
if (!sportsSeason) {
|
|
|
|
|
return { error: "Sports season not found" };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Automatically mark major tournaments as qualifying events for qualifying_points sports seasons
|
|
|
|
|
const isQualifyingEvent =
|
|
|
|
|
sportsSeason.scoringPattern === "qualifying_points" &&
|
|
|
|
|
eventType === "major_tournament";
|
|
|
|
|
|
2026-03-15 10:22:42 -07:00
|
|
|
// eventStartsAt is a UTC ISO string produced by localDateTimeToUtcIso on the client
|
|
|
|
|
const eventStartsAt =
|
|
|
|
|
typeof eventStartsAtRaw === "string" && eventStartsAtRaw
|
|
|
|
|
? new Date(eventStartsAtRaw)
|
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
|
|
// Derive eventDate from eventStartsAt (UTC date portion)
|
|
|
|
|
const eventDate = eventStartsAt ? new Date(eventStartsAt.toISOString().split("T")[0]) : undefined;
|
|
|
|
|
|
2025-10-31 22:13:12 -07:00
|
|
|
const eventData: CreateScoringEventData = {
|
|
|
|
|
sportsSeasonId: params.id,
|
|
|
|
|
name: name.trim(),
|
2026-03-07 21:59:29 -08:00
|
|
|
eventType: eventType as "playoff_game" | "major_tournament" | "schedule_event",
|
2026-03-15 10:22:42 -07:00
|
|
|
eventDate,
|
|
|
|
|
eventStartsAt,
|
2025-11-11 10:08:25 -08:00
|
|
|
isQualifyingEvent,
|
2025-10-31 22:13:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const event = await createScoringEvent(eventData);
|
|
|
|
|
return redirect(`/admin/sports-seasons/${params.id}/events/${event.id}`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error creating scoring event:", error);
|
|
|
|
|
return { error: "Failed to create scoring event. Please try again." };
|
|
|
|
|
}
|
|
|
|
|
}
|