Files
biblical-guide.com/app/api/highlights/bulk/route.ts
2025-11-12 07:07:21 +00:00

74 lines
1.7 KiB
TypeScript

import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
import { getAuth } from '@clerk/nextjs/server'
export const runtime = 'nodejs'
export async function POST(request: Request) {
try {
const { userId } = await getAuth(request)
if (!userId) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const body = await request.json()
const { highlights } = body
if (!Array.isArray(highlights)) {
return NextResponse.json({ error: 'Invalid input' }, { status: 400 })
}
const synced = []
const errors = []
for (const item of highlights) {
try {
const existing = await prisma.userHighlight.findFirst({
where: {
userId,
verseId: item.verseId
}
})
if (existing) {
await prisma.userHighlight.update({
where: { id: existing.id },
data: {
color: item.color,
updatedAt: new Date()
}
})
} else {
await prisma.userHighlight.create({
data: {
userId,
verseId: item.verseId,
color: item.color,
createdAt: new Date(),
updatedAt: new Date()
}
})
}
synced.push(item.verseId)
} catch (e) {
errors.push({
verseId: item.verseId,
error: 'Failed to sync'
})
}
}
return NextResponse.json({
synced: synced.length,
errors,
serverTime: Date.now()
})
} catch (error) {
console.error('Error bulk syncing highlights:', error)
return NextResponse.json(
{ error: 'Failed to sync highlights' },
{ status: 500 }
)
}
}