81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
|
|
import projectModel from "../../model/project/project-model.ts";
|
||
|
|
import userModel from "../../model/user-Model.ts";
|
||
|
|
import { Types } from 'mongoose';
|
||
|
|
interface CreateProjectInput {
|
||
|
|
projectName: string;
|
||
|
|
projectUuid: string;
|
||
|
|
createdBy: string; // user ID
|
||
|
|
thumbnail?: string;
|
||
|
|
sharedUsers?: string[];
|
||
|
|
organization:string
|
||
|
|
}
|
||
|
|
|
||
|
|
export const createProject = async (data: CreateProjectInput) => {
|
||
|
|
console.log('data: ', data);
|
||
|
|
try {
|
||
|
|
const{projectName,projectUuid,createdBy,thumbnail,sharedUsers,organization}=data
|
||
|
|
console.log('createdBy: ', typeof createdBy);
|
||
|
|
const userExisting =await existingUser(createdBy,organization)
|
||
|
|
if (!userExisting)
|
||
|
|
{
|
||
|
|
return {
|
||
|
|
status: "user_not_found",
|
||
|
|
};
|
||
|
|
|
||
|
|
}
|
||
|
|
const projectExisting = await existingProject(projectUuid, organization);
|
||
|
|
console.log('projectExisting: ', projectExisting);
|
||
|
|
|
||
|
|
if (projectExisting) {
|
||
|
|
return {
|
||
|
|
status: "project_exists",
|
||
|
|
project: projectExisting,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
const project = await projectModel(organization).create({
|
||
|
|
projectName: projectName,
|
||
|
|
projectUuid: projectUuid,
|
||
|
|
createdBy: createdBy,
|
||
|
|
thumbnail: thumbnail || "",
|
||
|
|
sharedUsers: sharedUsers || [],
|
||
|
|
isArchive: false,
|
||
|
|
});
|
||
|
|
return {
|
||
|
|
status: "success",
|
||
|
|
project: project,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
console.log('error: ', error);
|
||
|
|
return {
|
||
|
|
exists: false,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
};
|
||
|
|
|
||
|
|
export const existingProject = async (projectUuid: string,organization:string) => {
|
||
|
|
console.log("projectUuid",typeof projectUuid);
|
||
|
|
const projectData= await projectModel(organization).findOne({projectUuid:projectUuid,isArchive:false})
|
||
|
|
console.log('projectData: ', projectData);
|
||
|
|
return projectData
|
||
|
|
};
|
||
|
|
|
||
|
|
export const existingUser = async (createdBy: string, organization: string) => {
|
||
|
|
console.log('createdBy: ', typeof createdBy);
|
||
|
|
if (!Types.ObjectId.isValid(createdBy)) {
|
||
|
|
console.log('Invalid ObjectId format');
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
const userData = await userModel(organization).findOne({
|
||
|
|
_id: createdBy,
|
||
|
|
});
|
||
|
|
console.log('userData:', userData);
|
||
|
|
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 });
|
||
|
|
};
|