Files
accounted/lib/parties/suggest.ts
T
b996da60ee feat(parties): Förslag från bokföringen, confirmed straight into Leverantörer and Kunder (#2206)
* feat(parties): Kontakter register, suggestion queue, dossier and merge

Phase 1's two surfaces on top of the parties substrate:

- /parties page: one list with the five-way switch (Alla, Kunder,
  Leverantörer, Förslag, Bara i bokföringen), search, a 12-month/all
  period picker, and at most one attention line. Confirmed rows show
  roles as muted text, rhythm, underlag, dominant account and money.
  Observed rows are computed and never stored; a generic band keeps
  unattributed spend visible.
- Suggestion queue: a reason per row, hard-key rows pre-ticked, bulk
  confirm behind one dialog, dismiss on hover, undo on the toast.
- Dossier slide-over: Pengar, Bokföring, Vad Accounted vet (facts and
  identities with source and count), Underlag och verifikat, Historik.
- Merge dialog with a visible, swappable survivor and undo.
- API: GET /api/parties, GET /api/parties/[id], POST suggest, decide,
  decide/undo, merge, merge/undo (withRouteContext, Zod, 15 tests).
- Migration 20260903090000: decide_parties snapshots the reason it
  clears; undo_party_decisions reverses confirm/dismiss within 30 days;
  decision kind 'undo'.
- The pipeline runs after SIE import and provider migration (non-blocking)
  so a migrant's register is full on arrival.
- Nav entry under Register; sv/en strings.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(parties): pass explicit interpolation values to next-intl

next build's type check rejects a typed interface where the translator
wants an index-signature record.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(parties): retry label on the load-failed state

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(parties): hard keys for companies without org number, readable names, look-alikes at read time

- get_ledger_key_evidence dropped every document for a company whose own
  org number is NULL (the self check compared against NULL). Replaced in
  20260903100000 with a coalesced comparison; pg test covers it.
- Display names come from the printed name on documents, otherwise from
  the voucher text with the AP/AR prefix and supplier number removed.
- Look-alike parties (same core, or one core extending the other by whole
  words: Fortnox / Fortnox Finans) are detected when the register is read,
  never stored, and feed the Dubblett? chip and the merge dialog.
- Queue shows Intäkt beside Kostnad; dossier hides zero money rows and
  formats bankgiro/plusgiro; merge dialog cancels with Avbryt; no
  synchronous setState inside effects.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* feat(parties): link every new supplier and customer to a party on write

The backfill covered the rows that existed on 2026-09-02; 108 rows
created since had no party and never reached the register. A BEFORE
INSERT/UPDATE trigger on customers and suppliers now calls ensure_party
on every write path at once: find-or-create by org number inside the
company, never by name; a private customer gets a kind=person party
without any number; a nameless row stays unlinked; a foreign party id is
refused with the same error as the composite foreign key; a link to a
merged party follows the chain to the survivor; the clear that ON DELETE
SET NULL performs is kept. ensure_party lets the trigger act for the
row's owner (pg_trigger_depth() > 0); the RPC path is unchanged. The
migration also links the rows created since the backfill.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(parties): dossier hides dismissed parties and follows merges to the survivor

The register hid archived parties while the dossier still served them by
id, and a merged party's dossier pointed at a dead row. Superagent P2 on
#2206; three unit tests.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* chore(parties): move the role-link migration past main's 20260903110000

Two files with one version would collide in schema_migrations.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* feat(parties): confirm suggestions into Leverantörer and Kunder, no third noun

Founder decision after the walkthrough: users know two words. The page
becomes the queue 'Förslag från bokföringen' with 'Bara i bokföringen'
beside it; the Kontakter nav entry and the Alla/Kunder/Leverantörer
views go. Each suggestion shows what it becomes (Blir), read from the
ledger side and changeable per row; confirming calls promote_parties,
which creates the supplier and/or customer row from the party's facts,
never a duplicate, and is undoable for 30 days through
undo_party_promotions (the created rows are archived, the party returns
to the queue). Leverantörer and Kunder carry the one attention line that
leads here. The dossier offers Lägg upp som leverantör / som kund.

Migration 20260903130000, 5 pg tests, route and unit tests updated.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(parties): write bankgiro and plusgiro the way the supplier form does

Identities are stored as digits; suppliers carry 5317-0900.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* chore(parties): move the four queue migrations past main's 20260903170000

Main merged 20260903120000_skattekonto_transactions_realtime_publication
with the same version as the role-link trigger; the preview database
refused the duplicate key. All four now sit after main's newest so the
set applies in one ordered run on prod.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-03 19:49:25 +02:00

291 lines
10 KiB
TypeScript

/**
* Parties, phase 1c: the suggestion pipeline.
*
* Turns what the ledger already knows about a company into suggested parties
* so a migrant's register is full on arrival. Inputs are the observed parties
* (posted vouchers grouped by ledger key, get_observed_parties) and the hard
* keys read from documents linked to those vouchers (get_ledger_key_evidence:
* org number, VAT number, bankgiro, plusgiro, printed name). Output is a list
* of items for apply_party_suggestions, which writes parties with status
* 'suggested' and never merges on name: a key attaches to an existing party
* only through its org number or because that exact key is already an alias.
*
* Keys that look alike (same core) are reported in the reason as
* similar_to, so the queue can offer "same as X?" for a person to decide.
* The model selection step is not wired here yet; it runs in shadow through
* scripts/parties/eval-selection.ts until its decisions are labelled.
*/
import type { SupabaseClient } from '@supabase/supabase-js'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
import { coreKey, displayNameFromVoucherText } from './ledger-key'
import { getObservedParties, type ObservedParty } from './observed'
export interface IdentityEvidence {
value: string
n: number
first_seen: string | null
last_seen: string | null
}
export interface LedgerKeyEvidence {
key: string
docs: number
self_docs: number
orgs: Array<{ org: string; n: number }>
vat_numbers: Array<{ vat: string; n: number }>
names: Array<{ name: string; n: number }>
bankgiro: IdentityEvidence[]
plusgiro: IdentityEvidence[]
}
export interface ExistingParty {
id: string
display_name: string
org_number: string | null
alias_keys: string[]
status: 'suggested' | 'confirmed'
}
export interface SuggestionFact {
field: string
value: unknown
source: 'ledger' | 'document'
reference?: Record<string, unknown>
}
export interface SuggestionIdentity {
scheme: 'bankgiro' | 'plusgiro'
value: string
first_seen: string | null
last_seen: string | null
seen_count: number
}
export interface SuggestionReason {
/** How the key attaches, or why it becomes a new party. */
attach: 'party_id' | 'org_number' | 'alias_key' | 'new'
occurrences: number
expense_sek: number
revenue_sek: number
first_seen: string
last_seen: string
docs: number
/** Documents whose supplier org number is the company's own: sales side. */
self_docs: number
org_number?: string
/** More than one org number seen under the key: hard key withheld. */
ambiguous_orgs?: string[]
dominant_account?: string | null
/** Live parties with the same core: a merge question, never a merge. */
similar_to?: Array<{ party_id: string; display_name: string }>
}
export interface SuggestionItem {
key: string
display_name: string
legal_name?: string
kind: 'company'
origin: 'ledger' | 'document'
org_number?: string
vat_number?: string
party_id?: string
alias_keys: string[]
reason: SuggestionReason
facts: SuggestionFact[]
identities: SuggestionIdentity[]
}
export interface SuggestionSkip {
key: string
label: ObservedParty['label']
}
export interface BuildResult {
items: SuggestionItem[]
skipped: SuggestionSkip[]
}
function pickName(observed: ObservedParty, evidence: LedgerKeyEvidence | undefined): { display: string; legal?: string } {
// A printed supplier name from a document beats the voucher text, which is
// upper-cased, truncated and prefixed by whatever the source system did.
const printed = evidence?.names[0]?.name
if (printed && printed.length >= 2) return { display: printed, legal: printed }
return { display: displayNameFromVoucherText(observed.name || observed.key) }
}
function identitiesFrom(evidence: LedgerKeyEvidence | undefined): SuggestionIdentity[] {
if (!evidence) return []
const out: SuggestionIdentity[] = []
for (const scheme of ['bankgiro', 'plusgiro'] as const) {
for (const e of evidence[scheme]) {
out.push({ scheme, value: e.value, first_seen: e.first_seen, last_seen: e.last_seen, seen_count: e.n })
}
}
return out
}
/**
* Pure: decide what each observed party key becomes. Only keys the
* pre-classifier calls 'party' continue; the rest are returned as skipped so
* callers can show why "Inköp av varor" is not a supplier.
*/
export function buildSuggestions(input: {
observed: ObservedParty[]
evidence: LedgerKeyEvidence[]
existing: ExistingParty[]
}): BuildResult {
const evidenceByKey = new Map(input.evidence.map((e) => [e.key, e]))
const byOrg = new Map<string, ExistingParty>()
const byAlias = new Map<string, ExistingParty>()
const byCore = new Map<string, ExistingParty[]>()
for (const p of input.existing) {
if (p.org_number && !byOrg.has(p.org_number)) byOrg.set(p.org_number, p)
for (const a of p.alias_keys) if (!byAlias.has(a)) byAlias.set(a, p)
const c = coreKey(p.display_name)
if (c) byCore.set(c, [...(byCore.get(c) ?? []), p])
for (const a of p.alias_keys) {
const ac = coreKey(a)
if (ac && ac !== c) byCore.set(ac, [...(byCore.get(ac) ?? []), p])
}
}
const items: SuggestionItem[] = []
const skipped: SuggestionSkip[] = []
for (const o of input.observed) {
if (o.label !== 'party') {
skipped.push({ key: o.key, label: o.label })
continue
}
const ev = evidenceByKey.get(o.key)
const orgs = ev?.orgs ?? []
const org = orgs.length === 1 ? orgs[0]!.org : undefined
const existing = (org && byOrg.get(org)) || byAlias.get(o.key) || undefined
const name = pickName(o, ev)
const reason: SuggestionReason = {
attach: existing ? (org && byOrg.get(org) === existing ? 'org_number' : 'alias_key') : 'new',
occurrences: o.occurrences,
expense_sek: o.expense_sek,
revenue_sek: o.revenue_sek,
first_seen: o.first_seen,
last_seen: o.last_seen,
docs: ev?.docs ?? 0,
self_docs: ev?.self_docs ?? 0,
dominant_account: o.dominant_account_number,
}
if (org) reason.org_number = org
if (orgs.length > 1) reason.ambiguous_orgs = orgs.map((x) => x.org)
if (!existing) {
const similar = (byCore.get(coreKey(o.key)) ?? []).filter((p) => !org || p.org_number !== org)
if (similar.length) reason.similar_to = similar.slice(0, 6).map((p) => ({ party_id: p.id, display_name: p.display_name }))
}
const facts: SuggestionFact[] = []
if (o.dominant_account_number) {
facts.push({
field: 'dominant_account',
value: { account: o.dominant_account_number, share: o.dominant_account_share, count: o.dominant_account_count },
source: 'ledger',
reference: { occurrences: o.occurrences, first_seen: o.first_seen, last_seen: o.last_seen },
})
}
if (o.cadence_days != null) {
facts.push({ field: 'cadence_days', value: o.cadence_days, source: 'ledger', reference: { occurrences: o.occurrences } })
}
if (org) {
facts.push({ field: 'org_number', value: org, source: 'document', reference: { docs: orgs[0]!.n } })
}
if (name.legal) {
facts.push({ field: 'legal_name', value: name.legal, source: 'document', reference: { docs: ev?.names[0]?.n ?? 0 } })
}
// Identities only when the hard key is unambiguous: a key that mixes two
// org numbers would otherwise attach one supplier's bankgiro to another.
const identities = orgs.length > 1 ? [] : identitiesFrom(ev)
const vat = ev?.vat_numbers[0]?.vat
items.push({
key: o.key,
display_name: name.display,
...(name.legal ? { legal_name: name.legal } : {}),
kind: 'company',
origin: org ? 'document' : 'ledger',
...(org ? { org_number: org } : {}),
...(vat && orgs.length <= 1 ? { vat_number: vat } : {}),
...(existing ? { party_id: existing.id } : {}),
alias_keys: [o.key],
reason,
facts,
identities,
})
}
return { items, skipped }
}
export interface SuggestSummary {
observed: number
suggested: number
skipped: number
created: number
attached: number
identities: number
facts: number
}
/**
* Run the pipeline for one company and persist the result. Safe to re-run:
* apply_party_suggestions is idempotent.
*/
export async function suggestPartiesForCompany(
supabase: SupabaseClient,
companyId: string,
userId: string,
options: { fromDate?: string | null; limit?: number; chunkSize?: number } = {},
): Promise<SuggestSummary> {
const observed = await getObservedParties(supabase, companyId, {
fromDate: options.fromDate ?? null,
limit: options.limit ?? 5000,
})
const { data: evidenceData, error: evidenceError } = await supabase.rpc('get_ledger_key_evidence', {
p_company_id: companyId,
})
if (evidenceError) throw new Error(`get_ledger_key_evidence failed: ${evidenceError.message}`)
const evidence = (Array.isArray(evidenceData) ? evidenceData : []) as LedgerKeyEvidence[]
const existing = await fetchAllRows<ExistingParty>(({ from, to }) =>
supabase
.from('parties')
.select('id, display_name, org_number, alias_keys, status')
.eq('company_id', companyId)
.is('merged_into', null)
.is('archived_at', null)
.order('created_at', { ascending: true })
.range(from, to),
)
const { items, skipped } = buildSuggestions({ observed, evidence, existing })
const summary: SuggestSummary = {
observed: observed.length,
suggested: items.length,
skipped: skipped.length,
created: 0,
attached: 0,
identities: 0,
facts: 0,
}
const chunk = Math.max(1, options.chunkSize ?? 200)
for (let i = 0; i < items.length; i += chunk) {
const { data, error } = await supabase.rpc('apply_party_suggestions', {
p_company_id: companyId,
p_user_id: userId,
p_items: items.slice(i, i + chunk),
})
if (error) throw new Error(`apply_party_suggestions failed: ${error.message}`)
const r = (data ?? {}) as Partial<Record<'created' | 'attached' | 'identities' | 'facts', number>>
summary.created += r.created ?? 0
summary.attached += r.attached ?? 0
summary.identities += r.identities ?? 0
summary.facts += r.facts ?? 0
}
return summary
}