Files
fintrack/app/api/banking/route.ts
Julien Froidefond e9e44916fd chore: init from v0
2025-11-27 09:51:18 +01:00

80 lines
2.2 KiB
TypeScript

import { NextResponse } from "next/server"
import { prisma } from "@/lib/prisma"
import type { BankingData } from "@/lib/types"
export async function GET() {
try {
const [accounts, transactions, folders, categories, categoryRules] = await Promise.all([
prisma.account.findMany({
include: {
folder: true,
},
}),
prisma.transaction.findMany({
include: {
account: true,
category: true,
},
}),
prisma.folder.findMany(),
prisma.category.findMany(),
prisma.categoryRule.findMany(),
])
// Transform Prisma models to match our types
const data: BankingData = {
accounts: accounts.map((a) => ({
id: a.id,
name: a.name,
bankId: a.bankId,
accountNumber: a.accountNumber,
type: a.type as "CHECKING" | "SAVINGS" | "CREDIT_CARD" | "OTHER",
folderId: a.folderId,
balance: a.balance,
currency: a.currency,
lastImport: a.lastImport,
})),
transactions: transactions.map((t) => ({
id: t.id,
accountId: t.accountId,
date: t.date,
amount: t.amount,
description: t.description,
type: t.type as "DEBIT" | "CREDIT",
categoryId: t.categoryId,
isReconciled: t.isReconciled,
fitId: t.fitId,
memo: t.memo ?? undefined,
checkNum: t.checkNum ?? undefined,
})),
folders: folders.map((f) => ({
id: f.id,
name: f.name,
parentId: f.parentId,
color: f.color,
icon: f.icon,
})),
categories: categories.map((c) => ({
id: c.id,
name: c.name,
color: c.color,
icon: c.icon,
keywords: JSON.parse(c.keywords) as string[],
parentId: c.parentId,
})),
categoryRules: categoryRules.map((r) => ({
id: r.id,
categoryId: r.categoryId,
pattern: r.pattern,
isRegex: r.isRegex,
})),
}
return NextResponse.json(data)
} catch (error) {
console.error("Error fetching banking data:", error)
return NextResponse.json({ error: "Failed to fetch data" }, { status: 500 })
}
}