Fix budget sign logic: CC CREDIT=spend, CC DEBIT=refund (type-flip aware)

This commit is contained in:
2026-04-21 10:46:05 -04:00
parent c2b9184f2c
commit 1a984d1eac
2 changed files with 16 additions and 2 deletions

View File

@@ -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}

View File

@@ -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}