81 lines
2.0 KiB
TypeScript
81 lines
2.0 KiB
TypeScript
import { prisma } from "@/lib/prisma";
|
|
import type {
|
|
BankingData,
|
|
Account,
|
|
Transaction,
|
|
Folder,
|
|
Category,
|
|
} from "@/lib/types";
|
|
|
|
export const bankingService = {
|
|
async getAllData(): Promise<BankingData> {
|
|
const [accounts, transactions, folders, categories] = await Promise.all([
|
|
prisma.account.findMany({
|
|
include: {
|
|
folder: true,
|
|
},
|
|
}),
|
|
prisma.transaction.findMany({
|
|
include: {
|
|
account: true,
|
|
category: true,
|
|
},
|
|
}),
|
|
prisma.folder.findMany(),
|
|
prisma.category.findMany(),
|
|
]);
|
|
|
|
// Transform Prisma models to match our types
|
|
return {
|
|
accounts: accounts.map(
|
|
(a): Account => ({
|
|
id: a.id,
|
|
name: a.name,
|
|
bankId: a.bankId,
|
|
accountNumber: a.accountNumber,
|
|
type: a.type as Account["type"],
|
|
folderId: a.folderId,
|
|
balance: a.balance,
|
|
currency: a.currency,
|
|
lastImport: a.lastImport,
|
|
externalUrl: a.externalUrl,
|
|
}),
|
|
),
|
|
transactions: transactions.map(
|
|
(t): Transaction => ({
|
|
id: t.id,
|
|
accountId: t.accountId,
|
|
date: t.date,
|
|
amount: t.amount,
|
|
description: t.description,
|
|
type: t.type as Transaction["type"],
|
|
categoryId: t.categoryId,
|
|
isReconciled: t.isReconciled,
|
|
fitId: t.fitId,
|
|
memo: t.memo ?? undefined,
|
|
checkNum: t.checkNum ?? undefined,
|
|
}),
|
|
),
|
|
folders: folders.map(
|
|
(f): Folder => ({
|
|
id: f.id,
|
|
name: f.name,
|
|
parentId: f.parentId,
|
|
color: f.color,
|
|
icon: f.icon,
|
|
}),
|
|
),
|
|
categories: categories.map(
|
|
(c): Category => ({
|
|
id: c.id,
|
|
name: c.name,
|
|
color: c.color,
|
|
icon: c.icon,
|
|
keywords: JSON.parse(c.keywords) as string[],
|
|
parentId: c.parentId,
|
|
}),
|
|
),
|
|
};
|
|
},
|
|
};
|