feat(mcp): support serial invoice workflows

This commit is contained in:
2026-09-09 14:30:42 +02:00
parent d0bc8a5e0f
commit 2ec162c0e7
5 changed files with 172 additions and 22 deletions

View File

@@ -14,9 +14,13 @@ import { documentTemplateHandlebars } from "../utils/handlebars";
dayjs.extend(quarterOfYear);
export const executeManualGeneration = async (server:FastifyInstance,executionDate,templateIds,tenantId,executedBy) => {
try {
console.log(executedBy)
export const executeManualGeneration = async (
server: FastifyInstance,
executionDate: string | Date,
templateIds: number[],
tenantId: number,
executedBy: string,
) => {
const executionDayjs = dayjs(executionDate);
@@ -40,13 +44,14 @@ export const executeManualGeneration = async (server:FastifyInstance,executionDa
and(
eq(schema.createddocuments.tenant, tenantId),
eq(schema.createddocuments.type, "serialInvoices"),
eq(schema.createddocuments.archived, false),
inArray(schema.createddocuments.id, templateIds)
)
);
if (templates.length === 0) {
console.warn("Keine passenden Vorlagen gefunden.");
return [];
throw new Error("Keine passenden Serienrechnungsvorlagen gefunden.");
}
// 3. Folder & FileType IDs holen (Hilfsfunktionen unten)
@@ -88,13 +93,10 @@ export const executeManualGeneration = async (server:FastifyInstance,executionDa
}
}
return results;
} catch (error) {
console.log(error);
}
return { execution: executionRecord, results };
}
export const finishManualGeneration = async (server: FastifyInstance, executionId: number) => {
export const finishManualGeneration = async (server: FastifyInstance, executionId: string, tenantId: number) => {
try {
console.log(`Beende Ausführung ${executionId}...`);
@@ -103,15 +105,16 @@ export const finishManualGeneration = async (server: FastifyInstance, executionI
const [executionRecord] = await server.db
.select()
.from(schema.serialExecutions)// @ts-ignore
.where(eq(schema.serialExecutions.id, executionId))
.where(and(
eq(schema.serialExecutions.id, executionId),
eq(schema.serialExecutions.tenant, tenantId),
))
.limit(1);
if (!executionRecord) throw new Error("Execution nicht gefunden");
console.log(executionRecord);
const tenantId = executionRecord.tenant;
console.log(tenantId)
// Tenant laden (für Settings etc.)
@@ -132,7 +135,11 @@ export const finishManualGeneration = async (server: FastifyInstance, executionI
const documents = await server.db
.select()
.from(schema.createddocuments)
.where(eq(schema.createddocuments.serialexecution, executionId));
.where(and(
eq(schema.createddocuments.serialexecution, executionId),
eq(schema.createddocuments.tenant, tenantId),
eq(schema.createddocuments.archived, false),
));
console.log(`${documents.length} Dokumente werden finalisiert...`);
@@ -228,7 +235,10 @@ export const finishManualGeneration = async (server: FastifyInstance, executionI
status: finalStatus,
summary: `Abgeschlossen: ${successCount} erfolgreich, ${errorCount} Fehler.`
})// @ts-ignore
.where(eq(schema.serialExecutions.id, executionId));
.where(and(
eq(schema.serialExecutions.id, executionId),
eq(schema.serialExecutions.tenant, tenantId),
));
return { success: true, processed: successCount, errors: errorCount };
@@ -240,7 +250,10 @@ export const finishManualGeneration = async (server: FastifyInstance, executionI
.update(schema.serialExecutions)
.set({ status: "error", summary: "Kritischer Fehler beim Finalisieren." })
//@ts-ignore
.where(eq(schema.serialExecutions.id, executionId));
.where(and(
eq(schema.serialExecutions.id, executionId),
eq(schema.serialExecutions.tenant, tenantId),
));
throw error;
}
}