added contract history

This commit is contained in:
2026-02-08 19:24:37 +01:00
parent ee4f1aacdd
commit e348e86c60
33 changed files with 3200 additions and 743 deletions
+32 -2
View File
@@ -2,6 +2,7 @@ import { Request, Response } from 'express';
import { PrismaClient } from '@prisma/client';
import * as contractService from '../services/contract.service.js';
import * as contractCockpitService from '../services/contractCockpit.service.js';
import * as contractHistoryService from '../services/contractHistory.service.js';
import { ApiResponse, AuthRequest } from '../types/index.js';
const prisma = new PrismaClient();
@@ -116,9 +117,38 @@ export async function deleteContract(req: Request, res: Response): Promise<void>
}
}
export async function createFollowUp(req: Request, res: Response): Promise<void> {
export async function createFollowUp(req: AuthRequest, res: Response): Promise<void> {
try {
const contract = await contractService.createFollowUpContract(parseInt(req.params.id));
const previousContractId = parseInt(req.params.id);
// Vorgängervertrag laden für Vertragsnummer
const previousContract = await prisma.contract.findUnique({
where: { id: previousContractId },
select: { contractNumber: true },
});
if (!previousContract) {
res.status(404).json({ success: false, error: 'Vorgängervertrag nicht gefunden' } as ApiResponse);
return;
}
const contract = await contractService.createFollowUpContract(previousContractId);
const createdBy = req.user?.email || 'unbekannt';
// Historie-Eintrag für den Vorgängervertrag erstellen
await contractHistoryService.createFollowUpHistoryEntry(
previousContractId,
contract.contractNumber,
createdBy
);
// Historie-Eintrag für den neuen Folgevertrag erstellen
await contractHistoryService.createNewContractFromPredecessorEntry(
contract.id,
previousContract.contractNumber,
createdBy
);
res.status(201).json({ success: true, data: contract } as ApiResponse);
} catch (error) {
res.status(400).json({
@@ -0,0 +1,81 @@
import { Request, Response } from 'express';
import * as contractHistoryService from '../services/contractHistory.service.js';
import { ApiResponse, AuthRequest } from '../types/index.js';
export async function getHistoryEntries(req: AuthRequest, res: Response): Promise<void> {
try {
const contractId = parseInt(req.params.contractId);
const entries = await contractHistoryService.getHistoryEntries(contractId);
res.json({ success: true, data: entries } as ApiResponse);
} catch (error) {
res.status(500).json({
success: false,
error: 'Fehler beim Laden der Historie',
} as ApiResponse);
}
}
export async function createHistoryEntry(req: AuthRequest, res: Response): Promise<void> {
try {
const contractId = parseInt(req.params.contractId);
const { title, description } = req.body;
if (!title || typeof title !== 'string' || title.trim().length === 0) {
res.status(400).json({
success: false,
error: 'Titel ist erforderlich',
} as ApiResponse);
return;
}
const entry = await contractHistoryService.createHistoryEntry(contractId, {
title: title.trim(),
description: description?.trim() || undefined,
isAutomatic: false,
createdBy: req.user?.email || 'unbekannt',
});
res.status(201).json({ success: true, data: entry } as ApiResponse);
} catch (error) {
res.status(400).json({
success: false,
error: error instanceof Error ? error.message : 'Fehler beim Erstellen des Eintrags',
} as ApiResponse);
}
}
export async function updateHistoryEntry(req: AuthRequest, res: Response): Promise<void> {
try {
const contractId = parseInt(req.params.contractId);
const entryId = parseInt(req.params.entryId);
const { title, description } = req.body;
const entry = await contractHistoryService.updateHistoryEntry(contractId, entryId, {
title: title?.trim(),
description: description?.trim(),
});
res.json({ success: true, data: entry } as ApiResponse);
} catch (error) {
res.status(400).json({
success: false,
error: error instanceof Error ? error.message : 'Fehler beim Aktualisieren des Eintrags',
} as ApiResponse);
}
}
export async function deleteHistoryEntry(req: AuthRequest, res: Response): Promise<void> {
try {
const contractId = parseInt(req.params.contractId);
const entryId = parseInt(req.params.entryId);
await contractHistoryService.deleteHistoryEntry(contractId, entryId);
res.json({ success: true, message: 'Eintrag gelöscht' } as ApiResponse);
} catch (error) {
res.status(400).json({
success: false,
error: error instanceof Error ? error.message : 'Fehler beim Löschen des Eintrags',
} as ApiResponse);
}
}
+2
View File
@@ -25,6 +25,7 @@ import appSettingRoutes from './routes/appSetting.routes.js';
import emailProviderRoutes from './routes/emailProvider.routes.js';
import cachedEmailRoutes from './routes/cachedEmail.routes.js';
import invoiceRoutes from './routes/invoice.routes.js';
import contractHistoryRoutes from './routes/contractHistory.routes.js';
dotenv.config();
@@ -61,6 +62,7 @@ app.use('/api/settings', appSettingRoutes);
app.use('/api/email-providers', emailProviderRoutes);
app.use('/api', cachedEmailRoutes);
app.use('/api/energy-details', invoiceRoutes);
app.use('/api', contractHistoryRoutes);
// Health check
app.get('/api/health', (req, res) => {
@@ -0,0 +1,39 @@
import { Router } from 'express';
import * as contractHistoryController from '../controllers/contractHistory.controller.js';
import { authenticate, requirePermission } from '../middleware/auth.js';
const router = Router();
// Alle Einträge für einen Vertrag
router.get(
'/contracts/:contractId/history',
authenticate,
requirePermission('contracts:read'),
contractHistoryController.getHistoryEntries
);
// Neuen Eintrag erstellen
router.post(
'/contracts/:contractId/history',
authenticate,
requirePermission('contracts:update'),
contractHistoryController.createHistoryEntry
);
// Eintrag aktualisieren
router.put(
'/contracts/:contractId/history/:entryId',
authenticate,
requirePermission('contracts:update'),
contractHistoryController.updateHistoryEntry
);
// Eintrag löschen
router.delete(
'/contracts/:contractId/history/:entryId',
authenticate,
requirePermission('contracts:update'),
contractHistoryController.deleteHistoryEntry
);
export default router;
+1 -1
View File
@@ -626,7 +626,7 @@ export async function createFollowUpContract(previousContractId: number) {
// Explicitly NOT copying: providerName, tariffName, portalUsername, portalPassword, price fields
cancellationPeriodId: previousContract.cancellationPeriodId ?? undefined,
contractDurationId: previousContract.contractDurationId ?? undefined,
notes: `Folgevertrag zu ${previousContract.contractNumber}`,
// notes nicht mehr automatisch setzen - wird jetzt über Historie-Eintrag dokumentiert
};
// Copy type-specific details (without credentials)
@@ -0,0 +1,133 @@
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export interface CreateHistoryEntryData {
title: string;
description?: string;
isAutomatic?: boolean;
createdBy: string;
}
/**
* Alle Historie-Einträge für einen Vertrag abrufen
*/
export async function getHistoryEntries(contractId: number) {
return prisma.contractHistoryEntry.findMany({
where: { contractId },
orderBy: { createdAt: 'desc' },
});
}
/**
* Einzelnen Historie-Eintrag abrufen
*/
export async function getHistoryEntry(contractId: number, entryId: number) {
return prisma.contractHistoryEntry.findFirst({
where: { id: entryId, contractId },
});
}
/**
* Neuen Historie-Eintrag erstellen
*/
export async function createHistoryEntry(contractId: number, data: CreateHistoryEntryData) {
// Prüfen ob Vertrag existiert
const contract = await prisma.contract.findUnique({
where: { id: contractId },
});
if (!contract) {
throw new Error('Vertrag nicht gefunden');
}
return prisma.contractHistoryEntry.create({
data: {
contractId,
title: data.title,
description: data.description,
isAutomatic: data.isAutomatic ?? false,
createdBy: data.createdBy,
},
});
}
/**
* Historie-Eintrag aktualisieren (nur manuelle Einträge)
*/
export async function updateHistoryEntry(
contractId: number,
entryId: number,
data: { title?: string; description?: string }
) {
const entry = await prisma.contractHistoryEntry.findFirst({
where: { id: entryId, contractId },
});
if (!entry) {
throw new Error('Historie-Eintrag nicht gefunden');
}
if (entry.isAutomatic) {
throw new Error('Automatische Einträge können nicht bearbeitet werden');
}
return prisma.contractHistoryEntry.update({
where: { id: entryId },
data: {
title: data.title,
description: data.description,
},
});
}
/**
* Historie-Eintrag löschen (nur manuelle Einträge)
*/
export async function deleteHistoryEntry(contractId: number, entryId: number) {
const entry = await prisma.contractHistoryEntry.findFirst({
where: { id: entryId, contractId },
});
if (!entry) {
throw new Error('Historie-Eintrag nicht gefunden');
}
if (entry.isAutomatic) {
throw new Error('Automatische Einträge können nicht gelöscht werden');
}
return prisma.contractHistoryEntry.delete({ where: { id: entryId } });
}
/**
* Automatischen Historie-Eintrag für Folgevertrag erstellen (im Vorgängervertrag)
*/
export async function createFollowUpHistoryEntry(
previousContractId: number,
newContractNumber: string,
createdBy: string
) {
return createHistoryEntry(previousContractId, {
title: `Folgevertrag erstellt: ${newContractNumber}`,
description: `Ein neuer Folgevertrag (${newContractNumber}) wurde aus diesem Vertrag erstellt.`,
isAutomatic: true,
createdBy,
});
}
/**
* Automatischen Historie-Eintrag für neuen Folgevertrag erstellen (im neuen Vertrag selbst)
*/
export async function createNewContractFromPredecessorEntry(
newContractId: number,
previousContractNumber: string,
createdBy: string
) {
return createHistoryEntry(newContractId, {
title: `Folgevertrag zu ${previousContractNumber}`,
description: `Dieser Vertrag wurde als Folgevertrag zu ${previousContractNumber} erstellt.`,
isAutomatic: true,
createdBy,
});
}