leistungsbilanz-ts/src/server/controllers/external-model-configuration.controller.ts

60 lines
2.9 KiB
TypeScript

import { randomUUID } from "node:crypto";
import type { Request, Response } from "express";
import { createExternalModelConfigurationUpdateProjectCommand } from "../../domain/models/external-model-configuration-project-command.model.js";
import { assertExternalModelConfiguration } from "../../external-model/configuration/external-model-configuration.js";
import { parseExternalXlsx } from "../../external-model/xlsx/external-xlsx-transport.js";
import { externalModelConfigurationRepository } from "../composition/application-repositories.js";
import { projectCommandService } from "../composition/project-command-stores.js";
import { respondWithProjectCommandError } from "./project-command.controller.js";
export function getExternalModelConfiguration(req: Request, res: Response) {
const projectId = String(req.params.projectId ?? "");
const result = externalModelConfigurationRepository.getByProject(projectId);
if (!result.projectExists) return res.status(404).json({ error: "Projekt nicht gefunden." });
return res.json({ configuration: result.configuration });
}
export function updateExternalModelConfiguration(req: Request, res: Response) {
const projectId = String(req.params.projectId ?? "");
const expectedRevision = req.body?.expectedRevision;
if (!Number.isSafeInteger(expectedRevision) || expectedRevision < 0) {
return res.status(400).json({ error: "Ungültige Projektrevision." });
}
try {
assertExternalModelConfiguration(req.body?.configuration);
const current = externalModelConfigurationRepository.getByProject(projectId);
if (!current.projectExists) return res.status(404).json({ error: "Projekt nicht gefunden." });
const target = {
id: current.configuration?.id ?? randomUUID(),
projectId,
configuration: req.body.configuration,
};
const result = projectCommandService.executeUser({
projectId,
expectedRevision,
description: "Revit-XLSX-Einstellungen gespeichert",
command: createExternalModelConfigurationUpdateProjectCommand(current.configuration, target),
});
return res.json({ configuration: target, history: result.history });
} catch (error) {
return respondWithProjectCommandError(error, res);
}
}
export async function detectExternalModelWorkbook(req: Request, res: Response) {
try {
assertExternalModelConfiguration(req.body?.configuration);
const document = await parseExternalXlsx({
fileName: req.body?.fileName,
bytes: Buffer.from(req.body?.contentBase64 ?? "", "base64"),
configuration: req.body.configuration,
});
return res.json({
sheetName: document.sheetName,
columnCount: document.columns.length,
objectCount: document.rows.filter((row) => row.classification === "object").length,
});
} catch (error) {
return res.status(400).json({ error: error instanceof Error ? error.message : "Musterdatei konnte nicht gelesen werden." });
}
}