119 lines
4.4 KiB
TypeScript
119 lines
4.4 KiB
TypeScript
import panelSchema from "../../../shared/model/vizualization/panelmodel.ts";
|
|
import widgetSchema from "../../../shared/model/vizualization/widgemodel.ts";
|
|
export const addWidget = async (data: any) => {
|
|
const { organization,panel,zoneId,widget,} = data
|
|
console.log('data: ', data);
|
|
try {
|
|
const existingPanel = await panelSchema(organization).findOne({
|
|
panelName: widget.panel,
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingPanel)
|
|
|
|
return { success: false, message: "panelName not found",organization: organization}
|
|
|
|
|
|
if (existingPanel.panelName === widget.panel) {
|
|
const existingWidget = await widgetSchema(organization).findOne({
|
|
panelID: existingPanel._id,
|
|
widgetID: widget.id,
|
|
isArchive: false,
|
|
// widgetOrder: widget.widgetOrder,
|
|
});
|
|
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 { success: false, message: "Widget updated successfully",data:updateWidget,organization: organization}
|
|
|
|
}
|
|
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 || "1hr",
|
|
// },
|
|
});
|
|
if (newWidget) {
|
|
existingPanel.widgets.push(newWidget._id);
|
|
await existingPanel.save();
|
|
return { success: true, message: "Widget already exist for the widgetID",data:existingPanel,organization: organization}
|
|
}
|
|
}
|
|
return { success: false, message: "Type mismatch",organization: organization}
|
|
|
|
} catch (error: any) {
|
|
return { success: false, message: 'widge not found', error,organization: organization }
|
|
}
|
|
}
|
|
export const Widgetdelete = async (data: any) => {
|
|
const { widgetID, organization } = data
|
|
console.log('data: ', data);
|
|
try {
|
|
const findWidget = await widgetSchema(organization).findOne({
|
|
widgetID: widgetID,
|
|
isArchive: false,
|
|
});
|
|
if (!findWidget)
|
|
return { success: false, message: "Widget not found",organization: organization}
|
|
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);
|
|
}
|
|
const panelDeletedata = await panelData.save();
|
|
console.log('Widget deleted successfully: ');
|
|
return { success: false, message: "Widget deleted successfully",data:panelDeletedata,organization: organization}
|
|
|
|
}
|
|
|
|
} catch (error: any) {
|
|
return { success: false, message: error?.message || "An unknown error occurred.", error ,organization: organization}
|
|
}
|
|
}
|