375 lines
13 KiB
TypeScript
375 lines
13 KiB
TypeScript
import { Mixed } from "mongoose";
|
|
import EventsDataModel from "../../V1Models/Simulation/eventsDataModel.ts";
|
|
import ProductModel from "../../V1Models/Simulation/productModel.ts";
|
|
import { existingProjectById, existingUser } from "../helpers/v1projecthelperFns.ts";
|
|
|
|
interface Iproduct {
|
|
productName: string;
|
|
productId: string;
|
|
eventDatas: {
|
|
modelUuid: string
|
|
modelName: string
|
|
position: [Number]
|
|
rotation: [Number]
|
|
type: string
|
|
speed: string
|
|
point: Mixed
|
|
points: Mixed
|
|
};
|
|
userId: string;
|
|
organization: string;
|
|
projectId: string;
|
|
}
|
|
interface IResult {
|
|
status: string;
|
|
data?: object;
|
|
}
|
|
interface IEventDataDelete {
|
|
productId: string;
|
|
modelUuid: string
|
|
userId: string;
|
|
organization: string;
|
|
projectId: string;
|
|
}
|
|
export const productAdd = async (data: Iproduct): Promise<IResult> => {
|
|
try {
|
|
const { productName, productId, eventDatas, projectId, userId, organization } = 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 existingProduct = await ProductModel(organization).findOne({
|
|
productId: productId, projectId: projectId,
|
|
isArchive: false,
|
|
});
|
|
if (existingProduct) {
|
|
const existingEventData = await EventsDataModel(organization).findOne({
|
|
productId: productId,
|
|
projectId: projectId,
|
|
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 {
|
|
status: "EventData updated successfully"
|
|
}
|
|
// 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 {
|
|
status: "EventData add successfully"
|
|
}
|
|
// 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 {
|
|
status: "Success"
|
|
}
|
|
// return res
|
|
// .status(201)
|
|
// .json({ message: "Product created successfully" });
|
|
}
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
}
|
|
export const getProductDatas = async (data: Iproduct): Promise<IResult> => {
|
|
try {
|
|
const { productId, projectId, userId, organization } = 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 existingProduct = await ProductModel(organization).findOne({
|
|
productId: productId, projectId: projectId,
|
|
isArchive: false,
|
|
});
|
|
|
|
if (!existingProduct)
|
|
return { status: "Product not found" };
|
|
// return res.status(404).json({ message: "Product not found" });
|
|
|
|
const existingEventDatas = await EventsDataModel(organization)
|
|
.find({ productId: productId, projectId: projectId })
|
|
.select("-productId");
|
|
return { status: "Success", data: existingEventDatas };
|
|
// return res.status(200).json(existingEventDatas);
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
}
|
|
export const productDataDelete = async (data: Iproduct): Promise<IResult> => {
|
|
try {
|
|
const { productId, projectId, userId, organization } = 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 existingProduct = await ProductModel(organization).findOne({
|
|
productId: productId,
|
|
isArchive: false,
|
|
});
|
|
|
|
if (!existingProduct)
|
|
return { status: "Product not found" };
|
|
// return res.status(404).json({ message: "Product not found" });
|
|
|
|
await ProductModel(organization).findOneAndUpdate(
|
|
{ productId: productId, projectId: projectId },
|
|
{
|
|
isArchive: true,
|
|
},
|
|
{ new: true }
|
|
);
|
|
const existingEventDatas = await EventsDataModel(organization).find({
|
|
productId: productId,
|
|
});
|
|
if (existingEventDatas) {
|
|
await EventsDataModel(organization).updateMany(
|
|
{ productId, projectId },
|
|
{ $set: { isArchive: true } }
|
|
);
|
|
}
|
|
return {
|
|
status: "Success",
|
|
// data: assetDatas,
|
|
};
|
|
// return res.status(201).json({ message: "product deleted successfully" });
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
}
|
|
export const EventDataDelete = async (data: IEventDataDelete): Promise<IResult> => {
|
|
try {
|
|
const { modelUuid, productId, projectId, userId, organization } = 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 existingProduct = await ProductModel(organization).findOne({
|
|
productId: productId, projectId: projectId,
|
|
isArchive: false,
|
|
});
|
|
|
|
if (!existingProduct)
|
|
return { status: "Product not found" };
|
|
// return res.status(404).json({ message: "Product not found" });
|
|
await EventsDataModel(organization).findOneAndUpdate(
|
|
{ productId: productId, projectId: projectId, modelUuid: modelUuid },
|
|
{
|
|
isArchive: true,
|
|
},
|
|
{ new: true }
|
|
);
|
|
|
|
// return res
|
|
// .status(201)
|
|
// .json({ message: "EventData deleted successfully" });
|
|
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 AllProductDatas = async (data: IEventDataDelete): Promise<IResult> => {
|
|
try {
|
|
const { projectId, userId, organization } = 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 existingProduct = await ProductModel(organization).find({
|
|
isArchive: false,
|
|
});
|
|
if (!existingProduct) {
|
|
// return res.status(404).json({ message: "No products found" });
|
|
return {
|
|
status: "No products found",
|
|
// data: result,
|
|
};
|
|
}
|
|
const result = [];
|
|
|
|
for (const product of existingProduct) {
|
|
const eventDatas = await EventsDataModel(organization)
|
|
.find({ projectId: product.projectId, productId: product.productId, isArchive: false })
|
|
.select("-productId -isArchive -createdAt -updatedAt -__v -_id");
|
|
|
|
result.push({
|
|
// product: {
|
|
projectId: product.projectId,
|
|
productName: product.productName,
|
|
productId: product.productId,
|
|
eventDatas,
|
|
// },
|
|
});
|
|
}
|
|
|
|
// return res.status(200).json(result);
|
|
return {
|
|
status: "Success",
|
|
data: result,
|
|
};
|
|
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
}
|
|
export const productRename = async (data: Iproduct): Promise<IResult> => {
|
|
try {
|
|
const { productName, productId, projectId, userId, organization } = 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 existingProduct = await ProductModel(organization).findOne({
|
|
productId: productId, projectId: projectId,
|
|
isArchive: false,
|
|
});
|
|
|
|
if (!existingProduct)
|
|
return { status: "Product not found" };
|
|
|
|
await ProductModel(organization).findOneAndUpdate(
|
|
{ productId: productId },
|
|
{
|
|
productName: productName,
|
|
},
|
|
{ new: true }
|
|
);
|
|
|
|
// return res.status(201).json({ message: "product Rename successfully" });
|
|
return {
|
|
status: "Success",
|
|
// data: assetDatas,
|
|
};
|
|
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
return {
|
|
status: error.message,
|
|
};
|
|
} else {
|
|
return {
|
|
status: "An unexpected error occurred",
|
|
};
|
|
}
|
|
}
|
|
} |