import { NextResponse } from 'next/server'; import { withAuth, withPermission } from '@/lib/api/helpers'; import { errorResponse } from '@/lib/errors'; import { logger } from '@/lib/logger'; import { getResolvedOcrConfig } from '@/lib/services/ocr-config.service'; import { runOcr, type ParsedReceipt } from '@/lib/services/ocr-providers'; const EMPTY: ParsedReceipt = { establishment: null, date: null, amount: null, currency: null, lineItems: [], confidence: 0, }; export const POST = withAuth( withPermission('expenses', 'create', async (req, ctx) => { try { const formData = await req.formData(); const file = formData.get('file') as File | null; if (!file) { return NextResponse.json({ error: 'No file provided' }, { status: 400 }); } const buffer = Buffer.from(await file.arrayBuffer()); const mimeType = file.type || 'image/jpeg'; const config = await getResolvedOcrConfig(ctx.portId); if (!config.apiKey) { // Manual-entry path — no OCR configured. Frontend will show the // verify form with empty fields so the user can fill it in. return NextResponse.json({ data: { parsed: EMPTY, source: 'manual', reason: 'no-ocr-configured' }, }); } try { const parsed = await runOcr({ provider: config.provider, model: config.model, apiKey: config.apiKey, imageBuffer: buffer, mimeType, }); return NextResponse.json({ data: { parsed, source: 'ai', provider: config.provider, model: config.model }, }); } catch (err) { logger.error({ err, provider: config.provider }, 'OCR provider call failed'); // Provider hiccup — degrade to manual entry rather than 500-ing. return NextResponse.json({ data: { parsed: EMPTY, source: 'manual', reason: 'provider-error', providerError: err instanceof Error ? err.message.slice(0, 200) : 'Unknown error', }, }); } } catch (error) { return errorResponse(error); } }), );