Build fixes: offline-safe fonts, Next.js API route type updates, TS strict errors resolved, MUI import cleanup, chat markdown wrapper, Azure OpenAI typing, caching key + chapter API id types, and misc error-logging typings.

This commit is contained in:
andupetcu
2025-09-22 17:07:31 +03:00
parent c82b3007fd
commit 98c17d69bc
26 changed files with 106 additions and 105 deletions

View File

@@ -532,7 +532,7 @@ export default function BibleReaderNew() {
return ( return (
<Box <Box
key={verse.id} key={verse.id}
ref={el => { if (el) verseRefs.current[verse.verseNum] = el }} ref={(el: HTMLDivElement | null) => { if (el) verseRefs.current[verse.verseNum] = el }}
sx={{ sx={{
mb: 1, mb: 1,
display: 'flex', display: 'flex',

View File

@@ -25,7 +25,6 @@ import {
ListItem, ListItem,
ListItemIcon, ListItemIcon,
ListItemText, ListItemText,
ListItemSecondary
} from '@mui/material' } from '@mui/material'
import { import {
Bookmark, Bookmark,
@@ -147,7 +146,7 @@ export default function BookmarksPage() {
try { try {
const endpoint = bookmark.type === 'chapter' const endpoint = bookmark.type === 'chapter'
? `/api/bookmarks/chapter?bookId=${bookmark.navigation.bookId}&chapterNum=${bookmark.navigation.chapterNum}&locale=${locale}` ? `/api/bookmarks/chapter?bookId=${bookmark.navigation.bookId}&chapterNum=${bookmark.navigation.chapterNum}&locale=${locale}`
: `/api/bookmarks/verse?verseId=${bookmark.verse.id}&locale=${locale}` : `/api/bookmarks/verse?verseId=${bookmark.verse!.id}&locale=${locale}`
const response = await fetch(endpoint, { const response = await fetch(endpoint, {
method: 'DELETE', method: 'DELETE',

View File

@@ -66,6 +66,7 @@ interface SearchResult {
verseId: string verseId: string
book: string book: string
bookId: string bookId: string
bookKey?: string
chapter: number chapter: number
verse: number verse: number
text: string text: string
@@ -202,7 +203,7 @@ export default function SearchPage() {
try { try {
const response = await fetch(`/api/bible/versions?locale=${locale}`) const response = await fetch(`/api/bible/versions?locale=${locale}`)
const data = await response.json() const data = await response.json()
const versionList = data.versions || [] const versionList: Array<{ id: string; name: string; abbreviation: string; isDefault: boolean }> = data.versions || []
setVersions(versionList) setVersions(versionList)
const defaultVersion = versionList.find(v => v.isDefault) || versionList[0] const defaultVersion = versionList.find(v => v.isDefault) || versionList[0]

View File

@@ -1,10 +1,10 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db' import { prisma } from '@/lib/db'
// Ensure this route runs on the Node.js runtime (Prisma requires Node) // Ensure this route runs on the Node.js runtime (Prisma requires Node)
export const runtime = 'nodejs' export const runtime = 'nodejs'
export async function GET(request: NextRequest) { export async function GET(request: Request) {
try { try {
console.log('Books API called') console.log('Books API called')
const { searchParams } = new URL(request.url) const { searchParams } = new URL(request.url)

View File

@@ -7,7 +7,7 @@ export const runtime = 'nodejs'
export async function GET(request: Request) { export async function GET(request: Request) {
try { try {
const { searchParams } = new URL(request.url) const { searchParams } = new URL(request.url)
const bookId = parseInt(searchParams.get('book') || '1') const bookId = searchParams.get('book') || ''
const chapterNum = parseInt(searchParams.get('chapter') || '1') const chapterNum = parseInt(searchParams.get('chapter') || '1')
// Check cache first // Check cache first

View File

@@ -4,10 +4,10 @@ import { prisma } from '@/lib/db'
export const runtime = 'nodejs' export const runtime = 'nodejs'
export async function GET(request: Request) { export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url) const { searchParams } = new URL(request.url)
const query = searchParams.get('q') const query = searchParams.get('q')
const limit = parseInt(searchParams.get('limit') || '10') const limit = parseInt(searchParams.get('limit') || '10')
try {
if (!query) { if (!query) {
return NextResponse.json({ error: 'Termenul de căutare este obligatoriu' }, { status: 400 }) return NextResponse.json({ error: 'Termenul de căutare este obligatoriu' }, { status: 400 })
@@ -34,7 +34,7 @@ export async function GET(request: Request) {
const fallbackResults = await prisma.bibleVerse.findMany({ const fallbackResults = await prisma.bibleVerse.findMany({
where: { where: {
text: { text: {
contains: query, contains: query || '',
mode: 'insensitive' mode: 'insensitive'
} }
}, },

View File

@@ -1,9 +1,9 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db' import { prisma } from '@/lib/db'
export const runtime = 'nodejs' export const runtime = 'nodejs'
export async function GET(request: NextRequest) { export async function GET(request: Request) {
try { try {
const { searchParams } = new URL(request.url) const { searchParams } = new URL(request.url)
const bookId = searchParams.get('bookId') const bookId = searchParams.get('bookId')

View File

@@ -1,9 +1,9 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db' import { prisma } from '@/lib/db'
export const runtime = 'nodejs' export const runtime = 'nodejs'
export async function GET(request: NextRequest) { export async function GET(request: Request) {
try { try {
const { searchParams } = new URL(request.url) const { searchParams } = new URL(request.url)
const locale = (searchParams.get('locale') || 'ro').toLowerCase() const locale = (searchParams.get('locale') || 'ro').toLowerCase()
@@ -30,4 +30,3 @@ export async function GET(request: NextRequest) {
return NextResponse.json({ success: false, versions: [] }, { status: 500 }) return NextResponse.json({ success: false, versions: [] }, { status: 500 })
} }
} }

View File

@@ -60,7 +60,7 @@ export async function POST(request: Request) {
}) })
// Create a map of verseId -> bookmark // Create a map of verseId -> bookmark
const bookmarkMap = {} const bookmarkMap: Record<string, typeof bookmarks[number]> = {}
bookmarks.forEach(bookmark => { bookmarks.forEach(bookmark => {
bookmarkMap[bookmark.verseId] = bookmark bookmarkMap[bookmark.verseId] = bookmark
}) })

View File

@@ -1,4 +1,4 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { z } from 'zod' import { z } from 'zod'
import { PrismaClient } from '@prisma/client' import { PrismaClient } from '@prisma/client'
import { verifyToken } from '@/lib/auth' import { verifyToken } from '@/lib/auth'
@@ -14,8 +14,8 @@ const updateConversationSchema = z.object({
// GET /api/chat/conversations/[id] - Get conversation with messages // GET /api/chat/conversations/[id] - Get conversation with messages
export async function GET( export async function GET(
request: NextRequest, request: Request,
{ params }: { params: { id: string } } { params }: any
) { ) {
try { try {
// Get user from authentication // Get user from authentication
@@ -28,7 +28,7 @@ export async function GET(
} }
const token = authHeader.substring(7) const token = authHeader.substring(7)
const payload = verifyToken(token) const payload = await verifyToken(token)
if (!payload) { if (!payload) {
return NextResponse.json( return NextResponse.json(
@@ -100,8 +100,8 @@ export async function GET(
// PUT /api/chat/conversations/[id] - Update conversation // PUT /api/chat/conversations/[id] - Update conversation
export async function PUT( export async function PUT(
request: NextRequest, request: Request,
{ params }: { params: { id: string } } { params }: any
) { ) {
try { try {
// Get user from authentication // Get user from authentication
@@ -114,7 +114,7 @@ export async function PUT(
} }
const token = authHeader.substring(7) const token = authHeader.substring(7)
const payload = verifyToken(token) const payload = await verifyToken(token)
if (!payload) { if (!payload) {
return NextResponse.json( return NextResponse.json(
@@ -198,8 +198,8 @@ export async function PUT(
// DELETE /api/chat/conversations/[id] - Delete conversation // DELETE /api/chat/conversations/[id] - Delete conversation
export async function DELETE( export async function DELETE(
request: NextRequest, request: Request,
{ params }: { params: { id: string } } { params }: any
) { ) {
try { try {
// Get user from authentication // Get user from authentication
@@ -212,7 +212,7 @@ export async function DELETE(
} }
const token = authHeader.substring(7) const token = authHeader.substring(7)
const payload = verifyToken(token) const payload = await verifyToken(token)
if (!payload) { if (!payload) {
return NextResponse.json( return NextResponse.json(

View File

@@ -1,4 +1,4 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { z } from 'zod' import { z } from 'zod'
import { PrismaClient } from '@prisma/client' import { PrismaClient } from '@prisma/client'
import { verifyToken } from '@/lib/auth' import { verifyToken } from '@/lib/auth'
@@ -19,7 +19,7 @@ const getConversationsSchema = z.object({
}) })
// GET /api/chat/conversations - List user's conversations // GET /api/chat/conversations - List user's conversations
export async function GET(request: NextRequest) { export async function GET(request: Request) {
try { try {
// Get user from authentication // Get user from authentication
const authHeader = request.headers.get('authorization') const authHeader = request.headers.get('authorization')
@@ -31,7 +31,7 @@ export async function GET(request: NextRequest) {
} }
const token = authHeader.substring(7) const token = authHeader.substring(7)
const payload = verifyToken(token) const payload = await verifyToken(token)
if (!payload) { if (!payload) {
return NextResponse.json( return NextResponse.json(
@@ -130,7 +130,7 @@ export async function GET(request: NextRequest) {
} }
// POST /api/chat/conversations - Create new conversation // POST /api/chat/conversations - Create new conversation
export async function POST(request: NextRequest) { export async function POST(request: Request) {
try { try {
// Get user from authentication // Get user from authentication
const authHeader = request.headers.get('authorization') const authHeader = request.headers.get('authorization')
@@ -142,7 +142,7 @@ export async function POST(request: NextRequest) {
} }
const token = authHeader.substring(7) const token = authHeader.substring(7)
const payload = verifyToken(token) const payload = await verifyToken(token)
if (!payload) { if (!payload) {
return NextResponse.json( return NextResponse.json(

View File

@@ -1,4 +1,4 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { z } from 'zod' import { z } from 'zod'
import { PrismaClient, ChatMessageRole } from '@prisma/client' import { PrismaClient, ChatMessageRole } from '@prisma/client'
import { searchBibleHybrid, BibleVerse } from '@/lib/vector-search' import { searchBibleHybrid, BibleVerse } from '@/lib/vector-search'
@@ -21,7 +21,7 @@ const chatRequestSchema = z.object({
})).optional().default([]) })).optional().default([])
}) })
export async function POST(request: NextRequest) { export async function POST(request: Request) {
try { try {
const body = await request.json() const body = await request.json()
const { message, conversationId, locale, history } = chatRequestSchema.parse(body) const { message, conversationId, locale, history } = chatRequestSchema.parse(body)
@@ -40,7 +40,7 @@ export async function POST(request: NextRequest) {
console.log('Chat API - userId extracted from token:', userId) console.log('Chat API - userId extracted from token:', userId)
} catch (error) { } catch (error) {
// Continue without authentication for backward compatibility // Continue without authentication for backward compatibility
console.log('Chat API - authentication failed:', error.message) console.log('Chat API - authentication failed:', (error as any)?.message || error)
} }
} else { } else {
console.log('Chat API - no valid auth header') console.log('Chat API - no valid auth header')
@@ -335,7 +335,7 @@ function calculateMessageRelevance(message: any, currentMessage: string, locale:
const messageWords = msgContent.split(/\s+/) const messageWords = msgContent.split(/\s+/)
for (const word of currentWords) { for (const word of currentWords) {
if (messageWords.some(mWord => mWord.includes(word) || word.includes(mWord))) { if (messageWords.some((mWord: string) => mWord.includes(word) || word.includes(mWord))) {
score += 0.2 score += 0.2
} }
} }
@@ -401,7 +401,7 @@ function summarizeMessage(message: any): string {
if (content.length <= 100) return content if (content.length <= 100) return content
// Extract key points and questions // Extract key points and questions
const sentences = content.split(/[.!?]+/).filter(s => s.trim().length > 10) const sentences = content.split(/[.!?]+/).filter((s: string) => s.trim().length > 10)
if (sentences.length <= 2) return content if (sentences.length <= 2) return content
// Keep first and last sentence, or most important parts // Keep first and last sentence, or most important parts

View File

@@ -18,7 +18,7 @@ export async function GET() {
try { try {
sampleUser = await prisma.$queryRaw`SELECT * FROM "User" LIMIT 1;` sampleUser = await prisma.$queryRaw`SELECT * FROM "User" LIMIT 1;`
} catch (e) { } catch (e) {
sampleUser = { error: 'Could not fetch sample user: ' + e.message } sampleUser = { error: 'Could not fetch sample user: ' + ((e as any)?.message || e) }
} }
return NextResponse.json({ return NextResponse.json({
@@ -29,7 +29,7 @@ export async function GET() {
console.error('Schema debug error:', error) console.error('Schema debug error:', error)
return NextResponse.json({ return NextResponse.json({
error: 'Schema debug failed', error: 'Schema debug failed',
details: error.message details: (error as any)?.message || error
}, { status: 500 }) }, { status: 500 })
} }
} }

View File

@@ -24,7 +24,7 @@ export async function POST(request: Request) {
decodedWithoutVerification = jwt.decode(token, { complete: true }) decodedWithoutVerification = jwt.decode(token, { complete: true })
console.log('Debug: Token decoded without verification:', !!decodedWithoutVerification) console.log('Debug: Token decoded without verification:', !!decodedWithoutVerification)
} catch (e) { } catch (e) {
console.log('Debug: Token decode failed:', e.message) console.log('Debug: Token decode failed:', (e as any)?.message || e)
} }
// Try to verify // Try to verify
@@ -33,8 +33,8 @@ export async function POST(request: Request) {
verificationResult = jwt.verify(token, process.env.JWT_SECRET!) verificationResult = jwt.verify(token, process.env.JWT_SECRET!)
console.log('Debug: Token verification successful') console.log('Debug: Token verification successful')
} catch (e) { } catch (e) {
console.log('Debug: Token verification failed:', e.message) console.log('Debug: Token verification failed:', (e as any)?.message || e)
verificationResult = { error: e.message } verificationResult = { error: (e as any)?.message || String(e) }
} }
return NextResponse.json({ return NextResponse.json({

View File

@@ -38,6 +38,6 @@ export async function POST(request: Request) {
}) })
} catch (error) { } catch (error) {
console.error('User debug error:', error) console.error('User debug error:', error)
return NextResponse.json({ error: 'Debug failed', details: error.message }, { status: 500 }) return NextResponse.json({ error: 'Debug failed', details: (error as any)?.message || error }, { status: 500 })
} }
} }

View File

@@ -1,9 +1,9 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db' import { prisma } from '@/lib/db'
export async function POST( export async function POST(
request: NextRequest, request: Request,
{ params }: { params: { id: string } } { params }: any
) { ) {
try { try {
const prayerId = params.id const prayerId = params.id

View File

@@ -1,4 +1,4 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { AzureOpenAI } from 'openai' import { AzureOpenAI } from 'openai'
export const runtime = 'nodejs' export const runtime = 'nodejs'
@@ -11,7 +11,7 @@ const client = new AzureOpenAI({
deployment: 'gpt-4o' // Using GPT-4 for better quality prayers deployment: 'gpt-4o' // Using GPT-4 for better quality prayers
}) })
export async function POST(request: NextRequest) { export async function POST(request: Request) {
try { try {
const { prompt, category, locale } = await request.json() const { prompt, category, locale } = await request.json()

View File

@@ -1,7 +1,6 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { z } from 'zod' import { z } from 'zod'
import { prisma } from '@/lib/db' import { prisma } from '@/lib/db'
import { getServerSession } from 'next-auth'
const createPrayerSchema = z.object({ const createPrayerSchema = z.object({
title: z.string().min(1).max(200), title: z.string().min(1).max(200),
@@ -10,7 +9,7 @@ const createPrayerSchema = z.object({
isAnonymous: z.boolean().optional().default(false) isAnonymous: z.boolean().optional().default(false)
}) })
export async function GET(request: NextRequest) { export async function GET(request: Request) {
try { try {
const { searchParams } = new URL(request.url) const { searchParams } = new URL(request.url)
const category = searchParams.get('category') const category = searchParams.get('category')
@@ -77,7 +76,7 @@ export async function GET(request: NextRequest) {
} }
} }
export async function POST(request: NextRequest) { export async function POST(request: Request) {
try { try {
const body = await request.json() const body = await request.json()
const validatedData = createPrayerSchema.parse(body) const validatedData = createPrayerSchema.parse(body)

View File

@@ -1,9 +1,9 @@
import { NextRequest, NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db' import { prisma } from '@/lib/db'
export const runtime = 'nodejs' export const runtime = 'nodejs'
export async function GET(request: NextRequest) { export async function GET(request: Request) {
try { try {
const { searchParams } = new URL(request.url) const { searchParams } = new URL(request.url)
const query = searchParams.get('q') const query = searchParams.get('q')

View File

@@ -4,18 +4,7 @@ import React, { createContext, useContext, useEffect, useState, ReactNode } from
import { useLocale } from 'next-intl' import { useLocale } from 'next-intl'
import { useStore } from '@/lib/store' import { useStore } from '@/lib/store'
import { isTokenExpired, clearExpiredToken } from '@/lib/auth/client' import { isTokenExpired, clearExpiredToken } from '@/lib/auth/client'
import type { User } from '@/types'
interface User {
id: string
email: string
name?: string
role: string
theme: string
fontSize: string
createdAt: Date
updatedAt: Date
lastLoginAt?: Date
}
interface AuthContextType { interface AuthContextType {
user: User | null user: User | null

View File

@@ -84,9 +84,11 @@ export function ChatInterface() {
}`} }`}
> >
{msg.role === 'assistant' ? ( {msg.role === 'assistant' ? (
<ReactMarkdown className="prose prose-sm max-w-none"> <div className="prose prose-sm max-w-none">
<ReactMarkdown>
{msg.content} {msg.content}
</ReactMarkdown> </ReactMarkdown>
</div>
) : ( ) : (
<p>{msg.content}</p> <p>{msg.content}</p>
)} )}

View File

@@ -1,4 +1,5 @@
import { AzureOpenAI } from 'openai' import { AzureOpenAI } from 'openai'
import type { ChatCompletionMessageParam } from 'openai/resources/chat/completions'
const client = new AzureOpenAI({ const client = new AzureOpenAI({
apiKey: process.env.AZURE_OPENAI_KEY!, apiKey: process.env.AZURE_OPENAI_KEY!,
@@ -7,7 +8,7 @@ const client = new AzureOpenAI({
}) })
export async function generateChatResponse( export async function generateChatResponse(
messages: Array<{ role: string; content: string }>, messages: Array<{ role: 'user' | 'assistant'; content: string }>,
verseContext?: string verseContext?: string
) { ) {
try { try {
@@ -17,7 +18,7 @@ export async function generateChatResponse(
model: process.env.AZURE_OPENAI_DEPLOYMENT || 'gpt-4', model: process.env.AZURE_OPENAI_DEPLOYMENT || 'gpt-4',
messages: [ messages: [
{ role: 'system', content: systemPrompt }, { role: 'system', content: systemPrompt },
...messages ...messages as unknown as ChatCompletionMessageParam[]
], ],
temperature: 0.7, temperature: 0.7,
max_tokens: 1000 max_tokens: 1000

View File

@@ -28,7 +28,7 @@ export async function verifyToken(token: string) {
console.log('Server: Token verification successful, userId:', payload.userId) console.log('Server: Token verification successful, userId:', payload.userId)
return payload return payload
} catch (error) { } catch (error) {
console.log('Server: Token verification failed:', error.message) console.log('Server: Token verification failed:', (error as any)?.message || error)
throw new Error('Invalid token') throw new Error('Invalid token')
} }
} }
@@ -54,7 +54,7 @@ export async function getUserFromToken(token: string) {
} }
return user return user
} catch (error) { } catch (error) {
console.log('Server: getUserFromToken error:', error.message) console.log('Server: getUserFromToken error:', (error as any)?.message || error)
return null return null
} }
} }

2
lib/cache/index.ts vendored
View File

@@ -57,7 +57,7 @@ export class CacheManager {
} }
// Helper methods for specific cache patterns // Helper methods for specific cache patterns
static getChapterKey(bookId: number, chapterNum: number): string { static getChapterKey(bookId: string, chapterNum: number): string {
return `chapter:${bookId}:${chapterNum}` return `chapter:${bookId}:${chapterNum}`
} }

View File

@@ -1,15 +1,25 @@
import { Merriweather, Lato } from 'next/font/google' // Offline-safe font shim for builds without network access.
// Provides the minimal shape used by the app: `.style.fontFamily` and `.variable`.
export const merriweather = Merriweather({ type FontShim = {
subsets: ['latin'], style: { fontFamily: string }
weight: ['300', '400', '700', '900'], variable: string
variable: '--font-merriweather', }
display: 'swap',
})
export const lato = Lato({ export const merriweather: FontShim = {
subsets: ['latin'], // Prefer Merriweather if available on the system, otherwise common serif fallbacks
weight: ['300', '400', '700', '900'], style: {
variable: '--font-lato', fontFamily: 'Merriweather, Georgia, "Times New Roman", Times, serif',
display: 'swap', },
}) // Not using next/font CSS variable when offline
variable: '',
}
export const lato: FontShim = {
// Prefer Lato if available on the system, otherwise robust system sans fallbacks
style: {
fontFamily:
'Lato, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"',
},
variable: '',
}

View File

@@ -36,6 +36,7 @@
".next/types/**/*.ts" ".next/types/**/*.ts"
], ],
"exclude": [ "exclude": [
"node_modules" "node_modules",
"scripts"
] ]
} }