diff --git a/backend/controllers/file.ts b/backend/controllers/file.ts index d4fef9d..a25b683 100644 --- a/backend/controllers/file.ts +++ b/backend/controllers/file.ts @@ -729,9 +729,9 @@ class FileController { try { const fileID = req.body.id; const userID = req.user._id; - const parentID = req.body.parent; + const folderID = req.body.parentID; - await fileService.moveFile(userID, fileID, parentID); + await fileService.moveFile(userID, fileID, folderID); res.send(); } catch (e: unknown) { diff --git a/backend/db/utils/fileUtils/index.ts b/backend/db/utils/fileUtils/index.ts index 6ea6c69..dcbad7d 100644 --- a/backend/db/utils/fileUtils/index.ts +++ b/backend/db/utils/fileUtils/index.ts @@ -188,7 +188,8 @@ class DbUtil { "metadata.parent": parent, "metadata.parentList": parentList, }, - } + }, + { new: true } ); return file; diff --git a/backend/services/FileService/index.ts b/backend/services/FileService/index.ts index 9ae07a6..f923325 100644 --- a/backend/services/FileService/index.ts +++ b/backend/services/FileService/index.ts @@ -38,8 +38,7 @@ class MongoFileService { removeLink = async (userID: string, fileID: string) => { const file = await dbUtilsFile.removeLink(fileID, userID); - if (!file.lastErrorObject.updatedExisting) - throw new NotFoundError("Remove Link File Not Found Error"); + if (!file) throw new NotFoundError("Remove Link File Not Found Error"); }; makePublic = async (userID: string, fileID: string) => { @@ -47,17 +46,13 @@ class MongoFileService { const file = await dbUtilsFile.makePublic(fileID, userID, token); - if (!file.lastErrorObject.updatedExisting) - throw new NotFoundError("Make Public File Not Found Error"); + if (!file) throw new NotFoundError("Make Public File Not Found Error"); return token; }; getPublicInfo = async (fileID: string, tempToken: string) => { - const file: FileInterface = await dbUtilsFile.getPublicInfo( - fileID, - tempToken - ); + const file = await dbUtilsFile.getPublicInfo(fileID, tempToken); if (!file || !file.metadata.link || file.metadata.link !== tempToken) { throw new NotFoundError("Public Info Not Found"); @@ -71,8 +66,7 @@ class MongoFileService { const file = await dbUtilsFile.makeOneTimePublic(fileID, userID, token); - if (!file.lastErrorObject.updatedExisting) - throw new NotFoundError("Make One Time Public Not Found Error"); + if (!file) throw new NotFoundError("Make One Time Public Not Found Error"); return token; }; @@ -319,28 +313,29 @@ class MongoFileService { return file; }; - moveFile = async (userID: string, fileID: string, parentID: string) => { - let parentList = ["/"]; + moveFile = async (userID: string, fileID: string, folderID: string) => { + const file = await dbUtilsFile.getFileInfo(fileID, userID); - if (parentID.length !== 1) { - const parentFile = await dbUtilsFolder.getFolderInfo(parentID, userID); - if (!parentFile) - throw new NotFoundError("Rename Parent File Not Found Error"); - const parentList = parentFile.parentList; - parentList.push(parentID); - } + if (!file) throw new NotFoundError("Move File Not Found Error"); - const file = await dbUtilsFile.moveFile( + const folder = await dbUtilsFolder.getFolderInfo(folderID, userID); + + if (!folder) throw new NotFoundError("Move Folder Not Found Error"); + + const newParentList = [...folder.parentList, folder._id]; + + const updatedFile = await dbUtilsFile.moveFile( fileID, userID, - parentID, - parentList.toString() + folderID, + newParentList.toString() ); - if (!file || !file.lastErrorObject.updatedExisting) - throw new NotFoundError("Rename File Not Found Error"); + if (!updatedFile) { + throw new NotFoundError("Move Updated File Not Found Error"); + } - return file; + return updatedFile; }; } diff --git a/backend/services/FolderService/index.ts b/backend/services/FolderService/index.ts index afb4450..542c1c4 100644 --- a/backend/services/FolderService/index.ts +++ b/backend/services/FolderService/index.ts @@ -219,6 +219,16 @@ class FolderService { await utilsFile.restoreFilesByParent(parentList.toString(), userID); }; + renameFolder2 = async (folderID: string, userID: string, title: string) => { + const folder = await utilsFolder.getFolderInfo(folderID, userID); + + if (!folder) throw new NotFoundError("Rename Folder Not Found"); + + folder.name = title; + + await folder.save(); + }; + moveFolder = async (userID: string, folderID: string, parentID: string) => { let parentList = ["/"]; diff --git a/src/components/ParentBar/index.tsx b/src/components/ParentBar/index.tsx index ef2846e..a716ce2 100644 --- a/src/components/ParentBar/index.tsx +++ b/src/components/ParentBar/index.tsx @@ -5,7 +5,6 @@ import { useFolder } from "../../hooks/folders"; const ParentBar = memo(() => { const { data: folder } = useFolder(); - console.log("folder", folder); const navigate = useNavigate(); const { isHome, isTrash } = useUtils();