26 lines
1.1 KiB
TypeScript
26 lines
1.1 KiB
TypeScript
import { readBody, getRouterParam, 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; active?: boolean }>(event)
|
|
const id = getRouterParam(event, 'id')!
|
|
|
|
const asset = await prisma.asset.findFirst({ where: { id, userId: user.id } })
|
|
if (!asset) throw createError({ statusCode: 404, statusMessage: 'Asset not found' })
|
|
if (body.name !== undefined && !body.name.trim()) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Asset name is required' })
|
|
}
|
|
|
|
return prisma.asset.update({
|
|
where: { id },
|
|
data: {
|
|
...(body.name !== undefined ? { name: body.name.trim() } : {}),
|
|
...(body.balanceType !== undefined ? { balanceType: body.balanceType } : {}),
|
|
...(body.initialBalance !== undefined ? { initialBalance: Number(body.initialBalance) } : {}),
|
|
...(body.active !== undefined ? { active: body.active } : {})
|
|
}
|
|
})
|
|
})
|