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: []; rotation: object; eventData: Mixed; modelfileID: string; isLocked: boolean; isVisible: boolean; organization: string; projectId: string; userId: string; } interface AssetUpdate { modelUuid: string; modelName: string; position: []; 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 { status: "Updated successfully", data: updatevalue, }; } else { let assetData: any = { projectId, userId, modelUuid, modelName, position, modelfileID, rotation, isLocked, isVisible, }; 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 { 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 { status: "Failed to archive asset", }; } const updatedEvents = await EventsDataModel(organization).updateMany( { modelUuid, productId: projectId }, { $set: { isArchive: true } } ); 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" }; } 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, type: typedEventData?.type || existingModel?.type, }, { new: true } ); 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" }; } const updateAsset = await assetModel(organization).updateMany( { modelUuid: modelUuid, projectId: projectId, modelName: modelName, isArchive: false, }, { position: position, rotation: rotation, isVisible: isVisible, isLocked: isLocked, } ); return { status: "Success", data: updateAsset, }; } 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" }; } 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 { status: "Success", data: response, }; } catch (error: unknown) { if (error instanceof Error) { return { status: error.message, }; } else { return { status: "An unexpected error occurred", }; } } };