-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const mongoose = require('mongoose'); | ||
const Answer = require('../../../models/answers'); | ||
|
||
module.exports = async (req, res, next) => { | ||
try { | ||
const payload = res.locals.decode; | ||
const { answerId } = req.body; | ||
|
||
if (!payload.isSuperAdmin) { | ||
return res.status(401).json({ error: 'You are not authorized to perform this action' }); | ||
} | ||
|
||
if (!mongoose.Types.ObjectId.isValid(answerId)) { | ||
return res.status(400).json({ error: 'Invalid answer ID' }); | ||
} | ||
|
||
const answer = await Answer.findByIdAndDelete(answerId); | ||
if (!answer) { | ||
return res.status(404).json({ error: 'Answer not found' }); | ||
} | ||
|
||
return res.status(200).json({ message: 'Answer deleted successfully' }); | ||
} catch (error) { | ||
return res.status(500).json({ error: 'Internal server error' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const mongoose = require('mongoose'); | ||
const Question = require('../../../models/question'); | ||
const Answer = require('../../../models/answers'); | ||
|
||
module.exports = async (req, res, next) => { | ||
try { | ||
const payload = res.locals.decode; | ||
const { questionId } = req.body; | ||
|
||
if (!payload.isSuperAdmin) { | ||
return res.status(401).json({ error: 'You are not authorized to perform this action' }); | ||
} | ||
|
||
if (!mongoose.Types.ObjectId.isValid(questionId)) { | ||
return res.status(400).json({ error: 'Invalid question ID' }); | ||
} | ||
|
||
const question = await Question.findByIdAndDelete(questionId); | ||
if (!question) { | ||
return res.status(404).json({ error: 'Question not found' }); | ||
} | ||
|
||
await Answer.deleteMany({ question_id: questionId }); | ||
|
||
return res.status(200).json({ message: 'Question and its answers deleted successfully' }); | ||
} catch (error) { | ||
return res.status(500).json({ error: 'Internal server error' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters