- Add 8 new Zod schemas (UpdateCustomer, UpdateSupplier, UpdateSupplierInvoice, UpdateAccount, BankUnlink, RunReconciliation, CorrectJournalEntry, EvaluateMappingRules) and wire validateBody() into 24 JSON-body API routes - Remove redundant manual validation checks replaced by Zod - Add comprehensive schema tests (222 tests) - Improve type definitions in types/index.ts with expanded interfaces - Refactor extension types (push-notifications, receipt-ocr) for cleaner imports - Update transaction components (BatchCategorySelector, SwipeCategorizationView, QuickReviewDialog, VatTreatmentSelect) and invoice inbox workspace - Add invoice-inbox utilities and type decoupling tests - Fix NE-bilaga, SRU export, and invoice PDF template type usage - Update CLAUDE.md with expanded architecture documentation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { evaluateMappingRules } from '@/lib/bookkeeping/mapping-engine'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { EvaluateMappingRulesSchema } from '@/lib/api/schemas'
|
|
import type { Transaction } from '@/types'
|
|
|
|
export async function POST(request: Request) {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const validation = await validateBody(request, EvaluateMappingRulesSchema)
|
|
if (!validation.success) return validation.response
|
|
const body = validation.data
|
|
|
|
// Accept either a transaction ID or raw transaction data
|
|
let transaction: Transaction
|
|
|
|
if ('transaction_id' in body) {
|
|
const { data, error } = await supabase
|
|
.from('transactions')
|
|
.select('*')
|
|
.eq('id', body.transaction_id)
|
|
.eq('user_id', user.id)
|
|
.single()
|
|
|
|
if (error || !data) {
|
|
return NextResponse.json({ error: 'Transaction not found' }, { status: 404 })
|
|
}
|
|
|
|
transaction = data as Transaction
|
|
} else {
|
|
transaction = body as unknown as Transaction
|
|
}
|
|
|
|
try {
|
|
const result = await evaluateMappingRules(user.id, transaction)
|
|
return NextResponse.json({ data: result })
|
|
} catch (err) {
|
|
return NextResponse.json(
|
|
{ error: err instanceof Error ? err.message : 'Evaluation failed' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|