136 lines
3.3 KiB
TypeScript
136 lines
3.3 KiB
TypeScript
|
|
import projectModel from "../../model/project/project-model.ts";
|
||
|
|
import userModel from "../../model/user-Model.ts";
|
||
|
|
import { Types } from "mongoose";
|
||
|
|
import versionModel from "../../model/version/versionModel.ts";
|
||
|
|
interface CreateProjectInput {
|
||
|
|
projectName: string;
|
||
|
|
projectUuid: string;
|
||
|
|
userId: string; // user ID
|
||
|
|
thumbnail?: string;
|
||
|
|
sharedUsers?: string[];
|
||
|
|
organization: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export const createProject = async (data: CreateProjectInput) => {
|
||
|
|
try {
|
||
|
|
const {
|
||
|
|
projectName,
|
||
|
|
projectUuid,
|
||
|
|
userId,
|
||
|
|
thumbnail,
|
||
|
|
sharedUsers,
|
||
|
|
organization,
|
||
|
|
} = data;
|
||
|
|
if (
|
||
|
|
!projectName ||
|
||
|
|
!projectUuid ||
|
||
|
|
!userId ||
|
||
|
|
!thumbnail ||
|
||
|
|
// !sharedUsers ||
|
||
|
|
!organization
|
||
|
|
)
|
||
|
|
return { status: "All fields are required" };
|
||
|
|
const userExisting = await existingUser(userId, organization);
|
||
|
|
if (!userExisting) {
|
||
|
|
return {
|
||
|
|
status: "user_not_found",
|
||
|
|
};
|
||
|
|
}
|
||
|
|
const projectExisting = await existingProject(projectUuid, organization);
|
||
|
|
|
||
|
|
if (projectExisting) {
|
||
|
|
return {
|
||
|
|
status: "project_exists",
|
||
|
|
project: projectExisting,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
const project = await projectModel(organization).create({
|
||
|
|
projectName: projectName,
|
||
|
|
projectUuid: projectUuid,
|
||
|
|
createdBy: userId,
|
||
|
|
thumbnail: thumbnail || "",
|
||
|
|
sharedUsers: sharedUsers || [],
|
||
|
|
isArchive: false,
|
||
|
|
});
|
||
|
|
const versionData = previousVersion(project._id, organization);
|
||
|
|
if (!versionData) {
|
||
|
|
await versionModel(organization).create({
|
||
|
|
projectId: project._id,
|
||
|
|
createdBy: userId,
|
||
|
|
version: 0.01,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
status: "success",
|
||
|
|
project: project,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
console.log('error: ', error);
|
||
|
|
return {
|
||
|
|
status: error,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
export const GetAllProjects = async (data: CreateProjectInput) => {
|
||
|
|
try {
|
||
|
|
const { userId, organization } = data;
|
||
|
|
await existingUser(userId, organization);
|
||
|
|
if (!existingUser) return { status: "User not found" };
|
||
|
|
const projectDatas = await projectModel(organization)
|
||
|
|
.find({
|
||
|
|
isArchive: false,
|
||
|
|
})
|
||
|
|
.select("_id projectName createdBy thumbnail");
|
||
|
|
if (projectDatas) return { Datas: projectDatas };
|
||
|
|
} catch (error: unknown) {
|
||
|
|
return { status: error };
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
export const existingProject = async (
|
||
|
|
projectUuid: string,
|
||
|
|
organization: string
|
||
|
|
) => {
|
||
|
|
const projectData = await projectModel(organization).findOne({
|
||
|
|
projectUuid: projectUuid,
|
||
|
|
isArchive: false,
|
||
|
|
});
|
||
|
|
return projectData;
|
||
|
|
};
|
||
|
|
|
||
|
|
export const existingUser = async (userId: string, organization: string) => {
|
||
|
|
if (!Types.ObjectId.isValid(userId)) {
|
||
|
|
console.log("Invalid ObjectId format");
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
const userData = await userModel(organization).findOne({
|
||
|
|
_id: userId,
|
||
|
|
});
|
||
|
|
return userData; // ✅ Make sure you return it
|
||
|
|
};
|
||
|
|
|
||
|
|
export const archiveProject = async (
|
||
|
|
projectId: string,
|
||
|
|
organization: string
|
||
|
|
) => {
|
||
|
|
return await projectModel(organization).findByIdAndUpdate(
|
||
|
|
projectId,
|
||
|
|
{ isArchive: true },
|
||
|
|
{ new: true }
|
||
|
|
);
|
||
|
|
};
|
||
|
|
export const previousVersion = async (
|
||
|
|
projectId: string,
|
||
|
|
organization: string
|
||
|
|
): Promise<void> => {
|
||
|
|
const result = await versionModel(organization)
|
||
|
|
.findOne({
|
||
|
|
projectId: projectId,
|
||
|
|
isArchive: false,
|
||
|
|
})
|
||
|
|
.sort({ version: -1 });
|
||
|
|
return result;
|
||
|
|
};
|