diff --git a/src/app/(app)/budgets/page.tsx b/src/app/(app)/budgets/page.tsx index 5aee553..67322e4 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 0 END), 0)::bigint AS total + COALESCE(SUM(CASE 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} diff --git a/src/app/(app)/dashboard/page.tsx b/src/app/(app)/dashboard/page.tsx index 1e8b56e..ed91244 100644 --- a/src/app/(app)/dashboard/page.tsx +++ b/src/app/(app)/dashboard/page.tsx @@ -42,7 +42,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 0 END), 0)::bigint AS total + COALESCE(SUM(CASE 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} diff --git a/src/app/api/admin/fix-cc-types/route.ts b/src/app/api/admin/fix-cc-types/route.ts deleted file mode 100644 index b2698e9..0000000 --- a/src/app/api/admin/fix-cc-types/route.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { NextResponse } from 'next/server' -import { auth } from '@/lib/auth' -import { prisma } from '@/lib/prisma' - -// One-time migration: flips DEBIT↔CREDIT on all CREDIT_CARD account transactions. -// CC CSVs have inverted sign semantics (CREDIT = charge, DEBIT = refund), but we now -// normalise them to standard semantics (DEBIT = spend) at upload time. Existing rows -// uploaded before that fix need their type flipped. -export async function POST() { - const session = await auth() - if (!session?.user?.id) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) - - const userId = session.user.id - - const ccAccounts = await prisma.account.findMany({ - where: { userId, type: 'CREDIT_CARD' }, - select: { id: true }, - }) - - if (ccAccounts.length === 0) { - return NextResponse.json({ updated: 0, message: 'No credit card accounts found' }) - } - - const ccIds = ccAccounts.map((a) => a.id) - - // Flip both directions in one raw statement to avoid stepping on our own update - const result = await prisma.$executeRaw` - UPDATE "Transaction" - SET type = CASE WHEN type = 'DEBIT' THEN 'CREDIT'::"TransactionType" - ELSE 'DEBIT'::"TransactionType" END - WHERE "accountId" = ANY(${ccIds}::text[]) - ` - - return NextResponse.json({ updated: result }) -}