172 lines
4.2 KiB
TypeScript
172 lines
4.2 KiB
TypeScript
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, state, userId, position, rotation, comments, organization } = 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,
|
|
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, 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, 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, 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,
|
|
};
|
|
}
|
|
} |