import type { UnitOption } from "@/components/positions/CreatePositionModal"; import { PositionsPageClient } from "@/components/positions/PositionsPageClient"; import { currentUserId } from "@/lib/auth/session"; import { kostenstellenAbfrage } from "@/lib/cost-centers"; import { withUser } from "@/lib/db"; import { jsonArrayFrom } from "@/lib/db/json"; import { daysBetweenIso, todayIso } from "@/lib/format"; import { buildOrgMaps, orgMapsAbfragen } from "@/lib/org"; import { offeneStellenAbfrage, resolveOpenPositions, type OffeneStelle } from "@/lib/positions"; export default async function PositionsPage() { const today = todayIso(); const { openPositions, orgMaps, chiefRows, kostenstellen } = await withUser(await currentUserId(), async (tx) => { // Alles, was ohne Vorwissen geht, in einer Rundreise (lib/db/json.ts). const g = await tx .selectNoFrom((eb) => [ ...orgMapsAbfragen(eb), jsonArrayFrom(offeneStellenAbfrage(eb, today)).as("open"), jsonArrayFrom(kostenstellenAbfrage(eb, today)).as("kostenstellen"), // Wo es schon eine gültige Leitungsplanstelle gibt, lässt der // Unique-Index keine zweite zu — das gehört in den Dialog, nicht in eine // Fehlermeldung nach dem Absenden. jsonArrayFrom( eb.selectFrom("om_positions").select("org_unit_id").where("is_chief", "=", true).where("valid_to", "is", null) ).as("chiefRows"), ]) .executeTakeFirstOrThrow(); const orgMaps = buildOrgMaps(g.units as never, g.locations as never); return { orgMaps, chiefRows: g.chiefRows, kostenstellen: g.kostenstellen, openPositions: await resolveOpenPositions(tx, orgMaps, g.open as OffeneStelle[], today), }; }); const withChief = new Set(chiefRows.map((r) => r.org_unit_id)); const units: UnitOption[] = orgMaps.unitList.map((u) => ({ id: u.id, name: u.name, unit_type: u.unit_type, depth: orgMaps.depthOf.get(u.id) ?? 0, hasChief: withChief.has(u.id), })); const openPositionsWithDays = openPositions.map((p) => ({ ...p, daysOpen: daysBetweenIso(p.vacantSince) })); return ; }