refactor: replace direct Prisma calls with service layer methods for banking operations
This commit is contained in:
67
services/account.service.ts
Normal file
67
services/account.service.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
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,
|
||||
},
|
||||
})
|
||||
|
||||
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,
|
||||
}
|
||||
},
|
||||
|
||||
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,
|
||||
},
|
||||
})
|
||||
|
||||
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,
|
||||
}
|
||||
},
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
// Transactions will be deleted automatically due to onDelete: Cascade
|
||||
await prisma.account.delete({
|
||||
where: { id },
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user