refactor: replace direct Prisma calls with service layer methods for banking operations

This commit is contained in:
Julien Froidefond
2025-11-27 10:07:03 +01:00
parent c6299de8b2
commit 8ffb65f596
10 changed files with 396 additions and 271 deletions

View File

@@ -1,24 +1,11 @@
import { NextResponse } from "next/server"
import { prisma } from "@/lib/prisma"
import { accountService } from "@/services/account.service"
import type { Account } from "@/lib/types"
export async function POST(request: Request) {
try {
const account: Omit<Account, "id"> = await request.json()
const created = await prisma.account.create({
data: {
name: account.name,
bankId: account.bankId,
accountNumber: account.accountNumber,
type: account.type,
folderId: account.folderId,
balance: account.balance,
currency: account.currency,
lastImport: account.lastImport,
},
})
const data: Omit<Account, "id"> = await request.json()
const created = await accountService.create(data)
return NextResponse.json(created)
} catch (error) {
console.error("Error creating account:", error)
@@ -29,21 +16,7 @@ export async function POST(request: Request) {
export async function PUT(request: Request) {
try {
const account: Account = await request.json()
const updated = await prisma.account.update({
where: { id: account.id },
data: {
name: account.name,
bankId: account.bankId,
accountNumber: account.accountNumber,
type: account.type,
folderId: account.folderId,
balance: account.balance,
currency: account.currency,
lastImport: account.lastImport,
},
})
const updated = await accountService.update(account.id, account)
return NextResponse.json(updated)
} catch (error) {
console.error("Error updating account:", error)
@@ -60,15 +33,10 @@ export async function DELETE(request: Request) {
return NextResponse.json({ error: "Account ID is required" }, { status: 400 })
}
// Transactions will be deleted automatically due to onDelete: Cascade
await prisma.account.delete({
where: { id },
})
await accountService.delete(id)
return NextResponse.json({ success: true })
} catch (error) {
console.error("Error deleting account:", error)
return NextResponse.json({ error: "Failed to delete account" }, { status: 500 })
}
}