mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 11:01:45 +00:00
fix(dashboard): usa pagador admin cacheado nas consultas
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { and, eq, 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 { PAYER_ROLE_ADMIN } from "@/shared/lib/payers/constants";
|
||||
import { getAdminPayerId } from "@/shared/lib/payers/get-admin-id";
|
||||
import { safeToNumber as toNumber } from "@/shared/utils/number";
|
||||
|
||||
type RawDashboardAccount = {
|
||||
@@ -34,6 +34,8 @@ export type DashboardAccountsSnapshot = {
|
||||
export async function fetchDashboardAccounts(
|
||||
userId: string,
|
||||
): Promise<DashboardAccountsSnapshot> {
|
||||
const adminPayerId = await getAdminPayerId(userId);
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: financialAccounts.id,
|
||||
@@ -62,15 +64,12 @@ export async function fetchDashboardAccounts(
|
||||
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),
|
||||
sql`(${transactions.id} IS NULL OR ${payers.role} = ${PAYER_ROLE_ADMIN})`,
|
||||
),
|
||||
)
|
||||
.where(eq(financialAccounts.userId, userId))
|
||||
.groupBy(
|
||||
financialAccounts.id,
|
||||
financialAccounts.name,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { and, eq, inArray, isNull, or, sql } from "drizzle-orm";
|
||||
import { categories, payers, transactions } from "@/db/schema";
|
||||
import { categories, transactions } from "@/db/schema";
|
||||
import { ACCOUNT_AUTO_INVOICE_NOTE_PREFIX } from "@/shared/lib/accounts/constants";
|
||||
import { db } from "@/shared/lib/db";
|
||||
import { PAYER_ROLE_ADMIN } from "@/shared/lib/payers/constants";
|
||||
import { getAdminPayerId } from "@/shared/lib/payers/get-admin-id";
|
||||
import { CATEGORY_COLORS } from "@/shared/utils/category-colors";
|
||||
import { safeToNumber as toNumber } from "@/shared/utils/number";
|
||||
import {
|
||||
@@ -85,6 +85,17 @@ export async function fetchCategoryHistory(
|
||||
// Fetch all categories for the selector
|
||||
const allCategories = await fetchAllCategories(userId);
|
||||
|
||||
const adminPayerId = await getAdminPayerId(userId);
|
||||
|
||||
if (!adminPayerId) {
|
||||
return {
|
||||
months: monthLabels,
|
||||
categories: [],
|
||||
chartData: monthLabels.map((month) => ({ month })),
|
||||
allCategories,
|
||||
};
|
||||
}
|
||||
|
||||
// Fetch monthly data for ALL categories with transactions
|
||||
const monthlyDataQuery = (await db
|
||||
.select({
|
||||
@@ -98,13 +109,12 @@ export async function fetchCategoryHistory(
|
||||
})
|
||||
.from(transactions)
|
||||
.innerJoin(categories, eq(transactions.categoryId, categories.id))
|
||||
.innerJoin(payers, eq(transactions.payerId, payers.id))
|
||||
.where(
|
||||
and(
|
||||
eq(transactions.userId, userId),
|
||||
eq(categories.userId, userId),
|
||||
inArray(transactions.period, periods),
|
||||
eq(payers.role, PAYER_ROLE_ADMIN),
|
||||
eq(transactions.payerId, adminPayerId),
|
||||
or(
|
||||
isNull(transactions.note),
|
||||
sql`${
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { and, eq, isNotNull, isNull, or, sql } from "drizzle-orm";
|
||||
import { cards, payers, transactions } from "@/db/schema";
|
||||
import { cards, transactions } from "@/db/schema";
|
||||
import {
|
||||
ACCOUNT_AUTO_INVOICE_NOTE_PREFIX,
|
||||
INITIAL_BALANCE_NOTE,
|
||||
} from "@/shared/lib/accounts/constants";
|
||||
import { db } from "@/shared/lib/db";
|
||||
import { PAYER_ROLE_ADMIN } from "@/shared/lib/payers/constants";
|
||||
import { getAdminPayerId } from "@/shared/lib/payers/get-admin-id";
|
||||
import {
|
||||
buildDateOnlyStringFromPeriodDay,
|
||||
parseLocalDateString,
|
||||
@@ -65,6 +65,12 @@ export type InstallmentAnalysisData = {
|
||||
export async function fetchInstallmentAnalysis(
|
||||
userId: string,
|
||||
): Promise<InstallmentAnalysisData> {
|
||||
const adminPayerId = await getAdminPayerId(userId);
|
||||
|
||||
if (!adminPayerId) {
|
||||
return { installmentGroups: [], totalPendingInstallments: 0 };
|
||||
}
|
||||
|
||||
// 1. Buscar todos os lançamentos parcelados não antecipados do pagador admin
|
||||
const installmentRows = await db
|
||||
.select({
|
||||
@@ -87,15 +93,14 @@ export async function fetchInstallmentAnalysis(
|
||||
})
|
||||
.from(transactions)
|
||||
.leftJoin(cards, eq(transactions.cardId, cards.id))
|
||||
.leftJoin(payers, eq(transactions.payerId, payers.id))
|
||||
.where(
|
||||
and(
|
||||
eq(transactions.userId, userId),
|
||||
eq(transactions.payerId, adminPayerId),
|
||||
eq(transactions.transactionType, "Despesa"),
|
||||
eq(transactions.condition, "Parcelado"),
|
||||
eq(transactions.isAnticipated, false),
|
||||
isNotNull(transactions.seriesId),
|
||||
eq(payers.role, PAYER_ROLE_ADMIN),
|
||||
or(
|
||||
isNull(transactions.note),
|
||||
and(
|
||||
|
||||
Reference in New Issue
Block a user