Files
accounted/app/api/bookkeeping/mapping-rules/route.ts
T
Mattsson e11f70b347 Bug/gh issues fiz (#1103)
* refactor: optimize page loading and data fetching

* fix: resolve recurring production runtime errors

* feat: add MCP company and customer updates

* fix: handle year-end tax adjustments

* feat: harden annual report compliance

* fix: expand invoice logo and font support

* fix: sanitize API route error responses

* fix: sanitize user-facing error messages

* feat: persist onboarding and tax assessment notices

* fix: reduce cloud backup audit churn

* feat: refine invoice editor layout

* fix: show saved tax adjustments in INK2

* fix: complete annual report API mappings

* docs: record operational safeguards and decisions

* fix: harden annual report review findings

* fix: adjust column span for description based on VAT registration

* New css class name
2026-07-21 23:00:15 +02:00

70 lines
2.3 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateBody } from '@/lib/api/validate'
import { CreateMappingRuleSchema } from '@/lib/api/schemas'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
export const GET = withRouteContext('mapping_rules.list', async (_request, ctx) => {
const { supabase, companyId } = ctx
const { data, error } = await supabase
.from('mapping_rules')
.select('*')
.or(`company_id.eq.${companyId},company_id.is.null`)
.eq('is_active', true)
.order('priority')
if (error) {
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 500 })
}
return NextResponse.json({ data })
})
export const POST = withRouteContext(
'mapping_rules.create',
async (request, ctx) => {
const { supabase, companyId, user, log } = ctx
const result = await validateBody(request, CreateMappingRuleSchema, {
log,
operation: 'mapping_rules.create',
})
if (!result.success) return result.response
const body = result.data
const { data, error } = await supabase
.from('mapping_rules')
.insert({
user_id: user.id,
company_id: companyId,
rule_name: body.rule_name,
rule_type: body.rule_type,
// ?? not || — the schema allows 0 for priority and confidence_score,
// and || would silently coerce those to the defaults.
priority: body.priority ?? 10,
mcc_codes: body.mcc_codes ?? null,
merchant_pattern: body.merchant_pattern ?? null,
description_pattern: body.description_pattern ?? null,
amount_min: body.amount_min ?? null,
amount_max: body.amount_max ?? null,
debit_account: body.debit_account,
credit_account: body.credit_account,
vat_treatment: body.vat_treatment ?? null,
risk_level: body.risk_level ?? 'NONE',
default_private: body.default_private ?? false,
requires_review: body.requires_review ?? false,
confidence_score: body.confidence_score ?? 0.9,
})
.select()
.single()
if (error) {
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 500 })
}
return NextResponse.json({ data })
},
{ requireWrite: true },
)