Files
Dwinzo-Backend-V0.0/src/shared/services/visualization/widget3dService.ts

281 lines
7.3 KiB
TypeScript

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" };
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",
};
} else return { status: "3dWidget not updated" };
} 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" };
}
} 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",
};
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 { status: "Widget not updated" };
} else {
return { status: "widget not found" };
}
} 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",
};
const existing3Dwidget = await widget3dModel(organization).findOne({
widgetID: id,
isArchive: false,
zoneId: zoneId,
});
if (!existing3Dwidget) {
return { status: "3D widget not found for the ID" };
}
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 { 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",
};
const widgetData = await widget3dModel(organization).find({
zoneId: zoneId,
isArchive: false,
});
if (!widgetData || widgetData.length === 0) {
return { status: "All 3Dwidgets" };
}
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 };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};