Files
alpenwerk-hr/lib/positions.ts
Maximilian Stubhan 901c5c426e Consolidation pass: HR-only access, effective-dated mutations, data integrity guards, test suite
Reworks the app from a two-role (hr_admin/manager) model to a single
HR-only role gated by profiles.is_active, fixes transfer/promote/karenz/
reorg RPCs to actually defer future-dated changes via a new
pending_org_changes table instead of writing them immediately (applied
by a daily Vercel Cron route), makes reorg undo append-only instead of
deleting history, adds Karenz-return and history-date integrity guards,
deprecates the salary column, and adds explicit schema grants + perf
indexes needed to run against a fresh (non-hosted) Postgres instance.

Adds vitest unit + integration test suites (the latter against a real
local Supabase instance) covering all of the above, plus lint/typecheck/
build wiring (`npm run check`).
2026-07-14 20:32:20 +02:00

41 lines
1.6 KiB
TypeScript

import type { SupabaseClient } from "@supabase/supabase-js";
import { breadcrumbLabel, loadOrgMaps } from "./org";
import type { Database } from "./supabase/types";
export type OpenPositionResolved = {
id: string;
position_number: string;
title: string;
team_id: string;
division_id: string;
is_lead: boolean;
reports_to_employee_id: string | null;
created_at: string;
managerName: string | null;
orgLabel: string;
};
// Shared by the Hire Wizard (position lookup) and the Positions & Bereiche page.
export async function loadOpenPositions(supabase: SupabaseClient<Database>): Promise<OpenPositionResolved[]> {
const orgMaps = await loadOrgMaps(supabase);
const { data: positions } = await supabase
.from("positions")
.select("id, position_number, title, team_id, division_id, is_lead, reports_to_employee_id, created_at")
.eq("status", "open")
.order("created_at", { ascending: false });
const managerIds = Array.from(
new Set((positions ?? []).map((p) => p.reports_to_employee_id).filter((id): id is string => Boolean(id)))
);
const { data: managers } = managerIds.length
? await supabase.from("employees").select("id, first_name, last_name").in("id", managerIds)
: { data: [] as { id: string; first_name: string; last_name: string }[] };
const managerNameById = new Map((managers ?? []).map((m) => [m.id, `${m.first_name} ${m.last_name}`]));
return (positions ?? []).map((p) => ({
...p,
managerName: p.reports_to_employee_id ? (managerNameById.get(p.reports_to_employee_id) ?? null) : null,
orgLabel: breadcrumbLabel(orgMaps, p.division_id, p.team_id),
}));
}