vizualization updates
This commit is contained in:
127
src/api-server/controller/visualization/widgetService.ts
Normal file
127
src/api-server/controller/visualization/widgetService.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { Request, Response } from "express";
|
||||
import panelSchema from "../../../shared/model/vizualization/panelmodel.ts";
|
||||
import widgetSchema from "../../../shared/model/vizualization/widgemodel.ts";
|
||||
export class widgetService {
|
||||
static async addWidget(req: Request, res: Response): Promise<any> {
|
||||
try {
|
||||
const {
|
||||
organization,
|
||||
panelID,
|
||||
widgetName,
|
||||
widgetOrder,
|
||||
type,
|
||||
widgetID,
|
||||
panel,
|
||||
Data,
|
||||
} = req.body;
|
||||
const existingPanel = await panelSchema(organization).findOne({
|
||||
_id: panelID,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!existingPanel)
|
||||
return res.status(404).json({ message: "PanelID not found" });
|
||||
|
||||
if (existingPanel.panelName === panel) {
|
||||
const existingWidget = await widgetSchema(organization).findOne({
|
||||
panelID: panelID,
|
||||
widgetID: widgetID,
|
||||
isArchive: false,
|
||||
widgetOrder: widgetOrder,
|
||||
});
|
||||
if (existingWidget)
|
||||
return res
|
||||
.status(409)
|
||||
.json({ message: "Widget already exist for the widgetID" });
|
||||
const newWidget = await widgetSchema(organization).create({
|
||||
widgetID: widgetID,
|
||||
elementType: type,
|
||||
widgetOrder: widgetOrder,
|
||||
widgetName: widgetName,
|
||||
panelID: panelID,
|
||||
widgetside: panel,
|
||||
Data: {
|
||||
measurements: Data.measurements || {},
|
||||
duration: Data.duration || "1hr",
|
||||
},
|
||||
});
|
||||
if (newWidget) {
|
||||
existingPanel.widgets.push(newWidget._id);
|
||||
await existingPanel.save();
|
||||
return res.status(201).json({
|
||||
message: "Widget created successfully",
|
||||
widgetID: newWidget._id,
|
||||
});
|
||||
}
|
||||
}
|
||||
return res.json({ message: "Type mismatch" });
|
||||
} catch (error: any) {
|
||||
return res.status(500).send(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
static async deleteWidget(req: Request, res: Response): Promise<any> {
|
||||
try {
|
||||
const { widgetID, organization } = req.body;
|
||||
const findWidget = await widgetSchema(organization).findOne({
|
||||
_id: widgetID,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!findWidget)
|
||||
return res.status(409).json({ message: "Widget already deleted" });
|
||||
const widgetData = await widgetSchema(organization).updateOne(
|
||||
{ _id: widgetID, isArchive: false },
|
||||
{ $set: { isArchive: true } }
|
||||
);
|
||||
|
||||
if (widgetData) {
|
||||
// Find all widgets in the same panel and sort them by widgetOrder
|
||||
const widgets = await widgetSchema(organization)
|
||||
.find({
|
||||
panelID: findWidget.panelID,
|
||||
isArchive: false,
|
||||
})
|
||||
.sort({ widgetOrder: 1 });
|
||||
|
||||
// Reassign widgetOrder values
|
||||
for (let i = 0; i < widgets.length; i++) {
|
||||
widgets[i].widgetOrder = (i + 1).toString(); // Convert to string
|
||||
await widgets[i].save();
|
||||
}
|
||||
const panelData = await panelSchema(organization).findOne({
|
||||
_id: findWidget.panelID,
|
||||
isArchive: false,
|
||||
});
|
||||
|
||||
if (panelData.widgets.includes(widgetID)) {
|
||||
panelData.widgets = panelData.widgets.filter(
|
||||
(widget: any) => widget !== findWidget._id
|
||||
);
|
||||
}
|
||||
await panelData.save();
|
||||
}
|
||||
return res.status(200).json({ message: "Widget deleted successfully" });
|
||||
} catch (error: any) {
|
||||
return res.status(500).send(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
static async getDatafromWidget(req: Request, res: Response): Promise<any> {
|
||||
const { organization, widgetID } = req.params;
|
||||
try {
|
||||
const existingWidget = await widgetSchema(organization)
|
||||
.findOne({
|
||||
_id: widgetID,
|
||||
isArchive: false,
|
||||
})
|
||||
.select("Data -_id");
|
||||
const Datastructure = {
|
||||
measurements: existingWidget.Data.measurements || {},
|
||||
duration: existingWidget.Data.duration || "1hr",
|
||||
};
|
||||
|
||||
if (existingWidget) return res.status(200).json({ Data: Datastructure });
|
||||
} catch (error: any) {
|
||||
return res.status(500).send(error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user