first build commit
This commit is contained in:
88
src/app/(app)/accounts/[id]/page.tsx
Normal file
88
src/app/(app)/accounts/[id]/page.tsx
Normal file
@@ -0,0 +1,88 @@
|
||||
import { notFound } from 'next/navigation'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { AccountBadge } from '@/components/accounts/AccountBadge'
|
||||
import { formatCents } from '@/lib/utils/currency'
|
||||
import { TransactionTable } from '@/components/transactions/TransactionTable'
|
||||
|
||||
const PAGE_LIMIT = 50
|
||||
|
||||
type Props = {
|
||||
params: Promise<{ id: string }>
|
||||
searchParams: Promise<Record<string, string | string[] | undefined>>
|
||||
}
|
||||
|
||||
export default async function AccountDetailPage({ params, searchParams }: Props) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return null
|
||||
|
||||
const { id } = await params
|
||||
const sp = await searchParams
|
||||
const page = Math.max(1, Number(sp.page) || 1)
|
||||
|
||||
const [account, budgets] = await Promise.all([
|
||||
prisma.account.findFirst({ where: { id, userId: session.user.id } }),
|
||||
prisma.budget.findMany({
|
||||
where: { userId: session.user.id, isActive: true },
|
||||
select: { id: true, name: true, color: true },
|
||||
orderBy: { name: 'asc' },
|
||||
}),
|
||||
])
|
||||
|
||||
if (!account) notFound()
|
||||
|
||||
const where = { accountId: id }
|
||||
const [transactions, total] = await Promise.all([
|
||||
prisma.transaction.findMany({
|
||||
where,
|
||||
include: {
|
||||
account: { select: { name: true, type: true } },
|
||||
budget: { select: { id: true, name: true, color: true } },
|
||||
},
|
||||
orderBy: { date: 'desc' },
|
||||
skip: (page - 1) * PAGE_LIMIT,
|
||||
take: PAGE_LIMIT,
|
||||
}),
|
||||
prisma.transaction.count({ where }),
|
||||
])
|
||||
|
||||
const rows = transactions.map((tx) => ({
|
||||
...tx,
|
||||
date: tx.date.toISOString(),
|
||||
createdAt: undefined,
|
||||
updatedAt: undefined,
|
||||
}))
|
||||
|
||||
return (
|
||||
<div className="p-6 space-y-6">
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="space-y-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<h1 className="text-2xl font-bold">{account.name}</h1>
|
||||
<AccountBadge type={account.type} />
|
||||
</div>
|
||||
{account.institution && (
|
||||
<p className="text-muted-foreground">{account.institution}</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<p className="text-3xl font-bold tabular-nums">
|
||||
{formatCents(account.currentBalanceCents)}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
{account.isActive ? 'Active' : 'Inactive'}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<TransactionTable
|
||||
transactions={rows}
|
||||
total={total}
|
||||
page={page}
|
||||
limit={PAGE_LIMIT}
|
||||
showAccount={false}
|
||||
budgets={budgets}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
19
src/app/(app)/accounts/page.tsx
Normal file
19
src/app/(app)/accounts/page.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { AccountList } from '@/components/accounts/AccountList'
|
||||
|
||||
export default async function AccountsPage() {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return null
|
||||
|
||||
const accounts = await prisma.account.findMany({
|
||||
where: { userId: session.user.id },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<AccountList accounts={accounts} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
47
src/app/(app)/budgets/page.tsx
Normal file
47
src/app/(app)/budgets/page.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { BudgetList } from '@/components/budgets/BudgetList'
|
||||
|
||||
export default async function BudgetsPage() {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return null
|
||||
|
||||
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([
|
||||
prisma.budget.findMany({
|
||||
where: { userId: session.user.id },
|
||||
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}
|
||||
AND t."budgetId" IS NOT NULL
|
||||
AND t.type = 'DEBIT'
|
||||
AND t.date >= ${monthStart}
|
||||
AND t.date <= ${monthEnd}
|
||||
GROUP BY t."budgetId"
|
||||
`,
|
||||
])
|
||||
|
||||
const spendMap = new Map(spendRows.map((r) => [r.budgetId, Number(r.total)]))
|
||||
|
||||
const budgetsWithSpend = budgets.map((b) => ({
|
||||
id: b.id,
|
||||
name: b.name,
|
||||
limitCents: b.limitCents,
|
||||
color: b.color,
|
||||
isActive: b.isActive,
|
||||
spendCents: spendMap.get(b.id) ?? 0,
|
||||
}))
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<BudgetList budgets={budgetsWithSpend} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
104
src/app/(app)/dashboard/page.tsx
Normal file
104
src/app/(app)/dashboard/page.tsx
Normal file
@@ -0,0 +1,104 @@
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { monthBounds, monthLabel } from '@/lib/utils/dates'
|
||||
import { NetWorthCard } from '@/components/dashboard/NetWorthCard'
|
||||
import { CashFlowCard } from '@/components/dashboard/CashFlowCard'
|
||||
import { RecentTransactions } from '@/components/dashboard/RecentTransactions'
|
||||
import { BudgetSummary } from '@/components/dashboard/BudgetSummary'
|
||||
|
||||
export default async function DashboardPage() {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return null
|
||||
|
||||
const userId = session.user.id
|
||||
const { start, end } = monthBounds()
|
||||
|
||||
const [accounts, cashFlowRows, budgets, spendRows, recentTx] = await Promise.all([
|
||||
prisma.account.findMany({
|
||||
where: { userId, isActive: true },
|
||||
select: { id: true, name: true, type: true, currentBalanceCents: true },
|
||||
orderBy: { name: 'asc' },
|
||||
}),
|
||||
prisma.$queryRaw<{ type: string; total: bigint }[]>`
|
||||
SELECT t.type, COALESCE(SUM(t."amountCents"), 0)::bigint AS total
|
||||
FROM "Transaction" t
|
||||
JOIN "Account" a ON t."accountId" = a.id
|
||||
WHERE a."userId" = ${userId}
|
||||
AND a.type = 'BANK'
|
||||
AND t.date >= ${start}
|
||||
AND t.date <= ${end}
|
||||
GROUP BY t.type
|
||||
`,
|
||||
prisma.budget.findMany({
|
||||
where: { userId, isActive: true },
|
||||
orderBy: { name: '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" = ${userId}
|
||||
AND t."budgetId" IS NOT NULL
|
||||
AND t.type = 'DEBIT'
|
||||
AND t.date >= ${start}
|
||||
AND t.date <= ${end}
|
||||
GROUP BY t."budgetId"
|
||||
`,
|
||||
prisma.transaction.findMany({
|
||||
where: { account: { userId } },
|
||||
include: { account: { select: { name: true } } },
|
||||
orderBy: { date: 'desc' },
|
||||
take: 5,
|
||||
}),
|
||||
])
|
||||
|
||||
const bankAccounts = accounts.filter((a) => a.type === 'BANK')
|
||||
const netWorthCents = bankAccounts.reduce((s, a) => s + a.currentBalanceCents, 0)
|
||||
|
||||
const cfMap = Object.fromEntries(cashFlowRows.map((r) => [r.type, Number(r.total)]))
|
||||
const creditsCents = cfMap['CREDIT'] ?? 0
|
||||
const debitsCents = cfMap['DEBIT'] ?? 0
|
||||
|
||||
const spendMap = new Map(spendRows.map((r) => [r.budgetId, Number(r.total)]))
|
||||
|
||||
const recentTransactions = recentTx.map((t) => ({
|
||||
id: t.id,
|
||||
date: t.date.toISOString(),
|
||||
description: t.description,
|
||||
amountCents: t.amountCents,
|
||||
type: t.type,
|
||||
accountName: t.account.name,
|
||||
}))
|
||||
|
||||
const budgetsWithSpend = budgets.map((b) => ({
|
||||
id: b.id,
|
||||
name: b.name,
|
||||
limitCents: b.limitCents,
|
||||
color: b.color,
|
||||
spendCents: spendMap.get(b.id) ?? 0,
|
||||
}))
|
||||
|
||||
return (
|
||||
<div className="p-6 space-y-6">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold">Dashboard</h1>
|
||||
<p className="text-sm text-muted-foreground">{monthLabel()}</p>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<NetWorthCard netWorthCents={netWorthCents} bankAccounts={bankAccounts} />
|
||||
<CashFlowCard
|
||||
monthLabel={monthLabel()}
|
||||
creditsCents={creditsCents}
|
||||
debitsCents={debitsCents}
|
||||
netCents={creditsCents - debitsCents}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 lg:grid-cols-2">
|
||||
<RecentTransactions transactions={recentTransactions} />
|
||||
<BudgetSummary budgets={budgetsWithSpend} />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
186
src/app/(app)/graphs/page.tsx
Normal file
186
src/app/(app)/graphs/page.tsx
Normal file
@@ -0,0 +1,186 @@
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { monthBounds, monthLabel, formatYearMonth, lastNMonthsStart } from '@/lib/utils/dates'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { NetWorthTrendChart } from '@/components/graphs/NetWorthTrendChart'
|
||||
import { CashFlowChart } from '@/components/graphs/CashFlowChart'
|
||||
import { MonthlySpendingChart } from '@/components/graphs/MonthlySpendingChart'
|
||||
import { CategoryBreakdownChart } from '@/components/graphs/CategoryBreakdownChart'
|
||||
import { BudgetChart } from '@/components/graphs/BudgetChart'
|
||||
|
||||
export default async function GraphsPage() {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return null
|
||||
|
||||
const userId = session.user.id
|
||||
const { start: monthStart, end: monthEnd } = monthBounds()
|
||||
const sixMonthsAgo = lastNMonthsStart(6)
|
||||
|
||||
const [netWorthRows, cashFlowRows, spendingRows, categoryRows, budgetSpendRows, budgets] =
|
||||
await Promise.all([
|
||||
// Net worth trend from BalanceSnapshot (all time, BANK only)
|
||||
prisma.$queryRaw<{ year: number; month: number; total: bigint }[]>`
|
||||
SELECT bs.year, bs.month, COALESCE(SUM(bs."balanceCents"), 0)::bigint AS total
|
||||
FROM "BalanceSnapshot" bs
|
||||
JOIN "Account" a ON bs."accountId" = a.id
|
||||
WHERE a."userId" = ${userId} AND a.type = 'BANK'
|
||||
GROUP BY bs.year, bs.month
|
||||
ORDER BY bs.year, bs.month
|
||||
`,
|
||||
// Monthly cash flow (last 6 months, BANK only)
|
||||
prisma.$queryRaw<{ year: number; month: number; type: string; total: bigint }[]>`
|
||||
SELECT
|
||||
EXTRACT(YEAR FROM t.date)::int AS year,
|
||||
EXTRACT(MONTH FROM t.date)::int AS month,
|
||||
t.type,
|
||||
COALESCE(SUM(t."amountCents"), 0)::bigint AS total
|
||||
FROM "Transaction" t
|
||||
JOIN "Account" a ON t."accountId" = a.id
|
||||
WHERE a."userId" = ${userId}
|
||||
AND a.type = 'BANK'
|
||||
AND t.date >= ${sixMonthsAgo}
|
||||
GROUP BY year, month, t.type
|
||||
ORDER BY year, month
|
||||
`,
|
||||
// Monthly total spending across ALL accounts (last 6 months, DEBIT only)
|
||||
prisma.$queryRaw<{ year: number; month: number; total: bigint }[]>`
|
||||
SELECT
|
||||
EXTRACT(YEAR FROM t.date)::int AS year,
|
||||
EXTRACT(MONTH FROM t.date)::int AS month,
|
||||
COALESCE(SUM(t."amountCents"), 0)::bigint AS total
|
||||
FROM "Transaction" t
|
||||
JOIN "Account" a ON t."accountId" = a.id
|
||||
WHERE a."userId" = ${userId}
|
||||
AND t.type = 'DEBIT'
|
||||
AND t.date >= ${sixMonthsAgo}
|
||||
GROUP BY year, month
|
||||
ORDER BY year, month
|
||||
`,
|
||||
// Category breakdown (current month, all accounts, DEBIT)
|
||||
prisma.$queryRaw<{ category: string; total: bigint }[]>`
|
||||
SELECT
|
||||
COALESCE(t.category, 'Uncategorized') AS category,
|
||||
COALESCE(SUM(t."amountCents"), 0)::bigint AS total
|
||||
FROM "Transaction" t
|
||||
JOIN "Account" a ON t."accountId" = a.id
|
||||
WHERE a."userId" = ${userId}
|
||||
AND t.type = 'DEBIT'
|
||||
AND t.date >= ${monthStart}
|
||||
AND t.date <= ${monthEnd}
|
||||
GROUP BY category
|
||||
ORDER BY total DESC
|
||||
`,
|
||||
// Budget spend (current month)
|
||||
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" = ${userId}
|
||||
AND t."budgetId" IS NOT NULL
|
||||
AND t.type = 'DEBIT'
|
||||
AND t.date >= ${monthStart}
|
||||
AND t.date <= ${monthEnd}
|
||||
GROUP BY t."budgetId"
|
||||
`,
|
||||
prisma.budget.findMany({
|
||||
where: { userId, isActive: true },
|
||||
orderBy: { name: 'asc' },
|
||||
}),
|
||||
])
|
||||
|
||||
// Net worth trend: take last 24 data points
|
||||
const netWorthData = netWorthRows.slice(-24).map((r) => ({
|
||||
label: formatYearMonth(r.year, r.month),
|
||||
totalCents: Number(r.total),
|
||||
}))
|
||||
|
||||
// Cash flow: build month-keyed map then fill in credits/debits
|
||||
const cfMap = new Map<string, { label: string; creditsCents: number; debitsCents: number }>()
|
||||
for (const r of cashFlowRows) {
|
||||
const key = `${r.year}-${r.month}`
|
||||
if (!cfMap.has(key)) {
|
||||
cfMap.set(key, { label: formatYearMonth(r.year, r.month), creditsCents: 0, debitsCents: 0 })
|
||||
}
|
||||
const entry = cfMap.get(key)!
|
||||
if (r.type === 'CREDIT') entry.creditsCents = Number(r.total)
|
||||
else entry.debitsCents = Number(r.total)
|
||||
}
|
||||
const cashFlowData = Array.from(cfMap.values())
|
||||
|
||||
// Monthly spending (all accounts)
|
||||
const monthlySpendData = spendingRows.map((r) => ({
|
||||
label: formatYearMonth(r.year, r.month),
|
||||
totalCents: Number(r.total),
|
||||
}))
|
||||
|
||||
// Category breakdown
|
||||
const categoryData = categoryRows.map((r) => ({
|
||||
category: r.category,
|
||||
totalCents: Number(r.total),
|
||||
}))
|
||||
|
||||
// Budget chart
|
||||
const spendMap = new Map(budgetSpendRows.map((r) => [r.budgetId, Number(r.total)]))
|
||||
const budgetData = budgets.map((b) => ({
|
||||
name: b.name,
|
||||
spendCents: spendMap.get(b.id) ?? 0,
|
||||
limitCents: b.limitCents ?? 0,
|
||||
color: b.color,
|
||||
}))
|
||||
|
||||
return (
|
||||
<div className="p-6 space-y-6">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold">Graphs</h1>
|
||||
<p className="text-sm text-muted-foreground">{monthLabel()}</p>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-6 lg:grid-cols-2">
|
||||
<Card className="lg:col-span-2">
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-base">Net Worth Trend</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<NetWorthTrendChart data={netWorthData} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-base">Cash Flow · Last 6 Months (Bank)</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<CashFlowChart data={cashFlowData} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-base">Monthly Spending · Last 6 Months (All Accounts)</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<MonthlySpendingChart data={monthlySpendData} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-base">Spending by Category · {monthLabel()}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<CategoryBreakdownChart data={categoryData} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-base">Budget Performance · {monthLabel()}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<BudgetChart data={budgetData} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
12
src/app/(app)/layout.tsx
Normal file
12
src/app/(app)/layout.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Sidebar } from '@/components/layout/Sidebar'
|
||||
|
||||
export default function AppLayout({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<div className="flex min-h-screen bg-background">
|
||||
<Sidebar />
|
||||
<main className="flex-1 overflow-auto">
|
||||
{children}
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
84
src/app/(app)/transactions/page.tsx
Normal file
84
src/app/(app)/transactions/page.tsx
Normal file
@@ -0,0 +1,84 @@
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { Prisma } from '@/generated/prisma/client'
|
||||
import { TransactionFilters } from '@/components/transactions/TransactionFilters'
|
||||
import { TransactionTable } from '@/components/transactions/TransactionTable'
|
||||
|
||||
const PAGE_LIMIT = 50
|
||||
|
||||
type SearchParams = Promise<Record<string, string | string[] | undefined>>
|
||||
|
||||
export default async function TransactionsPage({ searchParams }: { searchParams: SearchParams }) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return null
|
||||
|
||||
const sp = await searchParams
|
||||
const get = (key: string) => (Array.isArray(sp[key]) ? sp[key][0] : sp[key]) ?? ''
|
||||
|
||||
const page = Math.max(1, Number(get('page')) || 1)
|
||||
const accountId = get('accountId')
|
||||
const dateFrom = get('dateFrom')
|
||||
const dateTo = get('dateTo')
|
||||
const type = get('type') as 'DEBIT' | 'CREDIT' | ''
|
||||
const search = get('search')
|
||||
|
||||
const where: Prisma.TransactionWhereInput = {
|
||||
account: { userId: session.user.id },
|
||||
...(accountId && { accountId }),
|
||||
...(type && { type }),
|
||||
...(search && { description: { contains: search, mode: 'insensitive' } }),
|
||||
...((dateFrom || dateTo) && {
|
||||
date: {
|
||||
...(dateFrom && { gte: new Date(dateFrom) }),
|
||||
...(dateTo && { lte: new Date(dateTo + 'T23:59:59.999Z') }),
|
||||
},
|
||||
}),
|
||||
}
|
||||
|
||||
const [transactions, total, accounts, budgets] = await Promise.all([
|
||||
prisma.transaction.findMany({
|
||||
where,
|
||||
include: {
|
||||
account: { select: { name: true, type: true } },
|
||||
budget: { select: { id: true, name: true, color: true } },
|
||||
},
|
||||
orderBy: { date: 'desc' },
|
||||
skip: (page - 1) * PAGE_LIMIT,
|
||||
take: PAGE_LIMIT,
|
||||
}),
|
||||
prisma.transaction.count({ where }),
|
||||
prisma.account.findMany({
|
||||
where: { userId: session.user.id },
|
||||
select: { id: true, name: true },
|
||||
orderBy: { name: 'asc' },
|
||||
}),
|
||||
prisma.budget.findMany({
|
||||
where: { userId: session.user.id, isActive: true },
|
||||
select: { id: true, name: true, color: true },
|
||||
orderBy: { name: 'asc' },
|
||||
}),
|
||||
])
|
||||
|
||||
// Serialize dates for client components
|
||||
const rows = transactions.map((tx) => ({
|
||||
...tx,
|
||||
date: tx.date.toISOString(),
|
||||
createdAt: undefined,
|
||||
updatedAt: undefined,
|
||||
}))
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<h1 className="text-2xl font-bold mb-4">Transactions</h1>
|
||||
<TransactionFilters accounts={accounts} />
|
||||
<TransactionTable
|
||||
transactions={rows}
|
||||
total={total}
|
||||
page={page}
|
||||
limit={PAGE_LIMIT}
|
||||
showAccount
|
||||
budgets={budgets}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
24
src/app/(app)/upload/page.tsx
Normal file
24
src/app/(app)/upload/page.tsx
Normal file
@@ -0,0 +1,24 @@
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { UploadForm } from '@/components/upload/UploadForm'
|
||||
|
||||
export default async function UploadPage() {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return null
|
||||
|
||||
const accounts = await prisma.account.findMany({
|
||||
where: { userId: session.user.id, isActive: true },
|
||||
orderBy: { name: 'asc' },
|
||||
select: { id: true, name: true },
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="p-6 max-w-2xl">
|
||||
<h1 className="text-2xl font-bold mb-1">Upload Transactions</h1>
|
||||
<p className="text-sm text-muted-foreground mb-6">
|
||||
Import a bank CSV. Duplicates are silently skipped.
|
||||
</p>
|
||||
<UploadForm accounts={accounts} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
75
src/app/(auth)/login/page.tsx
Normal file
75
src/app/(auth)/login/page.tsx
Normal file
@@ -0,0 +1,75 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { signIn } from 'next-auth/react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
||||
export default function LoginPage() {
|
||||
const router = useRouter()
|
||||
const [error, setError] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
|
||||
e.preventDefault()
|
||||
setError('')
|
||||
setLoading(true)
|
||||
|
||||
const form = new FormData(e.currentTarget)
|
||||
const result = await signIn('credentials', {
|
||||
email: form.get('email') as string,
|
||||
password: form.get('password') as string,
|
||||
redirect: false,
|
||||
})
|
||||
|
||||
if (result?.error) {
|
||||
setError('Invalid email or password')
|
||||
setLoading(false)
|
||||
} else {
|
||||
router.push('/dashboard')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-background p-4">
|
||||
<Card className="w-full max-w-sm">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-2xl">Sign in</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="email">Email</Label>
|
||||
<Input
|
||||
id="email"
|
||||
name="email"
|
||||
type="email"
|
||||
autoComplete="email"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="password">Password</Label>
|
||||
<Input
|
||||
id="password"
|
||||
name="password"
|
||||
type="password"
|
||||
autoComplete="current-password"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
{error && (
|
||||
<p className="text-sm text-destructive">{error}</p>
|
||||
)}
|
||||
<Button type="submit" className="w-full" disabled={loading}>
|
||||
{loading ? 'Signing in…' : 'Sign in'}
|
||||
</Button>
|
||||
</form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
63
src/app/api/accounts/[id]/route.ts
Normal file
63
src/app/api/accounts/[id]/route.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { updateAccountSchema } from '@/lib/validations/account'
|
||||
|
||||
type Params = { params: Promise<{ id: string }> }
|
||||
|
||||
export async function GET(_req: Request, { params }: Params) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
const account = await prisma.account.findFirst({
|
||||
where: { id, userId: session.user.id },
|
||||
})
|
||||
|
||||
if (!account) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
return NextResponse.json(account)
|
||||
}
|
||||
|
||||
export async function PATCH(req: Request, { params }: Params) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
const body = await req.json()
|
||||
const parsed = updateAccountSchema.safeParse(body)
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 })
|
||||
}
|
||||
|
||||
const existing = await prisma.account.findFirst({
|
||||
where: { id, userId: session.user.id },
|
||||
})
|
||||
if (!existing) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
|
||||
const account = await prisma.account.update({
|
||||
where: { id },
|
||||
data: parsed.data,
|
||||
})
|
||||
|
||||
return NextResponse.json(account)
|
||||
}
|
||||
|
||||
export async function DELETE(_req: Request, { params }: Params) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
const existing = await prisma.account.findFirst({
|
||||
where: { id, userId: session.user.id },
|
||||
})
|
||||
if (!existing) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
|
||||
await prisma.account.delete({ where: { id } })
|
||||
return new NextResponse(null, { status: 204 })
|
||||
}
|
||||
37
src/app/api/accounts/route.ts
Normal file
37
src/app/api/accounts/route.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { createAccountSchema } from '@/lib/validations/account'
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const accounts = await prisma.account.findMany({
|
||||
where: { userId: session.user.id },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
})
|
||||
|
||||
return NextResponse.json(accounts)
|
||||
}
|
||||
|
||||
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 = createAccountSchema.safeParse(body)
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 })
|
||||
}
|
||||
|
||||
const account = await prisma.account.create({
|
||||
data: { ...parsed.data, userId: session.user.id },
|
||||
})
|
||||
|
||||
return NextResponse.json(account, { status: 201 })
|
||||
}
|
||||
3
src/app/api/auth/[...nextauth]/route.ts
Normal file
3
src/app/api/auth/[...nextauth]/route.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { handlers } from '@/lib/auth'
|
||||
|
||||
export const { GET, POST } = handlers
|
||||
45
src/app/api/budgets/[id]/route.ts
Normal file
45
src/app/api/budgets/[id]/route.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { updateBudgetSchema } from '@/lib/validations/budget'
|
||||
|
||||
type Params = { params: Promise<{ id: string }> }
|
||||
|
||||
export async function PATCH(req: Request, { params }: Params) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
const body = await req.json()
|
||||
const parsed = updateBudgetSchema.safeParse(body)
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 })
|
||||
}
|
||||
|
||||
const existing = await prisma.budget.findFirst({
|
||||
where: { id, userId: session.user.id },
|
||||
})
|
||||
if (!existing) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
|
||||
const budget = await prisma.budget.update({ where: { id }, data: parsed.data })
|
||||
return NextResponse.json(budget)
|
||||
}
|
||||
|
||||
export async function DELETE(_req: Request, { params }: Params) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
const existing = await prisma.budget.findFirst({
|
||||
where: { id, userId: session.user.id },
|
||||
})
|
||||
if (!existing) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
|
||||
// onDelete: SetNull in schema nulls out Transaction.budgetId automatically
|
||||
await prisma.budget.delete({ where: { id } })
|
||||
return new NextResponse(null, { status: 204 })
|
||||
}
|
||||
37
src/app/api/budgets/route.ts
Normal file
37
src/app/api/budgets/route.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { createBudgetSchema } from '@/lib/validations/budget'
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const budgets = await prisma.budget.findMany({
|
||||
where: { userId: session.user.id },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
})
|
||||
|
||||
return NextResponse.json(budgets)
|
||||
}
|
||||
|
||||
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 = createBudgetSchema.safeParse(body)
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 })
|
||||
}
|
||||
|
||||
const budget = await prisma.budget.create({
|
||||
data: { ...parsed.data, userId: session.user.id },
|
||||
})
|
||||
|
||||
return NextResponse.json(budget, { status: 201 })
|
||||
}
|
||||
78
src/app/api/dashboard/route.ts
Normal file
78
src/app/api/dashboard/route.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { monthBounds, monthLabel } from '@/lib/utils/dates'
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const userId = session.user.id
|
||||
const { start, end } = monthBounds()
|
||||
|
||||
const [accounts, cashFlowRows, budgets, spendRows, recentTx] = await Promise.all([
|
||||
prisma.account.findMany({
|
||||
where: { userId, isActive: true },
|
||||
select: { id: true, name: true, type: true, currentBalanceCents: true },
|
||||
orderBy: { name: 'asc' },
|
||||
}),
|
||||
prisma.$queryRaw<{ type: string; total: bigint }[]>`
|
||||
SELECT t.type, COALESCE(SUM(t."amountCents"), 0)::bigint AS total
|
||||
FROM "Transaction" t
|
||||
JOIN "Account" a ON t."accountId" = a.id
|
||||
WHERE a."userId" = ${userId}
|
||||
AND a.type = 'BANK'
|
||||
AND t.date >= ${start}
|
||||
AND t.date <= ${end}
|
||||
GROUP BY t.type
|
||||
`,
|
||||
prisma.budget.findMany({
|
||||
where: { userId, isActive: true },
|
||||
orderBy: { name: '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" = ${userId}
|
||||
AND t."budgetId" IS NOT NULL
|
||||
AND t.type = 'DEBIT'
|
||||
AND t.date >= ${start}
|
||||
AND t.date <= ${end}
|
||||
GROUP BY t."budgetId"
|
||||
`,
|
||||
prisma.transaction.findMany({
|
||||
where: { account: { userId } },
|
||||
include: { account: { select: { name: true } } },
|
||||
orderBy: { date: 'desc' },
|
||||
take: 5,
|
||||
}),
|
||||
])
|
||||
|
||||
const bankAccounts = accounts.filter((a) => a.type === 'BANK')
|
||||
const netWorthCents = bankAccounts.reduce((s, a) => s + a.currentBalanceCents, 0)
|
||||
|
||||
const cfMap = Object.fromEntries(cashFlowRows.map((r) => [r.type, Number(r.total)]))
|
||||
const creditsCents = cfMap['CREDIT'] ?? 0
|
||||
const debitsCents = cfMap['DEBIT'] ?? 0
|
||||
|
||||
const spendMap = new Map(spendRows.map((r) => [r.budgetId, Number(r.total)]))
|
||||
|
||||
return NextResponse.json({
|
||||
monthLabel: monthLabel(),
|
||||
netWorthCents,
|
||||
bankAccounts,
|
||||
cashFlow: { creditsCents, debitsCents, netCents: creditsCents - debitsCents },
|
||||
budgets: budgets.map((b) => ({ ...b, spendCents: spendMap.get(b.id) ?? 0 })),
|
||||
recentTransactions: recentTx.map((t) => ({
|
||||
id: t.id,
|
||||
date: t.date.toISOString(),
|
||||
description: t.description,
|
||||
amountCents: t.amountCents,
|
||||
type: t.type,
|
||||
accountName: t.account.name,
|
||||
})),
|
||||
})
|
||||
}
|
||||
11
src/app/api/health/route.ts
Normal file
11
src/app/api/health/route.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
await prisma.$queryRaw`SELECT 1`
|
||||
return NextResponse.json({ status: 'ok' })
|
||||
} catch {
|
||||
return NextResponse.json({ status: 'error', detail: 'database unreachable' }, { status: 503 })
|
||||
}
|
||||
}
|
||||
41
src/app/api/transactions/[id]/route.ts
Normal file
41
src/app/api/transactions/[id]/route.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { updateTransactionSchema } from '@/lib/validations/transaction'
|
||||
|
||||
type Params = { params: Promise<{ id: string }> }
|
||||
|
||||
export async function PATCH(req: Request, { params }: Params) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id } = await params
|
||||
const body = await req.json()
|
||||
const parsed = updateTransactionSchema.safeParse(body)
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 })
|
||||
}
|
||||
|
||||
// Scope check via the account's userId
|
||||
const existing = await prisma.transaction.findFirst({
|
||||
where: { id, account: { userId: session.user.id } },
|
||||
})
|
||||
if (!existing) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
|
||||
// Validate budgetId belongs to this user if provided
|
||||
if (parsed.data.budgetId) {
|
||||
const budget = await prisma.budget.findFirst({
|
||||
where: { id: parsed.data.budgetId, userId: session.user.id },
|
||||
})
|
||||
if (!budget) return NextResponse.json({ error: 'Budget not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
const transaction = await prisma.transaction.update({
|
||||
where: { id },
|
||||
data: parsed.data,
|
||||
})
|
||||
|
||||
return NextResponse.json(transaction)
|
||||
}
|
||||
50
src/app/api/transactions/route.ts
Normal file
50
src/app/api/transactions/route.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { transactionQuerySchema } from '@/lib/validations/transaction'
|
||||
import { Prisma } from '@/generated/prisma/client'
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(req.url)
|
||||
const parsed = transactionQuerySchema.safeParse(Object.fromEntries(searchParams))
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 })
|
||||
}
|
||||
|
||||
const { accountId, dateFrom, dateTo, type, search, budgetId, page, limit } = parsed.data
|
||||
|
||||
const where: Prisma.TransactionWhereInput = {
|
||||
account: { userId: session.user.id },
|
||||
...(accountId && { accountId }),
|
||||
...(type && { type }),
|
||||
...(budgetId !== undefined && { budgetId: budgetId || null }),
|
||||
...(search && { description: { contains: search, mode: 'insensitive' } }),
|
||||
...((dateFrom || dateTo) && {
|
||||
date: {
|
||||
...(dateFrom && { gte: new Date(dateFrom) }),
|
||||
...(dateTo && { lte: new Date(dateTo + 'T23:59:59.999Z') }),
|
||||
},
|
||||
}),
|
||||
}
|
||||
|
||||
const [transactions, total] = await prisma.$transaction([
|
||||
prisma.transaction.findMany({
|
||||
where,
|
||||
include: {
|
||||
account: { select: { name: true, type: true } },
|
||||
budget: { select: { id: true, name: true, color: true } },
|
||||
},
|
||||
orderBy: { date: 'desc' },
|
||||
skip: (page - 1) * limit,
|
||||
take: limit,
|
||||
}),
|
||||
prisma.transaction.count({ where }),
|
||||
])
|
||||
|
||||
return NextResponse.json({ transactions, total, page, limit, totalPages: Math.ceil(total / limit) })
|
||||
}
|
||||
172
src/app/api/upload/route.ts
Normal file
172
src/app/api/upload/route.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import Papa from 'papaparse'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { detectProfile } from '@/lib/csv/bank-profiles'
|
||||
import { normalizeRows } from '@/lib/csv/normalizer'
|
||||
import { columnMappingSchema } from '@/lib/validations/upload'
|
||||
import type { NormalizerConfig } from '@/lib/csv/bank-profiles'
|
||||
|
||||
const MAX_FILE_SIZE = 10 * 1024 * 1024 // 10 MB
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const formData = await req.formData()
|
||||
const file = formData.get('file') as File | null
|
||||
const accountId = formData.get('accountId') as string | null
|
||||
const columnMappingRaw = formData.get('columnMapping') as string | null
|
||||
|
||||
if (!file || !accountId) {
|
||||
return NextResponse.json({ error: 'file and accountId are required' }, { status: 400 })
|
||||
}
|
||||
if (file.size > MAX_FILE_SIZE) {
|
||||
return NextResponse.json({ error: 'File too large (max 10 MB)' }, { status: 400 })
|
||||
}
|
||||
if (!file.name.toLowerCase().endsWith('.csv')) {
|
||||
return NextResponse.json({ error: 'File must be a .csv file' }, { status: 400 })
|
||||
}
|
||||
const validMimes = ['text/csv', 'text/plain', 'application/vnd.ms-excel', 'application/csv', '']
|
||||
if (file.type && !validMimes.includes(file.type)) {
|
||||
return NextResponse.json({ error: 'Invalid file type' }, { status: 400 })
|
||||
}
|
||||
|
||||
const account = await prisma.account.findFirst({
|
||||
where: { id: accountId, userId: session.user.id },
|
||||
})
|
||||
if (!account) {
|
||||
return NextResponse.json({ error: 'Account not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
const content = await file.text()
|
||||
|
||||
// Parse all rows once — used for both detection and normalization
|
||||
const parsed = Papa.parse<Record<string, string>>(content, {
|
||||
header: true,
|
||||
skipEmptyLines: true,
|
||||
transformHeader: (h) => h.trim(),
|
||||
})
|
||||
const headers = (parsed.meta.fields ?? []).map((h) => h.trim())
|
||||
const allRows = parsed.data
|
||||
|
||||
// Detect profile or use provided manual mapping
|
||||
const detected = detectProfile(headers)
|
||||
|
||||
if (!detected && !columnMappingRaw) {
|
||||
return NextResponse.json({
|
||||
requiresMapping: true,
|
||||
headers,
|
||||
sampleRows: allRows.slice(0, 5),
|
||||
})
|
||||
}
|
||||
|
||||
let config: NormalizerConfig
|
||||
if (detected && !columnMappingRaw) {
|
||||
config = detected
|
||||
} else {
|
||||
const result = columnMappingSchema.safeParse(JSON.parse(columnMappingRaw!))
|
||||
if (!result.success) {
|
||||
return NextResponse.json({ error: result.error.flatten() }, { status: 400 })
|
||||
}
|
||||
config = result.data
|
||||
}
|
||||
|
||||
const normalized = normalizeRows(allRows, accountId, config)
|
||||
|
||||
const upload = await prisma.csvUpload.create({
|
||||
data: {
|
||||
accountId,
|
||||
fileName: file.name,
|
||||
rowCount: allRows.length,
|
||||
importedCount: 0,
|
||||
skippedCount: 0,
|
||||
status: 'PENDING',
|
||||
},
|
||||
})
|
||||
|
||||
try {
|
||||
const { count: importedCount } = await prisma.transaction.createMany({
|
||||
data: normalized.map((r) => ({
|
||||
accountId,
|
||||
uploadId: upload.id,
|
||||
date: r.date,
|
||||
description: r.description,
|
||||
amountCents: r.amountCents,
|
||||
type: r.type,
|
||||
dedupeHash: r.dedupeHash,
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
})
|
||||
const skippedCount = normalized.length - importedCount
|
||||
|
||||
// Recompute current balance
|
||||
const [balRow] = await prisma.$queryRaw<[{ balance: bigint }]>`
|
||||
SELECT COALESCE(SUM(
|
||||
CASE WHEN type = 'CREDIT' THEN "amountCents" ELSE -"amountCents" END
|
||||
), 0)::bigint AS balance
|
||||
FROM "Transaction"
|
||||
WHERE "accountId" = ${accountId}
|
||||
`
|
||||
await prisma.account.update({
|
||||
where: { id: accountId },
|
||||
data: { currentBalanceCents: Number(balRow.balance) },
|
||||
})
|
||||
|
||||
// Upsert balance snapshots for each affected month
|
||||
const months = [
|
||||
...new Map(
|
||||
normalized.map((r) => {
|
||||
const y = r.date.getFullYear()
|
||||
const m = r.date.getMonth() + 1
|
||||
return [`${y}-${m}`, { year: y, month: m }]
|
||||
}),
|
||||
).values(),
|
||||
]
|
||||
for (const { year, month } of months) {
|
||||
const endOfMonth = new Date(year, month, 0, 23, 59, 59, 999)
|
||||
const [snap] = await prisma.$queryRaw<[{ balance: bigint }]>`
|
||||
SELECT COALESCE(SUM(
|
||||
CASE WHEN type = 'CREDIT' THEN "amountCents" ELSE -"amountCents" END
|
||||
), 0)::bigint AS balance
|
||||
FROM "Transaction"
|
||||
WHERE "accountId" = ${accountId}
|
||||
AND date <= ${endOfMonth}
|
||||
`
|
||||
await prisma.balanceSnapshot.upsert({
|
||||
where: { accountId_year_month: { accountId, year, month } },
|
||||
update: { balanceCents: Number(snap.balance), computedAt: new Date() },
|
||||
create: { accountId, year, month, balanceCents: Number(snap.balance) },
|
||||
})
|
||||
}
|
||||
|
||||
const status =
|
||||
importedCount === 0 ? 'FAILED'
|
||||
: skippedCount > 0 ? 'PARTIAL'
|
||||
: 'SUCCESS'
|
||||
|
||||
await prisma.csvUpload.update({
|
||||
where: { id: upload.id },
|
||||
data: { importedCount, skippedCount, status },
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
detected: detected?.name,
|
||||
importedCount,
|
||||
skippedCount,
|
||||
fileName: file.name,
|
||||
})
|
||||
} catch (err) {
|
||||
await prisma.csvUpload.update({
|
||||
where: { id: upload.id },
|
||||
data: {
|
||||
status: 'FAILED',
|
||||
errorMessage: err instanceof Error ? err.message : 'Unknown error',
|
||||
},
|
||||
})
|
||||
return NextResponse.json({ error: 'Import failed' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,8 @@ const geistMono = Geist_Mono({
|
||||
});
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Create Next App",
|
||||
description: "Generated by create next app",
|
||||
title: "Finance",
|
||||
description: "Personal finance tracker",
|
||||
};
|
||||
|
||||
export default function RootLayout({
|
||||
|
||||
@@ -1,65 +1,5 @@
|
||||
import Image from "next/image";
|
||||
import { redirect } from 'next/navigation'
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div className="flex flex-col flex-1 items-center justify-center bg-zinc-50 font-sans dark:bg-black">
|
||||
<main className="flex flex-1 w-full max-w-3xl flex-col items-center justify-between py-32 px-16 bg-white dark:bg-black sm:items-start">
|
||||
<Image
|
||||
className="dark:invert"
|
||||
src="/next.svg"
|
||||
alt="Next.js logo"
|
||||
width={100}
|
||||
height={20}
|
||||
priority
|
||||
/>
|
||||
<div className="flex flex-col items-center gap-6 text-center sm:items-start sm:text-left">
|
||||
<h1 className="max-w-xs text-3xl font-semibold leading-10 tracking-tight text-black dark:text-zinc-50">
|
||||
To get started, edit the page.tsx file.
|
||||
</h1>
|
||||
<p className="max-w-md text-lg leading-8 text-zinc-600 dark:text-zinc-400">
|
||||
Looking for a starting point or more instructions? Head over to{" "}
|
||||
<a
|
||||
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
className="font-medium text-zinc-950 dark:text-zinc-50"
|
||||
>
|
||||
Templates
|
||||
</a>{" "}
|
||||
or the{" "}
|
||||
<a
|
||||
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
className="font-medium text-zinc-950 dark:text-zinc-50"
|
||||
>
|
||||
Learning
|
||||
</a>{" "}
|
||||
center.
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex flex-col gap-4 text-base font-medium sm:flex-row">
|
||||
<a
|
||||
className="flex h-12 w-full items-center justify-center gap-2 rounded-full bg-foreground px-5 text-background transition-colors hover:bg-[#383838] dark:hover:bg-[#ccc] md:w-[158px]"
|
||||
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<Image
|
||||
className="dark:invert"
|
||||
src="/vercel.svg"
|
||||
alt="Vercel logomark"
|
||||
width={16}
|
||||
height={16}
|
||||
/>
|
||||
Deploy Now
|
||||
</a>
|
||||
<a
|
||||
className="flex h-12 w-full items-center justify-center rounded-full border border-solid border-black/[.08] px-5 transition-colors hover:border-transparent hover:bg-black/[.04] dark:border-white/[.145] dark:hover:bg-[#1a1a1a] md:w-[158px]"
|
||||
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Documentation
|
||||
</a>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
export default function RootPage() {
|
||||
redirect('/dashboard')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user