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 <noreply@anthropic.com>
This commit is contained in:
@@ -27,7 +27,7 @@ export default async function BudgetsPage({ searchParams }: { searchParams: Sear
|
|||||||
}),
|
}),
|
||||||
prisma.$queryRaw<{ budgetId: string; total: bigint }[]>`
|
prisma.$queryRaw<{ budgetId: string; total: bigint }[]>`
|
||||||
SELECT t."budgetId",
|
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
|
FROM "Transaction" t
|
||||||
JOIN "Account" a ON t."accountId" = a.id
|
JOIN "Account" a ON t."accountId" = a.id
|
||||||
WHERE a."userId" = ${userId}
|
WHERE a."userId" = ${userId}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ export default async function DashboardPage({ searchParams }: { searchParams: Se
|
|||||||
}),
|
}),
|
||||||
prisma.$queryRaw<{ budgetId: string; total: bigint }[]>`
|
prisma.$queryRaw<{ budgetId: string; total: bigint }[]>`
|
||||||
SELECT t."budgetId",
|
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
|
FROM "Transaction" t
|
||||||
JOIN "Account" a ON t."accountId" = a.id
|
JOIN "Account" a ON t."accountId" = a.id
|
||||||
WHERE a."userId" = ${userId}
|
WHERE a."userId" = ${userId}
|
||||||
|
|||||||
Reference in New Issue
Block a user