28 lines
795 B
TypeScript
28 lines
795 B
TypeScript
import { requireAuth } from '~/server/utils/auth'
|
|
import { groups, userGroups } from '~/server/utils/db'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = requireAuth(event)
|
|
const body = await readBody(event)
|
|
const { name, description = '', isPublic = true } = body
|
|
|
|
if (!name?.trim()) {
|
|
throw createError({ statusCode: 400, message: 'Group name is required' })
|
|
}
|
|
|
|
const id = `group-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`
|
|
const now = new Date().toISOString()
|
|
|
|
const group = { id, name: name.trim(), description, isPublic, createdAt: now, updatedAt: now, ownerId: user.id }
|
|
groups.set(id, group)
|
|
|
|
userGroups.set(`${user.id}-${id}`, {
|
|
userId: user.id,
|
|
groupId: id,
|
|
role: 'admin',
|
|
joinedAt: now
|
|
})
|
|
|
|
return { group }
|
|
})
|