Files
accounted/app/api/bookkeeping/accounts/reference/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

40 lines
1.8 KiB
TypeScript

import { NextResponse } from 'next/server'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
import { withRouteContext } from '@/lib/api/with-route-context'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
/**
* GET /api/bookkeeping/accounts/reference
*
* Returns the company's chart-of-accounts activation status: one lightweight
* row per account it holds (active or not), shaped
* `{ account_number, is_active, is_system_account }`.
*
* The BAS catalog itself is static and already bundled into the client
* (lib/bookkeeping/bas-reference); the kontoplan UI merges this activation
* list against that bundled catalog to render the "BAS-katalog" tab. We
* deliberately do NOT re-send the full ~1,300-account catalog over the wire on
* every page load; the browser already has that payload.
*/
export const GET = withRouteContext('bookkeeping.accounts.reference', async (_request, ctx) => {
const { supabase, companyId } = ctx
// Paginated with a stable unique order: a full-BAS chart exceeds the
// 1000-row page size, and unordered .range() paging can duplicate or skip
// rows on page boundaries (see fetch-all.ts ordering invariant).
try {
const userAccounts = await fetchAllRows<{ account_number: string; is_active: boolean; is_system_account: boolean }>(({ from, to }) =>
supabase
.from('chart_of_accounts')
.select('account_number, is_active, is_system_account')
.eq('company_id', companyId)
.order('account_number', { ascending: true })
.range(from, to)
)
return NextResponse.json({ data: userAccounts })
} catch (error) {
return NextResponse.json({ error: error instanceof Error ? getUserErrorMessage(error) : 'Failed to fetch accounts' }, { status: 500 })
}
})