diff --git a/src/app/api/tenant/categories/route.ts b/src/app/api/tenant/categories/route.ts index dc6d006..0e7b06b 100644 --- a/src/app/api/tenant/categories/route.ts +++ b/src/app/api/tenant/categories/route.ts @@ -79,23 +79,38 @@ export async function POST(req: NextRequest) { } // Check for duplicate name within tenant - 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 }) + 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 } - const category = await (prisma as any).tenantCategory.create({ - data: { - tenantId, - name: name.trim(), - sortOrder: sortOrder ?? 0, - icon: icon || null, - }, - }) - - return NextResponse.json({ category }, { status: 201 }) + try { + const category = await (prisma as any).tenantCategory.create({ + data: { + tenantId, + name: name.trim(), + sortOrder: sortOrder ?? 0, + 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,25 +141,41 @@ export async function PATCH(req: NextRequest) { // If renaming, check for duplicates if (name) { - const existing = await (prisma as any).tenantCategory.findFirst({ - where: { - tenantId, - name: { equals: name.trim(), mode: 'insensitive' }, - id: { not: id }, - }, - }) - if (existing) { - return NextResponse.json({ error: 'Kategorie existiert bereits' }, { status: 409 }) + try { + const existing = await (prisma as any).tenantCategory.findFirst({ + where: { + tenantId, + name: { equals: name.trim(), mode: 'insensitive' }, + id: { not: id }, + }, + }) + 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 } } - const category = await (prisma as any).tenantCategory.updateMany({ - where: { id, tenantId }, - data, - }) + try { + const category = await (prisma as any).tenantCategory.updateMany({ + where: { id, tenantId }, + data, + }) - if (category.count === 0) { - return NextResponse.json({ error: 'Kategorie nicht gefunden' }, { status: 404 }) + 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 }) @@ -172,19 +203,36 @@ export async function DELETE(req: NextRequest) { if (!id) return NextResponse.json({ error: 'id erforderlich' }, { status: 400 }) // Check if category has symbols - const symbolCount = await (prisma as any).tenantSymbol.count({ - where: { categoryId: id, tenantId }, - }) - if (symbolCount > 0) { - return NextResponse.json( - { error: `Kategorie enthält ${symbolCount} Symbol(e) — bitte zuerst verschieben oder löschen` }, - { status: 409 } - ) + try { + const symbolCount = await (prisma as any).tenantSymbol.count({ + where: { categoryId: id, tenantId }, + }) + if (symbolCount > 0) { + return NextResponse.json( + { error: `Kategorie enthält ${symbolCount} Symbol(e) — bitte zuerst verschieben oder löschen` }, + { 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 + } } - await (prisma as any).tenantCategory.deleteMany({ - where: { id, tenantId }, - }) + 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) {