mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 11:01:45 +00:00
refactor: agrega queries e cache do dashboard
This commit is contained in:
@@ -1,100 +1,65 @@
|
||||
import { unstable_cache } from "next/cache";
|
||||
import { fetchDashboardAccounts } from "./accounts-queries";
|
||||
import { fetchDashboardBills } from "./bills-queries";
|
||||
import { fetchExpensesByCategory } from "./categories/expenses-by-category-queries";
|
||||
import { fetchIncomeByCategory } from "./categories/income-by-category-queries";
|
||||
import { fetchDashboardCardMetrics } from "./dashboard-metrics-queries";
|
||||
import { fetchInstallmentExpenses } from "./expenses/installment-expenses-queries";
|
||||
import { fetchRecurringExpenses } from "./expenses/recurring-expenses-queries";
|
||||
import { fetchTopExpenses } from "./expenses/top-expenses-queries";
|
||||
import { fetchGoalsProgressData } from "./goals-progress-queries";
|
||||
import { fetchIncomeExpenseBalance } from "./income-expense-balance-queries";
|
||||
import { fetchDashboardCategoryOverview } from "./category-overview-queries";
|
||||
import { fetchDashboardCurrentPeriodOverview } from "./current-period-overview-queries";
|
||||
import { fetchDashboardInvoices } from "./invoices-queries";
|
||||
import { fetchDashboardNotes } from "./notes-queries";
|
||||
import { fetchDashboardPayers } from "./payers-queries";
|
||||
import { fetchPaymentConditions } from "./payments/payment-conditions-queries";
|
||||
import { fetchPaymentMethods } from "./payments/payment-methods-queries";
|
||||
import { fetchPaymentStatus } from "./payments/payment-status-queries";
|
||||
import { fetchPurchasesByCategory } from "./purchases-by-category-queries";
|
||||
import { fetchTopEstablishments } from "./top-establishments-queries";
|
||||
import { fetchDashboardPeriodOverview } from "./period-overview-queries";
|
||||
|
||||
async function fetchDashboardDataInternal(userId: string, period: string) {
|
||||
const [
|
||||
metrics,
|
||||
periodOverview,
|
||||
accountsSnapshot,
|
||||
invoicesSnapshot,
|
||||
billsSnapshot,
|
||||
goalsProgressData,
|
||||
paymentStatusData,
|
||||
incomeExpenseBalanceData,
|
||||
currentPeriodOverview,
|
||||
categoryOverview,
|
||||
pagadoresSnapshot,
|
||||
notesData,
|
||||
paymentConditionsData,
|
||||
paymentMethodsData,
|
||||
recurringExpensesData,
|
||||
installmentExpensesData,
|
||||
topEstablishmentsData,
|
||||
topExpensesAll,
|
||||
topExpensesCardOnly,
|
||||
purchasesByCategoryData,
|
||||
incomeByCategoryData,
|
||||
expensesByCategoryData,
|
||||
] = await Promise.all([
|
||||
fetchDashboardCardMetrics(userId, period),
|
||||
fetchDashboardPeriodOverview(userId, period),
|
||||
fetchDashboardAccounts(userId),
|
||||
fetchDashboardInvoices(userId, period),
|
||||
fetchDashboardBills(userId, period),
|
||||
fetchGoalsProgressData(userId, period),
|
||||
fetchPaymentStatus(userId, period),
|
||||
fetchIncomeExpenseBalance(userId, period),
|
||||
fetchDashboardCurrentPeriodOverview(userId, period),
|
||||
fetchDashboardCategoryOverview(userId, period),
|
||||
fetchDashboardPayers(userId, period),
|
||||
fetchDashboardNotes(userId),
|
||||
fetchPaymentConditions(userId, period),
|
||||
fetchPaymentMethods(userId, period),
|
||||
fetchRecurringExpenses(userId, period),
|
||||
fetchInstallmentExpenses(userId, period),
|
||||
fetchTopEstablishments(userId, period),
|
||||
fetchTopExpenses(userId, period, false),
|
||||
fetchTopExpenses(userId, period, true),
|
||||
fetchPurchasesByCategory(userId, period),
|
||||
fetchIncomeByCategory(userId, period),
|
||||
fetchExpensesByCategory(userId, period),
|
||||
]);
|
||||
|
||||
return {
|
||||
metrics,
|
||||
metrics: periodOverview.metrics,
|
||||
accountsSnapshot,
|
||||
invoicesSnapshot,
|
||||
billsSnapshot,
|
||||
goalsProgressData,
|
||||
paymentStatusData,
|
||||
incomeExpenseBalanceData,
|
||||
billsSnapshot: currentPeriodOverview.billsSnapshot,
|
||||
goalsProgressData: categoryOverview.goalsProgressData,
|
||||
paymentStatusData: currentPeriodOverview.paymentStatusData,
|
||||
incomeExpenseBalanceData: periodOverview.incomeExpenseBalanceData,
|
||||
pagadoresSnapshot,
|
||||
notesData,
|
||||
paymentConditionsData,
|
||||
paymentMethodsData,
|
||||
recurringExpensesData,
|
||||
installmentExpensesData,
|
||||
topEstablishmentsData,
|
||||
topExpensesAll,
|
||||
topExpensesCardOnly,
|
||||
purchasesByCategoryData,
|
||||
incomeByCategoryData,
|
||||
expensesByCategoryData,
|
||||
paymentConditionsData: currentPeriodOverview.paymentConditionsData,
|
||||
paymentMethodsData: currentPeriodOverview.paymentMethodsData,
|
||||
recurringExpensesData: currentPeriodOverview.recurringExpensesData,
|
||||
installmentExpensesData: currentPeriodOverview.installmentExpensesData,
|
||||
topEstablishmentsData: currentPeriodOverview.topEstablishmentsData,
|
||||
topExpensesAll: currentPeriodOverview.topExpensesAll,
|
||||
topExpensesCardOnly: currentPeriodOverview.topExpensesCardOnly,
|
||||
purchasesByCategoryData: currentPeriodOverview.purchasesByCategoryData,
|
||||
incomeByCategoryData: categoryOverview.incomeByCategoryData,
|
||||
expensesByCategoryData: categoryOverview.expensesByCategoryData,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Cached dashboard data fetcher.
|
||||
* Uses unstable_cache with tags for revalidation on mutations.
|
||||
* Cache is keyed by userId + period, and invalidated via "dashboard" tag.
|
||||
* Cache is keyed by userId + period, and invalidated via user-scoped tags.
|
||||
*/
|
||||
export function fetchDashboardData(userId: string, period: string) {
|
||||
return unstable_cache(
|
||||
() => fetchDashboardDataInternal(userId, period),
|
||||
[`dashboard-${userId}-${period}`],
|
||||
{
|
||||
tags: ["dashboard", `dashboard-${userId}`],
|
||||
tags: [`dashboard-${userId}`],
|
||||
revalidate: 60,
|
||||
},
|
||||
)();
|
||||
|
||||
Reference in New Issue
Block a user