From 1a984d1eac67e1abae912b06680c082a3646372d Mon Sep 17 00:00:00 2001 From: jerick Date: Tue, 21 Apr 2026 10:46:05 -0400 Subject: [PATCH] Fix budget sign logic: CC CREDIT=spend, CC DEBIT=refund (type-flip aware) --- src/app/(app)/budgets/page.tsx | 9 ++++++++- src/app/(app)/dashboard/page.tsx | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/app/(app)/budgets/page.tsx b/src/app/(app)/budgets/page.tsx index 67322e4..e2dbc35 100644 --- a/src/app/(app)/budgets/page.tsx +++ b/src/app/(app)/budgets/page.tsx @@ -27,7 +27,14 @@ export default async function BudgetsPage({ searchParams }: { searchParams: Sear }), prisma.$queryRaw<{ budgetId: string; total: bigint }[]>` SELECT t."budgetId", - COALESCE(SUM(CASE WHEN t.type = 'DEBIT' THEN t."amountCents" ELSE -t."amountCents" END), 0)::bigint AS total + 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 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} diff --git a/src/app/(app)/dashboard/page.tsx b/src/app/(app)/dashboard/page.tsx index ad6668f..d48221c 100644 --- a/src/app/(app)/dashboard/page.tsx +++ b/src/app/(app)/dashboard/page.tsx @@ -42,7 +42,14 @@ export default async function DashboardPage({ searchParams }: { searchParams: Se }), prisma.$queryRaw<{ budgetId: string; total: bigint }[]>` SELECT t."budgetId", - COALESCE(SUM(CASE WHEN t.type = 'DEBIT' THEN t."amountCents" ELSE -t."amountCents" END), 0)::bigint AS total + 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 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}