Vertragsstatus-Trigger: Datum beim Upload miterfassen
Beim automatischen Status-Wechsel wird jetzt auch das passende Datum gesetzt, damit Status und Datumsfeld konsistent sind (Cockpit-Warnung "Datum fehlt" verschwindet sofort nach Upload). Backend: - Upload-Handler für Kündigungsbestätigung(s-Optionen) nimmt optional `confirmationDate` aus multipart an, speichert als cancellationConfirmationDate / cancellationConfirmationOptionsDate. Fallback: heute (nur falls Feld noch leer war). - maybeActivateOnDeliveryConfirmation nimmt optional deliveryDate, setzt Contract.startDate falls leer. Fallback: heute. Frontend: - ContractDetail: neues kleines Modal beim Kündigungsbestätigungs-Upload fragt das Bestätigungs-Datum ab (Default: heute oder bereits gesetzter Wert). Der bestehende inline-Datums-Editor bleibt für spätere Korrekturen. - ContractDocumentsSection: Datums-Input erscheint conditional im Upload-Bereich, sobald Typ "Lieferbestätigung" gewählt ist. - SaveAttachmentModal (E-Mail-Anhang → Vertragsdokument): gleicher Datums-Input conditional für "Lieferbestätigung". - API-Methoden uploadCancellationConfirmation / uploadDocument / saveAttachmentAsContractDocument nehmen optional Datum entgegen. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -86,8 +86,9 @@ export { runExpireCheck };
|
||||
|
||||
/**
|
||||
* Wird nach einem ContractDocument-Upload aufgerufen. Wenn der Typ eine
|
||||
* Lieferbestätigung ist UND der Vertrag aktuell DRAFT ist, wird er auf
|
||||
* ACTIVE gesetzt (+ Audit-Log). Andere Typen/Status bleiben unangetastet.
|
||||
* Lieferbestätigung ist:
|
||||
* - Contract.status von DRAFT auf ACTIVE setzen (falls DRAFT)
|
||||
* - Contract.startDate auf deliveryDate (oder heute) setzen, falls noch leer
|
||||
*
|
||||
* Schreibweise "Lieferbestätigung" stammt aus dem Frontend-Dropdown
|
||||
* (SaveAttachmentModal / ContractDetail). Vergleich case-insensitive +
|
||||
@@ -97,19 +98,43 @@ export async function maybeActivateOnDeliveryConfirmation(
|
||||
contractId: number,
|
||||
documentType: string,
|
||||
req: unknown,
|
||||
deliveryDate?: Date | string | null,
|
||||
): Promise<void> {
|
||||
if (!documentType || typeof documentType !== 'string') return;
|
||||
if (documentType.trim().toLowerCase() !== 'lieferbestätigung') return;
|
||||
|
||||
const contract = await prisma.contract.findUnique({
|
||||
where: { id: contractId },
|
||||
select: { status: true, contractNumber: true, customerId: true },
|
||||
select: { status: true, contractNumber: true, customerId: true, startDate: true },
|
||||
});
|
||||
if (!contract || contract.status !== 'DRAFT') return;
|
||||
if (!contract) return;
|
||||
|
||||
// deliveryDate parsen, Fallback auf heute
|
||||
let parsedDate: Date | null = null;
|
||||
if (deliveryDate) {
|
||||
const parsed = new Date(deliveryDate);
|
||||
if (!isNaN(parsed.getTime())) parsedDate = parsed;
|
||||
}
|
||||
const effectiveDate = parsedDate || new Date();
|
||||
|
||||
const updateData: Record<string, unknown> = {};
|
||||
const changes: Record<string, { vorher: unknown; nachher: unknown }> = {};
|
||||
|
||||
if (contract.status === 'DRAFT') {
|
||||
updateData.status = 'ACTIVE';
|
||||
changes.status = { vorher: 'DRAFT', nachher: 'ACTIVE' };
|
||||
}
|
||||
|
||||
if (!contract.startDate) {
|
||||
updateData.startDate = effectiveDate;
|
||||
changes.startDate = { vorher: null, nachher: effectiveDate.toISOString().split('T')[0] };
|
||||
}
|
||||
|
||||
if (Object.keys(updateData).length === 0) return;
|
||||
|
||||
await prisma.contract.update({
|
||||
where: { id: contractId },
|
||||
data: { status: 'ACTIVE' },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
await logChange({
|
||||
@@ -117,8 +142,8 @@ export async function maybeActivateOnDeliveryConfirmation(
|
||||
action: 'UPDATE',
|
||||
resourceType: 'Contract',
|
||||
resourceId: contractId.toString(),
|
||||
label: `Vertrag ${contract.contractNumber} automatisch auf ACTIVE gesetzt (Lieferbestätigung hochgeladen)`,
|
||||
details: { vorher: 'DRAFT', nachher: 'ACTIVE', trigger: 'Lieferbestätigung-Upload' },
|
||||
label: `Vertrag ${contract.contractNumber} automatisch aktualisiert (Lieferbestätigung hochgeladen)`,
|
||||
details: { ...changes, trigger: 'Lieferbestätigung-Upload' },
|
||||
customerId: contract.customerId,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user