Merge branch 'branch-v2' into branch-1

This commit is contained in:
2025-05-29 15:50:38 +05:30
61 changed files with 7918 additions and 337 deletions

View File

@@ -0,0 +1,119 @@
import environmentModel from "../../V1Models/Environment/environments-Model.ts";
import {
existingProjectById,
existingUser,
} from "../helpers/v1projecthelperFns.ts";
interface EnvironmentInput {
roofVisibility: boolean;
wallVisibility: boolean;
shadowVisibility: boolean;
renderDistance: number;
limitDistance: boolean;
organization: string;
projectId: string;
userId: string;
}
interface GetEnvironmentInput {
organization: string;
projectId: string;
userId: string;
}
export const setEnvironment = async (
data: EnvironmentInput
): Promise<{ status: string; data?: Object }> => {
try {
const {
roofVisibility,
wallVisibility,
renderDistance,
limitDistance,
shadowVisibility,
organization,
projectId,
userId,
} = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findvalue = await environmentModel(organization).findOne({
userId: userId,
isArchive: false,
});
if (findvalue) {
const updatevalue = await environmentModel(organization).findOneAndUpdate(
{ userId: userId, projectId: projectId, isArchive: false },
{
roofVisibility: roofVisibility,
wallVisibility: wallVisibility,
shadowVisibility: shadowVisibility,
renderDistance: renderDistance,
limitDistance: limitDistance,
},
{ new: true }
);
return { status: "environments updated", data: updatevalue };
} else {
const newValue = await environmentModel(organization).create({
userId,
projectId,
roofVisibility,
wallVisibility,
shadowVisibility,
});
return { status: "Success", data: newValue };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const getEnvironment = async (
data: GetEnvironmentInput
): Promise<{ status: string; data?: Object }> => {
try {
const { organization, projectId, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findValue = await environmentModel(organization).findOne({
userId: userId,
projectId: projectId,
isArchive: false,
});
if (!findValue) {
return { status: "Environment Not found for the User" };
} else {
return { status: "Success", data: findValue };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};

View File

@@ -0,0 +1,539 @@
import { Mixed } from "mongoose";
import assetModel from "../../V1Models/Builder/assetModel.ts";
import EventsDataModel from "../../V1Models/Simulation/eventsDataModel.ts";
import {
existingProjectById,
existingUser,
} from "../helpers/v1projecthelperFns.ts";
interface setAssetInput {
modelUuid: string;
modelName: string;
position: []; // user ID
rotation: object;
eventData: Mixed;
modelfileID: string;
isLocked: boolean;
isVisible: boolean;
organization: string;
projectId: string;
userId: string;
}
interface AssetUpdate {
modelUuid: string;
modelName: string;
position: []; // user ID
rotation: object;
isLocked: boolean;
isVisible: boolean;
organization: string;
projectId: string;
userId: string;
}
interface DelAssetInput {
modelUuid: string;
modelName: string;
organization: string;
projectId: string;
userId: string;
}
interface GetAssetInput {
organization: string;
projectId: string;
userId: string;
}
interface ReplaceEventInput {
organization: string;
projectId: string;
userId: string;
eventData: Mixed;
modelUuid: string;
}
export const setAssetModel = async (
data: setAssetInput
): Promise<{ status: string; data?: Object }> => {
try {
const {
modelUuid,
modelName,
position,
rotation,
eventData,
modelfileID,
isLocked,
isVisible,
organization,
projectId,
userId,
} = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findvalue = await assetModel(organization).findOne({
modelUuid: modelUuid,
projectId: projectId,
userId: userId,
isArchive: false,
});
if (findvalue) {
const updatevalue = await assetModel(organization).findOneAndUpdate(
{
modelUuid: modelUuid,
projectId: projectId,
userId: userId,
isArchive: false,
},
{
modelName: modelName,
position: position,
rotation: rotation,
isVisible: isVisible,
isLocked: isLocked,
eventData: eventData,
},
{ new: true }
);
// return {
// success: true,
// message: "Model updated successfully",
// data: updatevalue,
// organization: organization,
// };
return {
status: "Updated successfully",
data: updatevalue,
};
} else {
let assetData: any = {
projectId,
userId,
modelUuid,
modelName,
position,
modelfileID,
rotation,
isLocked,
isVisible,
};
// if (eventData) {
// if (eventData?.type === "Conveyor") {
// assetData.eventData = {
// type: eventData.type,
// // point:undefined,
// points: eventData.points,
// };
// } else {
// assetData.eventData = {
// type: eventData.type,
// point: eventData.point,
// // points: undefined
// };
// }
// }
if (eventData) {
const typedEventData = eventData as unknown as {
type: string;
point?: any;
points?: any[];
};
if (typedEventData.type === "Conveyor") {
assetData.eventData = {
type: typedEventData.type,
points: typedEventData.points,
};
} else {
assetData.eventData = {
type: typedEventData.type,
point: typedEventData.point,
};
}
}
if (eventData) {
const typedEventData = eventData as unknown as {
type: string;
point?: any;
points?: any[];
};
if (typedEventData.type === "Conveyor") {
assetData.eventData = {
type: typedEventData.type,
points: typedEventData.points,
};
} else {
assetData.eventData = {
type: typedEventData.type,
point: typedEventData.point,
};
}
}
const assetDoc = await assetModel(organization).create(assetData);
await assetDoc.save();
let assetDatas;
const typedEventData = eventData as unknown as { type: string };
if (typedEventData && typedEventData.type === "Conveyor") {
assetDatas = {
projectId: assetDoc.projectId,
userId: assetDoc.userId,
modelUuid: assetDoc.modelUuid,
modelName: assetDoc.modelName,
modelfileID: assetDoc.modelfileID,
position: assetDoc.position,
rotation: assetDoc.rotation,
isLocked: assetDoc.isLocked,
isVisible: assetDoc.isVisible,
eventData: eventData,
};
} else if (eventData && assetDoc.type === "Vehicle") {
assetDatas = {
projectId: assetDoc.projectId,
userId: assetDoc.userId,
modelUuid: assetDoc.modelUuid,
modelName: assetDoc.modelName,
modelfileID: assetDoc.modelfileID,
position: assetDoc.position,
rotation: assetDoc.rotation,
isLocked: assetDoc.isLocked,
isVisible: assetDoc.isVisible,
eventData: {
points: assetDoc.points,
type: assetDoc.type,
},
};
} else if (eventData && assetDoc.type === "ArmBot") {
assetDatas = {
projectId: assetDoc.projectId,
userId: assetDoc.userId,
modelUuid: assetDoc.modelUuid,
modelName: assetDoc.modelName,
modelfileID: assetDoc.modelfileID,
position: assetDoc.position,
rotation: assetDoc.rotation,
isLocked: assetDoc.isLocked,
isVisible: assetDoc.isVisible,
eventData: {
points: assetDoc.points,
type: assetDoc.type,
},
};
} else if (eventData && assetDoc.type === "StaticMachine") {
assetDatas = {
projectId: assetDoc.projectId,
userId: assetDoc.userId,
modelUuid: assetDoc.modelUuid,
modelName: assetDoc.modelName,
modelfileID: assetDoc.modelfileID,
position: assetDoc.position,
rotation: assetDoc.rotation,
isLocked: assetDoc.isLocked,
isVisible: assetDoc.isVisible,
eventData: {
points: assetDoc.points,
type: assetDoc.type,
},
};
} else {
assetDatas = {
projectId: assetDoc.projectId,
userId: assetDoc.userId,
modelUuid: assetDoc.modelUuid,
modelName: assetDoc.modelName,
modelfileID: assetDoc.modelfileID,
position: assetDoc.position,
rotation: assetDoc.rotation,
isLocked: assetDoc.isLocked,
isVisible: assetDoc.isVisible,
};
}
// return {
// success: true,
// message: "Model created successfully",
// data: assetDatas,
// organization: organization,
// };
return {
status: "Success",
data: assetDatas,
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const deleteAssetModel = async (
data: DelAssetInput
): Promise<{ status: string; data?: Object }> => {
try {
const { modelUuid, modelName, organization, projectId, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const asset = await assetModel(organization).findOne({
modelUuid,
modelName,
projectId,
isArchive: false,
});
if (!asset) {
return {
status: "model not found",
};
}
const archivedAsset = await assetModel(organization).findOneAndUpdate(
{ modelUuid, modelName, projectId },
{ $set: { isArchive: true } },
{ new: true }
);
if (!archivedAsset) {
// return {
// success: false,
// status: "Failed to archive asset",
// organization: organization,
// };
return {
status: "Failed to archive asset",
};
}
const updatedEvents = await EventsDataModel(organization).updateMany(
{ modelUuid, productId: projectId },
{ $set: { isArchive: true } }
);
// return {
// success: true,
// message: "Model deleted successfully",
// data: archivedAsset,
// organization: organization,
// };
return {
status: "Success",
data: archivedAsset,
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const replaceEventDatas = async (
data: ReplaceEventInput
): Promise<{ status: string; data?: Object }> => {
try {
const { modelUuid, organization, eventData, projectId, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const existingModel = await assetModel(organization).findOne({
modelUuid: modelUuid,
projectId: projectId,
isArchive: false,
});
if (!existingModel) {
return { status: "Model not for this UUID" };
// return {
// success: false,
// message: "Model not for this UUID",
// organization: organization,
// };
} else {
const typedEventData = eventData as unknown as {
speed: number;
points?: any[];
type?: string;
};
let speed;
if (existingModel.type === "Conveyor") {
speed = typedEventData?.speed;
}
const updatedModel = await assetModel(organization).findOneAndUpdate(
{ modelUuid, projectId, isArchive: false },
{
points: typedEventData?.points,
// speed: speed,
type: typedEventData?.type || existingModel?.type,
},
{ new: true }
);
// if (updatedModel)
// // return {
// // success: true,
// // message: "Data updated successfully",
// // data: updatedModel,
// // organization: organization,
// // };
// return {
// status: "Success",
// data: updatedModel,
// };
return {
status: "Success",
data: updatedModel,
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const updateAssetPositionRotation = async (
data: AssetUpdate
): Promise<{ status: string; data?: Object }> => {
try {
const {
modelUuid,
modelName,
position,
rotation,
isLocked,
isVisible,
organization,
projectId,
userId,
} = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const existingAsset = await assetModel(organization).findOne({
modelUuid: modelUuid,
projectId: projectId,
isArchive: false,
});
if (!existingAsset) {
return { status: "Asset not found" };
// return res.send("Asset not found");
}
const updateAsset = await assetModel(organization).updateMany(
{
modelUuid: modelUuid,
projectId: projectId,
modelName: modelName,
isArchive: false,
},
{
position: position,
rotation: rotation,
isVisible: isVisible,
isLocked: isLocked,
}
);
// if (updateAsset)
return {
status: "Success",
data: updateAsset,
};
// return res.status(200).json({ message: "Asset updated successfully" });
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const getFloorItems = async (
data: GetAssetInput
): Promise<{ status: string; data?: Object }> => {
try {
const { organization, projectId, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findValues = await assetModel(organization)
.find({ isArchive: false })
.select("-_id -isArchive");
if (!findValues || findValues.length === 0) {
return { status: "floorItems not found" };
// return res.status(200).json({ message: "floorItems not found" });
}
const response = findValues.map((item) => {
const responseItem: any = {
projectId: item.productId,
modelUuid: item.modelUuid,
modelName: item.modelName,
position: item.position,
rotation: item.rotation,
modelfileID: item.modelfileID,
isLocked: item.isLocked,
isVisible: item.isVisible,
eventData: item.eventData,
};
return responseItem;
});
// return res.status(200).json(response);
return {
status: "Success",
data: response,
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};

View File

@@ -1,9 +1,11 @@
import UsersDataModel from "../../V1Models/Auth/user.ts";
import cameraModel from "../../V1Models/Builder/cameraModel.ts";
import { existingProjectById } from "../helpers/v1projecthelperFns.ts";
import {
existingProjectById,
existingUser,
} from "../helpers/v1projecthelperFns.ts";
interface IcameraData {
userId: string;
role: string;
position: Object;
target: Object;
rotation: Object;
@@ -13,8 +15,12 @@ interface IcameraData {
}
interface IgetCameras {
organization: string;
userId?: string;
role: string;
projectId: string;
userId: string;
}
interface IOnline {
organization: string;
userId: string;
}
export const SetCamera = async (
data: IcameraData
@@ -22,7 +28,6 @@ export const SetCamera = async (
try {
const {
userId,
role,
position,
target,
rotation,
@@ -30,6 +35,8 @@ export const SetCamera = async (
projectId,
versionId,
} = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
@@ -38,6 +45,7 @@ export const SetCamera = async (
if (!LivingProject) return { status: "Project not found" };
const existingCamera = await cameraModel(organization).findOne({
userId: userId,
isArchive: false,
});
if (existingCamera) {
const updateCamera = await cameraModel(organization).findOneAndUpdate(
@@ -84,10 +92,14 @@ export const SetCamera = async (
export const GetCamers = async (
data: IgetCameras
): Promise<{ status: string; data?: Object }> => {
const { userId, organization, role } = data;
const { userId, organization, projectId } = data;
try {
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const findCamera = await cameraModel(organization).findOne({
userId: userId,
projectId: projectId,
isArchive: false,
});
if (!findCamera) {
return { status: "Camera not found" };
@@ -107,17 +119,20 @@ export const GetCamers = async (
}
};
export const onlineActiveDatas = async (
data: IgetCameras
data: IOnline
): Promise<{ status: string; data?: Object }> => {
const { organization } = data;
const { organization, userId } = data;
try {
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const findactiveUsers = await UsersDataModel(organization).find({
activeStatus: "online",
isArchive: false,
});
const cameraDataPromises = findactiveUsers.map(async (activeUser: any) => {
const cameraData = await cameraModel(organization)
.findOne({ userId: activeUser._id })
.findOne({ userId: activeUser._id, isArchive: false })
.select("position target rotation -_id");
if (cameraData) {
@@ -137,7 +152,7 @@ export const onlineActiveDatas = async (
});
const cameraDatas = (await Promise.all(cameraDataPromises)).filter(
(singledata: any) => singledata !== null
(singledata: unknown) => singledata !== null
);
return { status: "Success", data: cameraDatas };

View File

@@ -1,4 +1,5 @@
import lineModel from "../../V1Models/Builder/linesModel.ts";
import { existingProjectById, existingUser } from "../helpers/v1projecthelperFns.ts";
interface ILineItems {
organization: string;
layer: number;
@@ -7,7 +8,11 @@ interface ILineItems {
projectId: string;
userId: string;
}
interface ILineGet {
organization: string;
projectId: string;
userId: string;
}
interface ILineUpdate {
organization: string;
uuid: number;
@@ -38,6 +43,14 @@ export const CreateLineItems = async (
): Promise<{ status: string; data?: Object }> => {
try {
const { organization, line, type, layer, projectId, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const newLine = await lineModel(organization).create({
layer,
line,
@@ -62,19 +75,22 @@ export const UpdateLineItems = async (
): Promise<{ status: string; data?: Object }> => {
try {
const { organization, projectId, uuid, position, userId } = data;
const updateResult = await lineModel(organization).updateMany(
{ "line.uuid": uuid, projectId: projectId }, // Filter: Find the line with the given uuid
{ $set: { "line.$.position": position } } // Update the position and type
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
// return {
// success: true,
// status: "line updated",
// data: { uuid: uuid, position: position },
// organization: organization,
// };
if (!LivingProject) return { status: "Project not found" };
const updateResult= await lineModel(organization).updateMany(
{ "line.uuid": uuid, projectId: projectId },
{ $set: { "line.$.position": position } }
);
return {
status: "Success",
data: { uuid: uuid, position: position },
data: updateResult,
};
} catch (error: unknown) {
if (error instanceof Error) {
@@ -93,32 +109,32 @@ export const DeleteLineItems = async (
): Promise<{ status: string; data?: object }> => {
try {
const { organization, projectId, line, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const inputUuids = line.map((item: any) => item.uuid);
const findValue = await lineModel(organization).findOneAndDelete({
"line.uuid": { $all: inputUuids }, // Ensure all UUIDs are present in the `line` key
});
const findValue = await lineModel(organization).findOneAndDelete(
{ projectId: projectId, isArchive: false },
{
"line.uuid": { $all: inputUuids },
}
);
if (!findValue) {
return {
status: "line not found",
};
// return {
// success: false,
// message: "line not found",
// organization: organization,
// };
} else {
return {
status: "Success",
data: findValue,
};
// return {
// success: true,
// message: "line deleted",
// data: findValue,
// organization: organization,
// };
}
} catch (error: unknown) {
if (error instanceof Error) {
@@ -137,16 +153,27 @@ export const DeleteLayer = async (
): Promise<{ status: string; data?: object }> => {
try {
const { organization, projectId, layer, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findValue = await lineModel(organization).find({
layer: layer,
projectId: projectId,
isArchive: false,
});
if (!findValue) {
return { status: "layer not found" };
// return { success: false, message: "layer not found" };
} else {
await lineModel(organization).deleteMany({ layer: layer });
await lineModel(organization).deleteMany(
{ projectId: projectId },
{ layer: layer }
);
const updateResult = await lineModel(organization).updateMany(
{ layer: { $gt: layer } },
@@ -156,12 +183,6 @@ export const DeleteLayer = async (
status: "Success",
data: updateResult,
};
// return {
// success: true,
// message: "layer deleted",
// data: layer,
// organization: organization,
// };
}
} catch (error: unknown) {
if (error instanceof Error) {
@@ -176,38 +197,68 @@ export const DeleteLayer = async (
}
};
// export const DeleteLinePoints = async (
// data: ILinePointsDelete
// ): Promise<{ status: string; data?: object }> => {
// try {
// const { organization, projectId, uuid, userId } = data;
// const findValue = await lineModel(organization).deleteMany({
// "line.uuid": uuid,
// });
export const GetLinesService = async (
data: ILineGet
): Promise<{ status: string; data?: object }> => {
try {
const { organization, projectId, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const findValue = await lineModel(organization).find({
projectId: projectId,
isArchive: false,
});
if (!findValue) {
return { status: "user not found" };
} else {
return { status: "Success", data: findValue };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const DeleteLinePoints = async (
data: ILinePointsDelete
): Promise<{ status: string; data?: object }> => {
try {
const { organization, projectId, uuid, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findValue = await lineModel(organization).deleteMany(
{ projectId: projectId, isArchive: false },
{
"line.uuid": uuid,
}
);
// // if (!findValue) {
// // return {
// // success: false,
// // message: "line not found",
// // organization: organization,
// // };
// // } else {
// // return {
// // success: true,
// // message: "point deleted",
// // data: uuid,
// // organization: organization,
// // };
// // }
// } catch (error: unknown) {
// if (error instanceof Error) {
// return {
// status: error.message,
// };
// } else {
// return {
// status: "An unexpected error occurred",
// };
// }
// }
// };
if (!findValue) {
return { status: "Line not found" };
} else {
return { status: "Success" };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};

View File

@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import wallItemModel from "../../../shared/model/builder/assets/wallitems-Model.ts";
import { existingProjectById, existingUser } from "../helpers/v1projecthelperFns.ts";
interface IWallSetupData {
modelUuid: string;
modelName: string;
@@ -11,10 +11,10 @@ interface IWallSetupData {
scale: [];
organization: string;
projectId: string;
userId: string;
}
interface IWallGet {
userId: string;
role: string;
organization: string;
projectId: string;
}
@@ -22,7 +22,6 @@ interface IWallDelete {
userId: string;
modelUuid: string;
modelName: string;
role: string;
organization: string;
projectId: string;
}
@@ -30,131 +29,153 @@ interface IWallItemResult {
data?: Object;
status: string;
}
export class WallItems {
static async setWallItems(data: IWallSetupData): Promise<IWallItemResult> {
try {
const {
modelUuid,
modelName,
position,
type,
csgposition,
csgscale,
quaternion,
scale,
projectId,
organization,
} = data;
const findvalue = await wallItemModel(organization).findOne({
modelUuid: modelUuid,
});
export const setWallItems = async (data: IWallSetupData): Promise<IWallItemResult> => {
try {
const {
userId,
modelUuid,
modelName,
position,
type,
csgposition,
csgscale,
quaternion,
scale,
projectId,
organization,
} = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findvalue = await wallItemModel(organization).findOne({
modelUuid: modelUuid,
});
if (findvalue) {
const updatevalue = await wallItemModel(organization).findOneAndUpdate(
{ modelUuid: modelUuid, projectId: projectId },
{
modelName,
position,
type,
csgposition,
csgscale,
quaternion,
scale,
},
{ new: true } // Return the updated document
);
return {
status: "Updated successfully",
data: updatevalue,
};
// res.status(201).json(updatevalue);
} else {
const newValue = await wallItemModel(organization).create({
modelUuid,
if (findvalue) {
const updatevalue = await wallItemModel(organization).findOneAndUpdate(
{ modelUuid: modelUuid, projectId: projectId },
{
modelName,
position,
type,
projectId,
csgposition,
csgscale,
quaternion,
scale,
});
return {
status: "wall Item created successfully",
data: newValue,
};
// res.status(201).json(newValue);
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
}
static async getWallItems(data: IWallGet) {
try {
const { organization, role, userId, projectId } = data;
const findValue = await wallItemModel(organization).find();
if (!findValue) {
return {
status: "wallitems not found",
};
} else {
return {
status: "Success",
data: findValue,
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
}
static async deleteWallItems(data: IWallDelete): Promise<IWallItemResult> {
try {
const { modelUuid, modelName, organization, userId, projectId, role } =
data;
const findValue = await wallItemModel(organization).findOneAndDelete({
modelUuid: modelUuid,
modelName: modelName,
projectId: projectId,
},
{ new: true } // Return the updated document
);
return {
status: "Updated successfully",
data: updatevalue,
};
// res.status(201).json(updatevalue);
} else {
const newValue = await wallItemModel(organization).create({
modelUuid,
modelName,
position,
type,
projectId,
csgposition,
csgscale,
quaternion,
scale,
});
if (!findValue) {
return {
status: "model not found",
};
} else {
return {
status: "Success",
data: findValue,
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
return {
status: "wall Item created successfully",
data: newValue,
};
// res.status(201).json(newValue);
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
}
export const getWallItems = async (data: IWallGet) => {
try {
const { organization, userId, projectId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findValue = await wallItemModel(organization).find({
projectId: projectId,
});
if (!findValue) {
return {
status: "wallitems not found",
};
} else {
return {
status: "Success",
data: findValue,
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
}
export const deleteWallItems = async (data: IWallDelete): Promise<IWallItemResult> => {
try {
const { modelUuid, modelName, organization, userId, projectId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findValue = await wallItemModel(organization).findOneAndDelete({
modelUuid: modelUuid,
modelName: modelName,
projectId: projectId,
});
if (!findValue) {
return {
status: "model not found",
};
} else {
return {
status: "Success",
data: findValue,
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
}

View File

@@ -0,0 +1,396 @@
import zoneModel from "../../V1Models/Builder/zoneModel.ts";
import widget3dModel from "../../V1Models/Vizualization/3dwidget.ts";
import floatWidgetModel from "../../V1Models/Vizualization/floatWidget.ts";
import panelModel from "../../V1Models/Vizualization/panelmodel.ts";
import templateModel from "../../V1Models/Vizualization/templatemodel.ts";
import widgetModel from "../../V1Models/Vizualization/widgemodel.ts";
import { existingProjectById, existingUser } from "../helpers/v1projecthelperFns.ts";
interface ISetZone {
organization: string;
projectId: string;
zoneData: {
zoneId: string;
points: [];
zoneName: string;
layer: number;
viewPortCenter: [];
viewPortposition: [];
};
userId: string;
}
interface IZone {
organization: string;
projectId: string;
zoneId: string;
userId: string;
}
interface IVizZone {
organization: string;
projectId: string;
userId: string;
}
interface IResult {
status: string;
data?: object;
}
interface IGetZones {
organization: string;
projectId: string;
userId: string;
}
export const SetZone = async (data: ISetZone): Promise<IResult> => {
try {
const { organization, projectId, zoneData, userId } = data;
const zoneId = zoneData.zoneId;
const points = zoneData.points;
const zoneName = zoneData.zoneName;
const layer = zoneData.layer;
const viewPortCenter = zoneData.viewPortCenter;
const viewPortposition = zoneData.viewPortposition;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findZoneId = await zoneModel(organization).findOne({
projectId: projectId,
zoneId: zoneId,
});
if (findZoneId) {
const updateZone = await zoneModel(organization)
.findOneAndUpdate(
{ zoneId: zoneId, projectId: projectId, isArchive: false },
{
points: points,
viewPortposition: viewPortposition,
viewPortCenter: viewPortCenter,
},
{ new: true }
)
.select("-_id -__v");
return { status: "zone updated", data: updateZone };
} else {
const zoneCreate = await zoneModel(organization).create({
zoneId,
createdBy: userId,
projectId,
zoneName: zoneName,
points,
layer,
viewPortCenter,
viewPortposition,
});
const createdZone = await zoneModel(organization)
.findById(zoneCreate._id)
.select("-_id -__v");
return { status: "Success", data: createdZone };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const DelZone = async (data: IZone): Promise<IResult> => {
try {
const { organization, userId, zoneId, projectId } = data;
const findZoneId = await zoneModel(organization).findOne({
zoneId: zoneId,
projectId: projectId,
isArchive: false,
});
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
if (findZoneId) {
const deleteZone = await zoneModel(organization)
.findOneAndDelete({
zoneId: zoneId,
createdBy: userId,
projectId: projectId,
isArchive: false,
})
.select("-_id -__v");
if (deleteZone) {
const panels = await panelModel(organization).find({
zoneId,
isArchive: false,
});
const allWidgetIds = panels.reduce((ids: string[], panel: any) => {
return ids.concat(panel.widgets || []);
}, []);
await widgetModel(organization).updateMany(
{ _id: { $in: allWidgetIds } },
{ $set: { isArchive: true } }
);
await panelModel(organization).updateMany(
{ zoneId, isArchive: false },
{ $set: { isArchive: true } }
);
await Promise.all([
widget3dModel(organization).updateMany(
{ zoneId, isArchive: false },
{ $set: { isArchive: true } }
),
templateModel(organization).updateMany(
{ zoneId, isArchive: false },
{ $set: { isArchive: true } }
),
floatWidgetModel(organization).updateMany(
{ zoneId, isArchive: false },
{ $set: { isArchive: true } }
),
]);
}
return {
status: "Success",
data: deleteZone,
};
} else {
return {
status: "Invalid zone ID",
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const GetZones = async (data: IGetZones): Promise<IResult> => {
try {
const { organization, userId, projectId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const findZoneId = await zoneModel(organization)
.find({ projectId: projectId, isArchive: false })
.select(
"zoneId zoneName layer points viewPortCenter viewPortposition -_id"
);
if (!findZoneId) {
return { status: "Invalid zone" };
}
return { status: "Success", data: findZoneId };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const ZoneData = async (data: IZone): Promise<IResult> => {
try {
const { organization, userId, projectId, zoneId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const findZone = await zoneModel(organization).findOne({
zoneId: zoneId,
projectId: projectId,
isArchive: false,
});
if (findZone)
return {
status: "Success",
data: findZone,
};
else {
return { status: "Zone not found" };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const SingleZonePanelData = async (data: IZone): Promise<IResult> => {
try {
const { organization, userId, projectId, zoneId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const existingZone = await zoneModel(organization)
.findOne({
projectId: projectId,
zoneId: zoneId,
isArchive: false,
})
.select(
"panelOrder zoneName zonePoints lockedPanel zoneId viewPortCenter viewPortposition points"
);
if (!existingZone) {
return { status: "Zone not found for the UUID" };
} else {
const panelData = await panelModel(organization).find({
zoneId: zoneId,
isArchive: false,
});
const zoneName = existingZone.zoneName as string;
const widgets = await Promise.all(
panelData.map(async (data) => {
const widgetDataArray = await widgetModel(organization).find({
panelID: data._id,
isArchive: false,
});
return widgetDataArray.map((widgetData) => ({
id: widgetData.widgetID,
type: widgetData.elementType,
title: widgetData.widgetName,
panel: widgetData.widgetside,
data: widgetData.Data || [],
}));
})
);
const flattenedWidgets = widgets.flat();
const objectData = {
zoneName,
viewPortposition: existingZone.viewPortposition,
zoneId: existingZone.zoneId,
viewPortCenter: existingZone.viewPortCenter,
activeSides: existingZone.panelOrder || [],
panelOrder: existingZone.panelOrder || [],
lockedPanels: existingZone.lockedPanel || [],
points: existingZone.points || [],
widgets: flattenedWidgets,
};
return { status: "Success", data: objectData };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const VizZoneDatas = async (data: IVizZone): Promise<IResult> => {
try {
const { organization, userId, projectId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project not found" };
const existingZones = await zoneModel(organization)
.find({
projectId: projectId,
isArchive: false,
})
.select(
"panelOrder zoneName zonePoints lockedPanel zoneId viewPortCenter viewPortposition points"
);
if (!existingZones) {
return { status: "Zone not found for the UUID" };
} else {
const response = await Promise.all(
existingZones.map(async (zone) => {
const panelData = await panelModel(organization).find({
zoneId: zone._id,
isArchive: false,
});
const widgets = await Promise.all(
panelData.map(async (panel) => {
const widgetDataArray = await widgetModel(organization).find({
panelID: panel._id,
isArchive: false,
});
return widgetDataArray.map((widget) => ({
id: widget.widgetID,
type: widget.elementType,
title: widget.widgetName,
panel: widget.widgetside,
data: widget.Data || [],
}));
})
);
return {
zoneName: zone.zoneName,
zoneId: zone.zoneId,
viewPortposition: zone.viewPortposition,
viewPortCenter: zone.viewPortCenter,
activeSides: zone.panelOrder || [],
panelOrder: zone.panelOrder || [],
lockedPanels: zone.lockedPanel || [],
points: zone.points || [],
widgets: widgets.flat(),
};
})
);
return { status: "Success", data: response };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};

View File

@@ -20,9 +20,7 @@ export const existingUser = async (userId: string, organization: string) => {
console.log("Invalid ObjectId format");
return null;
}
const userData = await userModel(organization).findOne({
_id: userId,
});
const userData = await userModel(organization).findOne({ _id: userId });
return userData;
};

View File

@@ -1,6 +1,5 @@
import projectModel from "../../model/project/project-model.ts";
import userModel from "../../model/user-Model.ts";
import { Types } from "mongoose";
import versionModel from "../../model/version/versionModel.ts";
import {
existingProject,
@@ -98,10 +97,12 @@ export const GetAllProjects = async (data: GetProjectsInterface) => {
if (!existingUser) return { status: "User not found" };
const projectDatas = await projectModel(organization)
.find({
createdBy:userId,
createdBy: userId,
isArchive: false,
})
.select("_id projectName createdBy thumbnail createdAt projectUuid createdAt");
.select(
"_id projectName createdBy thumbnail createdAt projectUuid createdAt"
);
if (projectDatas) return { status: "Success", Datas: projectDatas };
} catch (error: unknown) {
return { status: error };

View File

@@ -1,15 +1,19 @@
import projectModel from "../../model/project/project-model.ts";
import { existingUser } from "../helpers/v1projecthelperFns.ts";
interface IOrg {
organization: string;
userId: string;
}
interface IRestore {
projectId: string;
organization: string;
userId: string;
}
export const TrashDatas = async (data: IOrg) => {
try {
const { organization } = data;
const { organization, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const TrashLists = await projectModel(organization).find({
isArchive: true,
isDeleted: false,
@@ -47,7 +51,9 @@ export const TrashDatas = async (data: IOrg) => {
};
export const RestoreTrashData = async (data: IRestore) => {
try {
const { projectId, organization } = data;
const { projectId, organization, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const findProject = await projectModel(organization).findOne({
_id: projectId,
isArchive: true,
@@ -64,3 +70,25 @@ export const RestoreTrashData = async (data: IRestore) => {
return { status: error };
}
};
export const TrashDelete = async (data: IRestore) => {
try {
const { projectId, organization, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const findProject = await projectModel(organization).findOne({
_id: projectId,
isArchive: true,
});
if (!findProject) return { status: "Project not found" };
const DeleteTrashData = await projectModel(organization).findOneAndUpdate(
{ _id: projectId, isArchive: true },
{ isDelete: true },
{ new: true }
);
if (!DeleteTrashData)
return { status: "Project Trash Delete unsuccessfull" };
return { status: "Trash Project Restored successfully" };
} catch (error) {
return { status: error };
}
};

View File

@@ -29,6 +29,7 @@ export const RecentlyAdded = async (data: IRecentData) => {
try {
const { userId, organization, role } = data;
const userExisting = await existingUser(userId, organization);
console.log('userExisting: ', userExisting);
if (!userExisting) return { status: "User not found" };
const userRecentData = await UsersDataModel(organization)
.findOne({ userId: userId, isArchive: false })

View File

@@ -0,0 +1,390 @@
import floatWidgetModel from "../../V1Models/Vizualization/floatWidget.ts";
import zoneModel from "../../V1Models/Builder/zoneModel.ts";
import { existingUser } from "../helpers/v1projecthelperFns.ts";
interface IResult {
status: string;
data?: object;
}
interface IAddFloatData {
userId: string;
organization: string;
widget: {
className: string;
id: string;
iconName: string;
header: string;
floatWidgetID: string;
position: {};
per: string;
value: string;
isArchive: boolean;
zoneId: string;
Data: {
measurements: {};
duration: string;
};
};
zoneId: string;
index: number;
projectId: string;
}
interface IDelFloat {
userId: string;
organization: string;
zoneId: string;
floatWidgetID: string;
projectId: string;
}
interface ISingleFloat {
userId: string;
organization: string;
floatWidgetID: string;
}
interface IGetZoneFloat {
userId: string;
organization: string;
zoneId: string;
projectId: string;
}
interface IDuplicateFloatData {
userId: string;
organization: string;
widget: {
className: string;
id: string;
iconName: string;
header: string;
floatWidgetID: string;
position: {};
per: string;
value: string;
isArchive: boolean;
zoneId: string;
Data: {
measurements: {};
duration: string;
};
};
zoneId: string;
index: number;
projectId: string;
}
export const AddFloat = async (data: IAddFloatData): Promise<IResult> => {
try {
const { organization, widget, zoneId, index, projectId, userId } = 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 existingFloatWidget = await floatWidgetModel(organization).findOne({
floatWidgetID: widget.id,
isArchive: false,
zoneId: zoneId,
});
if (existingFloatWidget) {
const updateFloatWidget = await floatWidgetModel(
organization
).findOneAndUpdate(
{
floatWidgetID: widget.id,
isArchive: false,
},
{
$set: {
Data: {
measurements: widget?.Data?.measurements,
duration: widget?.Data?.duration,
},
header: widget?.header,
position: widget?.position,
},
},
{
upsert: true,
new: true,
}
);
const floatUpdateDatas = {
position: updateFloatWidget.position,
index: index,
zoneId: zoneId,
zoneName: existingZone.zoneName,
};
return { status: "Widget updated successfully", data: floatUpdateDatas };
} else {
const newFloadWidget = await floatWidgetModel(organization).create({
className: widget.className,
iconName: widget.iconName,
header: widget.header,
floatWidgetID: widget.id,
position: widget.position,
per: widget.per,
value: widget.value,
zoneId: zoneId,
});
if (newFloadWidget) {
const floatDatas = {
widget: {
position: newFloadWidget.position,
header: newFloadWidget.header,
value: newFloadWidget.value,
per: newFloadWidget.per,
className: newFloadWidget.className,
id: newFloadWidget.floatWidgetID,
},
zoneId: zoneId,
zoneName: existingZone.zoneName,
};
return { status: "Success", data: floatDatas };
}
return { status: "Failed to create FloatWidget" };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const DelFloat = async (data: IDelFloat): Promise<IResult> => {
try {
const { organization, floatWidgetID, zoneId, projectId, userId } = 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 findfloatWidget = await floatWidgetModel(organization).findOne({
floatWidgetID: floatWidgetID,
isArchive: false,
});
if (!findfloatWidget) return { status: "FloatWidget not found for the Id" };
const widgetData = await floatWidgetModel(organization).findByIdAndUpdate(
{ _id: findfloatWidget._id, isArchive: false },
{ isArchive: true },
{ new: true }
);
if (widgetData) {
const floatDeleteData = {
floatWidgetID: findfloatWidget.floatWidgetID,
zoneId: findfloatWidget.zoneId,
zoneName: existingZone.zoneName,
};
return { status: "Success", data: floatDeleteData };
}
return { status: "FloatWidget not deleted" };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const DuplicateFloat = async (
data: IDuplicateFloatData
): Promise<IResult> => {
try {
const { organization, widget, zoneId, index, projectId, userId } = 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 existingFloatWidget = await floatWidgetModel(organization).findOne({
floatWidgetID: widget.id,
isArchive: false,
zoneId: zoneId,
});
if (existingFloatWidget) {
const updateFloatWidget = await floatWidgetModel(
organization
).findOneAndUpdate(
{
floatWidgetID: widget.id,
isArchive: false,
},
{
$set: {
Data: {
measurements: widget?.Data?.measurements,
duration: widget?.Data?.duration,
},
header: widget?.header,
position: widget?.position,
},
},
{
upsert: true,
new: true,
}
);
if (!updateFloatWidget) {
return { status: "FloatWidget update unsuccessfull" };
}
const floatUpdateDatas = {
position: updateFloatWidget.position,
index: index,
zoneId: zoneId,
zoneName: existingZone.zoneName,
};
return {
status: "Widget updated successfully",
data: floatUpdateDatas,
};
}
const newFloadWidget = await floatWidgetModel(organization).create({
className: widget.className,
header: widget.header,
floatWidgetID: widget.id,
position: widget.position,
per: widget.per,
value: widget.value,
zoneId: zoneId,
Data: {
measurements: widget?.Data?.measurements,
duration: widget?.Data?.duration,
},
});
if (newFloadWidget) {
const floatDatas = {
widget: {
position: newFloadWidget.position,
header: newFloadWidget.header,
value: newFloadWidget.value,
per: newFloadWidget.per,
className: newFloadWidget.className,
id: newFloadWidget.floatWidgetID,
},
zoneId: zoneId,
zoneName: existingZone.zoneName,
index: index,
};
return {
status: "Success",
data: floatDatas,
};
}
return {
status: "Failed to duplicate FloatWidget",
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const GetFloatWidget = async (data: IGetZoneFloat): Promise<IResult> => {
try {
const { organization, zoneId, projectId, userId } = 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 floatWidgetModel(organization)
.find({
zoneId: zoneId,
isArchive: false,
})
.select("-_id -zoneId -createdAt -updatedAt -__v");
if (!widgetData || widgetData.length === 0) {
return { status: "All Datas" };
}
const formattedWidgets = widgetData.map((widget) => ({
Data: {
measurements: widget.Data?.measurements || {},
duration: widget.Data?.duration || "1h",
},
className: widget.className,
iconName: widget?.iconName,
header: widget.header,
id: widget.floatWidgetID,
position: widget.position,
per: widget.per,
value: widget.value,
}));
return { status: "Success", data: formattedWidgets };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const SingleFloatWidget = async (
data: ISingleFloat
): Promise<IResult> => {
try {
const { organization, floatWidgetID, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const widgetData = await floatWidgetModel(organization)
.findOne({
floatWidgetID: floatWidgetID,
isArchive: false,
})
.select("-_id -zoneId -createdAt -updatedAt -__v");
if (!widgetData || widgetData.length === 0) {
return { status: "Widget not found" };
}
const Datastructure = {
measurements: widgetData?.Data?.measurements || {},
duration: widgetData?.Data?.duration || "1h",
};
const header = widgetData?.header;
return { status: "Success", data: { Datastructure, header } };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};

View File

@@ -0,0 +1,324 @@
import panelModel from "../../V1Models/Vizualization/panelmodel.ts";
import zoneModel from "../../V1Models/Builder/zoneModel.ts";
import widgetModel from "../../V1Models/Vizualization/widgemodel.ts";
import { existingUser } from "../helpers/v1projecthelperFns.ts";
interface IResult {
status: string;
data?: object;
}
interface IAddPanel {
organization: string;
zoneId: string;
panelOrder: string[];
userId: string;
projectId: string;
}
interface IPanel {
organization: string;
zoneId: string;
panelName: string;
userId: string;
projectId: string;
}
interface ILockedPanel {
organization: string;
zoneId: string;
lockedPanel: string[];
userId: string;
projectId: string;
}
export const AddPanel = async (data: IAddPanel): Promise<IResult> => {
try {
const { organization, zoneId, panelOrder, userId, 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" };
await zoneModel(organization).findOneAndUpdate(
{ zoneId: zoneId, isArchive: false },
{ panelOrder: panelOrder },
{ new: true }
);
const existingPanels = await panelModel(organization).find({
zoneId: zoneId,
isArchive: false,
});
const existingPanelNames = existingPanels.map(
(panel) => panel.panelName as string
);
const missingPanels = panelOrder.filter(
(panelName: string) => !existingPanelNames.includes(panelName)
);
const createdPanels = [];
for (const panelName of missingPanels) {
const newPanel = await panelModel(organization).create({
zoneId: zoneId,
panelName: panelName,
widgets: [],
isArchive: false,
});
createdPanels.push(newPanel);
}
if (createdPanels.length === 0) {
return { status: "No new panels were created. All panels already exist" };
}
const zoneAndPanelData = await getZoneAndPanelData(
organization,
zoneId,
projectId
);
if (!zoneAndPanelData) {
return zoneAndPanelData;
}
return { status: "Success", data: zoneAndPanelData };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const DelPanel = async (data: IPanel): Promise<IResult> => {
try {
const { organization, zoneId, panelName, userId, 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 existingPanel = await panelModel(organization).findOne({
zoneId: zoneId,
panelName: panelName,
isArchive: false,
});
if (!existingPanel) return { status: "Panel Already Deleted" };
await panelModel(organization).updateOne(
{ _id: existingPanel._id, isArchive: false },
{ $set: { isArchive: true } }
);
const existingWidgets = await widgetModel(organization).find({
panelID: existingPanel._id,
isArchive: false,
});
for (const widgetData of existingWidgets) {
widgetData.isArchive = true;
await widgetData.save();
}
if (existingZone.panelOrder.includes(existingPanel.panelName)) {
await zoneModel(organization).updateOne(
{ _id: existingZone._id },
{ $pull: { panelOrder: existingPanel.panelName } }
);
}
const zoneAndPanelData = await getZoneAndPanelData(
organization,
zoneId,
projectId
);
if (!zoneAndPanelData) {
return zoneAndPanelData;
}
return { status: "Success", data: zoneAndPanelData };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const ClearPanel = async (data: IPanel): Promise<IResult> => {
try {
const { organization, zoneId, panelName, userId, 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 existingPanel = await panelModel(organization).findOne({
zoneId: zoneId,
panelName: panelName,
isArchive: false,
});
if (!existingPanel) return { status: "Requested Panel not found" };
const existingWidgets = await widgetModel(organization).find({
panelID: existingPanel._id,
isArchive: false,
});
if (existingWidgets.length === 0) return { status: "No widgets to clear" };
const clearWidgetsofPanel = await widgetModel(organization).updateMany(
{ panelID: existingPanel._id, isArchive: false },
{ isArchive: true }
);
const removeWidgetsInPanel = await panelModel(
organization
).findOneAndUpdate(
{ _id: existingPanel._id, isArchive: false },
{ $set: { widgets: [] } },
{ new: true }
);
if (!clearWidgetsofPanel && !removeWidgetsInPanel)
return { status: "Failed to clear widgets in panel" };
const zoneAndPanelData = await getZoneAndPanelData(
organization,
zoneId,
projectId
);
if (!zoneAndPanelData) {
return zoneAndPanelData;
}
return {
status: "Success",
data: zoneAndPanelData,
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const LockedPanel = async (data: ILockedPanel): Promise<IResult> => {
try {
const { organization, zoneId, lockedPanel, userId, 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" };
else {
const updateLockedPanel = await zoneModel(organization).findOneAndUpdate(
{ zoneId: zoneId, isArchive: false },
{
lockedPanel: lockedPanel,
},
{ new: true }
);
const zoneAndPanelData = await getZoneAndPanelData(
organization,
zoneId,
projectId
);
if (!zoneAndPanelData) {
return zoneAndPanelData;
}
if (updateLockedPanel) {
return {
status: "Success",
data: zoneAndPanelData,
};
}
return { status: "locked panel not updated" };
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
const getZoneAndPanelData = async (
organization: string,
zoneId: string,
projectId: string
) => {
try {
const existingZone = await zoneModel(organization)
.findOne({
zoneId: zoneId,
isArchive: false,
projectId: projectId,
})
.select(
"panelOrder zoneName zonePoints lockedPanel zoneId viewPortCenter viewPortposition"
);
if (!existingZone) {
return { status: "Zone not found" };
} else {
const panelData = await panelModel(organization).find({
zoneId: zoneId,
isArchive: false,
});
const zoneName = existingZone.zoneName as string;
const widgets = await Promise.all(
panelData.map(async (data) => {
const widgetDataArray = await widgetModel(organization).find({
panelID: data._id,
isArchive: false,
});
return widgetDataArray.map((widgetData) => ({
id: widgetData.widgetID,
type: widgetData.elementType,
title: widgetData.widgetName,
panel: widgetData.widgetside,
data: widgetData.Data || [],
}));
})
);
const flattenedWidgets = widgets.flat();
const objectData = {
zoneName,
viewPortposition: existingZone.viewPortposition,
zoneId: existingZone.zoneId,
viewPortCenter: existingZone.viewPortCenter,
activeSides: existingZone.panelOrder || [],
panelOrder: existingZone.panelOrder || [],
lockedPanels: existingZone.lockedPanel || [],
points: existingZone.zonePoints || [],
widgets: flattenedWidgets,
};
return { data: objectData };
}
} catch (error: unknown) {
return { status: "Panel not found" };
}
};

View File

@@ -0,0 +1,304 @@
import templateModel from "../../V1Models/Vizualization/templatemodel.ts";
import panelModel from "../../V1Models/Vizualization/panelmodel.ts";
import zoneModel from "../../V1Models/Builder/zoneModel.ts";
import widgetModel from "../../V1Models/Vizualization/widgemodel.ts";
import floatWidgetModel from "../../V1Models/Vizualization/floatWidget.ts";
import { existingUser } from "../helpers/v1projecthelperFns.ts";
interface IResult {
status: string;
data?: object;
}
interface IAddTemplate {
organization: string;
template: {
id: string;
name: string;
snapshot: string;
panelOrder: [];
widgets: [];
floatingWidget: [];
Widgets3D: [];
};
projectId: string;
userId: string;
}
interface ITemplateToZone {
organization: string;
templateID: string;
projectId: string;
zoneId: string;
userId: string;
}
interface ITemplate {
organization: string;
templateID: string;
projectId: string;
userId: string;
}
interface IGetTemplate {
organization: string;
projectId: string;
userId: string;
}
export const AddTemplate = async (data: IAddTemplate): Promise<IResult> => {
try {
const { organization, template, projectId, userId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const existingTemplate = await templateModel(organization).findOne({
templateID: template.id,
isArchive: false,
projectId: projectId,
});
if (existingTemplate) return { status: "TemplateID alreay exists" };
const newTemplate = await templateModel(organization).create({
templateID: template.id,
templateName: template.name,
panelOrder: template.panelOrder,
widgets: template.widgets,
snapshot: template.snapshot,
floatWidgets: template.floatingWidget,
Widgets3D: template.Widgets3D,
});
if (newTemplate) {
const allTemplateDatas = await templateModel(organization)
.find({ isArchive: false })
.select("-_id -__v -isArchive -createdAt -updatedAt");
const formattedTemplates = allTemplateDatas.map(async (data) => ({
id: data.templateID,
name: data.templateName,
panelOrder: data.panelOrder,
widgets: data.widgets,
floatingWidget: data.floatWidgets,
widgets3D: data.Widgets3D,
snapshot: data.snapshot,
}));
return { status: "Success", data: formattedTemplates };
}
return { status: "Template not saved" };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const AddTemplateToZone = async (
data: ITemplateToZone
): Promise<IResult> => {
try {
const { organization, templateID, projectId, zoneId, userId } = 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 existingTemplate = await templateModel(organization).findOne({
templateID: templateID,
isArchive: false,
projectId: projectId,
});
if (!existingTemplate)
return {
status: "TemplateID not found",
};
if (existingZone.panelOrder.length > 0) {
existingZone.panelOrder = existingTemplate.panelOrder;
await existingZone.save();
const archivePanelDatas = await panelModel(organization).find({
zoneId,
isArchive: false,
});
for (const panelData of archivePanelDatas) {
await widgetModel(organization).deleteMany({
panelID: panelData._id,
isArchive: false,
});
}
await panelModel(organization).deleteMany({
zoneId,
isArchive: false,
});
await floatWidgetModel(organization).deleteMany({
zoneId,
isArchive: false,
});
}
existingZone.panelOrder = existingTemplate.panelOrder;
await existingZone.save();
const existingPanels = await panelModel(organization).find({
zoneId,
isArchive: false,
});
const existingPanelNames = existingPanels.map(
(panel) => panel.panelName as string
);
const missingPanels = existingTemplate.panelOrder.filter(
(panelName: string) => !existingPanelNames.includes(panelName)
);
await Promise.all(
missingPanels.map((panelName: any) =>
panelModel(organization).create({
zoneId,
panelName,
widgets: [],
isArchive: false,
})
)
);
for (const widgetData of existingTemplate.widgets) {
const addedExistingPanel = await panelModel(organization).findOne({
panelName: widgetData.panel,
zoneId,
isArchive: false,
});
if (!addedExistingPanel) continue;
const existingWidget = await widgetModel(organization).findOne({
panelID: addedExistingPanel._id,
widgetID: widgetData.id,
isArchive: false,
});
if (existingWidget) continue;
const newWidget = await widgetModel(organization).create({
widgetID: widgetData.id,
elementType: widgetData.type,
zoneId: zoneId,
widgetName: widgetData.widgetName || "Widget",
panelID: addedExistingPanel._id,
widgetside: widgetData.panel,
});
addedExistingPanel.widgets.push(newWidget._id);
await addedExistingPanel.save();
}
for (const floatData of existingTemplate.floatWidgets) {
const existingFloatWidget = await floatWidgetModel(organization).findOne({
floatWidgetID: floatData.id,
isArchive: false,
zoneId,
});
if (existingFloatWidget) continue;
await floatWidgetModel(organization).create({
className: floatData.className,
header: floatData.header,
floatWidgetID: floatData.id,
position: floatData.position,
per: floatData.per,
value: floatData.value,
zoneId,
});
}
const templateZoneDatas = {
template: {
id: existingTemplate.templateID,
name: existingTemplate.templateName,
panelOrder: existingTemplate.panelOrder,
widgets: existingTemplate.widgets,
snapshot: existingTemplate.snapshot,
floatingWidget: existingTemplate.floatWidgets,
},
zoneId: existingZone.zoneId,
zoneName: existingZone.zoneName,
};
return { status: "Success", data: templateZoneDatas };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const TemplateDelete = async (data: ITemplate): Promise<IResult> => {
try {
const { templateID, projectId, userId, organization } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const existingTemplate = await templateModel(organization).findOne({
templateID: templateID,
isArchive: false,
projectId: projectId,
});
if (existingTemplate) {
const newTemplate = await templateModel(organization).updateOne(
{ templateID: templateID, isArchive: false, projectId: projectId },
{ $set: { isArchive: true } }
);
if (newTemplate) {
const TemplateDeleteData = existingTemplate.templateID;
return {
status: "Success",
data: TemplateDeleteData,
};
}
return { status: "Template not Deleted" };
}
return { status: "Template not found" };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const GetAllTemplates = async (data: IGetTemplate): Promise<IResult> => {
try {
const { organization, userId, projectId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const templateDatas = await templateModel(organization)
.find({ projectId: projectId, isArchive: false })
.select("-_id -__v -isArchive -createdAt -updatedAt");
if (!templateDatas) return { status: "All Datas" };
const formattedTemplates = templateDatas.map((data) => ({
id: data.templateID,
name: data.templateName,
panelOrder: data.panelOrder,
widgets: data.widgets,
floatingWidget: data.floatWidgets,
widgets3D: data.Widgets3D,
snapshot: data.snapshot,
}));
return { status: "Success", data: formattedTemplates };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};

View File

@@ -0,0 +1,280 @@
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",
};
}
}
};

View File

@@ -0,0 +1,356 @@
import widgetModel from "../../V1Models/Vizualization/widgemodel.ts";
import zoneModel from "../../V1Models/Builder/zoneModel.ts";
import panelModel from "../../V1Models/Vizualization/panelmodel.ts";
import {
existingProjectById,
existingUser,
} from "../helpers/v1projecthelperFns.ts";
interface IResult {
status: string;
data?: object;
}
interface IWidgetCreate {
organization: string;
userId: string;
zoneId: string;
projectId: string;
widget: {
type: string;
title: string;
panel: string;
id: string;
Data: {
measurements: {};
duration: string;
};
};
}
interface IWidgetDelete {
organization: string;
userId: string;
zoneId: string;
projectId: string;
widgetID: string;
}
interface IWidgetUpdate {
organization: string;
userId: string;
zoneId: string;
projectId: string;
widgetID: string;
values: {
widgetName: string;
widgetSide: string;
type: string;
Data: {
measurement: {};
duration: string;
};
color: string;
fontFamily: string;
fontStyle: string;
fontWeight: string;
};
}
interface IGetWidget {
organization: string;
userId: string;
zoneId: string;
projectId: string;
widgetID: string;
}
export const AddWidget = async (data: IWidgetCreate): Promise<IResult> => {
try {
const { organization, widget, userId, zoneId, projectId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project 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 existingPanel = await panelModel(organization).findOne({
panelName: widget.panel,
zoneId: zoneId,
isArchive: false,
});
if (!existingPanel) return { status: "panelName not found" };
if (existingPanel.panelName === widget.panel) {
const existingWidget = await widgetModel(organization).findOne({
panelID: existingPanel._id,
widgetID: widget.id,
isArchive: false,
});
if (existingWidget) {
const updateWidget = await widgetModel(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 }
);
if (!updateWidget) {
return { status: "Widget update unsuccessfull" };
}
return { status: "Widget update successfully", data: updateWidget };
}
const newWidget = await widgetModel(organization).create({
widgetID: widget.id,
elementType: widget.type,
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,
};
const finaldata = {
widgetData: widgetData,
zoneId: existingZone.zoneId,
zoneName: existingZone.zoneName,
};
return {
status: "Success",
data: finaldata,
};
}
}
return {
status: "Type mismatch",
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const WidgetDelete = async (data: IWidgetDelete): Promise<IResult> => {
try {
const { organization, widgetID, userId, zoneId, projectId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project 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 findWidget = await widgetModel(organization).findOne({
widgetID: widgetID,
zoneId: zoneId,
isArchive: false,
});
if (!findWidget) return { status: "Widget not found" };
const widgetData = await widgetModel(organization).updateOne(
{ _id: findWidget._id, isArchive: false, zoneId: zoneId },
{ $set: { isArchive: true } }
);
if (widgetData) {
await widgetModel(organization).find({
panelID: findWidget.panelID,
zoneId: zoneId,
isArchive: false,
});
const panelData = await panelModel(organization).findOne({
_id: findWidget.panelID,
isArchive: false,
zoneId: zoneId,
});
if (panelData.widgets.includes(findWidget._id)) {
const index1 = panelData.widgets.indexOf(findWidget._id);
panelData.widgets.splice(index1, 1);
}
await panelData.save();
const activeWidgets = await widgetModel(organization).find({
zoneId: zoneId,
isArchive: false,
});
const formattedWidgets = activeWidgets.map((widget) => ({
id: widget.widgetID,
type: widget.elementType,
title: widget.widgetName,
panel: widget.widgetside,
data: {
duration: "1h",
measurements: {},
},
}));
const widgetData1 = {
widgetDeleteDatas: formattedWidgets,
zoneId: zoneId,
zoneName: existingZone.zoneName,
};
return { status: "Success", data: widgetData1 };
}
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 UpdateWidget = async (data: IWidgetUpdate): Promise<IResult> => {
try {
const { organization, widgetID, userId, projectId, zoneId, values } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project 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 findWidget = await widgetModel(organization).findOne({
widgetID: widgetID,
zoneId: zoneId,
isArchive: false,
});
if (!findWidget) return { status: "Data not found" };
const updateData = {
widgetName: values.widgetName,
widgetSide: values.widgetSide,
elementType: values.type,
Data: {
measurement: values.Data.measurement,
duration: values.Data.duration,
},
elementColor: values.color,
fontFamily: values.fontFamily,
fontStyle: values.fontStyle,
fontWeight: values.fontWeight,
isArchive: false,
};
await widgetModel(organization).findOneAndUpdate(
{ widgetID: widgetID, isArchive: false },
updateData,
{
new: true,
upsert: true,
setDefaultsOnInsert: true,
}
);
return {
status: "Success",
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const GetWidget = async (data: IGetWidget): Promise<IResult> => {
try {
const { organization, widgetID, userId, projectId, zoneId } = data;
const UserExists = await existingUser(userId, organization);
if (!UserExists) return { status: "User not found" };
const LivingProject = await existingProjectById(
projectId,
organization,
userId
);
if (!LivingProject) return { status: "Project 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 existingWidget = await widgetModel(organization)
.findOne({
widgetID: widgetID,
zoneId: zoneId,
isArchive: false,
})
.select("Data widgetName -_id");
if (!existingWidget) return { status: "Widget not found for the widgetID" };
const Datastructure = {
measurements: existingWidget.Data.measurements || {},
duration: existingWidget.Data.duration || "1h",
};
const widgetName = existingWidget.widgetName || "Widget";
const Data = { Datastructure, widgetName };
return { status: "Success", data: Data };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};