Hygiene R140: Datei-Loesch-Helfer konsolidieren + DB-vor-Datei

Pentester-Hygiene zu a6b1dac:

1) Konsolidierung: neuer utils/fileCleanup.ts mit deleteFileAbsolute
   (absoluter Pfad, z.B. Multer-Temp) + deleteUploadByRelativePath
   (in DB gespeicherter /uploads/-Pfad). Ersetzt die 3x kopierten
   deleteFileIfExists/cleanupFile in creditNote-, upload- und
   customer-Service.

2) Reihenfolge: In deleteCreditNote/updateCreditNote erst die DB-
   Operation, DANN die Datei loeschen. Schlaegt der DB-Schritt fehl,
   bleibt die Datei erhalten (kein ins-Leere-zeigender Zustand).

Verifiziert: Update -> pdfPath null + alte Datei weg; Delete -> gibt
geloeschte Row zurueck (Audit) + Datei weg. Kein Regression.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-08 22:57:40 +02:00
co-authored by Claude Opus 4.8
parent c29ffd7bea
commit 3e2d9395a7
5 changed files with 75 additions and 69 deletions
+8 -22
View File
@@ -2,21 +2,7 @@ import { CustomerType, ContractStatus } from '@prisma/client';
import prisma from '../lib/prisma.js';
import { generateCustomerNumber, paginate, buildPaginationResponse } from '../utils/helpers.js';
import { ApiError } from '../utils/apiError.js';
import fs from 'fs';
import path from 'path';
// Helper zum Löschen von Dateien
function deleteFileIfExists(filePath: string | null) {
if (!filePath) return;
const absolutePath = path.join(process.cwd(), filePath);
if (fs.existsSync(absolutePath)) {
try {
fs.unlinkSync(absolutePath);
} catch (error) {
console.error('Fehler beim Löschen der Datei:', absolutePath, error);
}
}
}
import { deleteUploadByRelativePath } from '../utils/fileCleanup.js';
export interface CustomerFilters {
search?: string;
@@ -205,17 +191,17 @@ export async function deleteCustomer(id: number) {
// Kundendokumente löschen
if (customer) {
deleteFileIfExists(customer.businessRegistrationPath);
deleteFileIfExists(customer.commercialRegisterPath);
deleteFileIfExists(customer.privacyPolicyPath);
deleteUploadByRelativePath(customer.businessRegistrationPath);
deleteUploadByRelativePath(customer.commercialRegisterPath);
deleteUploadByRelativePath(customer.privacyPolicyPath);
}
// Bankkarten- und Ausweisdokumente löschen
for (const card of bankCards) {
deleteFileIfExists(card.documentPath);
deleteUploadByRelativePath(card.documentPath);
}
for (const doc of identityDocs) {
deleteFileIfExists(doc.documentPath);
deleteUploadByRelativePath(doc.documentPath);
}
// Jetzt DB-Eintrag löschen (Cascade löscht die verknüpften Einträge)
@@ -353,7 +339,7 @@ export async function deleteBankCard(id: number) {
// Erst Datei-Pfad holen, dann Datei löschen, dann DB-Eintrag löschen
const bankCard = await prisma.bankCard.findUnique({ where: { id } });
if (bankCard?.documentPath) {
deleteFileIfExists(bankCard.documentPath);
deleteUploadByRelativePath(bankCard.documentPath);
}
return prisma.bankCard.delete({ where: { id } });
}
@@ -417,7 +403,7 @@ export async function deleteDocument(id: number) {
// Erst Datei-Pfad holen, dann Datei löschen, dann DB-Eintrag löschen
const document = await prisma.identityDocument.findUnique({ where: { id } });
if (document?.documentPath) {
deleteFileIfExists(document.documentPath);
deleteUploadByRelativePath(document.documentPath);
}
return prisma.identityDocument.delete({ where: { id } });
}