From d67a80b413489e1b136beab3b4d87d8779791a09 Mon Sep 17 00:00:00 2001 From: jerick Date: Mon, 20 Apr 2026 23:44:06 -0400 Subject: [PATCH] account specific budget queries --- src/app/(app)/budgets/page.tsx | 11 +++++++++-- src/app/(app)/dashboard/page.tsx | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/app/(app)/budgets/page.tsx b/src/app/(app)/budgets/page.tsx index 2de49f5..129d20d 100644 --- a/src/app/(app)/budgets/page.tsx +++ b/src/app/(app)/budgets/page.tsx @@ -26,12 +26,19 @@ export default async function BudgetsPage({ searchParams }: { searchParams: Sear orderBy: { createdAt: 'asc' }, }), prisma.$queryRaw<{ budgetId: string; total: bigint }[]>` - SELECT t."budgetId", COALESCE(SUM(t."amountCents"), 0)::bigint AS total + SELECT t."budgetId", + COALESCE(SUM( + CASE + WHEN a.type = 'CREDIT_CARD' AND t.type = 'CREDIT' THEN t."amountCents" + WHEN a.type = 'CREDIT_CARD' AND t.type = 'DEBIT' THEN -t."amountCents" + WHEN a.type = 'BANK' AND t.type = 'DEBIT' THEN t."amountCents" + ELSE -t."amountCents" + END + ), 0)::bigint AS total FROM "Transaction" t JOIN "Account" a ON t."accountId" = a.id WHERE a."userId" = ${userId} AND t."budgetId" IS NOT NULL - AND t.type = 'DEBIT' AND t.date >= ${start} AND t.date <= ${end} GROUP BY t."budgetId" diff --git a/src/app/(app)/dashboard/page.tsx b/src/app/(app)/dashboard/page.tsx index 3c92947..7c6681c 100644 --- a/src/app/(app)/dashboard/page.tsx +++ b/src/app/(app)/dashboard/page.tsx @@ -41,12 +41,19 @@ export default async function DashboardPage({ searchParams }: { searchParams: Se orderBy: { name: 'asc' }, }), prisma.$queryRaw<{ budgetId: string; total: bigint }[]>` - SELECT t."budgetId", COALESCE(SUM(t."amountCents"), 0)::bigint AS total + SELECT t."budgetId", + COALESCE(SUM( + CASE + WHEN a.type = 'CREDIT_CARD' AND t.type = 'CREDIT' THEN t."amountCents" + WHEN a.type = 'CREDIT_CARD' AND t.type = 'DEBIT' THEN -t."amountCents" + WHEN a.type = 'BANK' AND t.type = 'DEBIT' THEN t."amountCents" + ELSE -t."amountCents" + END + ), 0)::bigint AS total FROM "Transaction" t JOIN "Account" a ON t."accountId" = a.id WHERE a."userId" = ${userId} AND t."budgetId" IS NOT NULL - AND t.type = 'DEBIT' AND t.date >= ${start} AND t.date <= ${end} GROUP BY t."budgetId"