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
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 11m44s
This commit is contained in:
@@ -79,13 +79,22 @@ export async function POST(req: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for duplicate name within tenant
|
// Check for duplicate name within tenant
|
||||||
|
try {
|
||||||
const existing = await (prisma as any).tenantCategory.findFirst({
|
const existing = await (prisma as any).tenantCategory.findFirst({
|
||||||
where: { tenantId, name: { equals: name, mode: 'insensitive' } },
|
where: { tenantId, name: { equals: name, mode: 'insensitive' } },
|
||||||
})
|
})
|
||||||
if (existing) {
|
if (existing) {
|
||||||
return NextResponse.json({ error: 'Kategorie existiert bereits' }, { status: 409 })
|
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({
|
const category = await (prisma as any).tenantCategory.create({
|
||||||
data: {
|
data: {
|
||||||
tenantId,
|
tenantId,
|
||||||
@@ -94,8 +103,14 @@ export async function POST(req: NextRequest) {
|
|||||||
icon: icon || null,
|
icon: icon || null,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return NextResponse.json({ category }, { status: 201 })
|
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) {
|
} catch (error) {
|
||||||
console.error('Error creating tenant category:', error)
|
console.error('Error creating tenant category:', error)
|
||||||
return NextResponse.json({ error: 'Interner Fehler' }, { status: 500 })
|
return NextResponse.json({ error: 'Interner Fehler' }, { status: 500 })
|
||||||
@@ -126,6 +141,7 @@ export async function PATCH(req: NextRequest) {
|
|||||||
|
|
||||||
// If renaming, check for duplicates
|
// If renaming, check for duplicates
|
||||||
if (name) {
|
if (name) {
|
||||||
|
try {
|
||||||
const existing = await (prisma as any).tenantCategory.findFirst({
|
const existing = await (prisma as any).tenantCategory.findFirst({
|
||||||
where: {
|
where: {
|
||||||
tenantId,
|
tenantId,
|
||||||
@@ -136,8 +152,16 @@ export async function PATCH(req: NextRequest) {
|
|||||||
if (existing) {
|
if (existing) {
|
||||||
return NextResponse.json({ error: 'Kategorie existiert bereits' }, { status: 409 })
|
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({
|
const category = await (prisma as any).tenantCategory.updateMany({
|
||||||
where: { id, tenantId },
|
where: { id, tenantId },
|
||||||
data,
|
data,
|
||||||
@@ -146,6 +170,13 @@ export async function PATCH(req: NextRequest) {
|
|||||||
if (category.count === 0) {
|
if (category.count === 0) {
|
||||||
return NextResponse.json({ error: 'Kategorie nicht gefunden' }, { status: 404 })
|
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 })
|
return NextResponse.json({ success: true })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -172,6 +203,7 @@ export async function DELETE(req: NextRequest) {
|
|||||||
if (!id) return NextResponse.json({ error: 'id erforderlich' }, { status: 400 })
|
if (!id) return NextResponse.json({ error: 'id erforderlich' }, { status: 400 })
|
||||||
|
|
||||||
// Check if category has symbols
|
// Check if category has symbols
|
||||||
|
try {
|
||||||
const symbolCount = await (prisma as any).tenantSymbol.count({
|
const symbolCount = await (prisma as any).tenantSymbol.count({
|
||||||
where: { categoryId: id, tenantId },
|
where: { categoryId: id, tenantId },
|
||||||
})
|
})
|
||||||
@@ -181,10 +213,26 @@ export async function DELETE(req: NextRequest) {
|
|||||||
{ status: 409 }
|
{ 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({
|
await (prisma as any).tenantCategory.deleteMany({
|
||||||
where: { id, tenantId },
|
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 })
|
return NextResponse.json({ success: true })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user