205 lines
6.1 KiB
TypeScript
205 lines
6.1 KiB
TypeScript
import projectModel from "../../model/project/project-model.ts";
|
|
import userModel from "../../model/user-Model.ts";
|
|
import versionModel from "../../model/version/versionModel.ts";
|
|
import {
|
|
existingProject,
|
|
existingUser,
|
|
archiveProject,
|
|
previousVersion,
|
|
generateUntitledProjectName,
|
|
} from "../helpers/ProjecthelperFn.ts";
|
|
interface CreateProjectInput {
|
|
projectName: string;
|
|
projectUuid: string;
|
|
userId: string; // user ID
|
|
thumbnail?: string;
|
|
sharedUsers?: string[];
|
|
organization: string;
|
|
}
|
|
interface updateProjectInput {
|
|
projectName: string;
|
|
projectId: string;
|
|
userId: string; // user ID
|
|
thumbnail?: string;
|
|
sharedUsers?: string[];
|
|
organization: string;
|
|
}
|
|
interface GetProjectsInterface {
|
|
userId: string;
|
|
organization: string;
|
|
}
|
|
interface ProjectInterface {
|
|
projectId: string;
|
|
userId: string;
|
|
organization: string;
|
|
}
|
|
|
|
export const createProject = async (data: CreateProjectInput) => {
|
|
try {
|
|
const { userId, thumbnail, sharedUsers, organization, projectUuid } = data;
|
|
const userExisting = await existingUser(userId, organization);
|
|
if (!userExisting) {
|
|
return {
|
|
status: "user_not_found",
|
|
};
|
|
}
|
|
const projectExisting = await existingProject(
|
|
projectUuid,
|
|
organization,
|
|
userId
|
|
);
|
|
|
|
if (projectExisting) {
|
|
return {
|
|
status: "project_exists",
|
|
project: projectExisting,
|
|
};
|
|
}
|
|
const newProjectName = await generateUntitledProjectName(
|
|
organization,
|
|
userId
|
|
);
|
|
const project = await projectModel(organization).create({
|
|
projectName: newProjectName,
|
|
projectUuid: projectUuid,
|
|
createdBy: userId,
|
|
thumbnail: thumbnail,
|
|
sharedUsers: sharedUsers || [],
|
|
isArchive: false,
|
|
});
|
|
const versionData = await previousVersion(project._id, organization);
|
|
if (!versionData || versionData.length === 0) {
|
|
const newVersion = await versionModel(organization).create({
|
|
projectId: project._id,
|
|
createdBy: userId,
|
|
version: 0.01,
|
|
});
|
|
await projectModel(organization).findByIdAndUpdate(
|
|
{ _id: project._id, isArchive: false },
|
|
{ total_versions: `v-${newVersion.version.toFixed(2)}` }
|
|
);
|
|
}
|
|
return {
|
|
status: "Success",
|
|
project: project,
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
status: error,
|
|
};
|
|
}
|
|
};
|
|
|
|
export const GetAllProjects = async (data: GetProjectsInterface) => {
|
|
try {
|
|
const { userId, organization } = data;
|
|
await existingUser(userId, organization);
|
|
if (!existingUser) return { status: "User not found" };
|
|
const projectDatas = await projectModel(organization)
|
|
.find({
|
|
createdBy: userId,
|
|
isArchive: false,
|
|
})
|
|
.select(
|
|
"_id projectName createdBy thumbnail createdAt projectUuid createdAt"
|
|
);
|
|
if (projectDatas) return { status: "Success", Datas: projectDatas };
|
|
} catch (error: unknown) {
|
|
return { status: error };
|
|
}
|
|
};
|
|
|
|
export const DeleteProject = async (data: ProjectInterface) => {
|
|
try {
|
|
const { projectId, organization, userId } = data;
|
|
const ExistingUser = await existingUser(userId, organization);
|
|
if (!ExistingUser) return { status: "User not found" };
|
|
const existingProject = await projectModel(organization).findOne({
|
|
_id: projectId,
|
|
createdBy: userId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingProject) return { status: "Project not found" };
|
|
const updateProject = await projectModel(organization).findOneAndUpdate(
|
|
{ _id: projectId, isArchive: false },
|
|
{ isArchive: true, DeletedAt: new Date() },
|
|
{ new: true }
|
|
);
|
|
if (updateProject) return { status: "Success", project: updateProject };
|
|
} catch (error: unknown) {
|
|
return { status: error };
|
|
}
|
|
};
|
|
export const updateProject = async (data: updateProjectInput) => {
|
|
try {
|
|
const { projectId, organization, userId, projectName, thumbnail } = data;
|
|
const ExistingUser = await existingUser(userId, organization);
|
|
if (!ExistingUser) return { status: "User not found" };
|
|
const existingProject = await projectModel(organization).findOne({
|
|
_id: projectId,
|
|
createdBy: userId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingProject) return { status: "Project not found" };
|
|
if (projectName !== undefined) projectName;
|
|
if (thumbnail !== undefined) thumbnail;
|
|
const updateProject = await projectModel(organization)
|
|
.findOneAndUpdate(
|
|
{ _id: projectId, isArchive: false },
|
|
{ projectName: projectName, thumbnail: thumbnail },
|
|
{ new: true }
|
|
)
|
|
.select("_id projectName createdBy thumbnail createdAt projectUuid");
|
|
if (updateProject) return { status: "Success", data: updateProject };
|
|
} catch (error: unknown) {
|
|
return { status: error };
|
|
}
|
|
};
|
|
const maxLength: number = 6;
|
|
export const viewProject = async (data: ProjectInterface) => {
|
|
try {
|
|
const { projectId, organization, userId } = data;
|
|
const userExisting = await existingUser(userId, organization);
|
|
if (!userExisting) return { status: "User not found" };
|
|
const existingProject = await projectModel(organization).findOne({
|
|
_id: projectId,
|
|
createdBy: userId,
|
|
isArchive: false,
|
|
});
|
|
if (!existingProject) return { status: "Project not found" };
|
|
const newArr = userExisting?.recentlyViewed || [];
|
|
if (userExisting?.recentlyViewed.length === 0) {
|
|
newArr.push(projectId);
|
|
} else {
|
|
const index = newArr.indexOf(projectId);
|
|
if (index !== -1) {
|
|
newArr.splice(index, 1);
|
|
}
|
|
newArr.unshift(projectId);
|
|
|
|
if (newArr.length > maxLength) {
|
|
newArr.pop();
|
|
}
|
|
}
|
|
await userModel(organization).updateOne(
|
|
{ _id: userId },
|
|
{ recentlyViewed: newArr },
|
|
{ new: true }
|
|
);
|
|
const projectData = await projectModel(organization)
|
|
.findOneAndUpdate(
|
|
{
|
|
_id: projectId,
|
|
createdBy: userId,
|
|
isArchive: false,
|
|
},
|
|
{ isViewed: Date.now() },
|
|
{ new: true }
|
|
)
|
|
.select("_id projectName createdBy thumbnail createdAt");
|
|
return { status: "Success", data: projectData };
|
|
} catch (error: unknown) {
|
|
return { status: error };
|
|
}
|
|
};
|