Fix Edge-incompatible middleware; set Node runtime on Prisma/pg routes; add full Romanian Bible import + converter; import data JSON; resync RO bookKeys; stabilize /api/bible/books locale fallback; restart dev server.

This commit is contained in:
andupetcu
2025-09-20 18:01:04 +03:00
parent 500066450d
commit 88b251c100
28 changed files with 127926 additions and 175 deletions

View File

@@ -1,9 +1,64 @@
import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
// Ensure this route runs on the Node.js runtime (Prisma requires Node)
export const runtime = 'nodejs'
export async function GET(request: NextRequest) {
try {
console.log('Books API called')
const { searchParams } = new URL(request.url)
const locale = searchParams.get('locale') || 'ro'
const versionAbbr = searchParams.get('version') // Optional specific version
console.log('Locale:', locale, 'Version:', versionAbbr)
// Get the appropriate Bible version
let bibleVersion
const langCandidates = Array.from(new Set([locale, locale.toLowerCase(), locale.toUpperCase()]))
if (versionAbbr) {
// Use specific version if provided
bibleVersion = await prisma.bibleVersion.findFirst({
where: {
abbreviation: versionAbbr,
language: { in: langCandidates }
}
})
} else {
// Try default version for the language
bibleVersion = await prisma.bibleVersion.findFirst({
where: {
language: { in: langCandidates },
isDefault: true
}
})
// Fallback: any version for the language
if (!bibleVersion) {
bibleVersion = await prisma.bibleVersion.findFirst({
where: { language: { in: langCandidates } },
orderBy: { createdAt: 'asc' }
})
}
// Fallback: use English default if requested locale not found
if (!bibleVersion && locale !== 'en') {
bibleVersion = await prisma.bibleVersion.findFirst({
where: { language: 'en', isDefault: true }
})
}
}
if (!bibleVersion) {
return NextResponse.json({
success: false,
error: `No Bible version found for language: ${locale}`,
books: []
}, { status: 404 })
}
// Get books for this version
const books = await prisma.bibleBook.findMany({
where: {
versionId: bibleVersion.id
},
orderBy: {
orderNum: 'asc'
},
@@ -11,6 +66,13 @@ export async function GET(request: NextRequest) {
chapters: {
orderBy: {
chapterNum: 'asc'
},
include: {
_count: {
select: {
verses: true
}
}
}
}
}
@@ -18,7 +80,24 @@ export async function GET(request: NextRequest) {
return NextResponse.json({
success: true,
books: books
version: {
id: bibleVersion.id,
name: bibleVersion.name,
abbreviation: bibleVersion.abbreviation,
language: bibleVersion.language
},
books: books.map(book => ({
id: book.id,
name: book.name,
testament: book.testament,
orderNum: book.orderNum,
bookKey: book.bookKey,
chapters: book.chapters.map(chapter => ({
id: chapter.id,
chapterNum: chapter.chapterNum,
verseCount: chapter._count.verses
}))
}))
})
} catch (error) {
console.error('Error fetching books:', error)
@@ -31,4 +110,4 @@ export async function GET(request: NextRequest) {
{ status: 500 }
)
}
}
}