Add ability to remove voters from Meeting Planner polls
Add DELETE /meeting-planner/:id/voters/:voterKey endpoint and delete button on each voter row in the voting matrix. Includes voterKey in API response for voter identification. Bunker Admin
This commit is contained in:
@@ -124,6 +124,16 @@ adminRouter.post('/:id/convert-to-event', async (req: Request, res: Response, ne
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
// Remove voter
|
||||
adminRouter.delete('/:id/voters/:voterKey', async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const id = req.params.id as string;
|
||||
const voterKey = decodeURIComponent(req.params.voterKey as string);
|
||||
const poll = await meetingPlannerService.removeVoter(id, voterKey);
|
||||
res.json(poll);
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
// Delete comment
|
||||
adminRouter.delete('/:id/comments/:commentId', async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
|
||||
@@ -61,11 +61,11 @@ function groupVotesByVoter(votes: Array<{
|
||||
optionId: string;
|
||||
value: PollVoteValue;
|
||||
}>) {
|
||||
const voterMap = new Map<string, { name: string; votes: Record<string, PollVoteValue> }>();
|
||||
const voterMap = new Map<string, { name: string; voterKey: string; votes: Record<string, PollVoteValue> }>();
|
||||
for (const vote of votes) {
|
||||
const key = vote.userId || vote.voterToken || vote.voterName;
|
||||
if (!voterMap.has(key)) {
|
||||
voterMap.set(key, { name: vote.voterName, votes: {} });
|
||||
voterMap.set(key, { name: vote.voterName, voterKey: key, votes: {} });
|
||||
}
|
||||
voterMap.get(key)!.votes[vote.optionId] = vote.value;
|
||||
}
|
||||
@@ -343,6 +343,25 @@ export const meetingPlannerService = {
|
||||
});
|
||||
},
|
||||
|
||||
async removeVoter(pollId: string, voterKey: string) {
|
||||
const poll = await prisma.schedulingPoll.findUnique({ where: { id: pollId } });
|
||||
if (!poll) throw new AppError(404, 'Poll not found');
|
||||
|
||||
const result = await prisma.schedulingPollVote.deleteMany({
|
||||
where: {
|
||||
pollId,
|
||||
OR: [
|
||||
{ userId: voterKey },
|
||||
{ voterToken: voterKey },
|
||||
{ voterName: voterKey, userId: null, voterToken: null },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
if (result.count === 0) throw new AppError(404, 'Voter not found');
|
||||
return this.findById(pollId);
|
||||
},
|
||||
|
||||
async deleteComment(pollId: string, commentId: string) {
|
||||
const comment = await prisma.schedulingPollComment.findFirst({
|
||||
where: { id: commentId, pollId },
|
||||
|
||||
Reference in New Issue
Block a user