added contract history

This commit is contained in:
2026-02-08 19:24:37 +01:00
parent 746706ef01
commit e0fc26795e
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({