- nur betroffene window.win_*[slot] Werte werden ersetzt - gesamte Datei bleibt erhalten - handleSave übermittelt nur geänderte Felder und Slot
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
// /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" });
|
||
}
|
||
}
|