Add ledger search and filters

This commit is contained in:
2026-08-13 17:37:07 +09:00
parent f1e5075b74
commit 87cd0be64f
4 changed files with 152 additions and 7 deletions

View File

@@ -7,13 +7,30 @@ export default defineEventHandler(async (event) => {
const user = await requireUser(event)
const query = getQuery(event)
const month = typeof query.month === 'string' ? query.month : new Date().toISOString().slice(0, 7)
const type = query.type === 'income' || query.type === 'expense' ? query.type : undefined
const assetId = typeof query.assetId === 'string' && query.assetId ? query.assetId : undefined
const categoryId = typeof query.categoryId === 'string' && query.categoryId ? query.categoryId : undefined
const q = typeof query.q === 'string' ? query.q.trim() : ''
const monthStart = startOfMonth(new Date(`${month}-01T00:00:00`))
const monthEnd = endOfMonth(monthStart)
const transactions = await prisma.transaction.findMany({
where: {
userId: user.id,
occurredAt: { gte: monthStart, lte: monthEnd }
occurredAt: { gte: monthStart, lte: monthEnd },
...(type ? { type } : {}),
...(assetId ? { assetId } : {}),
...(categoryId ? { categoryId } : {}),
...(q
? {
OR: [
{ title: { contains: q, mode: 'insensitive' } },
{ memo: { contains: q, mode: 'insensitive' } },
{ asset: { name: { contains: q, mode: 'insensitive' } } },
{ category: { name: { contains: q, mode: 'insensitive' } } }
]
}
: {})
},
include: {
asset: true,