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

232 lines
5.4 KiB
TypeScript

import wallItemModel from "../../V1Models/Builder/wallItemsModel.ts";
import {
existingProjectById,
existingUser,
LivingCurrentVersion,
} from "../helpers/v1projecthelperFns.ts";
interface IWallSetupData {
modelUuid: string;
modelName: string;
type: string;
csgposition: [];
csgscale: [];
position: [];
quaternion: [];
scale: [];
organization: string;
projectId: string;
versionId: string;
userId: string;
assetId: string;
}
interface IWallGet {
userId: string;
organization: string;
projectId: string;
versionId: string;
}
interface IWallDelete {
userId: string;
modelUuid: string;
modelName: string;
organization: string;
projectId: string;
versionId: string;
}
interface IWallItemResult {
data?: Object;
status: string;
}
export const setWallItems = async (
data: IWallSetupData
): Promise<IWallItemResult> => {
try {
const {
userId,
modelUuid,
modelName,
assetId,
position,
type,
csgposition,
csgscale,
quaternion,
scale,
projectId,
versionId,
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 findvalue = await wallItemModel(organization).findOne({
modelUuid: modelUuid,
projectId: projectId,
versionId: versionId,
isArchive: false,
});
if (findvalue) {
const updatevalue = await wallItemModel(organization).findOneAndUpdate(
{
modelUuid: modelUuid,
projectId: projectId,
versionId: versionId,
isArchive: false,
},
{
modelName,
position,
type,
csgposition,
csgscale,
quaternion,
scale,
},
{ new: true }
);
return {
status: "Updated successfully",
data: updatevalue,
};
} else {
const newValue = await wallItemModel(organization).create({
modelUuid,
modelName,
position,
type,
versionId: versionId,
projectId: projectId,
csgposition,
csgscale,
quaternion,
scale,
userId,
assetId,
});
return {
// status: "wall Item created successfully",
status: "Success",
data: newValue,
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const getWallItems = async (data: IWallGet) => {
try {
const { organization, userId, projectId, versionId } = 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 findValue = await wallItemModel(organization).find({
projectId: projectId,
versionId: ExistingVersion._id,
isArchive: false,
});
if (!findValue) {
return {
status: "wallitems not found",
data: [],
};
} else {
return {
status: "Success",
data: findValue,
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};
export const deleteWallItems = async (
data: IWallDelete
): Promise<IWallItemResult> => {
try {
const { modelUuid, modelName, organization, userId, versionId, projectId } =
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 findValue = await wallItemModel(organization).findOneAndDelete({
modelUuid: modelUuid,
modelName: modelName,
projectId: projectId,
versionId: versionId,
isArchive: false,
});
if (!findValue) {
return {
status: "model not found",
};
} else {
return {
status: "Success",
data: findValue,
};
}
} catch (error: unknown) {
if (error instanceof Error) {
return {
status: error.message,
};
} else {
return {
status: "An unexpected error occurred",
};
}
}
};