Files
fintrack/services/account.service.ts

85 lines
2.3 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,
initialBalance: data.initialBalance ?? 0,
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,
initialBalance: created.initialBalance,
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,
initialBalance: data.initialBalance,
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,
initialBalance: updated.initialBalance,
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 },
});
},
async deleteMany(ids: string[]): Promise<void> {
// Transactions will be deleted automatically due to onDelete: Cascade
await prisma.account.deleteMany({
where: { id: { in: ids } },
});
},
};