111 lines
3.5 KiB
TypeScript
111 lines
3.5 KiB
TypeScript
import { Request, Response } from "express";
|
|
import panelSchema from "../../shared/model/vizualization/panelmodel.ts";
|
|
import zoneSchema from "../../shared/model/builder/lines/zone-Model.ts";
|
|
import widgetSchema from "../../shared/model/vizualization/widgemodel.ts";
|
|
|
|
export class panelService {
|
|
static async AddPanel(req: Request, res: Response): Promise<any> {
|
|
try {
|
|
const organization = req.body.organization;
|
|
const zoneId = req.body.zoneId;
|
|
const panelName = req.body.panelName;
|
|
const panelOrder = req.body.panelOrder;
|
|
const findZone = await zoneSchema(organization).findOne({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
|
|
if (!findZone) {
|
|
return res.status(404).json({ message: "Zone not found" });
|
|
}
|
|
const updatezone = await zoneSchema(organization).findOneAndUpdate(
|
|
{ zoneId: zoneId, isArchive: false },
|
|
{ panelOrder: panelOrder },
|
|
{ new: true }
|
|
);
|
|
const existingPanels = await panelSchema(organization).find({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
|
|
const existingPanelNames = existingPanels.map(
|
|
(panel: any) => panel.panelName
|
|
);
|
|
|
|
const missingPanels = panelOrder.filter(
|
|
(panelName: string) => !existingPanelNames.includes(panelName)
|
|
);
|
|
|
|
const createdPanels = [];
|
|
for (const panelName of missingPanels) {
|
|
const newPanel = await panelSchema(organization).create({
|
|
zoneId: zoneId,
|
|
panelName: panelName,
|
|
widgets: [],
|
|
isArchive: false,
|
|
});
|
|
createdPanels.push(newPanel);
|
|
}
|
|
|
|
if (createdPanels.length === 0) {
|
|
return res.status(200).json({
|
|
message: "No new panels were created. All panels already exist.",
|
|
});
|
|
}
|
|
const IDdata = createdPanels.map((ID: any) => {
|
|
return ID._id;
|
|
});
|
|
console.log("IDdata: ", IDdata);
|
|
return res.status(201).json({
|
|
message: "Panels created successfully",
|
|
panelID: IDdata,
|
|
});
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
static async deletePanel(req: Request, res: Response): Promise<any> {
|
|
try {
|
|
const { organization, panelID, zoneId } = req.body;
|
|
const existingZone = await zoneSchema(organization).findOne({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingZone)
|
|
return res.status(404).json({ message: "Zone not found" });
|
|
const existingPanel = await panelSchema(organization).findOne({
|
|
zoneId: zoneId,
|
|
_id: panelID,
|
|
isArchive: false,
|
|
});
|
|
if (!existingPanel)
|
|
return res.status(409).json({ message: "Panel Already Deleted" });
|
|
const updatePanel = await panelSchema(organization).updateOne(
|
|
{ _id: panelID, isArchive: false },
|
|
{ $set: { isArchive: true } }
|
|
);
|
|
const existingWidgets = await widgetSchema(organization).find({
|
|
panelID: panelID,
|
|
isArchive: false,
|
|
});
|
|
|
|
for (const widgetData of existingWidgets) {
|
|
widgetData.isArchive = true;
|
|
await widgetData.save();
|
|
}
|
|
|
|
if (existingZone.panelOrder.includes(existingPanel.panelName)) {
|
|
existingZone.panelOrder = existingZone.panelOrder.filter(
|
|
(panel: any) => panel !== existingPanel.panelName
|
|
);
|
|
|
|
await existingZone.save();
|
|
}
|
|
|
|
return res.status(200).json({ message: "Panel deleted successfully" });
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
}
|