Changed budget allocation

This commit is contained in:
2026-04-21 10:43:00 -04:00
parent 8c0e4ad684
commit c2b9184f2c
2 changed files with 4 additions and 4 deletions

View File

@@ -26,12 +26,12 @@ export default async function BudgetsPage({ searchParams }: { searchParams: Sear
orderBy: { createdAt: 'asc' }, orderBy: { createdAt: 'asc' },
}), }),
prisma.$queryRaw<{ budgetId: string; total: bigint }[]>` prisma.$queryRaw<{ budgetId: string; total: bigint }[]>`
SELECT t."budgetId", COALESCE(SUM(t."amountCents"), 0)::bigint AS total SELECT t."budgetId",
COALESCE(SUM(CASE WHEN t.type = 'DEBIT' THEN t."amountCents" ELSE -t."amountCents" 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}
AND t."budgetId" IS NOT NULL AND t."budgetId" IS NOT NULL
AND (a.type = 'CREDIT_CARD' OR t.type = 'DEBIT')
AND t.date >= ${start} AND t.date >= ${start}
AND t.date <= ${end} AND t.date <= ${end}
GROUP BY t."budgetId" GROUP BY t."budgetId"

View File

@@ -41,12 +41,12 @@ export default async function DashboardPage({ searchParams }: { searchParams: Se
orderBy: { name: 'asc' }, orderBy: { name: 'asc' },
}), }),
prisma.$queryRaw<{ budgetId: string; total: bigint }[]>` prisma.$queryRaw<{ budgetId: string; total: bigint }[]>`
SELECT t."budgetId", COALESCE(SUM(t."amountCents"), 0)::bigint AS total SELECT t."budgetId",
COALESCE(SUM(CASE WHEN t.type = 'DEBIT' THEN t."amountCents" ELSE -t."amountCents" 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}
AND t."budgetId" IS NOT NULL AND t."budgetId" IS NOT NULL
AND (a.type = 'CREDIT_CARD' OR t.type = 'DEBIT')
AND t.date >= ${start} AND t.date >= ${start}
AND t.date <= ${end} AND t.date <= ${end}
GROUP BY t."budgetId" GROUP BY t."budgetId"