84 lines
3.0 KiB
TypeScript
84 lines
3.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/db'
|
|
import { getSession } from '@/lib/auth'
|
|
|
|
// GET: List all icons with their tenant-specific active status
|
|
export async function GET() {
|
|
try {
|
|
const user = await getSession()
|
|
if (!user) return NextResponse.json({ error: 'Nicht autorisiert' }, { status: 401 })
|
|
if (user.role !== 'TENANT_ADMIN' && user.role !== 'SERVER_ADMIN') {
|
|
return NextResponse.json({ error: 'Keine Berechtigung' }, { status: 403 })
|
|
}
|
|
|
|
const tenantId = user.tenantId
|
|
if (!tenantId) return NextResponse.json({ error: 'Kein Mandant zugeordnet' }, { status: 400 })
|
|
|
|
// Get all system icons (active ones)
|
|
const icons = await (prisma as any).iconAsset.findMany({
|
|
where: { isActive: true },
|
|
include: { category: { select: { id: true, name: true } } },
|
|
orderBy: [{ category: { sortOrder: 'asc' } }, { name: 'asc' }],
|
|
})
|
|
|
|
// Get tenant-specific overrides
|
|
const overrides = await (prisma as any).tenantSymbol.findMany({
|
|
where: { tenantId },
|
|
})
|
|
|
|
const overrideMap = new Map(overrides.map((o: any) => [o.iconId, o.isActive]))
|
|
|
|
// Merge: default is active (true) unless override says otherwise
|
|
const symbols = icons.map((icon: any) => ({
|
|
id: icon.id,
|
|
name: icon.name,
|
|
fileKey: icon.fileKey,
|
|
mimeType: icon.mimeType,
|
|
iconType: icon.iconType,
|
|
categoryId: icon.categoryId,
|
|
categoryName: icon.category?.name || 'Ohne Kategorie',
|
|
isActive: overrideMap.has(icon.id) ? overrideMap.get(icon.id) : true,
|
|
}))
|
|
|
|
return NextResponse.json({ symbols })
|
|
} catch (error) {
|
|
console.error('Error fetching tenant symbols:', error)
|
|
return NextResponse.json({ error: 'Interner Fehler' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
// PATCH: Update symbol visibility for the tenant (bulk)
|
|
export async function PATCH(req: NextRequest) {
|
|
try {
|
|
const user = await getSession()
|
|
if (!user) return NextResponse.json({ error: 'Nicht autorisiert' }, { status: 401 })
|
|
if (user.role !== 'TENANT_ADMIN' && user.role !== 'SERVER_ADMIN') {
|
|
return NextResponse.json({ error: 'Keine Berechtigung' }, { status: 403 })
|
|
}
|
|
|
|
const tenantId = user.tenantId
|
|
if (!tenantId) return NextResponse.json({ error: 'Kein Mandant zugeordnet' }, { status: 400 })
|
|
|
|
const { updates } = await req.json()
|
|
if (!Array.isArray(updates)) {
|
|
return NextResponse.json({ error: 'updates Array erforderlich' }, { status: 400 })
|
|
}
|
|
|
|
// Upsert each symbol override
|
|
await Promise.all(
|
|
updates.map((u: { iconId: string; isActive: boolean }) =>
|
|
(prisma as any).tenantSymbol.upsert({
|
|
where: { tenantId_iconId: { tenantId, iconId: u.iconId } },
|
|
update: { isActive: u.isActive },
|
|
create: { tenantId, iconId: u.iconId, isActive: u.isActive },
|
|
})
|
|
)
|
|
)
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
console.error('Error updating tenant symbols:', error)
|
|
return NextResponse.json({ error: 'Interner Fehler' }, { status: 500 })
|
|
}
|
|
}
|