V2 API for visualization gomathi part completed
This commit is contained in:
@@ -6,50 +6,48 @@ export class widgetService {
|
||||
try {
|
||||
const {
|
||||
organization,
|
||||
panelID,
|
||||
widgetName,
|
||||
widgetOrder,
|
||||
type,
|
||||
widgetID,
|
||||
panel,
|
||||
Data,
|
||||
// panel,
|
||||
zoneId,
|
||||
widget,
|
||||
} = req.body;
|
||||
const existingPanel = await panelSchema(organization).findOne({
|
||||
_id: panelID,
|
||||
panelName: widget.panel,
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!existingPanel)
|
||||
return res.status(404).json({ message: "PanelID not found" });
|
||||
return res.status(404).json({ message: "panelName not found" });
|
||||
|
||||
if (existingPanel.panelName === panel) {
|
||||
if (existingPanel.panelName === widget.panel) {
|
||||
const existingWidget = await widgetSchema(organization).findOne({
|
||||
panelID: panelID,
|
||||
widgetID: widgetID,
|
||||
panelID: existingPanel._id,
|
||||
widgetID: widget.id,
|
||||
isArchive: false,
|
||||
widgetOrder: widgetOrder,
|
||||
// widgetOrder: widget.widgetOrder,
|
||||
});
|
||||
if (existingWidget)
|
||||
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",
|
||||
},
|
||||
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 || "1hr",
|
||||
// },
|
||||
});
|
||||
if (newWidget) {
|
||||
existingPanel.widgets.push(newWidget._id);
|
||||
await existingPanel.save();
|
||||
return res.status(201).json({
|
||||
message: "Widget created successfully",
|
||||
widgetID: newWidget._id,
|
||||
// widgetID: newWidget._id,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -63,13 +61,13 @@ export class widgetService {
|
||||
try {
|
||||
const { widgetID, organization } = req.body;
|
||||
const findWidget = await widgetSchema(organization).findOne({
|
||||
_id: widgetID,
|
||||
widgetID: widgetID,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!findWidget)
|
||||
return res.status(409).json({ message: "Widget already deleted" });
|
||||
const widgetData = await widgetSchema(organization).updateOne(
|
||||
{ _id: widgetID, isArchive: false },
|
||||
{ _id: findWidget._id, isArchive: false },
|
||||
{ $set: { isArchive: true } }
|
||||
);
|
||||
|
||||
@@ -92,7 +90,7 @@ export class widgetService {
|
||||
isArchive: false,
|
||||
});
|
||||
|
||||
if (panelData.widgets.includes(widgetID)) {
|
||||
if (panelData.widgets.includes(findWidget._id)) {
|
||||
panelData.widgets = panelData.widgets.filter(
|
||||
(widget: any) => widget !== findWidget._id
|
||||
);
|
||||
@@ -105,6 +103,48 @@ export class widgetService {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user