Skip to content

Commit

Permalink
🍁 [Backend] The DELETE teamMemebers API should also delete the respec…
Browse files Browse the repository at this point in the history
…tive file stored in the disk Fixed (#930)

* backend delete image fixed

* Update deleteTeamMember.js
  • Loading branch information
Hemu21 authored May 19, 2024
1 parent 3172361 commit 02e2e09
Showing 1 changed file with 23 additions and 16 deletions.
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);
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' });
}
};

0 comments on commit 02e2e09

Please sign in to comment.