From 6455be81bdda2fafb66fb388a9c4492d802d33cb Mon Sep 17 00:00:00 2001 From: florianfederspiel Date: Tue, 19 May 2026 16:53:53 +0200 Subject: [PATCH] KI-AGENT: Beleg-Relationswerte vor dem Speichern normalisieren --- backend/src/routes/resources/main.ts | 11 ++++++- frontend/pages/createDocument/edit/[[id]].vue | 31 +++++++++++-------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/backend/src/routes/resources/main.ts b/backend/src/routes/resources/main.ts index 72d617f..e72036b 100644 --- a/backend/src/routes/resources/main.ts +++ b/backend/src/routes/resources/main.ts @@ -936,7 +936,16 @@ export default async function resourceRoutes(server: FastifyInstance) { const normalizeDate = (val: any) => { const d = new Date(val); return isNaN(d.getTime()) ? null : d; } Object.keys(createData).forEach((key) => { - if (key.toLowerCase().includes("date") && key !== "deliveryDateType") createData[key] = normalizeDate(createData[key]) + const value = createData[key] + const shouldNormalize = + isDateLikeField(key) && + value !== null && + value !== undefined && + (typeof value === "string" || typeof value === "number" || value instanceof Date) + + if (shouldNormalize) { + createData[key] = normalizeDate(value) + } }) const [created] = await server.db.insert(table).values(createData).returning() diff --git a/frontend/pages/createDocument/edit/[[id]].vue b/frontend/pages/createDocument/edit/[[id]].vue index c207c38..e5df08d 100644 --- a/frontend/pages/createDocument/edit/[[id]].vue +++ b/frontend/pages/createDocument/edit/[[id]].vue @@ -303,6 +303,11 @@ watch( const loaded = ref(false) const normalizeEntityId = (value) => { if (value === null || typeof value === "undefined") return null + if (value instanceof Date) return null + if (typeof value === "string") { + const normalized = value.trim() + if (!/^\d+$/.test(normalized) && dayjs(normalized).isValid()) return null + } return typeof value === "object" ? (value.id ?? null) : value } const normalizeCreatedDocumentRow = (row) => { @@ -1567,14 +1572,14 @@ const saveSerialInvoice = async () => { let createData = { type: itemInfo.value.type, state: 'Erstellt', - customer: itemInfo.value.customer, - contact: itemInfo.value.contact, - contract: itemInfo.value.contract, + customer: normalizeEntityId(itemInfo.value.customer), + contact: normalizeEntityId(itemInfo.value.contact), + contract: normalizeEntityId(itemInfo.value.contract), address: itemInfo.value.address, - project: itemInfo.value.project, + project: normalizeEntityId(itemInfo.value.project), paymentDays: itemInfo.value.paymentDays, payment_type: itemInfo.value.payment_type, - outgoingsepamandate: itemInfo.value.outgoingsepamandate, + outgoingsepamandate: normalizeEntityId(itemInfo.value.outgoingsepamandate), deliveryDateType: "Leistungszeitraum", createdBy: itemInfo.value.createdBy, created_by: itemInfo.value.created_by, @@ -1650,19 +1655,19 @@ const saveDocument = async (state, resetup = false) => { type: itemInfo.value.type, taxType: ['invoices', 'cancellationInvoices', 'advanceInvoices', 'confirmationOrders', ...quoteLikeDocumentTypes].includes(itemInfo.value.type) ? normalizeTaxTypeValue(itemInfo.value.taxType) : null, state: itemInfo.value.state || "Entwurf", - customer: itemInfo.value.customer, - contact: itemInfo.value.contact, - contract: itemInfo.value.contract, + customer: normalizeEntityId(itemInfo.value.customer), + contact: normalizeEntityId(itemInfo.value.contact), + contract: normalizeEntityId(itemInfo.value.contract), address: itemInfo.value.address, - project: itemInfo.value.project, - plant: itemInfo.value.plant, + project: normalizeEntityId(itemInfo.value.project), + plant: normalizeEntityId(itemInfo.value.plant), documentNumber: itemInfo.value.documentNumber, documentDate: itemInfo.value.documentDate, deliveryDate: itemInfo.value.deliveryDate, deliveryDateEnd: itemInfo.value.deliveryDateEnd, paymentDays: itemInfo.value.paymentDays, payment_type: itemInfo.value.payment_type, - outgoingsepamandate: itemInfo.value.outgoingsepamandate, + outgoingsepamandate: normalizeEntityId(itemInfo.value.outgoingsepamandate), deliveryDateType: itemInfo.value.deliveryDateType, info: {}, createdBy: itemInfo.value.createdBy, @@ -1673,9 +1678,9 @@ const saveDocument = async (state, resetup = false) => { endText: itemInfo.value.endText, rows: itemInfo.value.rows, contactPerson: itemInfo.value.contactPerson, - createddocument: itemInfo.value.createddocument, + createddocument: normalizeEntityId(itemInfo.value.createddocument), agriculture: itemInfo.value.agriculture, - letterhead: itemInfo.value.letterhead, + letterhead: normalizeEntityId(itemInfo.value.letterhead), usedAdvanceInvoices: itemInfo.value.usedAdvanceInvoices, availableInPortal: itemInfo.value.availableInPortal, customSurchargePercentage: itemInfo.value.customSurchargePercentage,