Files
Dwinzo-Backend-V0.0/src/api-server/controller/lines/zoneService.ts

305 lines
10 KiB
TypeScript
Raw Normal View History

2025-03-28 12:45:59 +05:30
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<any> {
try {
const organization = req.body.organization;
const zoneDatas = req.body.zonesdata;
2025-03-28 12:45:59 +05:30
const existingZone = await zoneSchema(organization).findOne({
zoneId: zoneDatas.zoneId,
isArchive: false,
});
if (!existingZone) {
const newZone = await zoneSchema(organization).create({
zoneName: zoneDatas.zoneName,
2025-03-28 12:45:59 +05:30
zoneId: zoneDatas.zoneId,
zonePoints: zoneDatas.points,
viewPortposition: zoneDatas.viewportPosition,
viewPortCenter: zoneDatas.viewPortCenter,
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,
viewPortposition: zoneDatas.viewPortposition,
viewPortCenter: zoneDatas.viewPortCenter,
},
});
} else {
// const existingZoneName = await zoneSchema(organization).find({
// zoneName: zoneDatas.zoneName,
// isArchive: false,
// });
// if (existingZoneName.length > 0) {
// return res.json({ message: "Zone name already exists" });
// }
2025-03-28 12:45:59 +05:30
const replaceZone = await zoneSchema(organization).findOneAndUpdate(
{ zoneId: zoneDatas.zoneId, isArchive: false },
{
zoneName: zoneDatas.zoneName,
2025-03-28 12:45:59 +05:30
zonePoints: zoneDatas.points,
viewPortposition: zoneDatas.viewPortposition,
viewPortCenter: zoneDatas.viewPortCenter,
},
{ 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,
viewPortposition: replaceZone.viewPortposition,
viewPortCenter: replaceZone.viewPortCenter,
},
});
}
} catch (error: any) {
return res.status(500).send(error.message);
}
}
static async deleteAZone(req: Request, res: Response): Promise<any> {
const organization = req.query.organization;
const zoneId = req.params.zoneId;
try {
const existingZone = await zoneSchema(organization).findOne({
zoneId: zoneId,
isArchive: false,
});
if (!existingZone) {
return res.status(404).json({ message: "Zone not found for the UUID" });
} else {
const deleteZone = await zoneSchema(organization).findOneAndUpdate(
{ zoneId: zoneId, 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);
}
}
//single zode panel and widget data
2025-03-28 12:45:59 +05:30
static async singleZonePanelDatas(req: Request, res: Response): Promise<any> {
const organization = req.query.organization;
const zoneId = req.params.zoneId;
try {
const existingZone = await zoneSchema(organization)
.findOne({
zoneId: zoneId,
2025-03-28 12:45:59 +05:30
isArchive: false,
})
.select(
2025-04-11 09:23:44 +05:30
"panelOrder zoneName zonePoints lockedPanel zoneId viewPortCenter viewPortposition points"
);
2025-03-28 12:45:59 +05:30
if (!existingZone) {
return res.send({ message: "Zone not found for the UUID" });
} else {
const panelData = await panelSchema(organization).find({
zoneId: zoneId,
isArchive: false,
});
const zoneName = existingZone.zoneName as string;
const widgets = await Promise.all(
panelData.map(async (data) => {
const widgetDataArray = await widgetSchema(organization).find({
panelID: data._id,
isArchive: false,
});
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,
viewPortposition: existingZone.viewPortposition,
zoneId: existingZone.zoneId,
viewPortCenter: existingZone.viewPortCenter,
2025-03-28 12:45:59 +05:30
activeSides: existingZone.panelOrder || [],
panelOrder: existingZone.panelOrder || [],
lockedPanels: existingZone.lockedPanel || [],
2025-04-11 09:23:44 +05:30
points: existingZone.points || [],
2025-03-28 12:45:59 +05:30
widgets: flattenedWidgets,
};
return res.status(200).json(objectData);
}
} catch (error: any) {
return res.status(500).send(error.message);
}
}
//page full zone Datas with panel and widget
static async vizAllDatas(req: Request, res: Response): Promise<any> {
const organization = req.query.organization;
try {
const existingZones = await zoneSchema(organization)
.find({
isArchive: false,
})
.select(
2025-04-11 09:23:44 +05:30
"panelOrder zoneName zonePoints lockedPanel zoneId viewPortCenter viewPortposition points"
);
if (!existingZones) {
return res.send({ message: "Zone not found for the UUID" });
} else {
const response = await Promise.all(
existingZones.map(async (zone) => {
// Fetch all panels associated with the current zone
const panelData = await panelSchema(organization).find({
zoneId: zone._id,
isArchive: false,
});
// Fetch widgets for each panel
const widgets = await Promise.all(
panelData.map(async (panel) => {
const widgetDataArray = await widgetSchema(organization).find({
panelID: panel._id,
isArchive: false,
});
return widgetDataArray.map((widget) => ({
id: widget.widgetID,
type: widget.elementType,
title: widget.widgetName,
panel: widget.widgetside,
data: widget.Data || [],
}));
})
);
return {
zoneName: zone.zoneName,
zoneId: zone.zoneId,
viewPortposition: zone.viewPortposition,
viewPortCenter: zone.viewPortCenter,
activeSides: zone.panelOrder || [],
panelOrder: zone.panelOrder || [],
lockedPanels: zone.lockedPanel || [],
2025-04-11 09:23:44 +05:30
points: zone.points || [],
widgets: widgets.flat(),
};
})
);
return res.status(200).json(response);
}
} catch (error: any) {
return res.status(500).send(error.message);
}
}
//only for the name and zoneID
2025-03-28 12:45:59 +05:30
static async allZones(req: Request, res: Response): Promise<any> {
const organization = req.query.organization;
const sceneID = req.params.sceneID || "scene123";
try {
const Allzones = await zoneSchema(organization)
.find({ sceneID: sceneID, isArchive: false })
.select("zoneName zoneId");
2025-03-28 12:45:59 +05:30
if (!Allzones || Allzones.length === 0) {
return res.status(404).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<any> {
try {
console.log("req.params: ", req.params);
const organization = req.params.organization;
2025-03-28 12:45:59 +05:30
const zoneId = req.params.zoneId;
const findZone = await zoneSchema(organization).findOne({
zoneId: zoneId,
});
// .select("zoneName");
if (findZone) return res.status(200).json(findZone);
} catch (error: any) {
return res.status(500).send(error.message);
}
}
static async lockedPanel(req: Request, res: Response): Promise<any> {
console.log(req.body);
const organization = req.body.organization;
const zoneId = req.body.zoneId;
const lockedPanel = req.body.lockedPanel;
try {
const existingZone = await zoneSchema(organization).findOne({
zoneId: zoneId,
isArchive: false,
});
if (!existingZone) {
return res.status(404).json({ message: "Zone not found for the UUID" });
} else {
const updateLockedPanel = await zoneSchema(
organization
).findOneAndUpdate(
{ zoneId: zoneId, isArchive: false },
{
lockedPanel: lockedPanel,
},
{ new: true }
);
if (updateLockedPanel) {
return res
.status(200)
.json({ message: "locked panel updated successfully" });
}
}
} catch (error: any) {
return res.status(500).send(error.message);
}
}
2025-03-28 12:45:59 +05:30
// static async zoneIdgenerate(req: Request, res: Response): Promise<any> {
// const organization = req.query.organization;
// const sceneID = req.params.sceneID;
// try {
// const Allzones = await zoneSchema(organization)
// .find({ sceneID: sceneID, isArchive: false })
// .select("zoneName sceneID zoneId");
// 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);
// }
// }
}