From b3fef590270eb06d0b422d285ce522c298c2778b Mon Sep 17 00:00:00 2001 From: Hemu21 Date: Sun, 19 May 2024 00:10:37 +0530 Subject: [PATCH 1/2] backend delete image fixed --- .../app/routes/teamMember/deleteTeamMember.js | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/backend/app/routes/teamMember/deleteTeamMember.js b/backend/app/routes/teamMember/deleteTeamMember.js index d30b8a99..36a41003 100644 --- a/backend/app/routes/teamMember/deleteTeamMember.js +++ b/backend/app/routes/teamMember/deleteTeamMember.js @@ -1,19 +1,33 @@ 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 member = await teamMemberModel.findById(memberId); + if (!member) { + return res.status(401).json({ error: 'Invalid id' }); } - -} + + const result = await teamMemberModel.findByIdAndDelete(memberId); + if (!result) { + return res.status(401).json({ error: 'Invalid id' }); + } + + const fileName = path.basename(member.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' }); + } +}; From a53acb609dd18f973495cfe9eb27ab2affb6a834 Mon Sep 17 00:00:00 2001 From: Hemanth kumar Date: Sun, 19 May 2024 11:22:06 +0530 Subject: [PATCH 2/2] Update deleteTeamMember.js --- .../app/routes/teamMember/deleteTeamMember.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/backend/app/routes/teamMember/deleteTeamMember.js b/backend/app/routes/teamMember/deleteTeamMember.js index 36a41003..85c54b53 100644 --- a/backend/app/routes/teamMember/deleteTeamMember.js +++ b/backend/app/routes/teamMember/deleteTeamMember.js @@ -6,25 +6,18 @@ 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 member = await teamMemberModel.findById(memberId); - if (!member) { - return res.status(401).json({ error: 'Invalid id' }); - } - const result = await teamMemberModel.findByIdAndDelete(memberId); if (!result) { return res.status(401).json({ error: 'Invalid id' }); } - - const fileName = path.basename(member.image); - const imagePath = path.join(__dirname, '../../../uploads/teamMembersProfile/', fileName); - fs.unlink(imagePath, (err) => console.log(err)); - + 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);