import { Request, Response } from "express"; // import assetModel from "../../../shared/model/assets/flooritems-Model.ts"; import assetModel from "../../../shared/model/builder/assets/asset-Model.ts"; import actionModel from "../../../shared/model/simulation/actionmodel.ts"; import triggerModel from "../../../shared/model/simulation/triggersmodel.ts"; export class assetsFloorservice { static async setFloorassets(req: Request, res: Response) { try { const { modeluuid, modelname, position, modelfileID, rotation, isLocked, isVisible, organization, points, } = req.body; const findvalue = await assetModel(organization).findOne({ modeluuid: modeluuid, modelname: modelname, }); if (findvalue) { const updatevalue = await assetModel(organization).findOneAndUpdate( { modeluuid: modeluuid, modelname: modelname }, { position: position, rotation: rotation, isVisible: isVisible, isLocked: isLocked, }, { new: true } ); res.status(201).json(updatevalue); } else { // const newValue = await assetModel(organization).create({ // modeluuid, // modelfileID, // modelname, // position, // rotation, // isLocked, // isVisible, // points, // }); // for (const point of points) { // // Create actions if they exist // for (const action of point.actions || []) { // const actionData = await actionModel(organization).create({ // pointsUUID: point.uuid, // actionUUID: action.uuid, // eventData: { // uuid: action.uuid, // type: action.type, // material: action.material, // delay: action.delay, // spawn_Interval: action.spawnInterval, // }, // }); // // Push action ID to point // newValue.points // .find((p: any) => p.uuid === point.uuid) // ?.actions.push(actionData._id); // } // // Create triggers if they exist // for (const trigger of point.triggers || []) { // const triggerData = await triggerModel(organization).create({ // pointsUUID: point.uuid, // actionUUID: trigger.uuid, // triggerData: { // type: trigger.type, // }, // }); // // Push trigger ID to point // newValue.points // .find((p: any) => p.uuid === point.uuid) // ?.triggers.push(triggerData._id); // } // } // // Save the updated document // await newValue.save(); // res.status(201).json(newValue); let pointRefs = []; for (const point of points) { let actionRefs = []; let triggerRefs = []; // Store Actions (Events) if (point.actions && point.actions.length > 0) { for (const action of point.actions) { const actionDoc = await actionModel(organization).create({ pointsUUID: point.uuid, actionUUID: action.uuid, isArchive: false, eventData: [action], // Store the action data }); await actionDoc.save(); actionRefs.push(actionDoc._id); // Store reference } } // Store Triggers if (point.triggers && point.triggers.length > 0) { for (const trigger of point.triggers) { const triggerDoc = await triggerModel(organization).create({ pointsUUID: point.uuid, actionUUID: trigger.uuid, isArchive: false, triggerData: [trigger], // Store trigger data }); await triggerDoc.save(); triggerRefs.push(triggerDoc._id); // Store reference } } // Store the Point document pointRefs.push({ uuid: point.uuid, position: point.position, rotation: point.rotation, actions: actionRefs, triggers: triggerRefs, }); } // Store the main asset document const assetDoc = await assetModel(organization).create({ modeluuid, modelname, position, modelfileID, rotation, isLocked, isVisible, points: pointRefs, }); await assetDoc.save(); res.status(201).json({ message: "Model stored successfully", modelId: assetDoc._id, }); } } catch (error) { console.error("Error creating flooritems:", error); res.status(500).json({ message: "Failed to create flooritems" }); } } static async getFloorItems(req: Request, res: Response) { try { const { organization } = req.params; const findValue = await assetModel(organization) .find() .select("-_id -__v") .populate({ path: "points.actions", select: "-_id", populate: { path: "eventData", select: "uuid type -_id", }, }) .populate({ path: "points.triggers", select: "-_id", populate: { path: "triggerData", select: "uuid type -_id", }, }); if (!findValue) { res.status(200).json("floorItems not found"); } else { res.status(201).json(findValue); } } catch (error) { console.error("Error get flooritems:", error); res.status(500).json({ error: "Failed to get flooritems" }); } } static async deleteFloorItems(req: Request, res: Response) { try { const { modeluuid, modelname, organization } = req.body; const findValue = await assetModel(organization).findOneAndDelete({ modeluuid: modeluuid, modelname: modelname, }); if (!findValue) { res.status(200).json("user not found"); } else { res.status(201).json(findValue); } } catch (error) { console.error("Error get flooritems:", error); res.status(500).json({ error: "Failed to get flooritems" }); } } }