added contract history
This commit is contained in:
@@ -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,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user