Files
alpenwerk-hr/app/api/export/cornerstone/route.ts
Maximilian Stubhan 410acfe01f
Some checks failed
CI / Lint, Typen, Tests, Build (push) Successful in 11m34s
CI / Migrationen auf leerer Datenbank (push) Has been cancelled
Cornerstone Report: system values, not display names
Reworked against the load spec and the 27.09. test file. The values I
had guessed were the German display names, which is the first entry on
the list of errors from earlier loads: Cornerstone answers "ungültiger
Wert" and rejects the whole row, followed by "Alle abhängigen Felder
müssen gültig sein" as a follow-on.

  Status            Aktiv/Inaktiv    -> Active/Inactive
  Employment Status Arbeitend/...    -> Working/On Leave/Terminated
  User Type         Mitarbeiter      -> Employee
  Time Zone         CET              -> empty

The time zone is the second entry on that list: only a portal time zone
id is valid, an abbreviation gives "Zeitzonencode nicht eindeutig".
Empty means the portal or the OU decides.

Division ID is the GUID from the test file, not a name. Termination
fields and Leave Reason are filled only when the employment status
carries them -- a reason without a termination is an invalid state for
the load, not extra information.

Four fields now stay empty on purpose, because filling them would mean
inventing an identifier that belongs to the target system: Location ID
(locations has id/name/country and no Cornerstone id), Position ID (our
S-0001 is not a Cornerstone position), Months of Service (Cornerstone
derives it) and Rehired Employee (the value for "yes" is unconfirmed,
and an unconfirmed value costs the whole row). Retention Rules and
Organisationsstufe stay empty because the load ignores them.

Header and row now match the test file byte for byte, except User ID
(no TEST- prefix outside a test load), Required Training Approvals and
Location ID.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-27 20:10:56 +02:00

47 lines
2.2 KiB
TypeScript

import { NextResponse, type NextRequest } from "next/server";
import { baueCornerstoneZeile, CORNERSTONE_CSV, cornerstoneSpalten, type CornerstoneKontext } from "@/lib/cornerstone";
import { loadKontierungen } from "@/lib/cost-centers";
import { exportFilename, exportResponseHeaders, toCsv, toXlsx } from "@/lib/export";
import { exportParameter, ladeExportMitarbeiter } from "@/lib/export-auswahl";
import { todayIso } from "@/lib/format";
import { requireHrUser } from "@/lib/auth/require-hr";
import { withUser } from "@/lib/db";
// Der Export für Cornerstone — Spalten und Zuordnung stehen in
// lib/cornerstone.ts.
//
// Dieselben Personen wie im vollständigen Export daneben, mit denselben
// Filtern (lib/export-auswahl.ts). Wer nur die Aktiven einspielen will,
// stellt das auf der Berichtsseite ein, bevor er exportiert.
export async function GET(request: NextRequest) {
const gate = await requireHrUser();
if ("denied" in gate) return gate.denied;
const { format, asOf, filters } = exportParameter(request.nextUrl.searchParams);
const stichtag = asOf ?? todayIso();
const { zeilen } = await withUser(gate.userId, async (tx) => {
const { rows, managerNummer } = await ladeExportMitarbeiter(tx, { asOf, filters });
// Die eine Angabe, die die gemeinsame Auswahl nicht mitbringt: die
// Kostenstelle hängt an der Planstelle, nicht an der Person. Erst jetzt,
// weil erst jetzt feststeht, um welche Planstellen es geht.
const positionIds = [...new Set(rows.map((r) => r.position_id).filter((id): id is string => Boolean(id)))];
const kontext: CornerstoneKontext = {
managerNummer,
kostenstelle: await loadKontierungen(tx, { asOf: stichtag, positionIds }),
};
return { zeilen: rows.map((r) => baueCornerstoneZeile(r, kontext)) };
});
const filename = exportFilename("cornerstone-export", format);
// Form und Spalten kommen beide aus lib/cornerstone.ts — siehe dort.
const body =
format === "xlsx"
? await toXlsx(zeilen, cornerstoneSpalten(), "Worksheet")
: toCsv(zeilen, cornerstoneSpalten(), CORNERSTONE_CSV);
// Derselbe Umweg über Blob wie in den anderen Exporten (TS#59417).
return new NextResponse(new Blob([body as BlobPart]), { headers: exportResponseHeaders(filename, format) });
}