1
0
Fork 0
fahrPool/web/lib/audit/csv.ts

37 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { aenderungZeilen, aktionText, bereichText, formatiereZeitpunkt, type AuditEintrag } from "./bezeichnungen";
const KOPF = ["Zeitpunkt", "Akteur", "Rolle", "Bereich", "Aktion", "Auftragsnummer", "Datensatz", "Änderungen"];
// Semikolon fuer deutsches Excel; Formel-Praefix gegen CSV-Injection.
export function csvFeld(wert: string): string {
let w = /^(\s*[=+\-@]|[\t\r])/.test(wert) ? `'${wert}` : wert;
if (/[;"\r\n]/.test(w)) {
w = `"${w.replace(/"/g, '""')}"`;
}
return w;
}
export function auditCsv(eintraege: AuditEintrag[]): string {
const zeilen = [KOPF.join(";")];
for (const e of eintraege) {
const aenderungen = aenderungZeilen(e)
.map((z) => `${z.feld}: ${z.alt} → ${z.neu}`)
.join(" | ");
zeilen.push(
[
formatiereZeitpunkt(e.zeitpunkt),
e.akteur_name ?? "System",
e.akteur_rolle ?? "",
bereichText(e.bereich),
aktionText(e.aktion),
e.auftragsnummer ?? "",
e.datensatz_id ?? "",
aenderungen,
]
.map(csvFeld)
.join(";")
);
}
return `${zeilen.join("\r\n")}`;
}