Aisle and Simulation Product API and socket Completed
This commit is contained in:
@@ -5,10 +5,19 @@ 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;
|
||||
productId: string;
|
||||
productUuid: string;
|
||||
eventDatas: {
|
||||
modelUuid: string;
|
||||
modelName: string;
|
||||
@@ -23,22 +32,41 @@ interface Iproduct {
|
||||
organization: string;
|
||||
projectId: string;
|
||||
}
|
||||
interface IResult {
|
||||
status: string;
|
||||
data?: object;
|
||||
interface IProductEvent {
|
||||
productUuid: string;
|
||||
userId: string;
|
||||
organization: string;
|
||||
projectId: string;
|
||||
}
|
||||
interface IEventDataDelete {
|
||||
productId: 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,
|
||||
productId,
|
||||
productUuid,
|
||||
eventDatas,
|
||||
projectId,
|
||||
userId,
|
||||
@@ -52,73 +80,53 @@ export const productAdd = async (data: Iproduct): Promise<IResult> => {
|
||||
userId
|
||||
);
|
||||
if (!LivingProject) return { status: "Project not found" };
|
||||
const existingProduct = await ProductModel(organization).findOne({
|
||||
productId: productId,
|
||||
projectId: projectId,
|
||||
isArchive: false,
|
||||
});
|
||||
const existingProduct = await ProductAFind(
|
||||
productUuid,
|
||||
projectId,
|
||||
organization
|
||||
);
|
||||
if (existingProduct) {
|
||||
const existingEventData = await EventsDataModel(organization).findOne({
|
||||
productId: productId,
|
||||
productUuid: productUuid,
|
||||
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,
|
||||
}
|
||||
await EventUpdateFunction(
|
||||
organization,
|
||||
eventDatas,
|
||||
productUuid,
|
||||
projectId
|
||||
);
|
||||
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,
|
||||
});
|
||||
await EventCreateFunction(
|
||||
organization,
|
||||
eventDatas,
|
||||
productUuid,
|
||||
projectId
|
||||
);
|
||||
return {
|
||||
status: "EventData add successfully",
|
||||
};
|
||||
}
|
||||
} else {
|
||||
const newProduct = await ProductModel(organization).create({
|
||||
productId: productId,
|
||||
productUuid: productUuid,
|
||||
projectId: projectId,
|
||||
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,
|
||||
});
|
||||
await EventCreateFunction(
|
||||
organization,
|
||||
eventDatas,
|
||||
productUuid,
|
||||
projectId
|
||||
);
|
||||
}
|
||||
}
|
||||
return {
|
||||
@@ -137,9 +145,69 @@ export const productAdd = async (data: Iproduct): Promise<IResult> => {
|
||||
}
|
||||
}
|
||||
};
|
||||
export const getProductDatas = async (data: Iproduct): Promise<IResult> => {
|
||||
|
||||
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 { productId, projectId, userId, organization } = data;
|
||||
const { productUuid, projectId, userId, organization } = data;
|
||||
const UserExists = await existingUser(userId, organization);
|
||||
if (!UserExists) return { status: "User not found" };
|
||||
const LivingProject = await existingProjectById(
|
||||
@@ -148,17 +216,23 @@ export const getProductDatas = async (data: Iproduct): Promise<IResult> => {
|
||||
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" };
|
||||
const ExistingProduct = await ProductAFind(
|
||||
productUuid,
|
||||
projectId,
|
||||
organization
|
||||
);
|
||||
if (!ExistingProduct) return { status: "Product not found" };
|
||||
|
||||
const existingEventDatas = await EventsDataModel(organization)
|
||||
.find({ productId: productId, projectId: projectId })
|
||||
.select("-productId");
|
||||
.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) {
|
||||
@@ -172,9 +246,11 @@ export const getProductDatas = async (data: Iproduct): Promise<IResult> => {
|
||||
}
|
||||
}
|
||||
};
|
||||
export const productDataDelete = async (data: Iproduct): Promise<IResult> => {
|
||||
export const productDataDelete = async (
|
||||
data: IProductEvent
|
||||
): Promise<IResult> => {
|
||||
try {
|
||||
const { productId, projectId, userId, organization } = data;
|
||||
const { productUuid, projectId, userId, organization } = data;
|
||||
const UserExists = await existingUser(userId, organization);
|
||||
if (!UserExists) return { status: "User not found" };
|
||||
const LivingProject = await existingProjectById(
|
||||
@@ -183,26 +259,28 @@ export const productDataDelete = async (data: Iproduct): Promise<IResult> => {
|
||||
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" };
|
||||
const ExistingProduct = await ProductAFind(
|
||||
productUuid,
|
||||
projectId,
|
||||
organization
|
||||
);
|
||||
if (!ExistingProduct) return { status: "Product not found" };
|
||||
|
||||
await ProductModel(organization).findOneAndUpdate(
|
||||
{ productId: productId, projectId: projectId },
|
||||
{ productUuid: productUuid, projectId: projectId, isArchive: false },
|
||||
{
|
||||
isArchive: true,
|
||||
},
|
||||
{ new: true }
|
||||
);
|
||||
const existingEventDatas = await EventsDataModel(organization).find({
|
||||
productId: productId,
|
||||
productUuid: productUuid,
|
||||
projectId: projectId,
|
||||
isArchive: false,
|
||||
});
|
||||
if (existingEventDatas) {
|
||||
await EventsDataModel(organization).updateMany(
|
||||
{ productId, projectId },
|
||||
{ productUuid, projectId, isArchive: false },
|
||||
{ $set: { isArchive: true } }
|
||||
);
|
||||
}
|
||||
@@ -221,11 +299,9 @@ export const productDataDelete = async (data: Iproduct): Promise<IResult> => {
|
||||
}
|
||||
}
|
||||
};
|
||||
export const EventDataDelete = async (
|
||||
data: IEventDataDelete
|
||||
): Promise<IResult> => {
|
||||
export const EventDataDelete = async (data: IDelEvent): Promise<IResult> => {
|
||||
try {
|
||||
const { modelUuid, productId, projectId, userId, organization } = data;
|
||||
const { modelUuid, productUuid, projectId, userId, organization } = data;
|
||||
const UserExists = await existingUser(userId, organization);
|
||||
if (!UserExists) return { status: "User not found" };
|
||||
const LivingProject = await existingProjectById(
|
||||
@@ -234,21 +310,28 @@ export const EventDataDelete = async (
|
||||
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 EventsDataModel(organization).findOneAndUpdate(
|
||||
{ productId: productId, projectId: projectId, modelUuid: modelUuid },
|
||||
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",
|
||||
};
|
||||
@@ -265,7 +348,7 @@ export const EventDataDelete = async (
|
||||
}
|
||||
};
|
||||
export const AllProductDatas = async (
|
||||
data: IEventDataDelete
|
||||
data: IProjectProducts
|
||||
): Promise<IResult> => {
|
||||
try {
|
||||
const { projectId, userId, organization } = data;
|
||||
@@ -279,10 +362,12 @@ export const AllProductDatas = async (
|
||||
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 = [];
|
||||
@@ -291,15 +376,15 @@ export const AllProductDatas = async (
|
||||
const eventDatas = await EventsDataModel(organization)
|
||||
.find({
|
||||
projectId: product.projectId,
|
||||
productId: product.productId,
|
||||
productUuid: product.productUuid,
|
||||
isArchive: false,
|
||||
})
|
||||
.select("-productId -isArchive -createdAt -updatedAt -__v -_id");
|
||||
.select("-productUuid -isArchive -createdAt -updatedAt -__v -_id");
|
||||
|
||||
result.push({
|
||||
projectId: product.projectId,
|
||||
productName: product.productName,
|
||||
productId: product.productId,
|
||||
productUuid: product.productUuid,
|
||||
eventDatas,
|
||||
});
|
||||
}
|
||||
@@ -320,9 +405,9 @@ export const AllProductDatas = async (
|
||||
}
|
||||
}
|
||||
};
|
||||
export const productRename = async (data: Iproduct): Promise<IResult> => {
|
||||
export const productRename = async (data: IProductRename): Promise<IResult> => {
|
||||
try {
|
||||
const { productName, productId, projectId, userId, organization } = data;
|
||||
const { productName, productUuid, projectId, userId, organization } = data;
|
||||
const UserExists = await existingUser(userId, organization);
|
||||
if (!UserExists) return { status: "User not found" };
|
||||
const LivingProject = await existingProjectById(
|
||||
@@ -331,22 +416,25 @@ export const productRename = async (data: Iproduct): Promise<IResult> => {
|
||||
userId
|
||||
);
|
||||
if (!LivingProject) return { status: "Project not found" };
|
||||
const existingProduct = await ProductModel(organization).findOne({
|
||||
productId: productId,
|
||||
projectId: projectId,
|
||||
isArchive: false,
|
||||
});
|
||||
const ExistingProduct = await ProductAFind(
|
||||
productUuid,
|
||||
projectId,
|
||||
organization
|
||||
);
|
||||
if (!ExistingProduct) return { status: "Product not found" };
|
||||
|
||||
if (!existingProduct) return { status: "Product not found" };
|
||||
|
||||
await ProductModel(organization).findOneAndUpdate(
|
||||
{ productId: productId },
|
||||
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",
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user