Add the Honestly survey export to Berichte

A participant list for the employee survey on honestly.de, offered as
CSV and Excel right under the full data export. Columns: Personalnummer,
Email, Firstname, Last Name, Language, Location, OU, OU+1, ... , Role.
Language is always "de", Role always "Respondee".

The org columns run bottom-up: OU is the person's own unit, OU+1 the one
above, up to the top node. Their number follows the deepest chain in
the file, shorter chains are padded with empty cells, and there is
always at least one OU column so the file keeps its shape.

It exports exactly the people the full export would, with the same
filters. To guarantee that, the selection moved out of the full export's
route into lib/export-auswahl.ts and both routes use it; the full
export's columns are untouched.

Email is employees.email, which is the private address and optional --
there is no work address in the schema. Missing addresses stay empty
rather than getting a placeholder that would receive an invitation.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-18 15:04:45 +02:00
parent 7b7562799d
commit 779d6116b2
6 changed files with 405 additions and 92 deletions

131
tests/unit/honestly.test.ts Normal file
View File

@@ -0,0 +1,131 @@
import { describe, expect, it } from "vitest";
import { toCsv } from "@/lib/export";
import { baueHonestlyZeilen, honestlySpalten, orgKette, ouUeberschrift } from "@/lib/honestly";
import { buildOrgMaps, type OrgUnit } from "@/lib/org";
// Die Datei geht an eine fremde Plattform, die sie über Spaltennamen
// zuordnet. Ein Fehler darin zeigt sich nicht hier, sondern dort — als
// Befragung, die an die falschen Leute geht oder nach der falschen Einheit
// auswertet. Deshalb wird hier die Form festgehalten, nicht nur der Inhalt.
const units: OrgUnit[] = [
{ id: "g", org_number: "1", name: "Manner AG", parent_id: null, unit_type: "Gesellschaft" },
{ id: "b", org_number: "2", name: "Produktion", parent_id: "g", unit_type: "Bereich" },
{ id: "a", org_number: "3", name: "Waffel", parent_id: "b", unit_type: "Abteilung" },
{ id: "t", org_number: "4", name: "Linie 1", parent_id: "a", unit_type: "Team" },
{ id: "v", org_number: "5", name: "Vertrieb", parent_id: "g", unit_type: "Bereich" },
];
const maps = buildOrgMaps(units, []);
const standorte = new Map([["wien", "Wien"], ["wolkersdorf", "Wolkersdorf"]]);
function person(teil: Partial<Parameters<typeof baueHonestlyZeilen>[0][number]> = {}) {
return {
personnel_number: 4711,
email: "anna.berger@example.test",
first_name: "Anna",
last_name: "Berger",
location_id: "wien",
org_unit_id: "t",
...teil,
};
}
describe("orgKette", () => {
it("fängt bei der eigenen Einheit an und endet ganz oben", () => {
// OU ist die Einheit der Person, OU+1 die darüber — so ist es bestellt.
// ancestorsOf liefert es andersherum; ohne das Umdrehen stünde die
// Gesellschaft in der Spalte „OU".
expect(orgKette(maps, "t")).toEqual(["Linie 1", "Waffel", "Produktion", "Manner AG"]);
});
it("ist leer ohne Einheit", () => {
expect(orgKette(maps, null)).toEqual([]);
});
});
describe("Spalten", () => {
it("stehen in der vorgegebenen Reihenfolge", () => {
expect(honestlySpalten(3).map((s) => s.header)).toEqual([
"Personalnummer",
"Email",
"Firstname",
"Last Name",
"Language",
"Location",
"OU",
"OU+1",
"OU+2",
"Role",
]);
});
it("heissen OU, OU+1, OU+2 …", () => {
expect([0, 1, 2, 5].map(ouUeberschrift)).toEqual(["OU", "OU+1", "OU+2", "OU+5"]);
});
it("behalten mindestens eine OU-Spalte", () => {
// Eine Datei, deren Spalten je nach Inhalt verschwinden, bricht die
// Zuordnung, die auf der anderen Seite eingerichtet wurde.
expect(honestlySpalten(0).filter((s) => s.header.startsWith("OU"))).toHaveLength(1);
});
});
describe("baueHonestlyZeilen", () => {
it("richtet die Zahl der OU-Spalten nach der tiefsten Kette", () => {
const { tiefe } = baueHonestlyZeilen([person({ org_unit_id: "v" }), person({ org_unit_id: "t" })], maps, standorte);
expect(tiefe).toBe(4);
});
it("füllt kürzere Ketten hinten mit Leerzellen auf", () => {
const { zeilen, tiefe } = baueHonestlyZeilen([person({ org_unit_id: "v" }), person()], maps, standorte);
const spalten = honestlySpalten(tiefe);
const werte = (i: number) => spalten.map((s) => s.get(zeilen[i]));
// Vertrieb hängt direkt unter der Gesellschaft: zwei Einheiten, zwei leere.
expect(werte(0).slice(6, 10)).toEqual(["Vertrieb", "Manner AG", "", ""]);
expect(werte(1).slice(6, 10)).toEqual(["Linie 1", "Waffel", "Produktion", "Manner AG"]);
});
it("setzt Sprache und Rolle fest", () => {
const { zeilen, tiefe } = baueHonestlyZeilen([person()], maps, standorte);
const zeile = Object.fromEntries(honestlySpalten(tiefe).map((s) => [s.header, s.get(zeilen[0])]));
expect(zeile.Language).toBe("de");
expect(zeile.Role).toBe("Respondee");
});
it("übernimmt die Personendaten unverändert", () => {
const { zeilen, tiefe } = baueHonestlyZeilen([person()], maps, standorte);
const zeile = Object.fromEntries(honestlySpalten(tiefe).map((s) => [s.header, s.get(zeilen[0])]));
expect(zeile).toMatchObject({
Personalnummer: 4711,
Email: "anna.berger@example.test",
Firstname: "Anna",
"Last Name": "Berger",
Location: "Wien",
});
});
it("lässt eine fehlende E-Mail leer, statt sie zu erfinden", () => {
// Die Adresse ist privat und freiwillig (20260811140000). Eine leere
// Zelle fällt in Honestly auf; ein Platzhalter bekäme eine Einladung.
const { zeilen } = baueHonestlyZeilen([person({ email: null })], maps, standorte);
expect(honestlySpalten(1)[1].get(zeilen[0])).toBe("");
});
it("gibt Personen ohne Einheit eine leere Kette, aber eine Zeile", () => {
// Wer nicht zugeordnet ist, soll trotzdem befragt werden.
const { zeilen, tiefe } = baueHonestlyZeilen([person({ org_unit_id: null })], maps, standorte);
expect(zeilen).toHaveLength(1);
expect(tiefe).toBe(1);
expect(honestlySpalten(tiefe).find((s) => s.header === "OU")?.get(zeilen[0])).toBe("");
});
});
describe("als CSV", () => {
it("schreibt die Kopfzeile genau so, wie Honestly sie zuordnet", () => {
const { zeilen, tiefe } = baueHonestlyZeilen([person()], maps, standorte);
const kopf = toCsv(zeilen, honestlySpalten(tiefe)).replace(/^/, "").split(/\r?\n/)[0];
expect(kopf).toContain("Personalnummer");
expect(kopf).toContain("Last Name");
expect(kopf).toMatch(/OU\+3.*Role/);
});
});