import { Request, Response } from "express"; import pointModel from "../../../shared/model/builder/assets/assetPoint-Model.ts"; interface IPointBase { uuid: string; position: number[]; } interface IPointConveyor extends IPointBase { rotation: number[]; actions: Array<{ uuid: string; name: string; type: string; material: string; delay: number | string; spawnInterval: number | string; isUsed: boolean; }>; triggers: Array<{ uuid: string; name: string; type: string; isUsed: boolean; bufferTime: number; }>; connections: { source: { pathUUID: string; pointUUID: string }; targets: Array<{ pathUUID: string; pointUUID: string }>; }; } interface IPointVehicle extends IPointBase { actions: { uuid: string; name: string; type: string; hitCount: number; start: { x: number; y: number } | {}; end: { x: number; y: number } | {}; buffer: number; }; connections: { source: { modelUUID: string; pointUUID: string }; targets: Array<{ pathUUID: string; pointUUID: string }>; }; speed: number; } export class pointService { static async addPoints(req: Request, res: Response): Promise { const { type, modelfileID, organization } = req.body; // Validate type if (!["Conveyor", "Vehicle"].includes(type)) { return res.status(400).json({ message: "Invalid type requested" }); } try { const existingdata = await pointModel(organization).findOne({ modelfileID: modelfileID, isArchive: false, }); if (existingdata) return res.send("Data already exists"); if (type === "Conveyor") { const baseData = { modelfileID: "672a090f80d91ac979f4d0bd", type: "Conveyor", }; const conveyorPoints: IPointConveyor[] = [ { uuid: "point1UUID", position: [0, 0.85, 2.2], rotation: [0, 0, 0], actions: [ { uuid: "randomUUID", name: "Action 1", type: "actionType", material: "actionMaterial", delay: "Inherit", spawnInterval: "Inherit", isUsed: false, }, ], triggers: [ { uuid: "randomUUID", name: "trigger 1", type: "triggerType", bufferTime: 0, // delay: "Inherit", // spawnInterval: "Inherit", isUsed: false, }, ], connections: { source: { pathUUID: "modelUUID", pointUUID: "point1UUID" }, targets: [{ pathUUID: "modelUUID", pointUUID: "point1UUID" }], }, }, { uuid: "point2UUID", position: [0, 0.85, 0], rotation: [0, 0, 0], actions: [ { uuid: "randomUUID", name: "Action 1", type: "actionType", material: "actionMaterial", delay: "Inherit", spawnInterval: "Inherit", isUsed: false, }, ], triggers: [ { uuid: "randomUUID", name: "trigger 1", type: "triggerType", bufferTime: 0, isUsed: false, }, ], connections: { source: { pathUUID: "modelUUID", pointUUID: "point2UUID" }, targets: [{ pathUUID: "modelUUID", pointUUID: "point2UUID" }], }, }, { uuid: "point3UUID", position: [0, 0.85, -2.2], rotation: [0, 0, 0], actions: [ { uuid: "randomUUID", name: "Action 1", type: "actionType", material: "actionMaterial", delay: "Inherit", spawnInterval: "Inherit", isUsed: false, }, ], triggers: [ { uuid: "randomUUID", name: "trigger 1", type: "triggerType", bufferTime: 0, isUsed: false, }, ], connections: { source: { pathUUID: "modelUUID", pointUUID: "point3UUID" }, targets: [{ pathUUID: "modelUUID", pointUUID: "point3UUID" }], }, }, ]; await pointModel(organization).create({ ...baseData, points: conveyorPoints, }); } if (type === "Vehicle") { console.log("vehcile data"); const baseData = { modelfileID: "67e3da19c2e8f37134526e6a", type: "Vehicle", }; const vehiclePoint: IPointVehicle = { uuid: "point1UUID", position: [0, 1.3, 0], actions: { uuid: "randomUUID", name: "Action 1", type: "string", hitCount: 1, start: { x: 0, y: 0 }, end: { x: 0, y: 0 }, buffer: 0, }, connections: { source: { modelUUID: "modelUUID", pointUUID: "point1UUID" }, targets: [{ pathUUID: "modelUUID", pointUUID: "point1UUID" }], }, speed: 2, }; if ("rotation" in vehiclePoint) { return res .status(400) .json({ error: "Rotation not allowed for Vehicle points" }); } if ("triggers" in vehiclePoint) { return res .status(400) .json({ error: "Triggers not allowed for Vehicle points" }); } await pointModel(organization).create({ ...baseData, points: vehiclePoint, }); } return res.status(201).json({ message: "Points created successfully" }); } catch (error) { return res.status(500).json({ message: "Server error", error: error instanceof Error ? error.message : "Unknown error", }); } } static async gettypePoints(req: Request, res: Response): Promise { const { modelfileID, organization } = req.params; console.log("req.params: ", req.params); try { const pointData = await pointModel(organization) .findOne({ modelfileID: modelfileID, isArchive: false, }) .select("type points -_id"); if (!pointData) { return res.json({ message: "Data not found" }); } res.status(200).json(pointData); } catch (error: any) { res.status(500).json({ message: "Server error", error: error.message }); } } }