Add ledger search and filters
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user