import { NextResponse } from 'next/server'; import { getBOLDetail } from '@/services/shipments'; import { getQuestSession, getActiveCompany } from '@/lib/permissions'; import { cachedQuery } from '@/lib/cache'; export const dynamic = 'force-dynamic'; export async function GET( _request: Request, { params }: { params: Promise<{ bol: string }> } ) { const session = await getQuestSession(); const activeCompany = await getActiveCompany(); if (!session || !activeCompany) { return NextResponse.json({ error: 'No active company' }, { status: 401 }); } const { bol } = await params; const bolNum = parseInt(bol, 10); if (isNaN(bolNum)) { return NextResponse.json({ error: 'Invalid BOL number' }, { status: 400 }); } const custId = activeCompany.epicor_cust_id; try { const data = await cachedQuery( { key: `bol:${custId}:${bolNum}`, ttlSeconds: 600 }, () => getBOLDetail(bolNum, custId) ); if (!data) { return NextResponse.json({ error: 'BOL not found' }, { status: 404 }); } return NextResponse.json({ data }); } catch (err) { const message = err instanceof Error ? err.message : String(err); let details = ''; // Extract original error from EpicorQueryError if (err && typeof err === 'object' && 'originalError' in err) { const originalError = err.originalError; details = originalError instanceof Error ? originalError.message : String(originalError); } console.error('BOL detail error:', err); return NextResponse.json( { error: message, details }, { status: 500 } ); } }