Files
accounted/lib/bookkeeping/bas-catalog-client.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

41 lines
1.3 KiB
TypeScript

'use client'
import type { SearchableAccount } from '@/lib/bookkeeping/account-search'
/**
* Client-side loader for the full BAS catalogue used by AccountCombobox.
*
* The catalogue is static reference data, identical for every company, so we
* fetch it once per session and share the in-flight promise across every
* combobox instance and form mount. A failed fetch clears the cache so the
* next caller retries rather than being stuck with an empty list.
*/
export interface CatalogAccount extends SearchableAccount {
account_number: string
account_name: string
account_class: number
account_group: string
description: string | null
}
let cache: Promise<CatalogAccount[]> | null = null
export function loadBasCatalog(): Promise<CatalogAccount[]> {
if (!cache) {
cache = fetch('/api/bookkeeping/accounts/bas-catalog')
.then((res) => {
if (!res.ok) throw new Error(`bas-catalog ${res.status}`)
return res.json()
})
.then((body) => (body?.data as CatalogAccount[]) ?? [])
.catch((err) => {
// Callers degrade gracefully (search falls back to the active chart),
// so this log is the only trace a catalog fetch failure leaves.
console.error('[bas-catalog] fetch failed:', err)
cache = null // allow a retry on the next call
return []
})
}
return cache
}