79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { requireAuth } from '@/lib/auth'
|
|
import { indexEntity, reindexAll } from '@/lib/search'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const user = await requireAuth()
|
|
const body = await request.json()
|
|
const { action, entityType, entityId, title, content, metadata } = body
|
|
|
|
// Only allow admins or content owners to index entities
|
|
if (user.role !== 'admin') {
|
|
return NextResponse.json(
|
|
{ error: 'Admin access required' },
|
|
{ status: 403 }
|
|
)
|
|
}
|
|
|
|
if (action === 'reindex-all') {
|
|
// Reindex all entities - admin only operation
|
|
await reindexAll()
|
|
return NextResponse.json(
|
|
{ message: 'Search index rebuilt successfully' },
|
|
{ status: 200 }
|
|
)
|
|
}
|
|
|
|
if (action === 'index-entity') {
|
|
// Index a single entity
|
|
if (!entityType || !entityId || !title) {
|
|
return NextResponse.json(
|
|
{ error: 'entityType, entityId, and title are required' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const validTypes = ['song', 'album', 'artist', 'playlist']
|
|
if (!validTypes.includes(entityType)) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid entityType. Must be one of: song, album, artist, playlist' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const indexedEntity = await indexEntity(
|
|
entityType,
|
|
entityId,
|
|
title,
|
|
content,
|
|
metadata
|
|
)
|
|
|
|
return NextResponse.json(
|
|
{
|
|
message: 'Entity indexed successfully',
|
|
entity: indexedEntity,
|
|
},
|
|
{ status: 201 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json(
|
|
{ error: 'Invalid action. Must be "reindex-all" or "index-entity"' },
|
|
{ status: 400 }
|
|
)
|
|
} catch (error) {
|
|
console.error('Search index error:', error)
|
|
if (error instanceof Error && error.message === 'Unauthorized') {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
return NextResponse.json(
|
|
{ error: 'Failed to update search index' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
} |