import { isNotNull } from "drizzle-orm"; 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({ where: isNotNull(schema.sportsSeasons.externalSeasonId), 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 }); }