Controller service completed for Vizualization
This commit is contained in:
@@ -0,0 +1,333 @@
|
||||
import zoneModel from "../../V1Models/Builder/zoneModel.ts";
|
||||
import widget3dModel from "../../V1Models/Vizualization/3dwidget.ts";
|
||||
import { existingUser } from "../helpers/v1projecthelperFns.ts";
|
||||
interface IResult {
|
||||
status: string;
|
||||
data?: object;
|
||||
}
|
||||
interface IWidget3DAdd {
|
||||
organization: string;
|
||||
widget: {
|
||||
id: string;
|
||||
position: [];
|
||||
type: string;
|
||||
Data: {
|
||||
measurements: {};
|
||||
duration: string;
|
||||
};
|
||||
};
|
||||
projectId: string;
|
||||
zoneId: string;
|
||||
userId: string;
|
||||
}
|
||||
interface IWidget3dUpdate {
|
||||
organization: string;
|
||||
id: string;
|
||||
projectId: string;
|
||||
zoneId: string;
|
||||
userId: string;
|
||||
}
|
||||
interface IWidgetUpdate {
|
||||
organization: string;
|
||||
id: string;
|
||||
position: [];
|
||||
rotation: [];
|
||||
projectId: string;
|
||||
zoneId: string;
|
||||
userId: string;
|
||||
}
|
||||
interface I3dWidgetGet {
|
||||
organization: string;
|
||||
projectId: string;
|
||||
zoneId: string;
|
||||
userId: string;
|
||||
}
|
||||
export const Add3DWidget = async (data: IWidget3DAdd): Promise<IResult> => {
|
||||
try {
|
||||
const { organization, widget, userId, zoneId, projectId } = data;
|
||||
const UserExists = await existingUser(userId, organization);
|
||||
if (!UserExists) return { status: "User not found" };
|
||||
const existingZone = await zoneModel(organization).findOne({
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
projectId: projectId,
|
||||
});
|
||||
if (!existingZone) return { status: "Zone not found for the zoneId" };
|
||||
// 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) {
|
||||
return {
|
||||
status: "3dwidget update successfully",
|
||||
};
|
||||
// return {
|
||||
// success: true,
|
||||
// message: "widget update successfully",
|
||||
// organization: organization,
|
||||
// };
|
||||
} else return { status: "3dWidget not updated" };
|
||||
// return {
|
||||
// success: false,
|
||||
// message: "Widget not updated",
|
||||
// organization: organization,
|
||||
// };
|
||||
} else {
|
||||
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,
|
||||
};
|
||||
|
||||
return {
|
||||
status: "Success",
|
||||
data: widgemodel3D_Datas,
|
||||
};
|
||||
}
|
||||
return { status: "Widget 3d not created" };
|
||||
// return {
|
||||
// success: true,
|
||||
// message: "Widget created successfully",
|
||||
// data: widgemodel3D_Datas,
|
||||
// organization: organization,
|
||||
// };
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
return {
|
||||
status: error.message,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: "An unexpected error occurred",
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
export const Update3Dwidget = async (data: IWidgetUpdate): Promise<IResult> => {
|
||||
try {
|
||||
const { organization, id, position, rotation, userId, zoneId, projectId } =
|
||||
data;
|
||||
const UserExists = await existingUser(userId, organization);
|
||||
if (!UserExists) return { status: "User not found" };
|
||||
const existingZone = await zoneModel(organization).findOne({
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
projectId: projectId,
|
||||
});
|
||||
if (!existingZone)
|
||||
return {
|
||||
status: "Zone not found",
|
||||
};
|
||||
// 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 {
|
||||
status: "Success",
|
||||
data: updateDatas,
|
||||
};
|
||||
// return {
|
||||
// success: true,
|
||||
// message: "widget update successfully",
|
||||
// data: updateDatas,
|
||||
// organization: organization,
|
||||
// };
|
||||
}
|
||||
return { status: "Widget not updated" };
|
||||
} else {
|
||||
return { status: "widget not found" };
|
||||
// return {
|
||||
// success: false,
|
||||
// message: "widget not found",
|
||||
// organization: organization,
|
||||
// };
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
return {
|
||||
status: error.message,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: "An unexpected error occurred",
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
export const Delete3Dwidget = async (
|
||||
data: IWidget3dUpdate
|
||||
): Promise<IResult> => {
|
||||
try {
|
||||
const { organization, id, userId, zoneId, projectId } = data;
|
||||
const UserExists = await existingUser(userId, organization);
|
||||
if (!UserExists) return { status: "User not found" };
|
||||
const existingZone = await zoneModel(organization).findOne({
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
projectId: projectId,
|
||||
});
|
||||
if (!existingZone)
|
||||
return {
|
||||
status: "Zone not found",
|
||||
};
|
||||
// return {
|
||||
// success: false,
|
||||
// message: "Zone not found",
|
||||
// organization: organization,
|
||||
// };
|
||||
const existing3Dwidget = await widget3dModel(organization).findOne({
|
||||
widgetID: id,
|
||||
isArchive: false,
|
||||
zoneId: zoneId,
|
||||
});
|
||||
if (!existing3Dwidget) {
|
||||
return { status: "3D widget not found for the ID" };
|
||||
// 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 {
|
||||
status: "Success",
|
||||
data: delete_Datas,
|
||||
};
|
||||
// return {
|
||||
// success: true,
|
||||
// data: delete_Datas,
|
||||
// message: "3DWidget delete successfull",
|
||||
// organization: organization,
|
||||
// };
|
||||
}
|
||||
return { status: "3DWidget delete unsuccessfull" };
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
return {
|
||||
status: error.message,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: "An unexpected error occurred",
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
export const Get3Dwidget = async (data: I3dWidgetGet): Promise<IResult> => {
|
||||
try {
|
||||
const { organization, userId, zoneId, projectId } = data;
|
||||
const UserExists = await existingUser(userId, organization);
|
||||
if (!UserExists) return { status: "User not found" };
|
||||
const existingZone = await zoneModel(organization).findOne({
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
projectId: projectId,
|
||||
});
|
||||
if (!existingZone)
|
||||
return {
|
||||
status: "Zone not found",
|
||||
};
|
||||
// return res.send("Zone not found");
|
||||
const widgetData = await widget3dModel(organization).find({
|
||||
zoneId: zoneId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (!widgetData || widgetData.length === 0) {
|
||||
return { status: "All 3Dwidgets" };
|
||||
// return res.json([]);
|
||||
}
|
||||
|
||||
const zonebasedWidget = widgetData.map((widget) => ({
|
||||
Data: {
|
||||
measurements: widget?.Data?.measurements || {},
|
||||
duration: widget?.Data?.duration || "1h",
|
||||
},
|
||||
type: widget.type,
|
||||
id: widget.widgetID,
|
||||
position: widget.position,
|
||||
rotation: widget?.rotation,
|
||||
}));
|
||||
return { status: "Success", data: zonebasedWidget };
|
||||
// return res.status(200).json(zonebasedWidget);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
return {
|
||||
status: error.message,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: "An unexpected error occurred",
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user