Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backend] The DELETE teamMemebers API should also delete the respective file stored in the disk Fixed #930

Merged
merged 2 commits into from
May 19, 2024
Merged
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
39 changes: 23 additions & 16 deletions backend/app/routes/teamMember/deleteTeamMember.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
const teamMemberModel = require('../../models/TeamMember');
module.exports = async(req,res,next) => {
try {
const payload = res.locals.decode;
const memberId = req.body.memberId;
if (payload.isSuperAdmin === false) {
return res.status(401).json({ error: 'You are not an admin' });
}
const result = await teamMemberModel.findByIdAndDelete(memberId);
if(!result) {
return res.status(401).json({error:"Invalid id"});
}
return res.json({message:"Deleted successfully"});
const fs = require('fs');
const path = require('path');

module.exports = async (req, res, next) => {
try {
const payload = res.locals.decode;
const memberId = req.body.memberId;
if (payload.isSuperAdmin === false) {
return res.status(401).json({ error: 'You are not an admin' });
}
catch(error) {
return res.status(500).json({error:"Some internal server error"});
const result = await teamMemberModel.findByIdAndDelete(memberId);

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources High

This query object depends on a
user-provided value
.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This result should have the detail of delete object, can't we do like below? -

const result = await teamMemberModel.findByIdAndDelete(memberId);
if (!result) {
  return res.status(401).json({ error: 'Invalid id' });
}

const fileName = path.basename(result.image);
const imagePath = path.join(__dirname, '../../../uploads/teamMembersProfile/', fileName);
fs.unlink(imagePath, (err) => {
  if (err) {
    console.error('Failed to delete image file:', err);
  }
  console.log('Image file deleted:', imagePath);
});

return res.status(200).json({ message: 'Team member deleted successfully' });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kajol-Kumari thank you. I forget that delete has the deleted object. so I make extra. Now i updated it.

if (!result) {
return res.status(401).json({ error: 'Invalid id' });
}

}
if (result?.image) {
const fileName = path.basename(result.image);
const imagePath = path.join(__dirname, '../../../uploads/teamMembersProfile/', fileName);
fs.unlink(imagePath, (err) => console.log(err));
}
return res.json({ message: 'Deleted successfully' });
} catch (error) {
console.error(error);
return res.status(500).json({ error: 'Some internal server error' });
}
};
Loading