Dough Git

clove/pkviewer

web/src/routes/admin/+page.server.ts

back to web/src/routes/admin

/* web/src/routes/admin/+page.server.ts */
import type { PageServerLoad } from "./$types";
import { manageApi } from "$lib/server/api.ts";

export const load: PageServerLoad = async () => {
  const [assignments, credits] = await Promise.all([
    manageApi.get<{ assignments: Array<{ state: string }> }>("/admin/assignments"),
    manageApi.get<{ credits: unknown[]; sections: unknown[] }>("/admin/credits"),
  ]);

  const counts = {
    pending: 0,
    accepted: 0,
    declined: 0,
    hidden: 0,
    revoked: 0,
  };
  if (assignments.ok) {
    for (const assignment of assignments.value.assignments) {
      if (assignment.state in counts) counts[assignment.state as keyof typeof counts] += 1;
    }
  }

  return {
    counts,
    credits: credits.ok
      ? { entries: credits.value.credits.length, sections: credits.value.sections.length }
      : null,
  };
};