Visualization Part 2d and floating API Completed. Template Save and Get API completed
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
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,
|
||||
@@ -25,10 +33,35 @@ export class widgetService {
|
||||
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(409)
|
||||
.json({ message: "Widget already exist for the widgetID" });
|
||||
.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,
|
||||
@@ -39,7 +72,7 @@ export class widgetService {
|
||||
widgetside: widget.panel,
|
||||
// Data: {
|
||||
// measurements: widget.Data.measurements || {},
|
||||
// duration: widget.Data.duration || "1hr",
|
||||
// duration: widget.Data.duration || "1h",
|
||||
// },
|
||||
});
|
||||
if (newWidget) {
|
||||
@@ -65,7 +98,7 @@ export class widgetService {
|
||||
isArchive: false,
|
||||
});
|
||||
if (!findWidget)
|
||||
return res.status(409).json({ message: "Widget already deleted" });
|
||||
return res.status(409).json({ message: "Widget not found" });
|
||||
const widgetData = await widgetSchema(organization).updateOne(
|
||||
{ _id: findWidget._id, isArchive: false },
|
||||
{ $set: { isArchive: true } }
|
||||
@@ -73,27 +106,24 @@ export class widgetService {
|
||||
|
||||
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 });
|
||||
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();
|
||||
}
|
||||
// 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)) {
|
||||
panelData.widgets = panelData.widgets.filter(
|
||||
(widget: any) => widget !== findWidget._id
|
||||
);
|
||||
const index1 = panelData.widgets.indexOf(findWidget._id);
|
||||
panelData.widgets.splice(index1, 1);
|
||||
}
|
||||
await panelData.save();
|
||||
}
|
||||
@@ -150,13 +180,13 @@ export class widgetService {
|
||||
try {
|
||||
const existingWidget = await widgetSchema(organization)
|
||||
.findOne({
|
||||
_id: widgetID,
|
||||
widgetID: widgetID,
|
||||
isArchive: false,
|
||||
})
|
||||
.select("Data -_id");
|
||||
const Datastructure = {
|
||||
measurements: existingWidget.Data.measurements || {},
|
||||
duration: existingWidget.Data.duration || "1hr",
|
||||
duration: existingWidget.Data.duration || "1h",
|
||||
};
|
||||
|
||||
if (existingWidget) return res.status(200).json({ Data: Datastructure });
|
||||
|
||||
Reference in New Issue
Block a user