refactor: agrega queries e cache do dashboard

This commit is contained in:
Felipe Coutinho
2026-03-20 18:38:20 +00:00
parent 5b8d25d894
commit 41fd8226cb
24 changed files with 1648 additions and 690 deletions

View File

@@ -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,
},
)();