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
+1 -1
View File
@@ -5,7 +5,7 @@ export declare function getContract(req: AuthRequest, res: Response): Promise<vo
export declare function createContract(req: Request, res: Response): Promise<void>;
export declare function updateContract(req: Request, res: Response): Promise<void>;
export declare function deleteContract(req: Request, res: Response): Promise<void>;
export declare function createFollowUp(req: Request, res: Response): Promise<void>;
export declare function createFollowUp(req: AuthRequest, res: Response): Promise<void>;
export declare function getContractPassword(req: Request, res: Response): Promise<void>;
export declare function getSimCardCredentials(req: Request, res: Response): Promise<void>;
export declare function getInternetCredentials(req: Request, res: Response): Promise<void>;
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"contract.controller.d.ts","sourceRoot":"","sources":["../../src/controllers/contract.controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAI5C,OAAO,EAAe,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAI7D,wBAAsB,YAAY,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAwCjF;AAED,wBAAsB,WAAW,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CA8BhF;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAU/E;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAU/E;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAU/E;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE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cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAU/E;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAU/E;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAuCnF;AAED,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAiBpF;AAED,wBAAsB,qBAAqB,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAUtF;AAED,wBAAsB,sBAAsB,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAUvF;AAED,wBAAsB,iBAAiB,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAUlF;AAID,wBAAsB,UAAU,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAW/E;AAID,wBAAsB,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAyC/E"}
+17 -1
View File
@@ -48,6 +48,7 @@ exports.snoozeContract = snoozeContract;
const client_1 = require("@prisma/client");
const contractService = __importStar(require("../services/contract.service.js"));
const contractCockpitService = __importStar(require("../services/contractCockpit.service.js"));
const contractHistoryService = __importStar(require("../services/contractHistory.service.js"));
const prisma = new client_1.PrismaClient();
async function getContracts(req, res) {
try {
@@ -154,7 +155,22 @@ async function deleteContract(req, res) {
}
async function createFollowUp(req, res) {
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' });
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 });
}
catch (error) {
File diff suppressed because one or more lines are too long
+2
View File
@@ -29,6 +29,7 @@ const appSetting_routes_js_1 = __importDefault(require("./routes/appSetting.rout
const emailProvider_routes_js_1 = __importDefault(require("./routes/emailProvider.routes.js"));
const cachedEmail_routes_js_1 = __importDefault(require("./routes/cachedEmail.routes.js"));
const invoice_routes_js_1 = __importDefault(require("./routes/invoice.routes.js"));
const contractHistory_routes_js_1 = __importDefault(require("./routes/contractHistory.routes.js"));
dotenv_1.default.config();
const app = (0, express_1.default)();
const PORT = process.env.PORT || 3001;
@@ -60,6 +61,7 @@ app.use('/api/settings', appSetting_routes_js_1.default);
app.use('/api/email-providers', emailProvider_routes_js_1.default);
app.use('/api', cachedEmail_routes_js_1.default);
app.use('/api/energy-details', invoice_routes_js_1.default);
app.use('/api', contractHistory_routes_js_1.default);
// Health check
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
+1 -1
View File
@@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,sDAA8B;AAC9B,gDAAwB;AACxB,gDAAwB;AACxB,oDAA4B;AAE5B,6EAAiD;AACjD,qFAAyD;AACzD,mFAAuD;AACvD,qFAAyD;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,gCAAkB,CAAC,CAAC;AACpC,GAAG,CAAC,GAAG,CAAC,eAAe,EAAE,8BAAgB,CAAC,CAAC;AAC3C,GAAG,CAAC,GAAG,CAAC,sBAAsB,EAAE,iCAAmB,CAAC,CAAC;AACrD,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,+BAAiB,CAAC,CAAC;AACnC,GAAG,CAAC,GAAG,CAAC,qBAAqB,EAAE,2BAAa,CAAC,CAAC;AAC9C,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,mCAAqB,CAAC,CAAC;AAEvC,eAAe;AACf,GAAG,CAAC,GAAG,CAAC,aAAa,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;IAClC,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;AAClE,CAAC,CAAC,CAAC;AAEH,iBAAiB;AACjB,GAAG,CAAC,GAAG,CAAC,CAAC,GAAU,EAAE,GAAoB,EAAE,GAAqB,EAAE,IAA0B,EAAE,EAAE;IAC9F,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACzB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC,CAAC;AAC3E,CAAC,CAAC,CAAC;AAEH,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;IACpB,OAAO,CAAC,GAAG,CAAC,yBAAyB,IAAI,EAAE,CAAC,CAAC;AAC/C,CAAC,CAAC,CAAC"}
+1 -1
View File
@@ -467,7 +467,7 @@ async function createFollowUpContract(previousContractId) {
// 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)
if (previousContract.energyDetails) {
File diff suppressed because one or more lines are too long
+14 -3
View File
File diff suppressed because one or more lines are too long
+11
View File
@@ -443,6 +443,16 @@ exports.Prisma.ContractScalarFieldEnum = {
updatedAt: 'updatedAt'
};
exports.Prisma.ContractHistoryEntryScalarFieldEnum = {
id: 'id',
contractId: 'contractId',
title: 'title',
description: 'description',
isAutomatic: 'isAutomatic',
createdBy: 'createdBy',
createdAt: 'createdAt'
};
exports.Prisma.ContractTaskScalarFieldEnum = {
id: 'id',
contractId: 'contractId',
@@ -676,6 +686,7 @@ exports.Prisma.ModelName = {
Tariff: 'Tariff',
ContractCategory: 'ContractCategory',
Contract: 'Contract',
ContractHistoryEntry: 'ContractHistoryEntry',
ContractTask: 'ContractTask',
ContractTaskSubtask: 'ContractTaskSubtask',
EnergyContractDetails: 'EnergyContractDetails',
+1732 -6
View File
File diff suppressed because it is too large Load Diff
+14 -3
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,5 +1,5 @@
{
"name": "prisma-client-ab03eeebd49b41f4edbc7df102e0a7779d814508dbccb860745760460e9e271f",
"name": "prisma-client-652f85dbf9d7be282ff4b16714e4689fe4701aade21c76f6bcc5db624157e639",
"main": "index.js",
"types": "index.d.ts",
"browser": "index-browser.js",
+14
View File
@@ -566,11 +566,25 @@ model Contract {
tasks ContractTask[]
assignedEmails CachedEmail[] // Zugeordnete E-Mails aus dem E-Mail-Client
historyEntries ContractHistoryEntry[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
// ==================== CONTRACT HISTORY ====================
model ContractHistoryEntry {
id Int @id @default(autoincrement())
contractId Int
contract Contract @relation(fields: [contractId], references: [id], onDelete: Cascade)
title String // Kurzbeschreibung (z.B. "Folgevertrag erstellt", "kWh auf 18000 erhöht")
description String? @db.Text // Längere Beschreibung (optional)
isAutomatic Boolean @default(false) // true = automatisch erstellt, false = manuell
createdBy String // E-Mail des Erstellers
createdAt DateTime @default(now())
}
// ==================== CONTRACT TASKS ====================
enum ContractTaskStatus {
+11
View File
@@ -443,6 +443,16 @@ exports.Prisma.ContractScalarFieldEnum = {
updatedAt: 'updatedAt'
};
exports.Prisma.ContractHistoryEntryScalarFieldEnum = {
id: 'id',
contractId: 'contractId',
title: 'title',
description: 'description',
isAutomatic: 'isAutomatic',
createdBy: 'createdBy',
createdAt: 'createdAt'
};
exports.Prisma.ContractTaskScalarFieldEnum = {
id: 'id',
contractId: 'contractId',
@@ -676,6 +686,7 @@ exports.Prisma.ModelName = {
Tariff: 'Tariff',
ContractCategory: 'ContractCategory',
Contract: 'Contract',
ContractHistoryEntry: 'ContractHistoryEntry',
ContractTask: 'ContractTask',
ContractTaskSubtask: 'ContractTaskSubtask',
EnergyContractDetails: 'EnergyContractDetails',
@@ -0,0 +1,15 @@
-- CreateTable
CREATE TABLE `ContractHistoryEntry` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`contractId` INTEGER NOT NULL,
`title` VARCHAR(191) NOT NULL,
`description` TEXT NULL,
`isAutomatic` BOOLEAN NOT NULL DEFAULT false,
`createdBy` VARCHAR(191) NOT NULL,
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- AddForeignKey
ALTER TABLE `ContractHistoryEntry` ADD CONSTRAINT `ContractHistoryEntry_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
+14
View File
@@ -566,11 +566,25 @@ model Contract {
tasks ContractTask[]
assignedEmails CachedEmail[] // Zugeordnete E-Mails aus dem E-Mail-Client
historyEntries ContractHistoryEntry[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
// ==================== CONTRACT HISTORY ====================
model ContractHistoryEntry {
id Int @id @default(autoincrement())
contractId Int
contract Contract @relation(fields: [contractId], references: [id], onDelete: Cascade)
title String // Kurzbeschreibung (z.B. "Folgevertrag erstellt", "kWh auf 18000 erhöht")
description String? @db.Text // Längere Beschreibung (optional)
isAutomatic Boolean @default(false) // true = automatisch erstellt, false = manuell
createdBy String // E-Mail des Erstellers
createdAt DateTime @default(now())
}
// ==================== CONTRACT TASKS ====================
enum ContractTaskStatus {
+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,
});
}