This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { mutation, query } from "./_generated/server";
|
||||
import { v } from "convex/values";
|
||||
import { getRolesForUser } from "./roles";
|
||||
|
||||
const pollOptionValidator = v.object({
|
||||
id: v.string(),
|
||||
@@ -195,8 +196,17 @@ export const remove = mutation({
|
||||
handler: async (ctx, args) => {
|
||||
const poll = await ctx.db.get(args.pollId);
|
||||
if (!poll) return null;
|
||||
if (poll.createdBy !== args.userId) {
|
||||
throw new Error("Only the poll creator can delete it");
|
||||
const isCreator = poll.createdBy === args.userId;
|
||||
if (!isCreator) {
|
||||
// Mirror `messages.removeInternal` — users with `manage_messages`
|
||||
// can delete any poll, not just their own.
|
||||
const roles = await getRolesForUser(ctx, args.userId);
|
||||
const canManage = roles.some(
|
||||
(role) => (role.permissions as Record<string, boolean>)?.manage_messages,
|
||||
);
|
||||
if (!canManage) {
|
||||
throw new Error("Not authorized to delete this poll");
|
||||
}
|
||||
}
|
||||
const votes = await ctx.db
|
||||
.query("pollVotes")
|
||||
|
||||
Reference in New Issue
Block a user