import { Request, Response } from "express"; import zoneSchema from "../../shared/model/builder/lines/zone-Model.ts"; import panelSchema from "../../shared/model/vizualization/panelmodel.ts"; import widgetSchema from "../../shared/model/vizualization/widgemodel.ts"; export class Zoneservice { static async addandUpdateZone(req: Request, res: Response): Promise { const organization = req.body.organization; const zoneDatas = req.body.zonesdata; try { const existingZone = await zoneSchema(organization).findOne({ zoneUUID: zoneDatas.zoneId, isArchive: false, }); if (!existingZone) { const newZone = await zoneSchema(organization).create({ zoneName: zoneDatas.zonename, zoneUUID: zoneDatas.zoneId, zonePoints: zoneDatas.points, centerPoints: zoneDatas.viewportPosition, createdBy: zoneDatas.userid, layer: zoneDatas.layer, sceneID: zoneDatas.sceneID, }); if (newZone) return res.status(200).json({ message: "Zone created successfully", zoneData: { zoneName: newZone.zoneName, points: newZone.zonePoints, centerPoints: newZone.centerPoints, }, }); } else { const replaceZone = await zoneSchema(organization).findOneAndUpdate( { zoneUUID: zoneDatas.zoneId, isArchive: false }, { zonePoints: zoneDatas.points, centerPoints: zoneDatas.viewportPosition, }, { new: true } ); if (!replaceZone) return res.status(404).json({ message: "Zone not updated" }); else return res.status(200).json({ message: "updated successfully", zoneData: { zoneName: replaceZone.zoneName, points: replaceZone.zonePoints, centerPoints: replaceZone.centerPoints, }, }); } } catch (error: any) { return res.status(500).send(error.message); } } static async deleteAZone(req: Request, res: Response): Promise { const organization = req.query.organization; const zoneUUID = req.params.zoneId; try { const existingZone = await zoneSchema(organization).findOne({ zoneUUID: zoneUUID, isArchive: false, }); if (!existingZone) { return res.status(404).json({ message: "Zone not found for the UUID" }); } else { const deleteZone = await zoneSchema(organization).findOneAndUpdate( { zoneUUID: zoneUUID, isArchive: false }, { isArchive: true, }, { new: true } ); if (deleteZone) { return res.status(200).json({ message: "Zone deleted successfully" }); } } } catch (error: any) { return res.status(500).send(error.message); } } static async singleZonePanelDatas(req: Request, res: Response): Promise { const organization = req.query.organization; const zoneID = req.params.zoneID; try { const existingZone = await zoneSchema(organization) .findOne({ _id: req.params.zoneID, }) .select("panelOrder zoneName zonePoints lockedPanel"); if (!existingZone) { return res.send({ message: "Zone not found for the UUID" }); } else { const panelData = await panelSchema(organization).find({ zoneID: zoneID, }); const zoneName = existingZone.zoneName as string; const widgets = await Promise.all( panelData.map(async (data) => { const widgetDataArray = await widgetSchema(organization).find({ panelID: data._id, }); return widgetDataArray.map((widgetData) => ({ id: widgetData.widgetID, type: widgetData.elementType, title: widgetData.widgetName, panel: widgetData.widgetside, data: widgetData.Data || [], })); }) ); const flattenedWidgets = widgets.flat(); const objectData = { zoneName, activeSides: existingZone.panelOrder || [], panelOrder: existingZone.panelOrder || [], lockedPanels: existingZone.lockedPanel || [], points: existingZone.zonePoints || [], widgets: flattenedWidgets, }; return res.send(objectData); } } catch (error: any) { return res.status(500).send(error.message); } } static async allZones(req: Request, res: Response): Promise { const organization = req.query.organization; const sceneID = req.params.sceneID || "scene123"; try { const Allzones = await zoneSchema(organization) .find({ sceneID: sceneID, isArchive: false }) .select("zoneName sceneID zoneUUID"); if (!Allzones || Allzones.length === 0) { return res.status(200).json({ message: "Zone not found for the UUID" }); } return res.status(200).json(Allzones); } catch (error: any) { return res.status(500).send(error.message); } } static async ZoneData(req: Request, res: Response): Promise { try { const organization = req.query.organization; console.log("organization: ", organization); const zoneID = req.params.zoneID; console.log("zoneID: ", zoneID); const findZone = await zoneSchema(organization) .findOne({ _id: zoneID }) // .select("zoneName"); console.log("findZone: ", findZone); if (findZone) return res.status(200).json(findZone); } catch (error: any) { return res.status(500).send(error.message); } } // static async ZoneIDgenerate(req: Request, res: Response): Promise { // const organization = req.query.organization; // const sceneID = req.params.sceneID; // try { // const Allzones = await zoneSchema(organization) // .find({ sceneID: sceneID, isArchive: false }) // .select("zoneName sceneID zoneUUID"); // if (!Allzones || Allzones.length === 0) { // return res.send({ message: "Zone not found for the UUID" }); // } // return res.send(Allzones); // } catch (error: any) { // return res.status(500).send(error.message); // } // } }