Added delete poll route in backend

This commit is contained in:
Manik Maity
2024-11-09 22:31:19 +05:30
parent cd46cae87b
commit 8207767850
5 changed files with 77 additions and 11 deletions

View File

@@ -45,6 +45,32 @@ export async function getAllCreatedPollsService(id) {
const polls = await findPollsByCreatorId(id);
return polls;
}
catch(err){
throw err;
}
}
export async function deletePollService(pollId, user) {
try {
const userId = user._id;
const poll = await findPollById(pollId);
if (!poll) {
throw {
statusCode: 404,
message: "Poll not found"
}
}
console.log(poll?.creatorId._id.toString(), userId.toString(), user);
if (poll.creatorId._id.toString() !== userId.toString()) {
throw {
statusCode: 401,
message: "Unauthorized"
}
}
const deletedPoll = await deletePollService(pollId);
return deletedPoll;
}
catch(err){
throw err;
}