widget create and delete
This commit is contained in:
@@ -17,6 +17,7 @@ export interface widget extends Document {
|
||||
measurements: {};
|
||||
duration: string;
|
||||
};
|
||||
zoneId:string
|
||||
}
|
||||
const widgetSchema: Schema = new Schema(
|
||||
{
|
||||
@@ -35,6 +36,7 @@ const widgetSchema: Schema = new Schema(
|
||||
fontWeight: { type: String },
|
||||
isArchive: { type: Boolean, default: false },
|
||||
panelID: { type: mongoose.Schema.Types.ObjectId, ref: "Panel" },
|
||||
zoneId:{ type: String }
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
@@ -3,99 +3,106 @@ import panelSchema from "../../../shared/model/vizualization/panelmodel.ts";
|
||||
import widgetSchema from "../../../shared/model/vizualization/widgemodel.ts";
|
||||
import zoneSchema from "../../../shared/model/builder/lines/zone-Model.ts";
|
||||
export const addWidget = async (data: any) => {
|
||||
const { organization,panel,zoneId,widget,} = data
|
||||
try {
|
||||
const existingZone = await zoneSchema(organization).findOne({
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!existingZone)
|
||||
return { success: false, message: "Zone not found", organization: organization }
|
||||
const existingPanel = await panelSchema(organization).findOne({
|
||||
panelName: widget.panel,
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
})
|
||||
if (!existingPanel)
|
||||
const { organization, panel, zoneId, widget, } = data
|
||||
try {
|
||||
const existingZone = await zoneSchema(organization).findOne({
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!existingZone)
|
||||
return { success: false, message: "Zone not found", organization: organization }
|
||||
const existingPanel = await panelSchema(organization).findOne({
|
||||
panelName: widget.panel,
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
})
|
||||
if (!existingPanel)
|
||||
|
||||
return { success: false, message: "panelName not found",organization: organization}
|
||||
return { success: false, message: "panelName not found", organization: organization }
|
||||
|
||||
|
||||
if (existingPanel.panelName === widget.panel) {
|
||||
const existingWidget = await widgetSchema(organization).findOne({
|
||||
|
||||
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,
|
||||
// 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,
|
||||
},
|
||||
{
|
||||
$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: true, message: "Widget updated successfully",data:updateWidget,organization: organization}
|
||||
isArchive: false,
|
||||
},
|
||||
},
|
||||
{ upsert: true, new: true } // Upsert: create if not exists, new: return updated document
|
||||
);
|
||||
|
||||
}
|
||||
const newWidget = await widgetSchema(organization).create({
|
||||
widgetID: widget.id,
|
||||
elementType: widget.type,
|
||||
// widgetOrder: widgetOrder,
|
||||
widgetName: widget.title,
|
||||
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();
|
||||
const widgetData={
|
||||
type:newWidget.elementType,
|
||||
id:newWidget.widgetID,
|
||||
panel:newWidget.widgetside,
|
||||
title:newWidget.widgetName,
|
||||
}
|
||||
const finaldata={ widgetData:widgetData,zoneId:existingZone.zoneId,zoneName:existingZone.zoneName}
|
||||
// console.log('existingPanel: ', widgetData);
|
||||
return { success: true, message: "Widget created successfully",data:finaldata,organization: organization}
|
||||
}
|
||||
return { success: true, message: "Widget updated successfully", data: updateWidget, organization: organization }
|
||||
|
||||
}
|
||||
const newWidget = await widgetSchema(organization).create({
|
||||
widgetID: widget.id,
|
||||
elementType: widget.type,
|
||||
// widgetOrder: widgetOrder,
|
||||
widgetName: widget.title,
|
||||
panelID: existingPanel._id,
|
||||
widgetside: widget.panel,
|
||||
zoneId: zoneId
|
||||
// Data: {
|
||||
// measurements: widget.Data.measurements || {},
|
||||
// duration: widget.Data.duration || "1hr",
|
||||
// },
|
||||
});
|
||||
if (newWidget) {
|
||||
existingPanel.widgets.push(newWidget._id);
|
||||
await existingPanel.save();
|
||||
const widgetData = {
|
||||
type: newWidget.elementType,
|
||||
id: newWidget.widgetID,
|
||||
panel: newWidget.widgetside,
|
||||
title: newWidget.widgetName,
|
||||
}
|
||||
return { success: false, message: "Type mismatch",organization: organization}
|
||||
|
||||
} catch (error: any) {
|
||||
return { success: false, message: 'widge not found', error,organization: organization }
|
||||
const finaldata = { widgetData: widgetData, zoneId: existingZone.zoneId, zoneName: existingZone.zoneName }
|
||||
// console.log('existingPanel: ', widgetData);
|
||||
return { success: true, message: "Widget created successfully", data: finaldata, 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
|
||||
const { widgetID, zoneId, organization } = data
|
||||
console.log('data: ', data);
|
||||
try {
|
||||
const existingZone = await zoneSchema(organization).findOne({
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!existingZone)
|
||||
return { success: false, message: "Zone not found", organization: organization }
|
||||
const findWidget = await widgetSchema(organization).findOne({
|
||||
widgetID: widgetID,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!findWidget)
|
||||
return { success: false, message: "Widget not found",organization: organization}
|
||||
return { success: false, message: "Widget not found", organization: organization }
|
||||
const widgetData = await widgetSchema(organization).updateOne(
|
||||
{ _id: findWidget._id, isArchive: false },
|
||||
{ $set: { isArchive: true } }
|
||||
@@ -107,6 +114,7 @@ export const Widgetdelete = async (data: any) => {
|
||||
panelID: findWidget.panelID,
|
||||
isArchive: false,
|
||||
});
|
||||
console.log('widgets: ', widgets);
|
||||
// .sort({ widgetOrder: 1 });
|
||||
|
||||
// Reassign widgetOrder values
|
||||
@@ -122,13 +130,45 @@ export const Widgetdelete = async (data: any) => {
|
||||
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}
|
||||
const panelDeletedata = await panelData.save();
|
||||
|
||||
// console.log('Widget deleted successfully: ',panelDeletedata);
|
||||
// if (panelData?.widgets.includes(findWidget._id)) {
|
||||
// panelData.widgets = panelData.widgets.filter(
|
||||
// (id: any) => id.toString() !== findWidget._id.toString()
|
||||
// );
|
||||
// await panelData.save();
|
||||
// }
|
||||
|
||||
// Fetch active widgets after deletion
|
||||
const activeWidgets = await widgetSchema(organization).find({
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
});
|
||||
|
||||
// console.log('activeWidgets: ', activeWidgets);
|
||||
|
||||
// // Format data
|
||||
const formattedWidgets = activeWidgets.map((widget) => ({
|
||||
id: widget.widgetID,
|
||||
type: widget.elementType, // Ensure this field exists
|
||||
title: widget.widgetName,
|
||||
panel: widget.widgetside,
|
||||
data: {
|
||||
duration: "1h",
|
||||
measurements: {}, // Add actual data if available
|
||||
},
|
||||
}));
|
||||
const widgetData1 = {
|
||||
widgetDeleteDatas: formattedWidgets, zoneId: zoneId, zoneName: existingZone.zoneName
|
||||
|
||||
}
|
||||
// console.log("formattedWidgets",widgetData1);
|
||||
return { success: true, message: "Widget deleted successfully", data: widgetData1, organization: organization }
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (error: any) {
|
||||
return { success: false, message: error?.message || "An unknown error occurred.", error ,organization: organization}
|
||||
return { success: false, message: error?.message || "An unknown error occurred.", error, organization: organization }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -618,7 +618,7 @@ const widgetHandleEvent = async (event: string, socket: Socket, data: any, names
|
||||
// console.log(`👀 Active sockets in room:`, namespace.adapter.rooms.get(organization));
|
||||
// console.log('emitTarget: ', emitTarget);
|
||||
if (organization) {
|
||||
socket.emit(responseEvent, {
|
||||
socket.to(organization).emit(responseEvent, {
|
||||
success: result.success,
|
||||
message: result.message,
|
||||
data: result.data,
|
||||
|
||||
Reference in New Issue
Block a user