Files
money.sori.studio/server/api/assets/[id].patch.ts
2026-08-13 15:58:42 +09:00

23 lines
996 B
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' })
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 } : {})
}
})
})