diff --git a/src/shared/services/builder/EnvironmentService.ts b/src/shared/services/builder/EnvironmentService.ts index e69de29..5f5a87a 100644 --- a/src/shared/services/builder/EnvironmentService.ts +++ b/src/shared/services/builder/EnvironmentService.ts @@ -0,0 +1,86 @@ +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; +} +export const setEnvironment = async ( + data: EnvironmentInput +): Promise<{ status: string; data?: Object }> => { + try { + const { roofVisibility, wallVisibility, 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 }) + if (findvalue) { + const updatevalue = await environmentModel(organization).findOneAndUpdate( + { userId: userId, projectId: projectId }, { roofVisibility: roofVisibility, wallVisibility: wallVisibility, shadowVisibility: shadowVisibility }, { new: true }); + // return { success: true, message: 'evironments updated', data: updatevalue,organization:organization } + return { status: 'evironments updated', data: updatevalue } + } else { + const newValue = await environmentModel(organization).create({ userId, projectId, roofVisibility, wallVisibility, shadowVisibility }); + // return { success: true, message: 'evironments created', data: newValue,organization:organization } + 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: EnvironmentInput +): 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 + }); + if (!findValue) { + // res.status(200).json("user not found"); + return { status: 'evironments user not found' } + } else { + // res.status(201).json(findValue); + return { status: 'Success', data: findValue } + } + } catch (error: unknown) { + if (error instanceof Error) { + return { + status: error.message, + }; + } else { + return { + status: "An unexpected error occurred", + }; + } + } +}; \ No newline at end of file diff --git a/src/shared/services/builder/assetService.ts b/src/shared/services/builder/assetService.ts index e69de29..821733e 100644 --- a/src/shared/services/builder/assetService.ts +++ b/src/shared/services/builder/assetService.ts @@ -0,0 +1,473 @@ +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; +} +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: setAssetInput +): 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, + // }; + } + 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: "Model deleted successfully", + 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: setAssetInput +): 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: setAssetInput +): 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: setAssetInput +): 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", + }; + } + } +}; \ No newline at end of file