feat: category import from CSV + budget auto-assign rules

Category:
- Add categoryColumn to NormalizerConfig and NormalizedRow
- Map 'Category' column for Discover CC profile
- Write category to Transaction on upload

Budget rules:
- Add BudgetRule model (userId, budgetId, pattern)
- API: GET/POST /api/budget-rules, DELETE /api/budget-rules/:id
- Apply rules during upload (first case-insensitive match wins)
- Budgets page fetches and passes rules per budget
- BudgetCard 'Rules' menu item opens BudgetRulesDialog for add/delete

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jerick
2026-04-20 22:56:41 -04:00
parent efe42ac366
commit 60fc836b73
10 changed files with 234 additions and 10 deletions

View File

@@ -6,29 +6,41 @@ export default async function BudgetsPage() {
const session = await auth()
if (!session?.user?.id) return null
const userId = session.user.id
const now = new Date()
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1)
const monthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999)
const [budgets, spendRows] = await Promise.all([
const [budgets, spendRows, rules] = await Promise.all([
prisma.budget.findMany({
where: { userId: session.user.id },
where: { userId },
orderBy: { createdAt: 'asc' },
}),
prisma.$queryRaw<{ budgetId: string; total: bigint }[]>`
SELECT t."budgetId", COALESCE(SUM(t."amountCents"), 0)::bigint AS total
FROM "Transaction" t
JOIN "Account" a ON t."accountId" = a.id
WHERE a."userId" = ${session.user.id}
WHERE a."userId" = ${userId}
AND t."budgetId" IS NOT NULL
AND t.type = 'DEBIT'
AND t.date >= ${monthStart}
AND t.date <= ${monthEnd}
GROUP BY t."budgetId"
`,
prisma.budgetRule.findMany({
where: { userId },
orderBy: { createdAt: 'asc' },
select: { id: true, budgetId: true, pattern: true },
}),
])
const spendMap = new Map(spendRows.map((r) => [r.budgetId, Number(r.total)]))
const rulesMap = new Map<string, { id: string; pattern: string }[]>()
for (const rule of rules) {
const existing = rulesMap.get(rule.budgetId) ?? []
existing.push({ id: rule.id, pattern: rule.pattern })
rulesMap.set(rule.budgetId, existing)
}
const budgetsWithSpend = budgets.map((b) => ({
id: b.id,
@@ -37,6 +49,7 @@ export default async function BudgetsPage() {
color: b.color,
isActive: b.isActive,
spendCents: spendMap.get(b.id) ?? 0,
rules: rulesMap.get(b.id) ?? [],
}))
return (

View File

@@ -0,0 +1,16 @@
import { NextResponse } from 'next/server'
import { auth } from '@/lib/auth'
import { prisma } from '@/lib/prisma'
export async function DELETE(_req: Request, { params }: { params: Promise<{ id: string }> }) {
const session = await auth()
if (!session?.user?.id) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
const { id } = await params
const rule = await prisma.budgetRule.findFirst({ where: { id, userId: session.user.id } })
if (!rule) return NextResponse.json({ error: 'Not found' }, { status: 404 })
await prisma.budgetRule.delete({ where: { id } })
return new NextResponse(null, { status: 204 })
}

View File

@@ -0,0 +1,37 @@
import { NextResponse } from 'next/server'
import { auth } from '@/lib/auth'
import { prisma } from '@/lib/prisma'
import { createBudgetRuleSchema } from '@/lib/validations/budget-rule'
export async function GET() {
const session = await auth()
if (!session?.user?.id) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
const rules = await prisma.budgetRule.findMany({
where: { userId: session.user.id },
orderBy: { createdAt: 'asc' },
select: { id: true, budgetId: true, pattern: true },
})
return NextResponse.json(rules)
}
export async function POST(req: Request) {
const session = await auth()
if (!session?.user?.id) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
const body = await req.json()
const parsed = createBudgetRuleSchema.safeParse(body)
if (!parsed.success) return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 })
const { budgetId, pattern } = parsed.data
// Verify budget belongs to this user
const budget = await prisma.budget.findFirst({ where: { id: budgetId, userId: session.user.id } })
if (!budget) return NextResponse.json({ error: 'Budget not found' }, { status: 404 })
const rule = await prisma.budgetRule.create({
data: { userId: session.user.id, budgetId, pattern },
select: { id: true, budgetId: true, pattern: true },
})
return NextResponse.json(rule, { status: 201 })
}

View File

@@ -76,6 +76,22 @@ export async function POST(req: Request) {
const normalized = normalizeRows(allRows, accountId, config)
// Apply budget auto-assign rules (first match wins)
const budgetRules = await prisma.budgetRule.findMany({
where: { userId: session.user.id },
orderBy: { createdAt: 'asc' },
select: { pattern: true, budgetId: true },
})
const rowsWithBudgets = normalized.map((row) => {
const desc = row.description.toLowerCase()
for (const rule of budgetRules) {
if (desc.includes(rule.pattern.toLowerCase())) {
return { ...row, budgetId: rule.budgetId }
}
}
return { ...row, budgetId: null }
})
const upload = await prisma.csvUpload.create({
data: {
accountId,
@@ -89,13 +105,15 @@ export async function POST(req: Request) {
try {
const { count: importedCount } = await prisma.transaction.createMany({
data: normalized.map((r) => ({
data: rowsWithBudgets.map((r) => ({
accountId,
uploadId: upload.id,
date: r.date,
description: r.description,
amountCents: r.amountCents,
type: r.type,
category: r.category ?? null,
budgetId: r.budgetId,
dedupeHash: r.dedupeHash,
})),
skipDuplicates: true,
@@ -118,7 +136,7 @@ export async function POST(req: Request) {
// Upsert balance snapshots for each affected month
const months = [
...new Map(
normalized.map((r) => {
rowsWithBudgets.map((r) => {
const y = r.date.getFullYear()
const m = r.date.getMonth() + 1
return [`${y}-${m}`, { year: y, month: m }]