feat(team): add team settings with edit, leave, and kick functionality
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { useValidatedBody, useValidatedParams, z, zh } from 'h3-zod';
|
||||
import prisma from '~~/lib/prisma';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const user = await requireUserSession(event);
|
||||
const userId = user.user.id;
|
||||
|
||||
const { huntId, teamId } = await useValidatedParams(
|
||||
event,
|
||||
z.object({
|
||||
huntId: zh.numAsString,
|
||||
teamId: zh.numAsString
|
||||
})
|
||||
);
|
||||
|
||||
const body = await useValidatedBody(
|
||||
event,
|
||||
z.object({
|
||||
name: z.string().min(3).max(256).trim(),
|
||||
description: z.string().max(256).trim().default(''),
|
||||
password: z.string().min(1).max(256).trim()
|
||||
})
|
||||
);
|
||||
|
||||
// Only the team owner may edit the team
|
||||
const team = await prisma.huntTeam.findFirst({
|
||||
where: {
|
||||
id: teamId,
|
||||
huntId,
|
||||
ownerId: userId
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!team) {
|
||||
throw createError({ status: 403, statusText: 'Not allowed!' });
|
||||
}
|
||||
|
||||
return await prisma.huntTeam.update({
|
||||
where: {
|
||||
id: teamId
|
||||
},
|
||||
data: {
|
||||
name: body.name,
|
||||
description: body.description,
|
||||
password: body.password
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
password: true
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user