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

557 lines
14 KiB
TypeScript

import { Mixed } from "mongoose";
import EventsDataModel from "../../V1Models/Simulation/eventsDataModel.ts";
import ProductModel from "../../V1Models/Simulation/productModel.ts";
import {
existingProjectById,
existingUser,
LivingCurrentVersion,
} from "../helpers/v1projecthelperFns.ts";
import versionModel from "../../V1Models/Version/versionModel.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;
position: [number];
rotation: [number];
type: string;
speed: string;
point: Mixed;
points: Mixed;
};
userId: string;
organization: string;
projectId: string;
versionId: string;
}
interface IProductEvent {
productUuid: string;
userId: string;
organization: string;
projectId: string;
versionId: string;
}
interface IProductEventDel {
productUuid: string;
userId: string;
organization: string;
projectId: string;
versionId: string;
}
interface IDelEvent {
productUuid: string;
modelUuid: string;
userId: string;
organization: string;
projectId: string;
versionId: string;
}
interface IProjectProducts {
userId: string;
organization: string;
projectId: string;
versionId: string;
}
interface IProductRename {
userId: string;
organization: string;
projectId: string;
versionId: 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,
versionId,
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 ExistingVersion = await LivingCurrentVersion(
organization,
LivingProject._id,
versionId
);
if (!ExistingVersion) return { status: "Version Data not found" };
const existingProduct = await ProductAFind(
productUuid,
projectId,
versionId,
organization
);
if (existingProduct) {
const existingEventData = await EventsDataModel(organization).findOne({
productUuid: productUuid,
projectId: projectId,
versionId: versionId,
modelUuid: eventDatas.modelUuid,
isArchive: false,
});
if (existingEventData) {
await EventUpdateFunction(
organization,
eventDatas,
productUuid,
projectId,
versionId
);
return {
status: "EventData updated successfully",
};
} else {
await EventCreateFunction(
organization,
eventDatas,
productUuid,
projectId,
versionId
);
return {
status: "EventData add successfully",
};
}
} else {
const newProduct = await ProductModel(organization).create({
productUuid: productUuid,
projectId: projectId,
productName: productName,
versionId: versionId,
});
if (newProduct) {
if (eventDatas) {
await EventCreateFunction(
organization,
eventDatas,
productUuid,
projectId,
versionId
);
}
}
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,
versionId: string
) {
await EventsDataModel(organization).create({
versionId: versionId,
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,
versionId: string
) {
await EventsDataModel(organization).findOneAndUpdate(
{
projectId: projectId,
versionId: versionId,
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,
versionId: string,
organization: string
) {
const existingProduct = await ProductModel(organization).findOne({
productUuid: productUuid,
projectId: projectId,
versionId: versionId,
isArchive: false,
});
return existingProduct;
}
export const getProductDatas = async (
data: IProductEvent
): Promise<IResult> => {
try {
const { productUuid, projectId,versionId, 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 VersionGetId = versionId ? versionId : LivingProject.Present_version;
const ExistingVersion = await LivingCurrentVersion(
organization,
LivingProject._id,
VersionGetId
);
if (!ExistingVersion) return { status: "Version Data not found" };
const ExistingProduct = await ProductAFind(
productUuid,
projectId,
ExistingVersion._id,
organization
);
if (!ExistingProduct) return { status: "Product not found" };
const existingEventDatas = await EventsDataModel(organization)
.find({
productUuid: productUuid,
projectId: projectId,
versionId: ExistingVersion._id,
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: IProductEventDel
): Promise<IResult> => {
try {
const { productUuid, projectId, versionId, 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 ExistingVersion = await LivingCurrentVersion(
organization,
LivingProject._id,
versionId
);
if (!ExistingVersion) return { status: "Version Data not found" };
const ExistingProduct = await ProductAFind(
productUuid,
projectId,
versionId,
organization
);
if (!ExistingProduct) return { status: "Product not found" };
await ProductModel(organization).findOneAndUpdate(
{
productUuid: productUuid,
projectId: projectId,
versionId: versionId,
isArchive: false,
},
{
isArchive: true,
},
{ new: true }
);
const existingEventDatas = await EventsDataModel(organization).find({
productUuid: productUuid,
projectId: projectId,
versionId: versionId,
isArchive: false,
});
if (existingEventDatas) {
await EventsDataModel(organization).updateMany(
{
productUuid,
projectId,
versionId: versionId,
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,
versionId,
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 ExistingVersion = await LivingCurrentVersion(
organization,
LivingProject._id,
versionId
);
if (!ExistingVersion) return { status: "Version Data not found" };
const ExistingProduct = await ProductAFind(
productUuid,
projectId,
versionId,
organization
);
if (!ExistingProduct) return { status: "Product not found" };
const EventDel = await EventsDataModel(organization).findOneAndUpdate(
{
productUuid: productUuid,
projectId: projectId,
versionId: versionId,
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, versionId ,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 VersionGetId = versionId ? versionId : LivingProject.Present_version;
const ExistingVersion = await LivingCurrentVersion(
organization,
LivingProject._id,
VersionGetId
);
if (!ExistingVersion) return { status: "Version Data not found" };
const existingProduct = await ProductModel(organization).find({
isArchive: false,
versionId: ExistingVersion._id,
projectId: projectId,
});
if (!existingProduct) {
return {
status: "No products found",
data: [],
};
}
const result = [];
for (const product of existingProduct) {
const eventDatas = await EventsDataModel(organization)
.find({
versionId: ExistingVersion._id,
projectId: product.projectId,
productUuid: product.productUuid,
isArchive: false,
})
.select("-productUuid -isArchive -createdAt -updatedAt -__v -_id");
result.push({
projectId: product.projectId,
versionId: product.versionId,
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,
versionId,
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 ExistingVersion = await LivingCurrentVersion(
organization,
LivingProject._id,
versionId
);
if (!ExistingVersion) return { status: "Version Data not found" };
const ExistingProduct = await ProductAFind(
productUuid,
projectId,
versionId,
organization
);
if (!ExistingProduct) return { status: "Product not found" };
const UpdateName = await ProductModel(organization).findOneAndUpdate(
{
productUuid: productUuid,
projectId: projectId,
versionId: versionId,
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",
};
}
}
};