72 lines
2.2 KiB
TypeScript
72 lines
2.2 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";
|
|
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({
|
|
_id: zoneID,
|
|
isArchive: false,
|
|
});
|
|
|
|
if (!findZone) {
|
|
return res.status(404).json({ message: "Zone not found" });
|
|
}
|
|
const updatezone = await zoneSchema(organization).findOneAndUpdate(
|
|
{ _id: findZone._id, 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 {
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
}
|