Add asset detail period views
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { getQuery } from 'h3'
|
||||
import { startOfMonth, endOfMonth } from 'date-fns'
|
||||
import { startOfMonth, endOfMonth, startOfYear, endOfYear } from 'date-fns'
|
||||
import { prisma } from '../../utils/db'
|
||||
import { requireUser } from '../../utils/auth'
|
||||
|
||||
@@ -7,17 +7,20 @@ 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 year = typeof query.year === 'string' ? query.year : ''
|
||||
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 rangeStart = year ? startOfYear(new Date(`${year}-01-01T00:00:00`)) : monthStart
|
||||
const rangeEnd = year ? endOfYear(rangeStart) : monthEnd
|
||||
|
||||
const transactions = await prisma.transaction.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
occurredAt: { gte: monthStart, lte: monthEnd },
|
||||
occurredAt: { gte: rangeStart, lte: rangeEnd },
|
||||
...(type ? { type } : {}),
|
||||
...(assetId ? { assetId } : {}),
|
||||
...(categoryId ? { categoryId } : {}),
|
||||
|
||||
Reference in New Issue
Block a user