Files
fintrack/services/account.service.ts

74 lines
1.9 KiB
TypeScript

import { prisma } from "@/lib/prisma";
import type { Account } from "@/lib/types";
export const accountService = {
async create(data: Omit<Account, "id">): Promise<Account> {
const created = await prisma.account.create({
data: {
name: data.name,
bankId: data.bankId,
accountNumber: data.accountNumber,
type: data.type,
folderId: data.folderId,
balance: data.balance,
currency: data.currency,
lastImport: data.lastImport,
externalUrl: data.externalUrl,
},
});
return {
id: created.id,
name: created.name,
bankId: created.bankId,
accountNumber: created.accountNumber,
type: created.type as Account["type"],
folderId: created.folderId,
balance: created.balance,
currency: created.currency,
lastImport: created.lastImport,
externalUrl: created.externalUrl,
};
},
async update(
id: string,
data: Partial<Omit<Account, "id">>,
): Promise<Account> {
const updated = await prisma.account.update({
where: { id },
data: {
name: data.name,
bankId: data.bankId,
accountNumber: data.accountNumber,
type: data.type,
folderId: data.folderId,
balance: data.balance,
currency: data.currency,
lastImport: data.lastImport,
externalUrl: data.externalUrl,
},
});
return {
id: updated.id,
name: updated.name,
bankId: updated.bankId,
accountNumber: updated.accountNumber,
type: updated.type as Account["type"],
folderId: updated.folderId,
balance: updated.balance,
currency: updated.currency,
lastImport: updated.lastImport,
externalUrl: updated.externalUrl,
};
},
async delete(id: string): Promise<void> {
// Transactions will be deleted automatically due to onDelete: Cascade
await prisma.account.delete({
where: { id },
});
},
};