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,9 +1,9 @@
import { and, eq, ilike, not, sql } from "drizzle-orm";
import { financialAccounts, payers, transactions } from "@/db/schema";
import { financialAccounts, transactions } from "@/db/schema";
import { INITIAL_BALANCE_NOTE } from "@/shared/lib/accounts/constants";
import { db } from "@/shared/lib/db";
import { loadLogoOptions } from "@/shared/lib/logo/options";
import { PAYER_ROLE_ADMIN } from "@/shared/lib/payers/constants";
import { getAdminPayerId } from "@/shared/lib/payers/get-admin-id";
export type AccountData = {
id: string;
@@ -18,9 +18,12 @@ export type AccountData = {
excludeInitialBalanceFromIncome: boolean;
};
export async function fetchAccountsForUser(
async function fetchAccountsByStatus(
userId: string,
archived: boolean,
): Promise<{ accounts: AccountData[]; logoOptions: string[] }> {
const adminPayerId = await getAdminPayerId(userId);
const [accountRows, logoOptions] = await Promise.all([
db
.select({
@@ -53,14 +56,15 @@ export async function fetchAccountsForUser(
eq(transactions.accountId, financialAccounts.id),
eq(transactions.userId, userId),
eq(transactions.isSettled, true),
adminPayerId ? eq(transactions.payerId, adminPayerId) : sql`false`,
),
)
.leftJoin(payers, eq(transactions.payerId, payers.id))
.where(
and(
eq(financialAccounts.userId, userId),
not(ilike(financialAccounts.status, "inativa")),
sql`(${transactions.id} IS NULL OR ${payers.role} = ${PAYER_ROLE_ADMIN})`,
archived
? ilike(financialAccounts.status, "inativa")
: not(ilike(financialAccounts.status, "inativa")),
),
)
.groupBy(
@@ -95,81 +99,16 @@ export async function fetchAccountsForUser(
return { accounts, logoOptions };
}
export async function fetchAccountsForUser(
userId: string,
): Promise<{ accounts: AccountData[]; logoOptions: string[] }> {
return fetchAccountsByStatus(userId, false);
}
export async function fetchInactiveForUser(
userId: string,
): Promise<{ accounts: AccountData[]; logoOptions: string[] }> {
const [accountRows, logoOptions] = await Promise.all([
db
.select({
id: financialAccounts.id,
name: financialAccounts.name,
accountType: financialAccounts.accountType,
status: financialAccounts.status,
note: financialAccounts.note,
logo: financialAccounts.logo,
initialBalance: financialAccounts.initialBalance,
excludeFromBalance: financialAccounts.excludeFromBalance,
excludeInitialBalanceFromIncome:
financialAccounts.excludeInitialBalanceFromIncome,
balanceMovements: sql<number>`
coalesce(
sum(
case
when ${transactions.note} = ${INITIAL_BALANCE_NOTE} then 0
else ${transactions.amount}
end
),
0
)
`,
})
.from(financialAccounts)
.leftJoin(
transactions,
and(
eq(transactions.accountId, financialAccounts.id),
eq(transactions.userId, userId),
eq(transactions.isSettled, true),
),
)
.leftJoin(payers, eq(transactions.payerId, payers.id))
.where(
and(
eq(financialAccounts.userId, userId),
ilike(financialAccounts.status, "inativa"),
sql`(${transactions.id} IS NULL OR ${payers.role} = ${PAYER_ROLE_ADMIN})`,
),
)
.groupBy(
financialAccounts.id,
financialAccounts.name,
financialAccounts.accountType,
financialAccounts.status,
financialAccounts.note,
financialAccounts.logo,
financialAccounts.initialBalance,
financialAccounts.excludeFromBalance,
financialAccounts.excludeInitialBalanceFromIncome,
),
loadLogoOptions(),
]);
const accounts = accountRows.map((account) => ({
id: account.id,
name: account.name,
accountType: account.accountType,
status: account.status,
note: account.note,
logo: account.logo,
initialBalance: Number(account.initialBalance ?? 0),
balance:
Number(account.initialBalance ?? 0) +
Number(account.balanceMovements ?? 0),
excludeFromBalance: account.excludeFromBalance,
excludeInitialBalanceFromIncome: account.excludeInitialBalanceFromIncome,
}));
return { accounts, logoOptions };
return fetchAccountsByStatus(userId, true);
}
export async function fetchAllAccountsForUser(userId: string): Promise<{