fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s

Å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.
This commit is contained in:
2026-11-10 09:37:22 +01:00
parent 077251ac94
commit e7f0070e84
6135 changed files with 1718997 additions and 4 deletions
@@ -0,0 +1,56 @@
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 }
)
}
})
@@ -0,0 +1,69 @@
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 },
)