fix(categories): POST/PATCH/DELETE mit Fallback bei fehlendem Schema
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 11m44s

This commit is contained in:
Pepe Ziberi
2026-05-21 08:51:54 +02:00
parent 0d0d9a7257
commit 3722a04091

View File

@@ -79,13 +79,22 @@ export async function POST(req: NextRequest) {
}
// Check for duplicate name within tenant
try {
const existing = await (prisma as any).tenantCategory.findFirst({
where: { tenantId, name: { equals: name, mode: 'insensitive' } },
})
if (existing) {
return NextResponse.json({ error: 'Kategorie existiert bereits' }, { status: 409 })
}
} catch (dbErr: any) {
console.error('tenantCategory.findFirst failed:', dbErr)
if (dbErr?.message?.includes('tenant_categories') || dbErr?.code === 'P2021') {
return NextResponse.json({ error: 'Datenbank nicht bereit Migration läuft möglicherweise noch' }, { status: 503 })
}
throw dbErr
}
try {
const category = await (prisma as any).tenantCategory.create({
data: {
tenantId,
@@ -94,8 +103,14 @@ export async function POST(req: NextRequest) {
icon: icon || null,
},
})
return NextResponse.json({ category }, { status: 201 })
} catch (dbErr: any) {
console.error('tenantCategory.create failed:', dbErr)
if (dbErr?.message?.includes('tenant_categories') || dbErr?.code === 'P2021') {
return NextResponse.json({ error: 'Datenbank nicht bereit Migration läuft möglicherweise noch' }, { status: 503 })
}
throw dbErr
}
} catch (error) {
console.error('Error creating tenant category:', error)
return NextResponse.json({ error: 'Interner Fehler' }, { status: 500 })
@@ -126,6 +141,7 @@ export async function PATCH(req: NextRequest) {
// If renaming, check for duplicates
if (name) {
try {
const existing = await (prisma as any).tenantCategory.findFirst({
where: {
tenantId,
@@ -136,8 +152,16 @@ export async function PATCH(req: NextRequest) {
if (existing) {
return NextResponse.json({ error: 'Kategorie existiert bereits' }, { status: 409 })
}
} catch (dbErr: any) {
console.error('tenantCategory.findFirst failed:', dbErr)
if (dbErr?.message?.includes('tenant_categories') || dbErr?.code === 'P2021') {
return NextResponse.json({ error: 'Datenbank nicht bereit Migration läuft möglicherweise noch' }, { status: 503 })
}
throw dbErr
}
}
try {
const category = await (prisma as any).tenantCategory.updateMany({
where: { id, tenantId },
data,
@@ -146,6 +170,13 @@ export async function PATCH(req: NextRequest) {
if (category.count === 0) {
return NextResponse.json({ error: 'Kategorie nicht gefunden' }, { status: 404 })
}
} catch (dbErr: any) {
console.error('tenantCategory.updateMany failed:', dbErr)
if (dbErr?.message?.includes('tenant_categories') || dbErr?.code === 'P2021') {
return NextResponse.json({ error: 'Datenbank nicht bereit Migration läuft möglicherweise noch' }, { status: 503 })
}
throw dbErr
}
return NextResponse.json({ success: true })
} catch (error) {
@@ -172,6 +203,7 @@ export async function DELETE(req: NextRequest) {
if (!id) return NextResponse.json({ error: 'id erforderlich' }, { status: 400 })
// Check if category has symbols
try {
const symbolCount = await (prisma as any).tenantSymbol.count({
where: { categoryId: id, tenantId },
})
@@ -181,10 +213,26 @@ export async function DELETE(req: NextRequest) {
{ status: 409 }
)
}
} catch (dbErr: any) {
console.error('tenantSymbol.count failed:', dbErr)
if (dbErr?.message?.includes('tenant_symbols') || dbErr?.code === 'P2021') {
// Skip check if table doesn't exist
} else {
throw dbErr
}
}
try {
await (prisma as any).tenantCategory.deleteMany({
where: { id, tenantId },
})
} catch (dbErr: any) {
console.error('tenantCategory.deleteMany failed:', dbErr)
if (dbErr?.message?.includes('tenant_categories') || dbErr?.code === 'P2021') {
return NextResponse.json({ error: 'Datenbank nicht bereit Migration läuft möglicherweise noch' }, { status: 503 })
}
throw dbErr
}
return NextResponse.json({ success: true })
} catch (error) {