mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 21:35:19 +00:00
BREAKING: postgres + saas
This commit is contained in:
@@ -11,34 +11,36 @@ export type DashboardStats = {
|
||||
invoicesProcessed: number
|
||||
}
|
||||
|
||||
export const getDashboardStats = cache(async (filters: TransactionFilters = {}): Promise<DashboardStats> => {
|
||||
const where: Prisma.TransactionWhereInput = {}
|
||||
export const getDashboardStats = cache(
|
||||
async (userId: string, 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,
|
||||
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: { ...where, userId } })
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
@@ -47,7 +49,7 @@ export type ProjectStats = {
|
||||
invoicesProcessed: number
|
||||
}
|
||||
|
||||
export const getProjectStats = cache(async (projectId: string, filters: TransactionFilters = {}) => {
|
||||
export const getProjectStats = cache(async (userId: string, projectId: string, filters: TransactionFilters = {}) => {
|
||||
const where: Prisma.TransactionWhereInput = {
|
||||
projectCode: projectId,
|
||||
}
|
||||
@@ -59,7 +61,7 @@ export const getProjectStats = cache(async (projectId: string, filters: Transact
|
||||
}
|
||||
}
|
||||
|
||||
const transactions = await prisma.transaction.findMany({ where })
|
||||
const transactions = await prisma.transaction.findMany({ where: { ...where, userId } })
|
||||
const totalIncomePerCurrency = calcTotalPerCurrency(transactions.filter((t) => t.type === "income"))
|
||||
const totalExpensesPerCurrency = calcTotalPerCurrency(transactions.filter((t) => t.type === "expense"))
|
||||
const profitPerCurrency = Object.fromEntries(
|
||||
|
||||
Reference in New Issue
Block a user