198 lines
6.8 KiB
TypeScript
198 lines
6.8 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 widgetService {
|
|
static async addWidget(req: Request, res: Response): Promise<any> {
|
|
try {
|
|
console.log("req.body: ", req.body);
|
|
const {
|
|
organization,
|
|
// panel,
|
|
zoneId,
|
|
widget,
|
|
} = 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({
|
|
panelName: widget.panel,
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingPanel)
|
|
return res.status(404).json({ message: "panelName not found" });
|
|
|
|
if (existingPanel.panelName === widget.panel) {
|
|
const existingWidget = await widgetSchema(organization).findOne({
|
|
panelID: existingPanel._id,
|
|
widgetID: widget.id,
|
|
isArchive: false,
|
|
// widgetOrder: widget.widgetOrder,
|
|
});
|
|
// console.log('existingWidget: ', widget.data.measurements);
|
|
if (existingWidget) {
|
|
const updateWidget = await widgetSchema(
|
|
organization
|
|
).findOneAndUpdate(
|
|
{
|
|
panelID: existingPanel._id,
|
|
widgetID: widget.id,
|
|
isArchive: false,
|
|
},
|
|
{
|
|
$set: {
|
|
// panelID: existingPanel._id,
|
|
// widgetID: widget.id,
|
|
Data: {
|
|
measurements: widget.Data.measurements,
|
|
duration: widget.Data.duration,
|
|
},
|
|
// isArchive: false,
|
|
},
|
|
},
|
|
{ upsert: true, new: true } // Upsert: create if not exists, new: return updated document
|
|
);
|
|
return res
|
|
.status(200)
|
|
.json({ message: "Widget updated successfully" });
|
|
// return res
|
|
// .status(409)
|
|
// .json({ message: "Widget already exist for the widgetID" });
|
|
}
|
|
const newWidget = await widgetSchema(organization).create({
|
|
widgetID: widget.id,
|
|
elementType: widget.type,
|
|
// widgetOrder: widgetOrder,
|
|
widgetName: widget.widgetName,
|
|
panelID: existingPanel._id,
|
|
widgetside: widget.panel,
|
|
// Data: {
|
|
// measurements: widget.Data.measurements || {},
|
|
// duration: widget.Data.duration || "1h",
|
|
// },
|
|
});
|
|
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({
|
|
widgetID: widgetID,
|
|
isArchive: false,
|
|
});
|
|
if (!findWidget)
|
|
return res.status(409).json({ message: "Widget not found" });
|
|
const widgetData = await widgetSchema(organization).updateOne(
|
|
{ _id: findWidget._id, 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(findWidget._id)) {
|
|
const index1 = panelData.widgets.indexOf(findWidget._id);
|
|
panelData.widgets.splice(index1, 1);
|
|
}
|
|
await panelData.save();
|
|
}
|
|
return res.status(200).json({ message: "Widget deleted successfully" });
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
|
|
static async updatewidget(req: Request, res: Response): Promise<any> {
|
|
try {
|
|
const { organization, widgetID, values } = req.body;
|
|
const findwidget = await widgetSchema(organization).findOne({
|
|
widgetID: widgetID,
|
|
isArchive: false,
|
|
});
|
|
if (!findwidget)
|
|
return res.status(404).send({ message: "Data not found" });
|
|
const updateData = {
|
|
widgetName: values.widgetName,
|
|
widgetSide: values.widgetSide, // Fixed typo from widgetside to widgetSide
|
|
elementType: values.type,
|
|
Data: {
|
|
measurement: values.Data.measurement,
|
|
duration: values.Data.duration,
|
|
},
|
|
elementColor: values.color,
|
|
fontFamily: values.fontFamily,
|
|
fontStyle: values.fontStyle,
|
|
fontWeight: values.fontWeight,
|
|
isArchive: false,
|
|
};
|
|
|
|
const changedWidget = await widgetSchema(organization).findOneAndUpdate(
|
|
{ widgetID: widgetID, isArchive: false },
|
|
updateData,
|
|
{
|
|
new: true,
|
|
upsert: true,
|
|
setDefaultsOnInsert: true,
|
|
}
|
|
);
|
|
|
|
return res.status(200).json({
|
|
message: "Widget updated 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({
|
|
widgetID: widgetID,
|
|
isArchive: false,
|
|
})
|
|
.select("Data -_id");
|
|
const Datastructure = {
|
|
measurements: existingWidget.Data.measurements || {},
|
|
duration: existingWidget.Data.duration || "1h",
|
|
};
|
|
|
|
if (existingWidget) return res.status(200).json({ Data: Datastructure });
|
|
} catch (error: any) {
|
|
return res.status(500).send(error.message);
|
|
}
|
|
}
|
|
}
|