* 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
104 lines
3.3 KiB
TypeScript
104 lines
3.3 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { validateBody, validateQuery } from '@/lib/api/validate'
|
|
import { CreateAccountSchema } from '@/lib/api/schemas'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
// Response shapes are legacy `{ data }` / `{ error: string }` — several pages
|
|
// (import, supplier-invoices, article form) consume the list directly.
|
|
|
|
const ListQuerySchema = z.object({
|
|
class: z.coerce.number().int().min(1).max(8).optional(),
|
|
active: z.enum(['true', 'false']).optional(),
|
|
})
|
|
|
|
export const GET = withRouteContext('bookkeeping.accounts.list', async (request, ctx) => {
|
|
const { supabase, companyId, log } = ctx
|
|
|
|
const validated = validateQuery(request, ListQuerySchema, {
|
|
log,
|
|
operation: 'bookkeeping.accounts.list',
|
|
})
|
|
if (!validated.success) return validated.response
|
|
const accountClass = validated.data.class
|
|
const activeOnly = validated.data.active !== 'false'
|
|
|
|
try {
|
|
const data = await fetchAllRows(({ from, to }) => {
|
|
let query = supabase
|
|
.from('chart_of_accounts')
|
|
.select('*')
|
|
.eq('company_id', companyId)
|
|
.order('sort_order')
|
|
|
|
if (activeOnly) {
|
|
query = query.eq('is_active', true)
|
|
}
|
|
|
|
if (accountClass !== undefined) {
|
|
query = query.eq('account_class', accountClass)
|
|
}
|
|
|
|
return query.range(from, to)
|
|
})
|
|
|
|
return NextResponse.json({ data })
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ error: error instanceof Error ? getUserErrorMessage(error) : 'Failed to fetch accounts' },
|
|
{ status: 500 },
|
|
)
|
|
}
|
|
})
|
|
|
|
export const POST = withRouteContext(
|
|
'bookkeeping.accounts.create',
|
|
async (request, ctx) => {
|
|
const { supabase, companyId, user, log } = ctx
|
|
|
|
const validation = await validateBody(request, CreateAccountSchema, {
|
|
log,
|
|
operation: 'bookkeeping.accounts.create',
|
|
})
|
|
if (!validation.success) return validation.response
|
|
const body = validation.data
|
|
|
|
const { data, error } = await supabase
|
|
.from('chart_of_accounts')
|
|
.insert({
|
|
user_id: user.id,
|
|
company_id: companyId,
|
|
account_number: body.account_number,
|
|
account_name: body.account_name,
|
|
account_class: parseInt(body.account_number[0]),
|
|
account_group: body.account_number.substring(0, 2),
|
|
account_type: body.account_type,
|
|
normal_balance: body.normal_balance,
|
|
plan_type: body.plan_type || 'k1',
|
|
is_system_account: false,
|
|
description: body.description || null,
|
|
default_vat_code: body.default_vat_code || null,
|
|
default_vat_rate: body.default_vat_rate ?? null,
|
|
sru_code: body.sru_code || null,
|
|
sort_order: parseInt(body.account_number),
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
if (error.code === '23505') {
|
|
return NextResponse.json(
|
|
{ error: `Kontonummer ${body.account_number} finns redan i din kontoplan.` },
|
|
{ status: 409 },
|
|
)
|
|
}
|
|
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
},
|
|
{ requireWrite: true },
|
|
)
|