From 3b7bcb79402476212b7c7951aa301ba4fa70f374 Mon Sep 17 00:00:00 2001 From: florianfederspiel Date: Wed, 29 Apr 2026 15:51:07 +0200 Subject: [PATCH] =?UTF-8?q?Erweitere=20Dry-Run-Ausgabe=20f=C3=BCr=20Mitarb?= =?UTF-8?q?eiterimport?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/scripts/import-mitarbeiterliste.ts | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/backend/scripts/import-mitarbeiterliste.ts b/backend/scripts/import-mitarbeiterliste.ts index f64573c..028b064 100644 --- a/backend/scripts/import-mitarbeiterliste.ts +++ b/backend/scripts/import-mitarbeiterliste.ts @@ -29,6 +29,8 @@ type CliOptions = { branchMap: Record } +type ImportAction = "create" | "update" + function printHelp() { console.log(` Importiert die Excel-Datei "Mitarbeiterliste.xlsm" in auth_profiles. @@ -262,6 +264,29 @@ function toWeeklyHours(monthlyHours: number | null) { return Math.round(((monthlyHours * 12) / 52) * 100) / 100 } +function formatValue(value: unknown) { + if (value == null) return "-" + if (typeof value === "object") return JSON.stringify(value) + return String(value) +} + +function collectFieldChanges(existing: any, nextPayload: Record) { + if (!existing) return [] + + const changes: string[] = [] + for (const [field, nextValue] of Object.entries(nextPayload)) { + const currentValue = existing[field] + const currentFormatted = formatValue(currentValue) + const nextFormatted = formatValue(nextValue) + + if (currentFormatted !== nextFormatted) { + changes.push(`${field}: ${currentFormatted} -> ${nextFormatted}`) + } + } + + return changes +} + async function validateTenantAndBranches( db: any, tenantId: number, @@ -372,6 +397,7 @@ async function main() { let createdProfiles = 0 let updatedProfiles = 0 + const actionLogs: string[] = [] for (const row of preparedRows) { const nameParts = splitFullName(row.mitarbeiter) @@ -404,6 +430,21 @@ async function main() { active: existing?.active ?? true, } + const action: ImportAction = existing ? "update" : "create" + const fieldChanges = existing ? collectFieldChanges(existing, payload) : [] + const actionPrefix = action === "create" ? "ERSTELLEN" : "AKTUALISIEREN" + const branchLabel = `${row.betrieb} -> ${row.branchId}` + + if (action === "create") { + actionLogs.push( + `[${actionPrefix}] ${row.mitarbeiter} | Branch ${branchLabel} | Vertrag ${row.anstellung || "-"} | Position ${row.position || "-"}` + ) + } else { + actionLogs.push( + `[${actionPrefix}] ${row.mitarbeiter} | Branch ${branchLabel} | Änderungen: ${fieldChanges.length ? fieldChanges.join("; ") : "keine"}` + ) + } + if (!existing) { if (!options.dryRun) { const [created] = await db @@ -458,6 +499,12 @@ async function main() { console.log(`[IMPORT MITARBEITER] Zeilen gelesen: ${rows.length}`) console.log(`[IMPORT MITARBEITER] Profile erstellt: ${createdProfiles}`) console.log(`[IMPORT MITARBEITER] Profile aktualisiert: ${updatedProfiles}`) + if (actionLogs.length) { + console.log("[IMPORT MITARBEITER] Details:") + for (const logLine of actionLogs) { + console.log(` ${logLine}`) + } + } console.log("") } finally { await pool.end()