Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion hashroot-be/data/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 };
9 changes: 9 additions & 0 deletions hashroot-be/routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 7 additions & 0 deletions hashroot/src/api/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
29 changes: 25 additions & 4 deletions hashroot/src/components/Projects/ProjectDocuments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -113,6 +134,7 @@ const ProjectDocuments = ({ project, onClose, onUploadClick }) => {
document={document}
onClick={handleDocumentView}
handleDocumentDownload={handleDocumentDownload}
handleDocumentDelete={handleDocumentDelete}
key={document._id}
/>
))}
Expand Down Expand Up @@ -191,11 +213,10 @@ const DocumentItem = ({
className="link"
onClick={() => handleDocumentDownload(document)}
/>
{/* TODO: Need to implement delete file? */}
{/* <AiFillDelete
<AiFillDelete
className="link"
onClick={() => handleDocumentDelete(document)}
/> */}
/>
</Stack>
</Card.Footer>
</Card>
Expand Down