2026-06-12 21:53:06 +00:00
|
|
|
import { and, eq, isNotNull } from "drizzle-orm";
|
2026-06-12 20:46:30 +00:00
|
|
|
import { database } from "~/database/context";
|
|
|
|
|
import * as schema from "~/database/schema";
|
|
|
|
|
import { requireCronSecret } from "~/lib/cron-auth";
|
|
|
|
|
import { syncMatches } from "~/services/match-sync";
|
|
|
|
|
|
|
|
|
|
export async function action({ request }: { request: Request }) {
|
|
|
|
|
requireCronSecret(request);
|
|
|
|
|
|
|
|
|
|
const db = database();
|
|
|
|
|
const seasons = await db.query.sportsSeasons.findMany({
|
2026-06-12 21:53:06 +00:00
|
|
|
where: and(
|
|
|
|
|
isNotNull(schema.sportsSeasons.externalSeasonId),
|
|
|
|
|
eq(schema.sportsSeasons.status, "active")
|
|
|
|
|
),
|
2026-06-12 20:46:30 +00:00
|
|
|
with: { sport: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const synced: string[] = [];
|
|
|
|
|
const errors: { id: string; name: string; error: string }[] = [];
|
|
|
|
|
|
|
|
|
|
for (const season of seasons) {
|
|
|
|
|
try {
|
|
|
|
|
const result = await syncMatches(season.id);
|
|
|
|
|
synced.push(season.id);
|
|
|
|
|
if (result.errors.length > 0) {
|
|
|
|
|
for (const e of result.errors) {
|
|
|
|
|
errors.push({ id: season.id, name: season.name, error: e.error });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
errors.push({
|
|
|
|
|
id: season.id,
|
|
|
|
|
name: season.name,
|
|
|
|
|
error: err instanceof Error ? err.message : String(err),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Response.json({ synced, errors }, { status: errors.length > 0 && synced.length === 0 ? 500 : 200 });
|
|
|
|
|
}
|