2025-05-29 17:05:34 +05:30
|
|
|
import { Mixed } from "mongoose";
|
|
|
|
|
import EventsDataModel from "../../V1Models/Simulation/eventsDataModel.ts";
|
|
|
|
|
import ProductModel from "../../V1Models/Simulation/productModel.ts";
|
2025-06-02 11:22:06 +05:30
|
|
|
import {
|
|
|
|
|
existingProjectById,
|
|
|
|
|
existingUser,
|
|
|
|
|
} from "../helpers/v1projecthelperFns.ts";
|
2025-05-29 17:05:34 +05:30
|
|
|
|
|
|
|
|
interface Iproduct {
|
2025-06-02 11:22:06 +05:30
|
|
|
productName: string;
|
|
|
|
|
productId: string;
|
|
|
|
|
eventDatas: {
|
|
|
|
|
modelUuid: string;
|
|
|
|
|
modelName: string;
|
2025-06-02 16:48:44 +05:30
|
|
|
position: [number];
|
|
|
|
|
rotation: [number];
|
2025-06-02 11:22:06 +05:30
|
|
|
type: string;
|
|
|
|
|
speed: string;
|
|
|
|
|
point: Mixed;
|
|
|
|
|
points: Mixed;
|
|
|
|
|
};
|
|
|
|
|
userId: string;
|
|
|
|
|
organization: string;
|
|
|
|
|
projectId: string;
|
2025-05-29 17:05:34 +05:30
|
|
|
}
|
|
|
|
|
interface IResult {
|
2025-06-02 11:22:06 +05:30
|
|
|
status: string;
|
|
|
|
|
data?: object;
|
2025-05-29 17:05:34 +05:30
|
|
|
}
|
|
|
|
|
interface IEventDataDelete {
|
2025-06-02 11:22:06 +05:30
|
|
|
productId: string;
|
|
|
|
|
modelUuid: string;
|
|
|
|
|
userId: string;
|
|
|
|
|
organization: string;
|
|
|
|
|
projectId: string;
|
2025-05-29 17:05:34 +05:30
|
|
|
}
|
|
|
|
|
export const productAdd = async (data: Iproduct): Promise<IResult> => {
|
2025-06-02 11:22:06 +05:30
|
|
|
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,
|
2025-05-29 17:05:34 +05:30
|
|
|
isArchive: false,
|
2025-06-02 11:22:06 +05:30
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
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",
|
|
|
|
|
};
|
|
|
|
|
} 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,
|
2025-05-29 17:05:34 +05:30
|
|
|
});
|
2025-06-02 11:22:06 +05:30
|
|
|
return {
|
|
|
|
|
status: "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,
|
|
|
|
|
});
|
2025-05-29 17:05:34 +05:30
|
|
|
}
|
2025-06-02 11:22:06 +05:30
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
status: "Success",
|
|
|
|
|
};
|
2025-05-29 17:05:34 +05:30
|
|
|
}
|
2025-06-02 11:22:06 +05:30
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
return {
|
|
|
|
|
status: error.message,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
status: "An unexpected error occurred",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-05-29 17:05:34 +05:30
|
|
|
export const getProductDatas = async (data: Iproduct): Promise<IResult> => {
|
2025-06-02 11:22:06 +05:30
|
|
|
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,
|
|
|
|
|
});
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
if (!existingProduct) return { status: "Product not found" };
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
const existingEventDatas = await EventsDataModel(organization)
|
|
|
|
|
.find({ productId: productId, projectId: projectId })
|
|
|
|
|
.select("-productId");
|
|
|
|
|
return { status: "Success", data: existingEventDatas };
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
return {
|
|
|
|
|
status: error.message,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
status: "An unexpected error occurred",
|
|
|
|
|
};
|
2025-05-29 17:05:34 +05:30
|
|
|
}
|
2025-06-02 11:22:06 +05:30
|
|
|
}
|
|
|
|
|
};
|
2025-05-29 17:05:34 +05:30
|
|
|
export const productDataDelete = async (data: Iproduct): Promise<IResult> => {
|
2025-06-02 11:22:06 +05:30
|
|
|
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,
|
|
|
|
|
});
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
if (!existingProduct) return { status: "Product not found" };
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
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 } }
|
|
|
|
|
);
|
2025-05-29 17:05:34 +05:30
|
|
|
}
|
2025-06-02 11:22:06 +05:30
|
|
|
return {
|
|
|
|
|
status: "Success",
|
|
|
|
|
};
|
|
|
|
|
} 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,
|
|
|
|
|
});
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
if (!existingProduct) return { status: "Product not found" };
|
|
|
|
|
await EventsDataModel(organization).findOneAndUpdate(
|
|
|
|
|
{ productId: productId, projectId: projectId, modelUuid: modelUuid },
|
|
|
|
|
{
|
|
|
|
|
isArchive: true,
|
|
|
|
|
},
|
|
|
|
|
{ new: true }
|
|
|
|
|
);
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
return {
|
|
|
|
|
status: "Success",
|
|
|
|
|
};
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
return {
|
|
|
|
|
status: error.message,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
status: "An unexpected error occurred",
|
|
|
|
|
};
|
2025-05-29 17:05:34 +05:30
|
|
|
}
|
2025-06-02 11:22:06 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
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 {
|
|
|
|
|
status: "No products found",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const result = [];
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
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");
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
result.push({
|
|
|
|
|
projectId: product.projectId,
|
|
|
|
|
productName: product.productName,
|
|
|
|
|
productId: product.productId,
|
|
|
|
|
eventDatas,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
return {
|
|
|
|
|
status: "Success",
|
|
|
|
|
data: result,
|
|
|
|
|
};
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
return {
|
|
|
|
|
status: error.message,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
status: "An unexpected error occurred",
|
|
|
|
|
};
|
2025-05-29 17:05:34 +05:30
|
|
|
}
|
2025-06-02 11:22:06 +05:30
|
|
|
}
|
|
|
|
|
};
|
2025-05-29 17:05:34 +05:30
|
|
|
export const productRename = async (data: Iproduct): Promise<IResult> => {
|
2025-06-02 11:22:06 +05:30
|
|
|
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,
|
|
|
|
|
});
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
if (!existingProduct) return { status: "Product not found" };
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
await ProductModel(organization).findOneAndUpdate(
|
|
|
|
|
{ productId: productId },
|
|
|
|
|
{
|
|
|
|
|
productName: productName,
|
|
|
|
|
},
|
|
|
|
|
{ new: true }
|
|
|
|
|
);
|
2025-05-29 17:05:34 +05:30
|
|
|
|
2025-06-02 11:22:06 +05:30
|
|
|
return {
|
|
|
|
|
status: "Success",
|
|
|
|
|
};
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
return {
|
|
|
|
|
status: error.message,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
status: "An unexpected error occurred",
|
|
|
|
|
};
|
2025-05-29 17:05:34 +05:30
|
|
|
}
|
2025-06-02 11:22:06 +05:30
|
|
|
}
|
|
|
|
|
};
|