Files
accounted/lib/transactions/quick-review-defaults.ts
T
Jakob Wennberg 198d3092c7 fix: counterparty template pick crashes the page (#1291)
Picking a suggestion under "Tidigare motparter" in Bokför transaktion replaced
the page with "Något gick fel". handleOpenTemplateReview built the review state
from `{ id, name_sv } as BookingTemplate`, so `template.debit_account` was
undefined, reached QuickReviewDialog's required `defaultAccount: string`, and
threw on `accountOverride.startsWith('2')` during the first render.

Typed the dialog's template prop as a narrow ReviewTemplate whose optional
fields are actually optional, so the cast disappears and the compiler owns this
class of bug. Also carries the counterparty's learned accounts and VAT (the
preview showed the category fallback, not what the server books) and decides
"is this a counterparty booking" from the template id rather than the presence
of a line_pattern (single-line templates got an account/VAT editor the
categorize route discards).

Five more page-crashes of the same shape, adversarially verified:

- suppliers/[id] and supplier-invoices/[id] passed the error envelope OBJECT as
  a toast description. The Toaster is a sibling of {children} in the ROOT
  layout, so that throw escapes both segment error boundaries onto global-error.
- components/reports/views wrote the same object into a useState<string | null>
  at 13 sites and rendered it bare.
- components/ui/toaster.tsx now coerces non-renderable values as a choke point.
- skattekonto read data.informationstext.length off Skatteverket's raw JSON,
  where the field is not required.
- TicWorkspace read profile.statuses.length off a persisted jsonb blob. 17 of 17
  prod rows predate the TIC v2 upgrade (#584) and lack the key, so that
  workspace was in the error boundary for every company that had opened it.

Plus hardening: formatCurrency coerces a null currency to SEK (prod has 0 NULL
across 28 416 transactions, so defense not a live bug) and cleanSignatory
returns [] for a missing description.

Verified by rendering the real dialog against a throwaway /sandbox route: the
pre-fix prop shape reproduces the exact error boundary, the fixed one renders
D: 6570 Bankavgifter / K: 1930 Företagskonto and the matching verifikat.

No migrations.
2026-07-29 19:20:25 +02:00

73 lines
2.8 KiB
TypeScript

import {
getDefaultAccountForCategory,
getDefaultVatTreatmentForCategory,
} from '@/lib/bookkeeping/category-mapping'
import type { TransactionCategory, VatTreatment } from '@/types'
/**
* The template shape the transaction review dialog actually reads.
*
* Deliberately NOT `BookingTemplate`: the review dialog is opened from three
* sources, and only one of them has a full catalog template behind it.
*
* - a static catalog template (`BookingTemplate`, structurally assignable),
* - a user library template converted to the same shape,
* - a learned counterparty template ("Tidigare motparter"), which has no
* catalog entry at all: it carries a name and a debit/credit pair and
* nothing else.
*
* The counterparty case used to be forced into `BookingTemplate` with an
* `as BookingTemplate` cast on a two-field object literal. The cast made
* every missing field look present to the compiler, so `template.debit_account`
* read `undefined` at runtime, flowed into the dialog's `defaultAccount` prop
* (typed `string`), and crashed the page on `accountOverride.startsWith('2')`.
* Typing the optional fields as optional is what makes that class of bug a
* compile error instead of an error boundary.
*/
export interface ReviewTemplate {
id: string
name_sv: string
debit_account?: string
credit_account?: string
vat_treatment?: VatTreatment | null
vat_rate?: number
special_rules_sv?: string
deductibility_note_sv?: string
requires_vat_registration_data?: boolean
reverse_charge_supplier_type?: 'eu_business' | 'non_eu_business' | 'swedish_business'
}
export interface QuickReviewDefaults {
account: string
vat: VatTreatment | 'none'
}
/**
* Resolve the account + VAT the review dialog starts on.
*
* A template without a `templateId` (library or counterparty template) is not
* validated server-side against the static catalog, so its own debit account
* and VAT treatment seed the form. Anything the template leaves unset falls
* back to the transaction category's defaults, and finally to an empty
* account / no VAT: the caller feeds a required `string` prop, and an
* `undefined` there is what took the page down.
*/
export function resolveQuickReviewDefaults(
template: ReviewTemplate | null | undefined,
templateId: string | undefined,
category: TransactionCategory | null | undefined,
): QuickReviewDefaults {
const useTemplateDefaults = !templateId && !!template
const account =
(useTemplateDefaults ? template.debit_account : undefined) ||
(category ? getDefaultAccountForCategory(category) : '') ||
''
const vat: VatTreatment | 'none' = useTemplateDefaults
? (template.vat_treatment ?? 'none')
: (category ? (getDefaultVatTreatmentForCategory(category) ?? 'none') : 'none')
return { account, vat }
}