// /pages/api/cpl/updateTdrSettingsDataAPIHandler.ts import path from "path"; import fs from "fs/promises"; export default async function handler(req, res) { const { slot, value } = req.query; if (slot === undefined || value === undefined) { return res.status(400).json({ error: "Missing slot or value" }); } const key = "win_tdrActive"; const filePath = path.join( process.cwd(), "apiMockData/SERVICE/kabelueberwachungMockData.js" ); try { const fileContent = await fs.readFile(filePath, "utf-8"); const regex = new RegExp(`(var\\s+${key}\\s*=\\s*\\[)([^\\]]*)(\\])`); const match = fileContent.match(regex); if (!match) { return res .status(404) .json({ error: `Key "${key}" not found in mock data.` }); } const values = match[2].split(",").map((v) => v.trim()); values[Number(slot)] = value; // Optional: Entferne leere Einträge while (values.length > 0 && !values[values.length - 1]) values.pop(); const newArray = `var ${key} = [\n ${values.join(", ")}\n];`; const updated = fileContent.replace(regex, newArray); await fs.writeFile(filePath, updated, "utf-8"); return res.status(200).json({ success: true, slot, value }); } catch (err) { console.error("API error:", err); return res.status(500).json({ error: "Internal server error." }); } }