Files
accounted/app/api/bookkeeping/mapping-rules/evaluate/route.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

57 lines
1.9 KiB
TypeScript

import { NextResponse } from 'next/server'
import { resolveCompanyEntityType } from '@/lib/company/entity-type'
import { withRouteContext } from '@/lib/api/with-route-context'
import { evaluateMappingRules } from '@/lib/bookkeeping/mapping-engine'
import { validateBody } from '@/lib/api/validate'
import { EvaluateMappingRulesSchema } from '@/lib/api/schemas'
import type { Transaction } from '@/types'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
export const POST = withRouteContext('mapping_rules.evaluate', async (request, ctx) => {
const { supabase, companyId, log } = ctx
const validation = await validateBody(request, EvaluateMappingRulesSchema, {
log,
operation: 'mapping_rules.evaluate',
})
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('company_id', companyId)
.single()
if (error || !data) {
return NextResponse.json({ error: 'Transaction not found' }, { status: 404 })
}
transaction = data as Transaction
} else {
// Schema-validated (amount required, passthrough for optional signal
// fields) — the mapping engine only reads the fields it knows.
transaction = body as unknown as Transaction
}
try {
const result = await evaluateMappingRules(
supabase,
companyId,
transaction,
await resolveCompanyEntityType(supabase, companyId),
)
return NextResponse.json({ data: result })
} catch (err) {
return NextResponse.json(
{ error: err instanceof Error ? getUserErrorMessage(err) : 'Evaluation failed' },
{ status: 500 }
)
}
})