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

@@ -7,6 +7,7 @@ export interface Project extends Document {
projectName: string;
createdBy: User["_id"];
isArchive: boolean;
isDeleted: boolean;
thumbnail: string;
sharedUsers: [];
DeletedAt: Date;
@@ -22,6 +23,7 @@ const projectSchema: Schema = new Schema(
createdBy: { type: Schema.Types.ObjectId, ref: "user" },
sharedUsers: [{ type: Schema.Types.ObjectId, ref: "user" }],
DeletedAt: { type: Date, default: null },
isDeleted: { type: Boolean, default: false },
total_versions: { type: String },
Present_version: { type: String },
},

View File

@@ -0,0 +1,49 @@
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";
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;
}
const userData = await userModel(organization).findOne({
_id: userId,
});
return userData;
};
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
) => {
const result = await versionModel(organization).findOne({
projectId: projectId,
isArchive: false,
});
// .sort({ version: -1 });
return result;
};

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;
};

View File

@@ -0,0 +1,68 @@
import projectModel from "../../model/project/project-model.ts";
interface IOrg {
organization: string;
}
interface IRestore {
projectId: string;
organization: string;
}
export const TrashDatas = async (data: IOrg) => {
try {
const { organization } = data;
const TrashLists = await projectModel(organization).find({
isArchive: true,
isDeleted: false,
});
if (!TrashLists) return { staus: "Trash is Empty" };
const TrashDocs: any[] = [];
for (const Trash of TrashLists) {
const now = new Date();
const deletedPlus15 = new Date(
Trash.DeletedAt.getTime() + 15 * 24 * 60 * 60 * 1000
);
if (now > deletedPlus15) {
console.log("now > deletedPlus15: ", now > deletedPlus15);
await projectModel(organization).updateOne(
{ _id: Trash._id },
{ $set: { isDeleted: true } }
);
} else {
TrashDocs.push(Trash);
}
}
const ListDatas = TrashDocs.map((data) => {
return {
projectName: data.projectName,
thumbnail: data.thumbnail,
createdBy: data.createdBy,
_id: data._id,
};
});
return { status: "Success", ListDatas };
} catch (error) {
return { status: error };
}
};
export const RestoreTrashData = async (data: IRestore) => {
try {
const { projectId, organization } = data;
console.log("projectId: ", projectId);
const findProject = await projectModel(organization).findOne({
_id: projectId,
isArchive: true,
});
console.log("findProject: ", findProject);
if (!findProject) return { status: "Project not found" };
const restoreData = await projectModel(organization).findOneAndUpdate(
{ _id: projectId, isArchive: true },
{ isArchive: false, DeletedAt: null },
{ new: true }
);
if (!restoreData) return { status: "Project Restore unsuccessfull" };
return { status: "Project Restored successfully" };
} catch (error) {
return { status: error };
}
};