211 lines
5.4 KiB
TypeScript
211 lines
5.4 KiB
TypeScript
import zoneSchema from "../../../shared/model/builder/lines/zone-Model.ts";
|
|
import widget3dModel from "../../../shared/model/vizualization/3dwidget.ts";
|
|
export const add3Dwidget = async (data: any,callback:any) => {
|
|
const { organization, widget, zoneId } = data;
|
|
|
|
try {
|
|
const existingZone = await zoneSchema(organization).findOne({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingZone)
|
|
return {
|
|
success: false,
|
|
message: "Zone not found for the zoneId",
|
|
organization: organization,
|
|
};
|
|
const existing3Dwidget = await widget3dModel(organization).findOne({
|
|
widgetID: widget.id,
|
|
isArchive: false,
|
|
});
|
|
if (existing3Dwidget) {
|
|
const update3dwidget = await widget3dModel(organization).findOneAndUpdate(
|
|
{
|
|
widgetID: widget.id,
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
},
|
|
{ position: widget.position },
|
|
{ upsert: true, new: true }
|
|
);
|
|
if (update3dwidget){
|
|
if (callback !== undefined) {
|
|
callback({
|
|
success: true,
|
|
message: "widget update successfully",
|
|
})
|
|
}
|
|
return {
|
|
success: true,
|
|
message: "widget update successfully",
|
|
organization: organization,
|
|
};
|
|
|
|
}
|
|
else
|
|
return {
|
|
success: false,
|
|
message: "Widget not updated",
|
|
organization: organization,
|
|
};
|
|
}
|
|
const newWidget3d = await widget3dModel(organization).create({
|
|
type: widget.type,
|
|
widgetID: widget.id,
|
|
position: widget.position,
|
|
zoneId,
|
|
Data: {
|
|
measurements: widget?.Data?.measurements || {},
|
|
duration: widget?.Data?.duration || "1h",
|
|
},
|
|
});
|
|
if (newWidget3d) {
|
|
const widgemodel3D_Datas = {
|
|
widget: {
|
|
id: newWidget3d.widgetID,
|
|
type: newWidget3d.type,
|
|
position: newWidget3d.position,
|
|
},
|
|
Data: newWidget3d.Data,
|
|
zoneId: zoneId,
|
|
};
|
|
if (callback !== undefined) {
|
|
callback({
|
|
success: true,
|
|
message: "Widget created successfully",
|
|
})}
|
|
return {
|
|
success: true,
|
|
message: "Widget created successfully",
|
|
data: widgemodel3D_Datas,
|
|
organization: organization,
|
|
};
|
|
}
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error?.message || "Error occurred while 3Dwidget",
|
|
error,
|
|
organization: organization,
|
|
};
|
|
}
|
|
};
|
|
export const update3D = async (data: any) => {
|
|
const { organization, id, position, rotation, zoneId } = data;
|
|
try {
|
|
const existingZone = await zoneSchema(organization).findOne({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingZone)
|
|
return {
|
|
success: false,
|
|
message: "Zone not found",
|
|
organization: organization,
|
|
};
|
|
const existing3Dwidget = await widget3dModel(organization).findOne({
|
|
widgetID: id,
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (existing3Dwidget) {
|
|
const update3dwidget = await widget3dModel(organization).findOneAndUpdate(
|
|
{
|
|
widgetID: id,
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
},
|
|
{ position: position, rotation: rotation },
|
|
{ upsert: true, new: true }
|
|
);
|
|
if (update3dwidget) {
|
|
const updateDatas = {
|
|
widget: {
|
|
id: update3dwidget.widgetID,
|
|
type: update3dwidget.type,
|
|
position: update3dwidget.position,
|
|
rotation: update3dwidget.rotation,
|
|
},
|
|
zoneId: zoneId,
|
|
};
|
|
return {
|
|
success: true,
|
|
message: "widget update successfully",
|
|
data: updateDatas,
|
|
organization: organization,
|
|
};
|
|
}
|
|
} else {
|
|
return {
|
|
success: false,
|
|
message: "widget not found",
|
|
|
|
organization: organization,
|
|
};
|
|
}
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error?.message || "Error occurred while 3Dwidget",
|
|
error,
|
|
organization: organization,
|
|
};
|
|
}
|
|
};
|
|
export const delete3Dwidget = async (data: any) => {
|
|
const { organization, id, zoneId } = data;
|
|
try {
|
|
const existingZone = await zoneSchema(organization).findOne({
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingZone)
|
|
return {
|
|
success: false,
|
|
message: "Zone not found",
|
|
organization: organization,
|
|
};
|
|
const existing3Dwidget = await widget3dModel(organization).findOne({
|
|
widgetID: id,
|
|
isArchive: false,
|
|
zoneId: zoneId,
|
|
});
|
|
if (!existing3Dwidget) {
|
|
return {
|
|
success: false,
|
|
message: "3D widget not found for the ID",
|
|
organization: organization,
|
|
};
|
|
}
|
|
const updateWidget = await widget3dModel(organization).findOneAndUpdate(
|
|
{
|
|
widgetID: id,
|
|
zoneId: zoneId,
|
|
isArchive: false,
|
|
},
|
|
{ isArchive: true },
|
|
{ new: true }
|
|
);
|
|
if (updateWidget) {
|
|
const delete_Datas = {
|
|
zoneId: zoneId,
|
|
id: existing3Dwidget.widgetID,
|
|
};
|
|
|
|
return {
|
|
success: true,
|
|
data: delete_Datas,
|
|
message: "3DWidget delete successfull",
|
|
organization: organization,
|
|
};
|
|
}
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error?.message || "Error occurred while 3Dwidget",
|
|
error,
|
|
organization: organization,
|
|
};
|
|
}
|
|
};
|