pstream-backend/server/routes/users/[id]/lists/[listId].delete.ts
2025-12-02 16:25:44 -06:00

47 lines
1,021 B
TypeScript

import { useAuth } from '#imports';
import { prisma } from '#imports';
export default defineEventHandler(async event => {
const userId = event.context.params?.id;
const listId = event.context.params?.listId;
const session = await useAuth().getCurrentSession();
if (session.user !== userId) {
throw createError({
statusCode: 403,
message: 'Cannot delete lists for other users',
});
}
const list = await prisma.lists.findUnique({
where: { id: listId },
});
if (!list) {
throw createError({
statusCode: 404,
message: 'List not found',
});
}
if (list.user_id !== userId) {
throw createError({
statusCode: 403,
message: "Cannot delete lists you don't own",
});
}
await prisma.$transaction(async tx => {
await tx.list_items.deleteMany({
where: { list_id: listId },
});
await tx.lists.delete({
where: { id: listId },
});
});
return {
id: listId,
message: 'List deleted successfully',
};
});