diff --git a/hashroot-be/data/files.js b/hashroot-be/data/files.js index a43746d..5883d5f 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 { @@ -115,4 +116,41 @@ 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(); + let 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(); + 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() }); + } +}; + +export { uploadPdfFile, downloadPdfFile, deletePdfFile }; diff --git a/hashroot-be/routes/files.js b/hashroot-be/routes/files.js index d28066b..a82a863 100644 --- a/hashroot-be/routes/files.js +++ b/hashroot-be/routes/files.js @@ -39,4 +39,13 @@ router } ); +router.route("/:fileId/delete").delete(async (req, res) => { + try { + let project = await filesData.deletePdfFile(req, res); + return res.status(200).json({ project }); + } 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 8e61e33..4cd341e 100644 --- a/hashroot/src/api/projects.js +++ b/hashroot/src/api/projects.js @@ -46,6 +46,13 @@ export const downloadProjectDocumentApi = async (projectId, fileId) => { return await http.get(`/projects/${projectId}/files/${fileId}/download`); }; +export const deleteProjectDocumentApi = async (projectId, fileId) => { + const { + data: { project }, + } = await http.delete(`/projects/${projectId}/files/${fileId}/delete`); + return project; +} + 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 26e0fff..3955cd5 100644 --- a/hashroot/src/components/Projects/ProjectDocuments.js +++ b/hashroot/src/components/Projects/ProjectDocuments.js @@ -18,12 +18,16 @@ import { BsFillStarFill, BsFillFileEarmarkDiffFill, } from "react-icons/bs"; -// import { AiFillDelete } from "react-icons/ai"; +import { AiFillDelete } from "react-icons/ai"; import { PROJECT_UPLOAD_TYPES } from "../../constants"; import { capitalize } from "../../utils/user"; import { downloadProjectDocumentApi } from "../../api/projects"; import { getProjectDocumentDownloadUrl } from "../../utils/files"; +import { deleteProjectDocumentApi } from "../../api/projects"; +import useProject from "../../hooks/useProject"; + +const { updateProject } = useProject(); import DocumentModal from "../shared/DocumentModal"; @@ -56,6 +60,23 @@ const ProjectDocuments = ({ project, onClose, onUploadClick }) => { } }; + const handleDocumentDelete = async (document) => { + try { + toast("Deleting Document...", { type: toast.TYPE.INFO, autoClose: 0 }); + const response = await deleteProjectDocumentApi( + project._id, + document.fileId + ); + updateProject(response); + 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; @@ -113,6 +134,7 @@ const ProjectDocuments = ({ project, onClose, onUploadClick }) => { document={document} onClick={handleDocumentView} handleDocumentDownload={handleDocumentDownload} + handleDocumentDelete={handleDocumentDelete} key={document._id} /> ))} @@ -191,11 +213,10 @@ const DocumentItem = ({ className="link" onClick={() => handleDocumentDownload(document)} /> - {/* TODO: Need to implement delete file? */} - {/* handleDocumentDelete(document)} - /> */} + />