Compare commits
2 Commits
1a984d1eac
...
62ca178308
| Author | SHA1 | Date | |
|---|---|---|---|
| 62ca178308 | |||
| a9c12b94e1 |
@@ -27,14 +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(
|
COALESCE(SUM(CASE WHEN t.type = 'DEBIT' THEN t."amountCents" ELSE -t."amountCents" END), 0)::bigint AS total
|
||||||
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 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}
|
||||||
|
|||||||
@@ -42,14 +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(
|
COALESCE(SUM(CASE WHEN t.type = 'DEBIT' THEN t."amountCents" ELSE -t."amountCents" END), 0)::bigint AS total
|
||||||
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 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}
|
||||||
|
|||||||
@@ -74,13 +74,7 @@ export async function POST(req: Request) {
|
|||||||
config = result.data
|
config = result.data
|
||||||
}
|
}
|
||||||
|
|
||||||
const rawNormalized = normalizeRows(allRows, accountId, config)
|
const normalized = normalizeRows(allRows, accountId, config)
|
||||||
|
|
||||||
// Credit card CSVs have inverted sign semantics: CREDIT = charge, DEBIT = refund.
|
|
||||||
// Flip types so the rest of the app can treat DEBIT = spending universally.
|
|
||||||
const normalized = account.type === 'CREDIT_CARD'
|
|
||||||
? rawNormalized.map((r) => ({ ...r, type: r.type === 'CREDIT' ? 'DEBIT' as const : 'CREDIT' as const }))
|
|
||||||
: rawNormalized
|
|
||||||
|
|
||||||
// Apply budget auto-assign rules (first match wins)
|
// Apply budget auto-assign rules (first match wins)
|
||||||
const budgetRules = await prisma.budgetRule.findMany({
|
const budgetRules = await prisma.budgetRule.findMany({
|
||||||
@@ -125,21 +119,6 @@ export async function POST(req: Request) {
|
|||||||
skipDuplicates: true,
|
skipDuplicates: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
// For CC accounts, also correct the type on any rows that already existed
|
|
||||||
// (uploaded before the CREDIT↔DEBIT flip was introduced).
|
|
||||||
if (account.type === 'CREDIT_CARD') {
|
|
||||||
const debitHashes = rowsWithBudgets.filter((r) => r.type === 'DEBIT').map((r) => r.dedupeHash)
|
|
||||||
const creditHashes = rowsWithBudgets.filter((r) => r.type === 'CREDIT').map((r) => r.dedupeHash)
|
|
||||||
await Promise.all([
|
|
||||||
debitHashes.length > 0
|
|
||||||
? prisma.transaction.updateMany({ where: { dedupeHash: { in: debitHashes }, type: 'CREDIT' }, data: { type: 'DEBIT' } })
|
|
||||||
: Promise.resolve(),
|
|
||||||
creditHashes.length > 0
|
|
||||||
? prisma.transaction.updateMany({ where: { dedupeHash: { in: creditHashes }, type: 'DEBIT' }, data: { type: 'CREDIT' } })
|
|
||||||
: Promise.resolve(),
|
|
||||||
])
|
|
||||||
}
|
|
||||||
|
|
||||||
const skippedCount = normalized.length - importedCount
|
const skippedCount = normalized.length - importedCount
|
||||||
|
|
||||||
// Recompute current balance
|
// Recompute current balance
|
||||||
|
|||||||
Reference in New Issue
Block a user