182 lines
4.3 KiB
TypeScript
182 lines
4.3 KiB
TypeScript
import wallItemModel from "../../../shared/model/builder/assets/wallitems-Model.ts";
|
|
import { existingProjectById, existingUser } from "../helpers/v1projecthelperFns.ts";
|
|
interface IWallSetupData {
|
|
modelUuid: string;
|
|
modelName: string;
|
|
type: string;
|
|
csgposition: [];
|
|
csgscale: [];
|
|
position: [];
|
|
quaternion: [];
|
|
scale: [];
|
|
organization: string;
|
|
projectId: string;
|
|
userId: string;
|
|
}
|
|
interface IWallGet {
|
|
userId: string;
|
|
organization: string;
|
|
projectId: string;
|
|
}
|
|
interface IWallDelete {
|
|
userId: string;
|
|
modelUuid: string;
|
|
modelName: string;
|
|
organization: string;
|
|
projectId: string;
|
|
}
|
|
interface IWallItemResult {
|
|
data?: Object;
|
|
status: string;
|
|
}
|
|
export const setWallItems = async (data: IWallSetupData): Promise<IWallItemResult> => {
|
|
try {
|
|
const {
|
|
userId,
|
|
modelUuid,
|
|
modelName,
|
|
position,
|
|
type,
|
|
csgposition,
|
|
csgscale,
|
|
quaternion,
|
|
scale,
|
|
projectId,
|
|
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 findvalue = await wallItemModel(organization).findOne({
|
|
modelUuid: modelUuid,
|
|
});
|
|
|
|
if (findvalue) {
|
|
const updatevalue = await wallItemModel(organization).findOneAndUpdate(
|
|
{ modelUuid: modelUuid, projectId: projectId },
|
|
{
|
|
modelName,
|
|
position,
|
|
type,
|
|
csgposition,
|
|
csgscale,
|
|
quaternion,
|
|
scale,
|
|
},
|
|
{ new: true } // Return the updated document
|
|
);
|
|
return {
|
|
status: "Updated successfully",
|
|
data: updatevalue,
|
|
};
|
|
// res.status(201).json(updatevalue);
|
|
} else {
|
|
const newValue = await wallItemModel(organization).create({
|
|
modelUuid,
|
|
modelName,
|
|
position,
|
|
type,
|
|
projectId,
|
|
csgposition,
|
|
csgscale,
|
|
quaternion,
|
|
scale,
|
|
});
|
|
return {
|
|
status: "wall Item created successfully",
|
|
data: newValue,
|
|
};
|
|
// res.status(201).json(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 } = 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 wallItemModel(organization).find({
|
|
projectId: projectId,
|
|
});
|
|
if (!findValue) {
|
|
return {
|
|
status: "wallitems 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",
|
|
};
|
|
}
|
|
}
|
|
}
|
|
export const deleteWallItems = async (data: IWallDelete): Promise<IWallItemResult> => {
|
|
try {
|
|
const { modelUuid, modelName, organization, userId, 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 findValue = await wallItemModel(organization).findOneAndDelete({
|
|
modelUuid: modelUuid,
|
|
modelName: modelName,
|
|
projectId: projectId,
|
|
});
|
|
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",
|
|
};
|
|
}
|
|
}
|
|
}
|