Fix signup defaults and asset management
This commit is contained in:
19
server/api/assets/[id].delete.ts
Normal file
19
server/api/assets/[id].delete.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { 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 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' })
|
||||
|
||||
const transactionCount = await prisma.transaction.count({ where: { assetId: id, userId: user.id } })
|
||||
if (transactionCount > 0) {
|
||||
throw createError({ statusCode: 409, statusMessage: 'Asset has transactions' })
|
||||
}
|
||||
|
||||
await prisma.asset.delete({ where: { id } })
|
||||
return { ok: true }
|
||||
})
|
||||
@@ -9,6 +9,9 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
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 },
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
import { readBody } from 'h3'
|
||||
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: body.name.trim(),
|
||||
name,
|
||||
balanceType: body.balanceType,
|
||||
initialBalance: body.balanceType === 'cumulative' ? Number(body.initialBalance || 0) : 0,
|
||||
sortOrder: count
|
||||
|
||||
Reference in New Issue
Block a user