Files
CPLv4.0/pages/api/cpl/updateKueDataAPIHandler.ts
Ismail Ali 28775ab63c feat: API zum gezielten Überschreiben einzelner KUE-Mock-Werte erstellt
- nur betroffene window.win_*[slot] Werte werden ersetzt
- gesamte Datei bleibt erhalten
- handleSave übermittelt nur geänderte Felder und Slot
2025-04-16 22:17:56 +02:00

53 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// /pages/api/cpl/updateKueDataAPIHandler.ts
import { NextApiRequest, NextApiResponse } from "next";
import path from "path";
import fs from "fs/promises";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
return res.status(405).json({ error: "Method not allowed" });
}
const { slot, changes } = req.body;
if (slot === undefined || !changes || typeof changes !== "object") {
return res.status(400).json({ error: "Missing slot or changes" });
}
const filePath = path.join(
process.cwd(),
"apiMockData",
"SERVICE",
"kabelueberwachungMockData.js"
);
try {
let fileContent = await fs.readFile(filePath, "utf-8");
for (const [varName, value] of Object.entries(changes)) {
const regex = new RegExp(
`(var\\s+${varName}\\s*=\\s*\\[)([^\\]]*)(\\])`,
"m"
);
const match = fileContent.match(regex);
if (match) {
const values = match[2].split(",").map((v) => v.trim());
values[slot] = JSON.stringify(value); // korrektes Format: z.B. `"FTZ_2"` oder `10.5`
const updatedArray = `${match[1]} ${values.join(", ")} ${match[3]}`;
fileContent = fileContent.replace(regex, updatedArray);
}
}
await fs.writeFile(filePath, fileContent, "utf-8");
res.status(200).json({ success: true });
} catch (error) {
console.error("Fehler beim Schreiben der Datei:", error);
res.status(500).json({ error: "Failed to write file" });
}
}