Files
Dwinzo-Backend-V0.0/src/api-server/controller/simulation/productService.ts

260 lines
9.5 KiB
TypeScript

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<any> {
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) {
const updateEventData = await EventsDataModel(organization).findOneAndUpdate(
{
modelUuid: eventDatas.modelUuid,
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 {
const addEventData = 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) {
return res.status(404).json({ message: "eventData not found" });
} else {
const addEventData = 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<any> {
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<any> {
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 productDelete = await ProductModel(organization).findOneAndUpdate(
{ productId: productId },
{
isArchive: true,
}, { new: true }
)
const existingEventDatas = await EventsDataModel(organization).find({ productId: productId })
if (existingEventDatas) {
for (const event of 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<any> {
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" });
const existingEventDatas = 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<any> {
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) {
// Fetch events data for each product, excluding productId field
const eventDatas = await EventsDataModel(organization)
.find({ productId: product.productId, isArchive: false })
.select("-productId -isArchive -createdAt -updatedAt -__v -_id");
// Combine product and event data
result.push({
// product: {
productName: product.productName,
productId: product.productId,
eventDatas,
// },
});
}
// Return combined data
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<any> {
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" });
const productDelete = 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" });
}
}
}