Skip to content

Commit

Permalink
Merge pull request #602 from idurar/dev
Browse files Browse the repository at this point in the history
🚚 Fix not found download pdf file
  • Loading branch information
salahlalami authored Oct 25, 2023
2 parents b5700a5 + 5a7bff6 commit 34fb7e6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
16 changes: 11 additions & 5 deletions backend/handlers/downloadHandler/downloadPdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ module.exports = downloadPdf = async (req, res, { directory, id }) => {
await custom.generatePdf(
modelName,
{ filename: modelName, format: 'A4' },
result.invoice ?? result,
result,
async (fileLocation) => {
return res.download(fileLocation, (err) => {
if (err) res.status(500).json({ success: false, message: "Couldn't find file" });
if (err)
res.status(500).json({
success: false,
result: null,
message: "Couldn't find file",
error: err.message,
});
});
}
);
Expand All @@ -31,15 +37,15 @@ module.exports = downloadPdf = async (req, res, { directory, id }) => {
return res.status(400).json({
success: false,
result: null,
error: err,
error: err.message,
message: 'Required fields are not supplied',
});
} else if (err.name == 'BSONTypeError') {
// If err is thrown by Mongoose due to invalid ID
return res.status(400).json({
success: false,
result: null,
error: err,
error: err.message,
message: 'Invalid ID',
});
} else {
Expand All @@ -48,7 +54,7 @@ module.exports = downloadPdf = async (req, res, { directory, id }) => {
return res.status(500).json({
success: false,
result: null,
error: err,
error: err.message,
message: 'Oops there is an Error',
});
}
Expand Down
38 changes: 25 additions & 13 deletions backend/routes/coreRoutes/coreDownloadRouter.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
const downloadPdf = require('@/handlers/downloadHandler/downloadPdf');
const express = require('express');

const path = require('path');
const router = express.Router();
const { hasPermission } = require('@/middlewares/permission');

router.route('/:subPath/:directory/:id').get(function (req, res) {
const { subPath, directory, id } = req.params;
// router.route('/:directory/:file').get(function (req, res) {
// const { directory, file } = req.params;

// Handle the /payment/* route
if (subPath == 'payment' && directory == 'invoice') {
downloadPdf(req, res, { directory: 'Payment', id });
} else {
downloadPdf(req, res, { directory, id });
}
});
// // Handle the /payment/* route

// router.route('/:directory/:id').get(function (req, res) {
// const { directory, id } = req.params;
// const options = {
// root: path.join(__dirname, `../../public/download/${directory}`),
// dotfiles: 'deny',
// headers: {
// 'Content-type': 'application/pdf',
// 'Content-disposition': 'inline; filename="' + file + '"',
// },
// };

// downloadPdf(req, res, { directory, id });
// res.status(200).sendFile(file, options, function (err) {
// if (err) {
// const id = file.slice(directory.length + 1).slice(0, -4); // extract id from file name
// downloadPdf(req, res, { directory, id });
// }
// });
// });

router.route('/:directory/:file').get(function (req, res) {
const { directory, file } = req.params;

const id = file.slice(directory.length + 1).slice(0, -4); // extract id from file name
downloadPdf(req, res, { directory, id });
});

module.exports = router;

0 comments on commit 34fb7e6

Please sign in to comment.