30 lines
993 B
TypeScript
30 lines
993 B
TypeScript
|
|
import { database } from "~/database/context";
|
||
|
|
import { eq, or } from "drizzle-orm";
|
||
|
|
import * as schema from "~/database/schema";
|
||
|
|
import { createDailySnapshot } from "~/models/standings";
|
||
|
|
import { requireCronSecret } from "~/lib/cron-auth";
|
||
|
|
|
||
|
|
export async function action({ request }: { request: Request }) {
|
||
|
|
requireCronSecret(request);
|
||
|
|
|
||
|
|
const db = database();
|
||
|
|
const activeSeasons = await db.query.seasons.findMany({
|
||
|
|
where: or(eq(schema.seasons.status, "active"), eq(schema.seasons.status, "draft")),
|
||
|
|
});
|
||
|
|
|
||
|
|
let succeeded = 0;
|
||
|
|
const errors: { id: string; error: string }[] = [];
|
||
|
|
|
||
|
|
for (const season of activeSeasons) {
|
||
|
|
try {
|
||
|
|
await createDailySnapshot(season.id, db);
|
||
|
|
succeeded++;
|
||
|
|
} catch (err) {
|
||
|
|
errors.push({ id: season.id, error: err instanceof Error ? err.message : String(err) });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const status = errors.length > 0 && succeeded === 0 ? 500 : 200;
|
||
|
|
return Response.json({ total: activeSeasons.length, succeeded, errors }, { status });
|
||
|
|
}
|