Trash Get, Restore API completed, Project Delete API completed

This commit is contained in:
2025-05-15 14:08:14 +05:30
parent 79b6a4fa75
commit 46dafd4417
10 changed files with 342 additions and 86 deletions

View File

@@ -2,8 +2,12 @@ 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";
import { uploadProjectThumbnail } from "../blob/blobServices.ts";
import {
existingProject,
existingUser,
archiveProject,
previousVersion,
} from "../helpers/ProjecthelperFn.ts";
interface CreateProjectInput {
projectName: string;
projectUuid: string;
@@ -12,8 +16,13 @@ interface CreateProjectInput {
sharedUsers?: string[];
organization: string;
}
interface GetInterface {
userId: string;
interface GetProjectsInterface {
userId: string;
organization: string;
}
interface DeleteProjectInterface {
projectId: string;
userId: string;
organization: string;
}
export const createProject = async (data: CreateProjectInput) => {
@@ -26,22 +35,17 @@ export const createProject = async (data: CreateProjectInput) => {
sharedUsers,
organization,
} = data;
if (
!projectName ||
!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(projectName, organization,userId);
const projectExisting = await existingProject(
projectName,
organization,
userId
);
if (projectExisting) {
return {
@@ -60,26 +64,29 @@ export const createProject = async (data: CreateProjectInput) => {
});
const versionData = await previousVersion(project._id, organization);
if (!versionData || versionData.length === 0) {
const newVersion= await versionModel(organization).create({
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)}`})
await projectModel(organization).findByIdAndUpdate(
{ _id: project._id, isArchive: false },
{ total_versions: `v-${newVersion.version.toFixed(2)}` }
);
}
return {
status: "success",
status: "Success",
project: project,
};
} catch (error) {
console.log('error: ', error);
console.log("error: ", error);
return {
status: error,
};
}
};
export const GetAllProjects = async (data: GetInterface) => {
export const GetAllProjects = async (data: GetProjectsInterface) => {
try {
const { userId, organization } = data;
await existingUser(userId, organization);
@@ -89,53 +96,30 @@ export const GetAllProjects = async (data: GetInterface) => {
isArchive: false,
})
.select("_id projectName createdBy thumbnail");
if (projectDatas) return {status:"success", Datas: projectDatas };
if (projectDatas) return { status: "Success", Datas: projectDatas };
} catch (error: unknown) {
return { status: error };
}
};
export const existingProject = async (
projectName: string,
organization: string,
userId:string
) => {
const projectData = await projectModel(organization).findOne({
projectName: projectName,
createdBy:userId,
isArchive: false,
});
return projectData;
};
export const existingUser = async (userId: string, organization: string) => {
if (!Types.ObjectId.isValid(userId)) {
console.log("Invalid ObjectId format");
return null;
export const DeleteProject = async (data: DeleteProjectInterface) => {
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" };
} catch (error: unknown) {
return { status: error };
}
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
)=> {
console.log('projectId: ', projectId);
const result = await versionModel(organization).findOne({ projectId: projectId, isArchive: false})
console.log('result: ', result);
// .sort({ version: -1 });
return result;
};