import { Request, Response } from "express"; import ProductModel from "../../../shared/model/simulation/productModel.ts"; import EventsDataModel from "../../../shared/model/simulation/eventsDataModel.ts"; export class ProductFlowService { static async productAdd(req: Request, res: Response): Promise { try { const { productName, productId, eventDatas, organization } = req.body; if (!organization) { return res.json({ message: "organization not found" }); } const existingProduct = await ProductModel(organization).findOne({ productId: productId, isArchive: false, }); if (existingProduct) { const existingEventData = await EventsDataModel(organization).findOne({ productId: productId, modelUuid: eventDatas.modelUuid, isArchive: false, }); if (existingEventData) { await EventsDataModel(organization).findOneAndUpdate( { modelUuid: eventDatas.modelUuid, productId: productId, isArchive: false, }, { modelUuid: eventDatas?.modelUuid, modelName: eventDatas?.modelName, position: eventDatas?.position, rotation: eventDatas?.rotation, type: eventDatas?.type, speed: eventDatas?.speed, point: eventDatas?.point, points: eventDatas?.points, } ); return res .status(200) .json({ message: "EventData updated successfully" }); } else { await EventsDataModel(organization).create({ productId: productId, modelUuid: eventDatas?.modelUuid, modelName: eventDatas?.modelName, position: eventDatas?.position, rotation: eventDatas?.rotation, type: eventDatas?.type, speed: eventDatas?.speed, point: eventDatas?.point, points: eventDatas?.points, }); return res .status(201) .json({ message: "EventData add successfully" }); } } else { const newProduct = await ProductModel(organization).create({ productId: productId, productName: productName, }); if (newProduct) { if (eventDatas) { await EventsDataModel(organization).create({ productId: productId, modelUuid: eventDatas?.modelUuid, modelName: eventDatas?.modelName, position: eventDatas?.position, rotation: eventDatas?.rotation, type: eventDatas?.type, speed: eventDatas?.speed, point: eventDatas?.point, points: eventDatas?.points, }); } } return res .status(201) .json({ message: "Product created successfully" }); } } catch (error) { res.status(500).json({ message: "Failed to create product" }); } } static async getProductDatas(req: Request, res: Response): Promise { try { const { productId, organization } = req.query; if (typeof productId !== "string" || typeof organization !== "string") { return res .status(400) .json({ message: "Missing or invalid query parameters" }); } const existingProduct = await ProductModel(organization).findOne({ productId: productId, isArchive: false, }); if (!existingProduct) return res.status(404).json({ message: "Product not found" }); const existingEventDatas = await EventsDataModel(organization) .find({ productId: productId }) .select("-productId"); return res.status(200).json(existingEventDatas); } catch (error) { res.status(500).json({ message: "Failed to get product" }); } } static async productDataDelete(req: Request, res: Response): Promise { try { const { productId, organization } = req.query; if (typeof productId !== "string" || typeof organization !== "string") { return res .status(400) .json({ message: "Missing or invalid query parameters" }); } const existingProduct = await ProductModel(organization).findOne({ productId: productId, isArchive: false, }); if (!existingProduct) return res.status(404).json({ message: "Product not found" }); await ProductModel(organization).findOneAndUpdate( { productId: productId }, { isArchive: true, }, { new: true } ); const existingEventDatas = await EventsDataModel(organization).find({ productId: productId, }); if (existingEventDatas) { await EventsDataModel(organization).updateMany( { productId }, { $set: { isArchive: true } } ); } return res.status(201).json({ message: "product deleted successfully" }); } catch (error) { res.status(500).json({ message: "Failed to delete product" }); } } static async EventDataDelete(req: Request, res: Response): Promise { try { const { productId, organization, modelUuid } = req.body; const existingProduct = await ProductModel(organization).findOne({ productId: productId, isArchive: false, }); if (!existingProduct) return res.status(404).json({ message: "Product not found" }); await EventsDataModel(organization).findOneAndUpdate( { productId: productId, modelUuid: modelUuid }, { isArchive: true, }, { new: true } ); return res .status(201) .json({ message: "EventData deleted successfully" }); } catch (error) { res.status(500).json({ message: "Failed to delete Eventdata" }); } } static async AllProductDatas(req: Request, res: Response): Promise { try { const { organization } = req.params; if (!organization) { return res.json({ message: "organization not found" }); } const existingProduct = await ProductModel(organization).find({ isArchive: false, }); if (!existingProduct) { return res.status(404).json({ message: "No products found" }); } const result = []; for (const product of existingProduct) { const eventDatas = await EventsDataModel(organization) .find({ productId: product.productId, isArchive: false }) .select("-productId -isArchive -createdAt -updatedAt -__v -_id"); result.push({ productName: product.productName, productId: product.productId, eventDatas, }); } return res.status(200).json(result); } catch (error) { res.status(500).json({ message: "Failed to get Allproduct" }); } } static async productRename(req: Request, res: Response): Promise { try { const { productId, productName, organization } = req.body; const existingProduct = await ProductModel(organization).findOne({ productId: productId, isArchive: false, }); if (!existingProduct) return res.status(404).json({ message: "Product not found" }); await ProductModel(organization).findOneAndUpdate( { productId: productId }, { productName: productName, }, { new: true } ); return res.status(201).json({ message: "product Rename successfully" }); } catch (error) { res.status(500).json({ message: "Failed to product Rename" }); } } }