account specific budget queries

This commit is contained in:
2026-04-20 23:44:06 -04:00
parent 91a33cdfec
commit d67a80b413
2 changed files with 18 additions and 4 deletions

View File

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