Files
Dwinzo-Backend-V0.0/src/shared/services/simulation/productService.ts

453 lines
11 KiB
TypeScript
Raw Normal View History

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 IEventDatainterface {
modelUuid: string;
modelName: string;
position: [number];
rotation: [number];
type: string;
speed: string;
point: Mixed;
points: Mixed;
}
interface Iproduct {
productName: string;
productUuid: string;
eventDatas: {
modelUuid: string;
modelName: string;
2025-06-02 16:48:44 +05:30
position: [number];
rotation: [number];
type: string;
speed: string;
point: Mixed;
points: Mixed;
};
userId: string;
organization: string;
projectId: string;
}
interface IProductEvent {
productUuid: string;
userId: string;
organization: string;
projectId: string;
}
interface IDelEvent {
productUuid: string;
modelUuid: string;
userId: string;
organization: string;
projectId: string;
}
interface IProjectProducts {
userId: string;
organization: string;
projectId: string;
}
interface IProductRename {
userId: string;
organization: string;
projectId: string;
productUuid: string;
productName: string;
}
interface IResult {
status: string;
data?: object;
}
export const productAdd = async (data: Iproduct): Promise<IResult> => {
try {
const {
productName,
productUuid,
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 ProductAFind(
productUuid,
projectId,
organization
);
if (existingProduct) {
const existingEventData = await EventsDataModel(organization).findOne({
productUuid: productUuid,
projectId: projectId,
modelUuid: eventDatas.modelUuid,
isArchive: false,
});
if (existingEventData) {
await EventUpdateFunction(
organization,
eventDatas,
productUuid,
projectId
);
return {
status: "EventData updated successfully",
};
} else {
await EventCreateFunction(
organization,
eventDatas,
productUuid,
projectId
);
return {
status: "EventData add successfully",
};
}
} else {
const newProduct = await ProductModel(organization).create({
productUuid: productUuid,
projectId: projectId,
productName: productName,
});
if (newProduct) {
if (eventDatas) {
await EventCreateFunction(
organization,
eventDatas,
productUuid,
projectId
);
}
}
return {
status: "Success",
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
async function EventCreateFunction(
organization: string,
eventDatas: IEventDatainterface,
productUuid: string,
projectId: string
) {
await EventsDataModel(organization).create({
projectId: projectId,
productUuid: productUuid,
modelUuid: eventDatas?.modelUuid as string,
modelName: eventDatas?.modelName,
position: eventDatas?.position,
rotation: eventDatas?.rotation,
type: eventDatas?.type,
speed: eventDatas?.speed,
point: eventDatas?.point,
points: eventDatas?.points,
});
}
async function EventUpdateFunction(
organization: string,
eventDatas: IEventDatainterface,
productUuid: string,
projectId: string
) {
await EventsDataModel(organization).findOneAndUpdate(
{
projectId: projectId,
modelUuid: eventDatas.modelUuid,
productUuid: productUuid,
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,
}
);
}
async function ProductAFind(
productUuid: string,
projectId: string,
organization: string
) {
const existingProduct = await ProductModel(organization).findOne({
productUuid: productUuid,
projectId: projectId,
isArchive: false,
});
return existingProduct;
}
export const getProductDatas = async (
data: IProductEvent
): Promise<IResult> => {
try {
const { productUuid, 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 ProductAFind(
productUuid,
projectId,
organization
);
if (!ExistingProduct) return { status: "Product not found" };
const existingEventDatas = await EventsDataModel(organization)
.find({
productUuid: productUuid,
projectId: projectId,
isArchive: false,
})
.select("-productUuid");
if (!existingEventDatas) {
return { status: "Events not found", data: [] };
}
return { status: "Success", data: existingEventDatas };
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const productDataDelete = async (
data: IProductEvent
): Promise<IResult> => {
try {
const { productUuid, 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 ProductAFind(
productUuid,
projectId,
organization
);
if (!ExistingProduct) return { status: "Product not found" };
await ProductModel(organization).findOneAndUpdate(
{ productUuid: productUuid, projectId: projectId, isArchive: false },
{
isArchive: true,
},
{ new: true }
);
const existingEventDatas = await EventsDataModel(organization).find({
productUuid: productUuid,
projectId: projectId,
isArchive: false,
});
if (existingEventDatas) {
await EventsDataModel(organization).updateMany(
{ productUuid, projectId, isArchive: false },
{ $set: { isArchive: true } }
);
}
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: IDelEvent): Promise<IResult> => {
try {
const { modelUuid, productUuid, 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 ProductAFind(
productUuid,
projectId,
organization
);
if (!ExistingProduct) return { status: "Product not found" };
const EventDel = await EventsDataModel(organization).findOneAndUpdate(
{
productUuid: productUuid,
projectId: projectId,
isArchive: false,
modelUuid: modelUuid,
},
{
isArchive: true,
},
{ new: true }
);
if (!EventDel)
return {
status: "Event Delete Unsuccessful",
};
return {
status: "Success",
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const AllProductDatas = async (
data: IProjectProducts
): 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,
projectId: projectId,
});
if (!existingProduct) {
return {
status: "No products found",
data: [],
};
}
const result = [];
for (const product of existingProduct) {
const eventDatas = await EventsDataModel(organization)
.find({
projectId: product.projectId,
productUuid: product.productUuid,
isArchive: false,
})
.select("-productUuid -isArchive -createdAt -updatedAt -__v -_id");
result.push({
projectId: product.projectId,
productName: product.productName,
productUuid: product.productUuid,
eventDatas,
});
}
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: IProductRename): Promise<IResult> => {
try {
const { productName, productUuid, 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 ProductAFind(
productUuid,
projectId,
organization
);
if (!ExistingProduct) return { status: "Product not found" };
const UpdateName = await ProductModel(organization).findOneAndUpdate(
{ productUuid: productUuid, projectId: projectId, isArchive: false },
{
productName: productName,
},
{ new: true }
);
if (!UpdateName) {
return {
status: "Rename Unsuccessful",
};
}
return {
status: "Success",
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};