import projectModel from "../../V1Models/Project/project-model.ts"; import { existingUser } from "../helpers/v1projecthelperFns.ts"; interface IOrg { organization: string; role: string; userId: string; } interface IRestore { projectId: string; organization: string; role: string; userId: string; } interface RoleFilter { isArchive: boolean; createdBy?: string; } export const TrashDatas = async (data: IOrg) => { try { const { organization, role, userId } = data; let filter = { isArchive: true, isDeleted: false } as RoleFilter; // if (role === "User") { // filter.createdBy = userId; // } const UserExists = await existingUser(userId, organization); if (!UserExists) return { status: "User not found" }; const TrashLists = await projectModel(organization).find(filter); 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, role, userId } = data; const UserExists = await existingUser(userId, organization); if (!UserExists) return { status: "User not found" }; let filter = { isArchive: true, _id: projectId } as RoleFilter; // if (role === "User") { // filter.createdBy = userId; // } const findProject = await projectModel(organization).findOne(filter); if (!findProject) return { status: "Project not found" }; const restoreData = await projectModel(organization).findOneAndUpdate( filter, { isArchive: false, DeletedAt: null }, { new: true } ); if (!restoreData) return { status: "Project Restore unsuccessfull" }; return { status: "Project Restored successfully" }; } catch (error) { return { status: error }; } }; export const TrashDelete = async (data: IRestore) => { try { const { projectId, organization, userId } = data; const UserExists = await existingUser(userId, organization); if (!UserExists) return { status: "User not found" }; const findProject = await projectModel(organization).findOne({ _id: projectId, isArchive: true, }); if (!findProject) return { status: "Project not found" }; const DeleteTrashData = await projectModel(organization).findOneAndUpdate( { _id: projectId, isArchive: true, isDeleted: false }, { isDeleted: true }, { new: true } ); if (!DeleteTrashData) return { status: "Project Already Deleted" }; return { status: "Trash Project Delete successfully" }; } catch (error) { return { status: error }; } };