23 lines
798 B
TypeScript
23 lines
798 B
TypeScript
import { readBody, createError } from 'h3'
|
|
import { prisma } from '../../utils/db'
|
|
import { requireUser } from '../../utils/auth'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = await requireUser(event)
|
|
const body = await readBody<{ name: string; balanceType: 'cumulative' | 'monthly_reset'; initialBalance?: number }>(event)
|
|
const name = body.name?.trim()
|
|
if (!name) throw createError({ statusCode: 400, statusMessage: 'Asset name is required' })
|
|
|
|
const count = await prisma.asset.count({ where: { userId: user.id } })
|
|
|
|
return prisma.asset.create({
|
|
data: {
|
|
userId: user.id,
|
|
name,
|
|
balanceType: body.balanceType,
|
|
initialBalance: body.balanceType === 'cumulative' ? Number(body.initialBalance || 0) : 0,
|
|
sortOrder: count
|
|
}
|
|
})
|
|
})
|