Files
Momento/memento-note/app/api/notes/reindex/route.ts
Antigravity e881004c77
Some checks failed
CI / Lint, Test & Build (push) Failing after 1m7s
CI / Deploy production (on server) (push) Has been skipped
feat(insights): fix DBSCAN, Persian embeddings crash, D3 physics layouts, and D3 node not found runtime error
2026-05-24 18:57:33 +00:00

50 lines
1.3 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/auth'
import { prisma } from '@/lib/prisma'
import { semanticSearchService } from '@/lib/ai/services/semantic-search.service'
export async function POST(req: NextRequest) {
try {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 })
}
const userId = session.user.id
const notes = await prisma.note.findMany({
where: { userId, trashedAt: null },
select: { id: true }
})
let processedCount = 0
let failedCount = 0
const BATCH_SIZE = 20
for (let i = 0; i < notes.length; i += BATCH_SIZE) {
const batch = notes.slice(i, i + BATCH_SIZE)
const results = await Promise.allSettled(
batch.map(note => semanticSearchService.indexNote(note.id, { force: true }))
)
for (const r of results) {
if (r.status === 'fulfilled') processedCount++
else failedCount++
}
}
return NextResponse.json({
success: true,
count: processedCount,
failed: failedCount,
total: notes.length
})
} catch (error) {
console.error('Reindex error:', error)
return NextResponse.json(
{ success: false, error: 'Failed to reindex notes' },
{ status: 500 }
)
}
}