Files
Dwinzo-Backend-V0.0/src/shared/services/builder/assetService.ts

481 lines
12 KiB
TypeScript
Raw Normal View History

2025-05-29 09:24:16 +05:30
import { Mixed } from "mongoose";
import assetModel from "../../V1Models/Builder/assetModel.ts";
import EventsDataModel from "../../V1Models/Simulation/eventsDataModel.ts";
import {
existingProjectById,
existingUser,
} from "../helpers/v1projecthelperFns.ts";
2025-05-29 09:24:16 +05:30
2025-06-02 16:48:44 +05:30
interface SetAssetInput {
modelUuid: string;
modelName: string;
position: [];
rotation: object;
eventData: Mixed;
modelfileID: string;
isLocked: boolean;
isVisible: boolean;
organization: string;
projectId: string;
userId: string;
}
interface AssetUpdate {
modelUuid: string;
modelName: string;
position: [];
rotation: object;
isLocked: boolean;
isVisible: boolean;
organization: string;
projectId: string;
userId: string;
}
interface DelAssetInput {
modelUuid: string;
modelName: string;
organization: string;
projectId: string;
userId: string;
}
interface GetAssetInput {
organization: string;
projectId: string;
userId: string;
}
interface ReplaceEventInput {
organization: string;
projectId: string;
userId: string;
eventData: Mixed;
modelUuid: string;
2025-05-29 09:24:16 +05:30
}
export const setAssetModel = async (
2025-06-02 16:48:44 +05:30
data: SetAssetInput
2025-05-29 09:24:16 +05:30
): Promise<{ status: string; data?: Object }> => {
try {
const {
modelUuid,
modelName,
position,
rotation,
eventData,
modelfileID,
isLocked,
isVisible,
organization,
projectId,
userId,
} = 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 findvalue = await assetModel(organization).findOne({
modelUuid: modelUuid,
projectId: projectId,
userId: userId,
isArchive: false,
});
console.log("findvalue: ", findvalue);
if (findvalue) {
const updatevalue = await assetModel(organization).findOneAndUpdate(
{
modelUuid: modelUuid,
projectId: projectId,
userId: userId,
isArchive: false,
},
{
modelName: modelName,
position: position,
rotation: rotation,
isVisible: isVisible,
isLocked: isLocked,
eventData: eventData,
},
{ new: true }
);
return {
status: "Updated successfully",
data: updatevalue,
};
} else {
let assetData: any = {
projectId,
userId,
modelUuid,
modelName,
position,
modelfileID,
rotation,
isLocked,
isVisible,
};
2025-05-29 09:24:16 +05:30
if (eventData) {
const typedEventData = eventData as unknown as {
type: string;
point?: any;
points?: any[];
};
2025-05-29 09:24:16 +05:30
if (typedEventData.type === "Conveyor") {
assetData.eventData = {
type: typedEventData.type,
points: typedEventData.points,
};
} else {
assetData.eventData = {
type: typedEventData.type,
point: typedEventData.point,
};
}
}
2025-05-29 09:24:16 +05:30
if (eventData) {
const typedEventData = eventData as unknown as {
type: string;
point?: any;
points?: any[];
};
2025-05-29 09:24:16 +05:30
if (typedEventData.type === "Conveyor") {
assetData.eventData = {
type: typedEventData.type,
points: typedEventData.points,
};
2025-05-29 09:24:16 +05:30
} else {
assetData.eventData = {
type: typedEventData.type,
point: typedEventData.point,
};
2025-05-29 09:24:16 +05:30
}
}
const assetDoc = await assetModel(organization).create(assetData);
await assetDoc.save();
let assetDatas;
const typedEventData = eventData as unknown as { type: string };
if (typedEventData && typedEventData.type === "Conveyor") {
assetDatas = {
projectId: assetDoc.projectId,
userId: assetDoc.userId,
modelUuid: assetDoc.modelUuid,
modelName: assetDoc.modelName,
modelfileID: assetDoc.modelfileID,
position: assetDoc.position,
rotation: assetDoc.rotation,
isLocked: assetDoc.isLocked,
isVisible: assetDoc.isVisible,
eventData: eventData,
};
} else if (eventData && assetDoc.type === "Vehicle") {
assetDatas = {
projectId: assetDoc.projectId,
userId: assetDoc.userId,
modelUuid: assetDoc.modelUuid,
modelName: assetDoc.modelName,
modelfileID: assetDoc.modelfileID,
position: assetDoc.position,
rotation: assetDoc.rotation,
isLocked: assetDoc.isLocked,
isVisible: assetDoc.isVisible,
eventData: {
points: assetDoc.points,
type: assetDoc.type,
},
};
} else if (eventData && assetDoc.type === "ArmBot") {
assetDatas = {
projectId: assetDoc.projectId,
userId: assetDoc.userId,
modelUuid: assetDoc.modelUuid,
modelName: assetDoc.modelName,
modelfileID: assetDoc.modelfileID,
position: assetDoc.position,
rotation: assetDoc.rotation,
isLocked: assetDoc.isLocked,
isVisible: assetDoc.isVisible,
eventData: {
points: assetDoc.points,
type: assetDoc.type,
},
};
} else if (eventData && assetDoc.type === "StaticMachine") {
assetDatas = {
projectId: assetDoc.projectId,
userId: assetDoc.userId,
modelUuid: assetDoc.modelUuid,
modelName: assetDoc.modelName,
modelfileID: assetDoc.modelfileID,
position: assetDoc.position,
rotation: assetDoc.rotation,
isLocked: assetDoc.isLocked,
isVisible: assetDoc.isVisible,
eventData: {
points: assetDoc.points,
type: assetDoc.type,
},
};
} else {
assetDatas = {
projectId: assetDoc.projectId,
userId: assetDoc.userId,
modelUuid: assetDoc.modelUuid,
modelName: assetDoc.modelName,
modelfileID: assetDoc.modelfileID,
position: assetDoc.position,
rotation: assetDoc.rotation,
isLocked: assetDoc.isLocked,
isVisible: assetDoc.isVisible,
};
}
return {
status: "Success",
data: assetDatas,
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
2025-05-29 09:24:16 +05:30
}
}
2025-05-29 09:24:16 +05:30
};
export const deleteAssetModel = async (
data: DelAssetInput
2025-05-29 09:24:16 +05:30
): Promise<{ status: string; data?: Object }> => {
try {
const { modelUuid, modelName, organization, projectId, userId } = 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 asset = await assetModel(organization).findOne({
modelUuid,
modelName,
projectId,
isArchive: false,
});
if (!asset) {
return {
status: "model not found",
};
}
const archivedAsset = await assetModel(organization).findOneAndUpdate(
{ modelUuid, modelName, projectId },
{ $set: { isArchive: true } },
{ new: true }
);
if (!archivedAsset) {
return {
status: "Failed to archive asset",
};
}
2025-06-02 16:48:44 +05:30
await EventsDataModel(organization).updateMany(
{ modelUuid, projectId: projectId },
{ $set: { isArchive: true } }
);
2025-05-29 09:24:16 +05:30
return {
status: "Success",
data: archivedAsset,
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
2025-05-29 09:24:16 +05:30
}
}
2025-05-29 09:24:16 +05:30
};
export const replaceEventDatas = async (
data: ReplaceEventInput
2025-05-29 09:24:16 +05:30
): Promise<{ status: string; data?: Object }> => {
try {
const { modelUuid, organization, eventData, projectId, userId } = 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 existingModel = await assetModel(organization).findOne({
modelUuid: modelUuid,
projectId: projectId,
isArchive: false,
});
if (!existingModel) {
return { status: "Model not for this UUID" };
} else {
const typedEventData = eventData as unknown as {
speed: number;
points?: any[];
type?: string;
};
let speed;
2025-05-29 09:24:16 +05:30
if (existingModel.type === "Conveyor") {
speed = typedEventData?.speed;
}
const updatedModel = await assetModel(organization).findOneAndUpdate(
{ modelUuid, projectId, isArchive: false },
{
points: typedEventData?.points,
2025-06-02 16:48:44 +05:30
type: typedEventData?.type ?? existingModel?.type,
},
{ new: true }
);
2025-05-29 09:24:16 +05:30
return {
status: "Success",
data: updatedModel,
};
2025-05-29 09:24:16 +05:30
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
2025-05-29 09:24:16 +05:30
};
export const updateAssetPositionRotation = async (
data: AssetUpdate
2025-05-29 09:24:16 +05:30
): Promise<{ status: string; data?: Object }> => {
try {
const {
modelUuid,
modelName,
position,
rotation,
isLocked,
isVisible,
organization,
projectId,
userId,
} = 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 existingAsset = await assetModel(organization).findOne({
modelUuid: modelUuid,
projectId: projectId,
isArchive: false,
});
if (!existingAsset) {
return { status: "Asset not found" };
2025-05-29 09:24:16 +05:30
}
const updateAsset = await assetModel(organization).updateMany(
{
modelUuid: modelUuid,
projectId: projectId,
modelName: modelName,
isArchive: false,
},
{
position: position,
rotation: rotation,
isVisible: isVisible,
isLocked: isLocked,
}
);
return {
status: "Success",
data: updateAsset,
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
2025-05-29 09:24:16 +05:30
};
export const getFloorItems = async (
data: GetAssetInput
2025-05-29 09:24:16 +05:30
): Promise<{ status: string; data?: Object }> => {
try {
const { organization, projectId, userId } = 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 findValues = await assetModel(organization)
.find({ isArchive: false, projectId: projectId })
.select("-_id -isArchive");
2025-05-29 09:24:16 +05:30
if (!findValues || findValues.length === 0) {
return { status: "floorItems not found", data: [] };
}
2025-05-29 09:24:16 +05:30
const response = findValues.map((item) => {
const responseItem: any = {
projectId: item.productId,
modelUuid: item.modelUuid,
modelName: item.modelName,
position: item.position,
rotation: item.rotation,
modelfileID: item.modelfileID,
isLocked: item.isLocked,
isVisible: item.isVisible,
eventData: item.eventData,
};
return responseItem;
});
2025-05-29 09:24:16 +05:30
return {
status: "Success",
data: response,
};
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
2025-05-29 09:24:16 +05:30
}
}
};