Files
TaxHacker/data/stats.ts
2025-03-17 18:36:25 +01:00

80 lines
2.6 KiB
TypeScript

import { prisma } from "@/lib/db"
import { calcTotalPerCurrency } from "@/lib/stats"
import { Prisma } from "@prisma/client"
import { cache } from "react"
import { TransactionFilters } from "./transactions"
export type DashboardStats = {
totalIncomePerCurrency: Record<string, number>
totalExpensesPerCurrency: Record<string, number>
profitPerCurrency: Record<string, number>
invoicesProcessed: number
}
export const getDashboardStats = cache(async (filters: TransactionFilters = {}): Promise<DashboardStats> => {
const where: Prisma.TransactionWhereInput = {}
if (filters.dateFrom || filters.dateTo) {
where.issuedAt = {
gte: filters.dateFrom ? new Date(filters.dateFrom) : undefined,
lte: filters.dateTo ? new Date(filters.dateTo) : undefined,
}
}
const transactions = await prisma.transaction.findMany({ where })
const totalIncomePerCurrency = calcTotalPerCurrency(transactions.filter((t) => t.type === "income"))
const totalExpensesPerCurrency = calcTotalPerCurrency(transactions.filter((t) => t.type === "expense"))
const profitPerCurrency = Object.fromEntries(
Object.keys(totalIncomePerCurrency).map((currency) => [
currency,
totalIncomePerCurrency[currency] - totalExpensesPerCurrency[currency],
])
)
const invoicesProcessed = transactions.length
return {
totalIncomePerCurrency,
totalExpensesPerCurrency,
profitPerCurrency,
invoicesProcessed,
}
})
export type ProjectStats = {
totalIncomePerCurrency: Record<string, number>
totalExpensesPerCurrency: Record<string, number>
profitPerCurrency: Record<string, number>
invoicesProcessed: number
}
export const getProjectStats = cache(async (projectId: string, filters: TransactionFilters = {}) => {
const where: Prisma.TransactionWhereInput = {
projectCode: projectId,
}
if (filters.dateFrom || filters.dateTo) {
where.issuedAt = {
gte: filters.dateFrom ? new Date(filters.dateFrom) : undefined,
lte: filters.dateTo ? new Date(filters.dateTo) : undefined,
}
}
const transactions = await prisma.transaction.findMany({ where })
const totalIncomePerCurrency = calcTotalPerCurrency(transactions.filter((t) => t.type === "income"))
const totalExpensesPerCurrency = calcTotalPerCurrency(transactions.filter((t) => t.type === "expense"))
const profitPerCurrency = Object.fromEntries(
Object.keys(totalIncomePerCurrency).map((currency) => [
currency,
totalIncomePerCurrency[currency] - totalExpensesPerCurrency[currency],
])
)
const invoicesProcessed = transactions.length
return {
totalIncomePerCurrency,
totalExpensesPerCurrency,
profitPerCurrency,
invoicesProcessed,
}
})