creation thread and comments in socket
This commit is contained in:
173
src/shared/services/Thread/ThreadService.ts
Normal file
173
src/shared/services/Thread/ThreadService.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
import ThreadModel from "../../V1Models/Thread/thread-Model.ts";
|
||||
import { existingProjectByIdWithoutUser, existingUser } from "../helpers/v1projecthelperFns.ts";
|
||||
|
||||
|
||||
interface IThread {
|
||||
projectId: string;
|
||||
versionId: string;
|
||||
state: string
|
||||
commentId: string;
|
||||
threadId: string;
|
||||
userId: string;
|
||||
createdAt: string;
|
||||
lastUpdatedAt: string;
|
||||
position: [number, number, number];
|
||||
rotation: [number, number, number];
|
||||
comments: string
|
||||
timestamp: number;
|
||||
|
||||
organization: string;
|
||||
}
|
||||
export const createThread = async (data: IThread) => {
|
||||
try {
|
||||
const { projectId, versionId, state, userId, position, rotation, comments, organization, threadId } = data
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) {
|
||||
return {
|
||||
status: "user_not_found",
|
||||
};
|
||||
}
|
||||
const projectExisting = await existingProjectByIdWithoutUser(
|
||||
projectId,
|
||||
organization,
|
||||
|
||||
);
|
||||
|
||||
if (!projectExisting) {
|
||||
return { status: "Project not found" };
|
||||
}
|
||||
const newThread = await ThreadModel(organization).create({
|
||||
projectId,
|
||||
versionId,
|
||||
state,
|
||||
createdBy: userId,
|
||||
position,
|
||||
rotation,
|
||||
comments,
|
||||
createdAt: Date.now()
|
||||
});
|
||||
return {
|
||||
status: "Success",
|
||||
data: newThread,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
status: error,
|
||||
};
|
||||
}
|
||||
}
|
||||
export const deleteThread = async (data: IThread) => {
|
||||
try {
|
||||
const { projectId, versionId, state, userId, organization, threadId } = data
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) {
|
||||
return {
|
||||
status: "user_not_found",
|
||||
};
|
||||
}
|
||||
const projectExisting = await existingProjectByIdWithoutUser(
|
||||
projectId,
|
||||
organization,
|
||||
|
||||
);
|
||||
|
||||
if (!projectExisting) {
|
||||
return { status: "Project not found" };
|
||||
}
|
||||
const findThreadId = await ThreadModel(organization).findOne({ _id: threadId, createdBy: userId })
|
||||
if (!findThreadId) {
|
||||
return { status: "can't deleted" };
|
||||
}
|
||||
const deleteThread = await ThreadModel(organization).findOneAndDelete({ _id: threadId, createdBy: userId })
|
||||
return {
|
||||
status: "Success",
|
||||
data: deleteThread,
|
||||
};
|
||||
} catch (error) {
|
||||
console.log("error: ", error);
|
||||
return {
|
||||
status: error,
|
||||
};
|
||||
}
|
||||
}
|
||||
export const addComments = async (data: IThread) => {
|
||||
try {
|
||||
const { projectId, versionId, userId, comments, organization, threadId } = data
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) {
|
||||
return {
|
||||
status: "user_not_found",
|
||||
};
|
||||
}
|
||||
const projectExisting = await existingProjectByIdWithoutUser(
|
||||
projectId,
|
||||
organization,
|
||||
);
|
||||
|
||||
if (!projectExisting) {
|
||||
return { status: "Project not found" };
|
||||
}
|
||||
const findThreadId = await ThreadModel(organization).findById(threadId)
|
||||
|
||||
const newComment = { userId, comment: comments, timestamp: Date.now() };
|
||||
findThreadId?.replies.push(newComment)
|
||||
await findThreadId?.save()
|
||||
return {
|
||||
status: "Success",
|
||||
data: newComment.comment,
|
||||
};
|
||||
} catch (error) {
|
||||
console.log("error: ", error);
|
||||
return {
|
||||
status: error,
|
||||
};
|
||||
}
|
||||
}
|
||||
export const deleteComments = async (data: IThread) => {
|
||||
try {
|
||||
const { projectId, versionId, userId, commentId, organization, threadId } = data
|
||||
const userExisting = await existingUser(userId, organization);
|
||||
if (!userExisting) {
|
||||
return {
|
||||
status: "user_not_found",
|
||||
};
|
||||
}
|
||||
const projectExisting = await existingProjectByIdWithoutUser(
|
||||
projectId,
|
||||
organization,
|
||||
);
|
||||
|
||||
if (!projectExisting) {
|
||||
return { status: "Project not found" };
|
||||
}
|
||||
const findThreadId = await ThreadModel(organization).findOne({ _id: threadId })
|
||||
if (!findThreadId) {
|
||||
return { status: "thread not found" };
|
||||
}
|
||||
|
||||
const deleted = await ThreadModel(organization).updateOne(
|
||||
{ _id: threadId },
|
||||
{
|
||||
$pull: {
|
||||
replies: {
|
||||
_id: commentId,
|
||||
userId: userId,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (deleted.modifiedCount === 0) {
|
||||
return { status: "unauthorized" };
|
||||
}
|
||||
return {
|
||||
status: "Success",
|
||||
data: deleted
|
||||
};
|
||||
} catch (error) {
|
||||
console.log("error: ", error);
|
||||
return {
|
||||
status: error,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -176,38 +176,38 @@ export const DeleteLayer = async (
|
||||
}
|
||||
};
|
||||
|
||||
export const DeleteLinePoints = async (
|
||||
data: ILinePointsDelete
|
||||
): Promise<{ status: string; data?: object }> => {
|
||||
try {
|
||||
const { organization, projectId, uuid, userId } = data;
|
||||
const findValue = await lineModel(organization).deleteMany({
|
||||
"line.uuid": uuid,
|
||||
});
|
||||
// export const DeleteLinePoints = async (
|
||||
// data: ILinePointsDelete
|
||||
// ): Promise<{ status: string; data?: object }> => {
|
||||
// try {
|
||||
// const { organization, projectId, uuid, userId } = data;
|
||||
// const findValue = await lineModel(organization).deleteMany({
|
||||
// "line.uuid": uuid,
|
||||
// });
|
||||
|
||||
if (!findValue) {
|
||||
return {
|
||||
success: false,
|
||||
message: "line not found",
|
||||
organization: organization,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: true,
|
||||
message: "point deleted",
|
||||
data: uuid,
|
||||
organization: organization,
|
||||
};
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
return {
|
||||
status: error.message,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
status: "An unexpected error occurred",
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
// // if (!findValue) {
|
||||
// // return {
|
||||
// // success: false,
|
||||
// // message: "line not found",
|
||||
// // organization: organization,
|
||||
// // };
|
||||
// // } else {
|
||||
// // return {
|
||||
// // success: true,
|
||||
// // message: "point deleted",
|
||||
// // data: uuid,
|
||||
// // organization: organization,
|
||||
// // };
|
||||
// // }
|
||||
// } catch (error: unknown) {
|
||||
// if (error instanceof Error) {
|
||||
// return {
|
||||
// status: error.message,
|
||||
// };
|
||||
// } else {
|
||||
// return {
|
||||
// status: "An unexpected error occurred",
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
|
||||
@@ -88,3 +88,13 @@ export const existingProjectById = async (
|
||||
});
|
||||
return projectData;
|
||||
};
|
||||
export const existingProjectByIdWithoutUser = async (
|
||||
projectId: string,
|
||||
organization: string,
|
||||
) => {
|
||||
const projectData = await projectModel(organization).findOne({
|
||||
_id: projectId,
|
||||
isArchive: false,
|
||||
});
|
||||
return projectData;
|
||||
};
|
||||
Reference in New Issue
Block a user