From 3e67a1fd88325ebb7b8bc685ea3b11d1b5cfe07a Mon Sep 17 00:00:00 2001 From: Naveen Mathews <52288469+naveenrenji@users.noreply.github.com> Date: Thu, 6 Apr 2023 16:58:35 -0400 Subject: [PATCH 1/3] delete pdf api --- hashroot-be/data/files.js | 37 ++++++++++++++++++- hashroot-be/routes/files.js | 11 ++++++ hashroot/src/api/projects.js | 4 ++ .../components/Projects/ProjectDocuments.js | 25 +++++++++++-- 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/hashroot-be/data/files.js b/hashroot-be/data/files.js index 9f19a8f..7c893fd 100644 --- a/hashroot-be/data/files.js +++ b/hashroot-be/data/files.js @@ -112,4 +112,39 @@ const downloadPdfFile = async (req, res) => { } }; -export { uploadPdfFile, downloadPdfFile }; +const deletePdfFile = async (req, res) => { + try { + const { fileId } = req.params; + if (!fileId) { + return res.status(400).json({ error: "File Id is required!" }); + } + + const projectCollection = await projects(); + const project = await projectCollection.findOneAndUpdate( + { + _id: new ObjectId(req.project._id), + "documents.fileId": new ObjectId(fileId), + }, + { + $pull: { + documents: { + fileId: new ObjectId(fileId), + }, + }, + }, + { returnDocument: "after" } + ); + + if (!project.value) { + return res.status(400).json({ error: "File not found!" }); + } + + const bucket = await getBucket(); + bucket.delete(new ObjectId(fileId)); + return "done"; + } catch (err) { + return res.status(500).json({ error: err.toString() }); + } +}; + +export { uploadPdfFile, downloadPdfFile, deletePdfFile }; diff --git a/hashroot-be/routes/files.js b/hashroot-be/routes/files.js index bc38e1e..10136de 100644 --- a/hashroot-be/routes/files.js +++ b/hashroot-be/routes/files.js @@ -37,4 +37,15 @@ router.route("/:fileId/sign").patch(async (req, res) => { } }); +router.route("/:fileId/delete").delete(async (req, res) => { + try { + let a = await filesData.deletePdfFile(req, res); + if(a==="done"){ + return res.status(200); + } + } catch (e) { + return res.status(500).json({ error: e.toString() }); + } +}); + export default router; diff --git a/hashroot/src/api/projects.js b/hashroot/src/api/projects.js index 1ce5f4e..4f68223 100644 --- a/hashroot/src/api/projects.js +++ b/hashroot/src/api/projects.js @@ -46,6 +46,10 @@ export const downloadProjectDocumentApi = async (projectId, fileId) => { return await http.get(`/projects/${projectId}/files/${fileId}/download`); }; +export const deleteProjectDocumentApi = async (projectId, fileId) => { + return await http.delete(`/projects/${projectId}/files/${fileId}/delete`); +} + export const signContractApi = async (projectId, documentId, body) => { const { data: { project }, diff --git a/hashroot/src/components/Projects/ProjectDocuments.js b/hashroot/src/components/Projects/ProjectDocuments.js index 2fa6b89..da92dd0 100644 --- a/hashroot/src/components/Projects/ProjectDocuments.js +++ b/hashroot/src/components/Projects/ProjectDocuments.js @@ -17,9 +17,10 @@ import { BsFillStarFill, BsFillFileEarmarkDiffFill, } from "react-icons/bs"; -// import { AiFillDelete } from "react-icons/ai"; +import { AiFillDelete } from "react-icons/ai"; import { downloadProjectDocumentApi } from "../../api/projects"; +import { deleteProjectDocumentApi } from "../../api/projects"; import { PROJECT_UPLOAD_TYPES } from "../../constants"; import { capitalize } from "../../utils/user"; @@ -42,6 +43,22 @@ const ProjectDocuments = ({ project, onClose }) => { } }; + const handleDocumentDelete = async (document) => { + try { + toast("Deleting Document...", { type: toast.TYPE.INFO, autoClose: 0 }); + const response = await deleteProjectDocumentApi( + project._id, + document.fileId + ); + toast("Document Deleted Successfully", { + type: toast.TYPE.INFO, + autoClose: 0, + }); + } catch (e) { + toast(" Error Deleting document"); + } + }; + const tabDocuments = React.useMemo(() => { if (currentTab === "all") { return project?.documents; @@ -89,6 +106,7 @@ const ProjectDocuments = ({ project, onClose }) => { ))} @@ -139,11 +157,10 @@ const DocumentItem = ({ className="link" onClick={() => handleDocumentDownload(document)} /> - {/* TODO: Need to implement delete file? */} - {/* handleDocumentDelete(document)} - /> */} + /> From fb7f54041dd07f3b6654dc8917b41d3ebf586387 Mon Sep 17 00:00:00 2001 From: Naveen Mathews <52288469+naveenrenji@users.noreply.github.com> Date: Thu, 6 Apr 2023 17:02:33 -0400 Subject: [PATCH 2/3] fix --- hashroot/src/components/Projects/ProjectDocuments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hashroot/src/components/Projects/ProjectDocuments.js b/hashroot/src/components/Projects/ProjectDocuments.js index da92dd0..2477570 100644 --- a/hashroot/src/components/Projects/ProjectDocuments.js +++ b/hashroot/src/components/Projects/ProjectDocuments.js @@ -46,7 +46,7 @@ const ProjectDocuments = ({ project, onClose }) => { const handleDocumentDelete = async (document) => { try { toast("Deleting Document...", { type: toast.TYPE.INFO, autoClose: 0 }); - const response = await deleteProjectDocumentApi( + await deleteProjectDocumentApi( project._id, document.fileId ); From f29920aca2be22d0f676d5e6a84d9d92707da5e8 Mon Sep 17 00:00:00 2001 From: Naveen Mathews <52288469+naveenrenji@users.noreply.github.com> Date: Sun, 9 Apr 2023 22:43:10 -0400 Subject: [PATCH 3/3] document delete --- hashroot-be/data/files.js | 9 ++++++--- hashroot-be/routes/files.js | 6 ++---- hashroot/src/api/projects.js | 5 ++++- hashroot/src/components/Projects/ProjectDocuments.js | 5 ++++- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/hashroot-be/data/files.js b/hashroot-be/data/files.js index 7c893fd..60f5b77 100644 --- a/hashroot-be/data/files.js +++ b/hashroot-be/data/files.js @@ -2,6 +2,7 @@ import uploadFile from "../middleware/uploadFile.js"; import { getBucket, projects } from "../config/mongoCollections.js"; import { ObjectId } from "mongodb"; import { PROJECT_UPLOAD_TYPES } from "../constants.js"; +import { getProjectById } from "./projects.js"; const uploadPdfFile = async (req, res) => { try { @@ -120,7 +121,7 @@ const deletePdfFile = async (req, res) => { } const projectCollection = await projects(); - const project = await projectCollection.findOneAndUpdate( + let project = await projectCollection.findOneAndUpdate( { _id: new ObjectId(req.project._id), "documents.fileId": new ObjectId(fileId), @@ -139,9 +140,11 @@ const deletePdfFile = async (req, res) => { return res.status(400).json({ error: "File not found!" }); } + const bucket = await getBucket(); - bucket.delete(new ObjectId(fileId)); - return "done"; + await bucket.delete(new ObjectId(fileId)); + project = await getProjectById(req.user, req.project._id.toString()); + return project; } catch (err) { return res.status(500).json({ error: err.toString() }); } diff --git a/hashroot-be/routes/files.js b/hashroot-be/routes/files.js index 10136de..d2cff90 100644 --- a/hashroot-be/routes/files.js +++ b/hashroot-be/routes/files.js @@ -39,10 +39,8 @@ router.route("/:fileId/sign").patch(async (req, res) => { router.route("/:fileId/delete").delete(async (req, res) => { try { - let a = await filesData.deletePdfFile(req, res); - if(a==="done"){ - return res.status(200); - } + let project = await filesData.deletePdfFile(req, res); + return res.status(200).json({ project }); } catch (e) { return res.status(500).json({ error: e.toString() }); } diff --git a/hashroot/src/api/projects.js b/hashroot/src/api/projects.js index 4f68223..c22a743 100644 --- a/hashroot/src/api/projects.js +++ b/hashroot/src/api/projects.js @@ -47,7 +47,10 @@ export const downloadProjectDocumentApi = async (projectId, fileId) => { }; export const deleteProjectDocumentApi = async (projectId, fileId) => { - return await http.delete(`/projects/${projectId}/files/${fileId}/delete`); + const { + data: { project }, + } = await http.delete(`/projects/${projectId}/files/${fileId}/delete`); + return project; } export const signContractApi = async (projectId, documentId, body) => { diff --git a/hashroot/src/components/Projects/ProjectDocuments.js b/hashroot/src/components/Projects/ProjectDocuments.js index 2477570..f8de58a 100644 --- a/hashroot/src/components/Projects/ProjectDocuments.js +++ b/hashroot/src/components/Projects/ProjectDocuments.js @@ -23,10 +23,12 @@ import { downloadProjectDocumentApi } from "../../api/projects"; import { deleteProjectDocumentApi } from "../../api/projects"; import { PROJECT_UPLOAD_TYPES } from "../../constants"; import { capitalize } from "../../utils/user"; +import useProject from "../../hooks/useProject"; const tabs = ["all", ...Object.values(PROJECT_UPLOAD_TYPES)]; const ProjectDocuments = ({ project, onClose }) => { + const { updateProject } = useProject(); const [currentTab, setCurrentTab] = React.useState("all"); const handleDocumentDownload = async (document) => { @@ -46,10 +48,11 @@ const ProjectDocuments = ({ project, onClose }) => { const handleDocumentDelete = async (document) => { try { toast("Deleting Document...", { type: toast.TYPE.INFO, autoClose: 0 }); - await deleteProjectDocumentApi( + const response = await deleteProjectDocumentApi( project._id, document.fileId ); + updateProject(response); toast("Document Deleted Successfully", { type: toast.TYPE.INFO, autoClose: 0,