Revert budget to net DEBIT-CREDIT formula, remove fix-cc-types endpoint

Budget formula correctly adds DEBIT (CC charges stored from positive CSV
values) and subtracts CREDIT (refunds from negative CSV values).
Remove the fix-cc-types endpoint which was flipping CC transaction types
and causing charges to appear as CREDIT.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-21 16:46:49 -04:00
parent 00d4796008
commit 587ac19b18
3 changed files with 2 additions and 37 deletions

View File

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

View File

@@ -42,7 +42,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 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 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}

View File

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