docs(production): standardiserad produktionsberedskap och gap-analys

Lägger docs/production-readiness.md och siax/production-gaps.json enligt SIAX Master Definition of Done. Underlag: git-, kod- och live-evidens 2026-11-11. Nivån är evidensbaserad, inte ett leveransgodkännande.
This commit is contained in:
2026-11-10 09:15:21 +01:00
parent 0fda8cae48
commit 2bee8eb778
6135 changed files with 4 additions and 1718997 deletions
-138
View File
@@ -1,138 +0,0 @@
/**
* /api/dimensions/rules/[id] — mutate one account dimension rule (PR10).
*
* PATCH { rule_type?, value_id?, is_active? } — value presence is
* re-validated against the EFFECTIVE rule_type (required ⇔ no value).
* DELETE — removes the rule; enforcement stops immediately. Pausing without
* losing the configuration is is_active: false.
*/
import { NextResponse } from 'next/server'
import { ensureInitialized } from '@/lib/init'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateBody } from '@/lib/api/validate'
import { UpdateAccountDimensionRuleSchema } from '@/lib/api/schemas'
import { errorResponse } from '@/lib/errors/get-structured-error'
import { RULE_SELECT, toRuleDto, type RawRule } from '../dto'
ensureInitialized()
export const PATCH = withRouteContext<{ params: Promise<{ id: string }> }>(
'dimension.rules.update',
async (request, ctx, { params }) => {
const { id } = await params
const { supabase, companyId, log, requestId } = ctx
const validation = await validateBody(request, UpdateAccountDimensionRuleSchema)
if (!validation.success) return validation.response
const body = validation.data
const { data: existing, error: existingError } = await supabase
.from('account_dimension_rules')
.select('id, rule_type, value_id, dimension_id')
.eq('id', id)
.eq('company_id', companyId)
.maybeSingle()
if (existingError) return errorResponse(existingError, log, { requestId })
if (!existing) {
return NextResponse.json(
{ error: { code: 'DIMENSION_RULE_NOT_FOUND', message: 'Regeln finns inte.' } },
{ status: 404 },
)
}
const effectiveType = body.rule_type ?? (existing.rule_type as string)
const effectiveValueId =
body.value_id !== undefined ? body.value_id : (existing.value_id as string | null)
if (effectiveType === 'required' && effectiveValueId) {
return NextResponse.json(
{ error: { code: 'VALIDATION_FAILED', message: 'En obligatorisk regel har inget värde — ta bort värdet eller byt regeltyp.' } },
{ status: 400 },
)
}
if (effectiveType !== 'required' && !effectiveValueId) {
return NextResponse.json(
{ error: { code: 'VALIDATION_FAILED', message: 'Välj vilket värde regeln ska använda.' } },
{ status: 400 },
)
}
if (body.value_id) {
const { data: value, error: valueError } = await supabase
.from('dimension_values')
.select('id, is_active')
.eq('id', body.value_id)
.eq('company_id', companyId)
.eq('dimension_id', existing.dimension_id)
.maybeSingle()
if (valueError) return errorResponse(valueError, log, { requestId })
if (!value) {
return NextResponse.json(
{ error: { code: 'DIMENSION_VALUE_NOT_FOUND', message: 'Värdet finns inte under regelns dimension.' } },
{ status: 404 },
)
}
if (!value.is_active) {
return NextResponse.json(
{ error: { code: 'DIMENSION_VALUE_ARCHIVED', message: 'Värdet är arkiverat — återaktivera det innan det används i en regel.' } },
{ status: 400 },
)
}
}
const updates: Record<string, unknown> = {}
if (body.rule_type !== undefined) updates.rule_type = body.rule_type
if (body.value_id !== undefined) updates.value_id = body.value_id
if (body.is_active !== undefined) updates.is_active = body.is_active
if (Object.keys(updates).length === 0) {
return NextResponse.json(
{ error: { code: 'VALIDATION_FAILED', message: 'Ingen ändring angiven.' } },
{ status: 400 },
)
}
const { data: rule, error: updateError } = await supabase
.from('account_dimension_rules')
.update(updates)
.eq('id', id)
.eq('company_id', companyId)
.select(RULE_SELECT)
.single()
if (updateError) {
log.error('dimension rule update failed', updateError)
return errorResponse(updateError, log, { requestId })
}
return NextResponse.json({ data: { rule: toRuleDto(rule as unknown as RawRule) } })
},
{ requireWrite: true },
)
export const DELETE = withRouteContext<{ params: Promise<{ id: string }> }>(
'dimension.rules.delete',
async (_request, ctx, { params }) => {
const { id } = await params
const { supabase, companyId, log, requestId } = ctx
const { error, count } = await supabase
.from('account_dimension_rules')
.delete({ count: 'exact' })
.eq('id', id)
.eq('company_id', companyId)
if (error) {
log.error('dimension rule delete failed', error)
return errorResponse(error, log, { requestId })
}
if (!count) {
return NextResponse.json(
{ error: { code: 'DIMENSION_RULE_NOT_FOUND', message: 'Regeln finns inte.' } },
{ status: 404 },
)
}
return NextResponse.json({ data: { deleted: true } })
},
{ requireWrite: true },
)
-29
View File
@@ -1,29 +0,0 @@
/** Shared select + DTO mapper for the account_dimension_rules routes (PR10). */
export const RULE_SELECT =
'id, account_number, rule_type, value_id, is_active, dimension:dimensions!account_dimension_rules_dimension_id_company_id_fkey(id, sie_dim_no, name), value:dimension_values!account_dimension_rules_value_id_fkey(code, name)'
export interface RawRule {
id: string
account_number: string
rule_type: 'required' | 'default' | 'fixed'
value_id: string | null
is_active: boolean
dimension: { id: string; sie_dim_no: number; name: string }
value: { code: string; name: string } | null
}
export function toRuleDto(row: RawRule) {
return {
account_dimension_rule_id: row.id,
account_number: row.account_number,
dimension_id: row.dimension.id,
sie_dim_no: row.dimension.sie_dim_no,
dimension_name: row.dimension.name,
rule_type: row.rule_type,
value_id: row.value_id,
value_code: row.value?.code ?? null,
value_name: row.value?.name ?? null,
is_active: row.is_active,
}
}
-157
View File
@@ -1,157 +0,0 @@
/**
* /api/dimensions/rules — per-account dimension policy (dimensions PR10).
*
* GET ?account_number=4010 (optional) → every rule (or the account's).
* POST → create a rule. 'required' blocks posting on the account without a
* value for the dimension (enforced at commitEntry + the bulk-book route);
* 'default' pre-fills at draft creation; 'fixed' always applies.
*
* Opt-in by construction: zero rules = the engine behaves exactly as before.
* There is deliberately NO settings toggle for enforcement — a rule that
* exists but is ignored would be worse than either extreme; pausing a single
* rule is what is_active is for.
*/
import { NextResponse } from 'next/server'
import { ensureInitialized } from '@/lib/init'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateBody, validateQuery } from '@/lib/api/validate'
import { CreateAccountDimensionRuleSchema, ListDimensionRulesQuerySchema } from '@/lib/api/schemas'
import { errorResponse } from '@/lib/errors/get-structured-error'
import { RULE_SELECT, toRuleDto, type RawRule } from './dto'
ensureInitialized()
export const GET = withRouteContext(
'dimension.rules.list',
async (request, ctx) => {
const { supabase, companyId, log, requestId } = ctx
const queryValidation = validateQuery(request, ListDimensionRulesQuerySchema, {
log,
operation: 'dimension.rules.list',
})
if (!queryValidation.success) return queryValidation.response
const { account_number: accountNumber } = queryValidation.data
let query = supabase
.from('account_dimension_rules')
.select(RULE_SELECT)
.eq('company_id', companyId)
.order('account_number', { ascending: true })
if (accountNumber) {
query = query.eq('account_number', accountNumber)
}
const { data, error } = await query
if (error) {
log.error('dimension rule list failed', error)
return errorResponse(error, log, { requestId })
}
return NextResponse.json({
data: { rules: ((data ?? []) as unknown as RawRule[]).map(toRuleDto) },
})
},
)
export const POST = withRouteContext(
'dimension.rules.create',
async (request, ctx) => {
const { supabase, companyId, log, requestId } = ctx
const validation = await validateBody(request, CreateAccountDimensionRuleSchema)
if (!validation.success) return validation.response
const body = validation.data
// The dimension must belong to this company (RLS backstops; this gives a
// clean Swedish 400 instead of an FK error).
const { data: dimension, error: dimensionError } = await supabase
.from('dimensions')
.select('id, is_active')
.eq('id', body.dimension_id)
.eq('company_id', companyId)
.maybeSingle()
if (dimensionError) return errorResponse(dimensionError, log, { requestId })
if (!dimension) {
return NextResponse.json(
{ error: { code: 'DIMENSION_NOT_FOUND', message: 'Dimensionen finns inte i registret.' } },
{ status: 404 },
)
}
// default/fixed: the value must belong to the SAME dimension + company
// and be active — a rule pointing at a foreign or archived value would
// make every booking on the account fail registry validation.
if (body.value_id) {
const { data: value, error: valueError } = await supabase
.from('dimension_values')
.select('id, is_active')
.eq('id', body.value_id)
.eq('company_id', companyId)
.eq('dimension_id', body.dimension_id)
.maybeSingle()
if (valueError) return errorResponse(valueError, log, { requestId })
if (!value) {
return NextResponse.json(
{ error: { code: 'DIMENSION_VALUE_NOT_FOUND', message: 'Värdet finns inte under den valda dimensionen.' } },
{ status: 404 },
)
}
if (!value.is_active) {
return NextResponse.json(
{ error: { code: 'DIMENSION_VALUE_ARCHIVED', message: 'Värdet är arkiverat — återaktivera det innan det används i en regel.' } },
{ status: 400 },
)
}
}
// The account must exist and be active in the company chart — a rule on
// a nonexistent account can never fire and only confuses.
const { data: account, error: accountError } = await supabase
.from('chart_of_accounts')
.select('account_number')
.eq('company_id', companyId)
.eq('account_number', body.account_number)
.eq('is_active', true)
.maybeSingle()
if (accountError) return errorResponse(accountError, log, { requestId })
if (!account) {
return NextResponse.json(
{ error: { code: 'ACCOUNT_NOT_FOUND', message: `Konto ${body.account_number} finns inte som aktivt konto i kontoplanen.` } },
{ status: 404 },
)
}
const { data: rule, error: insertError } = await supabase
.from('account_dimension_rules')
.insert({
company_id: companyId,
account_number: body.account_number,
dimension_id: body.dimension_id,
rule_type: body.rule_type,
value_id: body.value_id ?? null,
is_active: body.is_active ?? true,
})
.select(RULE_SELECT)
.single()
if (insertError) {
if (insertError.code === '23505') {
return NextResponse.json(
{ error: { code: 'DIMENSION_RULE_EXISTS', message: `Konto ${body.account_number} har redan en regel för den dimensionen.` } },
{ status: 409 },
)
}
log.error('dimension rule create failed', insertError)
return errorResponse(insertError, log, { requestId })
}
return NextResponse.json(
{ data: { rule: toRuleDto(rule as unknown as RawRule) } },
{ status: 201 },
)
},
{ requireWrite: true },
)