- Remove explicit ADMIN role checks in API handlers - Replace combined creator/member role checks with membership-only filters - Update seed data roles to reflect changed authorization logic - Add migration to insert hunt creators as members for consistent checks - Prevent removal of hunt creator as a member with new explicit check - Adjust related user role updates and queries accordingly - Upgrade dependencies including nuxt and zod for compatibility
65 lines
1.3 KiB
TypeScript
65 lines
1.3 KiB
TypeScript
import { useValidatedBody, useValidatedParams, z, zh } from 'h3-zod';
|
|
import prisma from '~~/lib/prisma';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = await requireUserSession(event);
|
|
|
|
const { huntId } = await useValidatedParams(
|
|
event,
|
|
z.object({
|
|
huntId: zh.numAsString
|
|
})
|
|
);
|
|
|
|
const { userId } = await useValidatedBody(
|
|
event,
|
|
z.object({
|
|
userId: z.int().gt(0)
|
|
})
|
|
);
|
|
|
|
const hunt = await prisma.hunt.findUnique({
|
|
where: {
|
|
id: huntId,
|
|
members: {
|
|
some: {
|
|
memberId: user.user.id
|
|
}
|
|
}
|
|
},
|
|
select: {
|
|
creatorId: true
|
|
}
|
|
});
|
|
|
|
if (!hunt) {
|
|
throw createError({ status: 404, statusText: 'Not found!' });
|
|
}
|
|
|
|
if (hunt.creatorId === userId) {
|
|
throw createError({
|
|
status: 403,
|
|
statusText: 'Cannot remove the hunt creator!'
|
|
});
|
|
}
|
|
|
|
try {
|
|
await prisma.huntMember.delete({
|
|
where: {
|
|
memberId_huntId: {
|
|
memberId: userId,
|
|
huntId
|
|
}
|
|
},
|
|
select: {
|
|
memberId: true
|
|
}
|
|
});
|
|
} catch (e) {
|
|
console.error('Kick member from hunt', huntId, userId, user.user.id, e);
|
|
throw createError({ status: 404, statusText: 'Not found!', cause: e });
|
|
}
|
|
|
|
return { success: true };
|
|
});
|