From dc45f489a6e89444068d1208cf219dbf76b060c7 Mon Sep 17 00:00:00 2001 From: jerick Date: Mon, 25 May 2026 17:15:11 +0000 Subject: [PATCH] Fix budget spend showing negative when TRANSFER transactions assigned TRANSFER type fell into the ELSE branch of the spend CASE expression, treating transfers as negative spend like refunds. Explicitly handle each type: DEBIT=positive, CREDIT=negative, TRANSFER=0. Co-Authored-By: Claude Sonnet 4.6 --- src/app/(app)/budgets/page.tsx | 2 +- src/app/(app)/dashboard/page.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/(app)/budgets/page.tsx b/src/app/(app)/budgets/page.tsx index 67322e4..3c13f50 100644 --- a/src/app/(app)/budgets/page.tsx +++ b/src/app/(app)/budgets/page.tsx @@ -27,7 +27,7 @@ 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 t.type = 'DEBIT' THEN t."amountCents" WHEN t.type = 'CREDIT' THEN -t."amountCents" ELSE 0 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 b606d72..4201b31 100644 --- a/src/app/(app)/dashboard/page.tsx +++ b/src/app/(app)/dashboard/page.tsx @@ -43,7 +43,7 @@ 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 t.type = 'DEBIT' THEN t."amountCents" WHEN t.type = 'CREDIT' THEN -t."amountCents" ELSE 0 END), 0)::bigint AS total FROM "Transaction" t JOIN "Account" a ON t."accountId" = a.id WHERE a."userId" = ${userId}