Adicionado aba de estabelecimentos e feita ajuste de interface. Detalhes adicionados no CHANGELOG.md

This commit is contained in:
Guilherme Bano
2026-02-20 00:39:50 -03:00
committed by Felipe Coutinho
parent ffde55f589
commit 9b78f839bf
23 changed files with 695 additions and 55 deletions

View File

@@ -7,6 +7,7 @@ import {
cartoes,
categorias,
contas,
estabelecimentos,
lancamentos,
pagadores,
} from "@/db/schema";
@@ -1639,43 +1640,64 @@ export async function deleteMultipleLancamentosAction(
}
}
// Get unique establishment names from the last 3 months
// Get unique establishment names: from estabelecimentos table + last 3 months from lancamentos
export async function getRecentEstablishmentsAction(): Promise<string[]> {
try {
const user = await getUser();
// Calculate date 3 months ago
const threeMonthsAgo = new Date();
threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
// Fetch establishment names from the last 3 months
const results = await db
.select({ name: lancamentos.name })
.from(lancamentos)
.where(
and(
eq(lancamentos.userId, user.id),
gte(lancamentos.purchaseDate, threeMonthsAgo),
),
)
.orderBy(desc(lancamentos.purchaseDate));
// Remove duplicates and filter empty names
const uniqueNames = Array.from(
new Set(
results
.map((r) => r.name)
.filter(
(name): name is string =>
name != null &&
name.trim().length > 0 &&
!name.toLowerCase().startsWith("pagamento fatura"),
const [estabelecimentosRows, lancamentosResults] = await Promise.all([
db.query.estabelecimentos.findMany({
columns: { name: true },
where: eq(estabelecimentos.userId, user.id),
}),
db
.select({ name: lancamentos.name })
.from(lancamentos)
.where(
and(
eq(lancamentos.userId, user.id),
gte(lancamentos.purchaseDate, threeMonthsAgo),
),
),
);
)
.orderBy(desc(lancamentos.purchaseDate)),
]);
// Return top 50 most recent unique establishments
return uniqueNames.slice(0, 100);
const fromTable = estabelecimentosRows
.map((r) => r.name)
.filter(
(name): name is string =>
name != null && name.trim().length > 0,
);
const fromLancamentos = lancamentosResults
.map((r) => r.name)
.filter(
(name): name is string =>
name != null &&
name.trim().length > 0 &&
!name.toLowerCase().startsWith("pagamento fatura"),
);
const seen = new Set<string>();
const unique: string[] = [];
for (const name of fromTable) {
const key = name.trim();
if (!seen.has(key)) {
seen.add(key);
unique.push(key);
}
}
for (const name of fromLancamentos) {
const key = name.trim();
if (!seen.has(key)) {
seen.add(key);
unique.push(key);
}
}
return unique.slice(0, 100);
} catch (error) {
console.error("Error fetching recent establishments:", error);
return [];