Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/view-models/src/crm/kontakt-csv.ts
T
2026-07-08 16:15:37 +02:00

57 lines
1.8 KiB
TypeScript

/**
* Contact CSV export builder (R-P6, P6 CRM core — Variante A). Pure function:
* the route resolves the rows (incl. display labels) and this turns them into an
* RFC 4180 string. No React/Prisma/server imports (DoD A.3).
*
* Decisions:
* - Delimiter `,`, line ending CRLF, fields quoted only when they contain a
* comma, double-quote or newline; embedded quotes are doubled (RFC 4180 §2).
* - Header row uses fixed GERMAN column names: the export is consumed by end
* users (not a machine contract), so human-readable German headers win over
* the raw column keys.
*/
export interface KontaktCsvRow {
readonly displayName: string;
readonly kontaktTyp: string;
readonly kontaktStatus: string;
readonly primaerEmail: string | null;
readonly primaerTelefon: string | null;
readonly zustaendigerLabel: string | null;
readonly createdAtIso: string;
}
const CSV_HEADER: readonly string[] = ["Name", "Typ", "Status", "E-Mail", "Telefon", "Zuständig", "Erstellt am"];
/** Escape a single field per RFC 4180 (quote when needed, double inner quotes). */
function csvField(value: string | null): string {
const raw = value ?? "";
if (/[",\r\n]/.test(raw)) {
return `"${raw.replace(/"/g, '""')}"`;
}
return raw;
}
function csvLine(fields: readonly (string | null)[]): string {
return fields.map(csvField).join(",");
}
/** Build an RFC 4180 CSV (CRLF-separated) with a German header row. */
export function buildKontaktCsv(rows: readonly KontaktCsvRow[]): string {
const lines: string[] = [csvLine(CSV_HEADER)];
for (const row of rows) {
lines.push(
csvLine([
row.displayName,
row.kontaktTyp,
row.kontaktStatus,
row.primaerEmail,
row.primaerTelefon,
row.zustaendigerLabel,
row.createdAtIso,
]),
);
}
return lines.join("\r\n");
}