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 { 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 { INITIAL_BALANCE_NOTE } from "@/shared/lib/accounts/constants";
|
||||||
import { db } from "@/shared/lib/db";
|
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";
|
import { safeToNumber as toNumber } from "@/shared/utils/number";
|
||||||
|
|
||||||
type RawDashboardAccount = {
|
type RawDashboardAccount = {
|
||||||
@@ -34,6 +34,8 @@ export type DashboardAccountsSnapshot = {
|
|||||||
export async function fetchDashboardAccounts(
|
export async function fetchDashboardAccounts(
|
||||||
userId: string,
|
userId: string,
|
||||||
): Promise<DashboardAccountsSnapshot> {
|
): Promise<DashboardAccountsSnapshot> {
|
||||||
|
const adminPayerId = await getAdminPayerId(userId);
|
||||||
|
|
||||||
const rows = await db
|
const rows = await db
|
||||||
.select({
|
.select({
|
||||||
id: financialAccounts.id,
|
id: financialAccounts.id,
|
||||||
@@ -62,15 +64,12 @@ export async function fetchDashboardAccounts(
|
|||||||
eq(transactions.accountId, financialAccounts.id),
|
eq(transactions.accountId, financialAccounts.id),
|
||||||
eq(transactions.userId, userId),
|
eq(transactions.userId, userId),
|
||||||
eq(transactions.isSettled, true),
|
eq(transactions.isSettled, true),
|
||||||
|
adminPayerId
|
||||||
|
? eq(transactions.payerId, adminPayerId)
|
||||||
|
: sql`false`,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.leftJoin(payers, eq(transactions.payerId, payers.id))
|
.where(eq(financialAccounts.userId, userId))
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(financialAccounts.userId, userId),
|
|
||||||
sql`(${transactions.id} IS NULL OR ${payers.role} = ${PAYER_ROLE_ADMIN})`,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.groupBy(
|
.groupBy(
|
||||||
financialAccounts.id,
|
financialAccounts.id,
|
||||||
financialAccounts.name,
|
financialAccounts.name,
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { and, eq, inArray, isNull, or, sql } from "drizzle-orm";
|
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 { ACCOUNT_AUTO_INVOICE_NOTE_PREFIX } from "@/shared/lib/accounts/constants";
|
||||||
import { db } from "@/shared/lib/db";
|
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 { CATEGORY_COLORS } from "@/shared/utils/category-colors";
|
||||||
import { safeToNumber as toNumber } from "@/shared/utils/number";
|
import { safeToNumber as toNumber } from "@/shared/utils/number";
|
||||||
import {
|
import {
|
||||||
@@ -85,6 +85,17 @@ export async function fetchCategoryHistory(
|
|||||||
// Fetch all categories for the selector
|
// Fetch all categories for the selector
|
||||||
const allCategories = await fetchAllCategories(userId);
|
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
|
// Fetch monthly data for ALL categories with transactions
|
||||||
const monthlyDataQuery = (await db
|
const monthlyDataQuery = (await db
|
||||||
.select({
|
.select({
|
||||||
@@ -98,13 +109,12 @@ export async function fetchCategoryHistory(
|
|||||||
})
|
})
|
||||||
.from(transactions)
|
.from(transactions)
|
||||||
.innerJoin(categories, eq(transactions.categoryId, categories.id))
|
.innerJoin(categories, eq(transactions.categoryId, categories.id))
|
||||||
.innerJoin(payers, eq(transactions.payerId, payers.id))
|
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(transactions.userId, userId),
|
eq(transactions.userId, userId),
|
||||||
eq(categories.userId, userId),
|
eq(categories.userId, userId),
|
||||||
inArray(transactions.period, periods),
|
inArray(transactions.period, periods),
|
||||||
eq(payers.role, PAYER_ROLE_ADMIN),
|
eq(transactions.payerId, adminPayerId),
|
||||||
or(
|
or(
|
||||||
isNull(transactions.note),
|
isNull(transactions.note),
|
||||||
sql`${
|
sql`${
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { and, eq, isNotNull, isNull, or, sql } from "drizzle-orm";
|
import { and, eq, isNotNull, isNull, or, sql } from "drizzle-orm";
|
||||||
import { cards, payers, transactions } from "@/db/schema";
|
import { cards, transactions } from "@/db/schema";
|
||||||
import {
|
import {
|
||||||
ACCOUNT_AUTO_INVOICE_NOTE_PREFIX,
|
ACCOUNT_AUTO_INVOICE_NOTE_PREFIX,
|
||||||
INITIAL_BALANCE_NOTE,
|
INITIAL_BALANCE_NOTE,
|
||||||
} from "@/shared/lib/accounts/constants";
|
} from "@/shared/lib/accounts/constants";
|
||||||
import { db } from "@/shared/lib/db";
|
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 {
|
import {
|
||||||
buildDateOnlyStringFromPeriodDay,
|
buildDateOnlyStringFromPeriodDay,
|
||||||
parseLocalDateString,
|
parseLocalDateString,
|
||||||
@@ -65,6 +65,12 @@ export type InstallmentAnalysisData = {
|
|||||||
export async function fetchInstallmentAnalysis(
|
export async function fetchInstallmentAnalysis(
|
||||||
userId: string,
|
userId: string,
|
||||||
): Promise<InstallmentAnalysisData> {
|
): 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
|
// 1. Buscar todos os lançamentos parcelados não antecipados do pagador admin
|
||||||
const installmentRows = await db
|
const installmentRows = await db
|
||||||
.select({
|
.select({
|
||||||
@@ -87,15 +93,14 @@ export async function fetchInstallmentAnalysis(
|
|||||||
})
|
})
|
||||||
.from(transactions)
|
.from(transactions)
|
||||||
.leftJoin(cards, eq(transactions.cardId, cards.id))
|
.leftJoin(cards, eq(transactions.cardId, cards.id))
|
||||||
.leftJoin(payers, eq(transactions.payerId, payers.id))
|
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(transactions.userId, userId),
|
eq(transactions.userId, userId),
|
||||||
|
eq(transactions.payerId, adminPayerId),
|
||||||
eq(transactions.transactionType, "Despesa"),
|
eq(transactions.transactionType, "Despesa"),
|
||||||
eq(transactions.condition, "Parcelado"),
|
eq(transactions.condition, "Parcelado"),
|
||||||
eq(transactions.isAnticipated, false),
|
eq(transactions.isAnticipated, false),
|
||||||
isNotNull(transactions.seriesId),
|
isNotNull(transactions.seriesId),
|
||||||
eq(payers.role, PAYER_ROLE_ADMIN),
|
|
||||||
or(
|
or(
|
||||||
isNull(transactions.note),
|
isNull(transactions.note),
|
||||||
and(
|
and(
|
||||||
|
|||||||
Reference in New Issue
Block a user