Files
accounted/tests/pg/party-promotion.pg.test.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

113 lines
7.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { getPool, withUserContext } from './setup'
import { seedCompany } from './fixtures'
const ORG = '5564300142'
async function suggested(companyId: string, userId: string, name: string, over: { org?: string; vat?: string; bankgiro?: string; kind?: string } = {}): Promise<string> {
const { rows } = await getPool().query<{ id: string }>(
`INSERT INTO public.parties (company_id, user_id, display_name, org_number, vat_number, kind, status, suggested_reason)
VALUES ($1, $2, $3, $4, $5, $6, 'suggested', '{"attach":"new","occurrences":3}'::jsonb) RETURNING id`,
[companyId, userId, name, over.org ?? null, over.vat ?? null, over.kind ?? 'company'],
)
if (over.bankgiro) {
await getPool().query(
`INSERT INTO public.party_identities (party_id, company_id, user_id, scheme, value, source, seen_count) VALUES ($1, $2, $3, 'bankgiro', $4, 'document', 3)`,
[rows[0]!.id, companyId, userId, over.bankgiro],
)
}
return rows[0]!.id
}
async function promote(companyId: string, userId: string, items: unknown[]) {
const { rows } = await getPool().query<{ r: Record<string, number> }>(`SELECT public.promote_parties($1, $2, $3::jsonb) AS r`, [companyId, userId, JSON.stringify(items)])
return rows[0]!.r
}
async function undo(companyId: string, userId: string, ids: string[]): Promise<number> {
const { rows } = await getPool().query<{ n: number }>(`SELECT public.undo_party_promotions($1, $2, $3::uuid[]) AS n`, [companyId, userId, ids])
return rows[0]!.n
}
describe('promote_parties / undo_party_promotions (pg)', () => {
it('creates a supplier from the party facts, confirms the party, and logs a role decision', async () => {
const c = await seedCompany()
const id = await suggested(c.companyId, c.userId, 'Beijer Byggmaterial AB', { org: ORG, vat: 'SE556430014201', bankgiro: '53170900' })
expect(await promote(c.companyId, c.userId, [{ party_id: id, roles: ['supplier'] }])).toEqual({ parties: 1, suppliers: 1, customers: 0 })
const s = await getPool().query<{ name: string; supplier_type: string; org_number: string; vat_number: string; bankgiro: string; party_id: string }>(
`SELECT name, supplier_type, org_number, vat_number, bankgiro, party_id FROM public.suppliers WHERE company_id = $1`,
[c.companyId],
)
expect(s.rows).toEqual([{ name: 'Beijer Byggmaterial AB', supplier_type: 'swedish_business', org_number: ORG, vat_number: 'SE556430014201', bankgiro: '5317-0900', party_id: id }])
const p = await getPool().query<{ status: string; reason: unknown }>(`SELECT status, suggested_reason AS reason FROM public.parties WHERE id = $1`, [id])
expect(p.rows[0]).toEqual({ status: 'confirmed', reason: null })
const d = await getPool().query<{ kind: string; created: string[] }>(
`SELECT kind, ARRAY(SELECT jsonb_array_elements_text(after->'created')) AS created FROM public.party_decisions WHERE party_id = $1`,
[id],
)
expect(d.rows).toEqual([{ kind: 'role', created: ['supplier'] }])
})
it('creates both roles for one party, never a duplicate for a role that already exists', async () => {
const c = await seedCompany()
const id = await suggested(c.companyId, c.userId, 'Nordic Studio HB', { org: ORG })
await promote(c.companyId, c.userId, [{ party_id: id, roles: ['supplier', 'customer'] }])
const again = await promote(c.companyId, c.userId, [{ party_id: id, roles: ['supplier', 'customer'] }])
expect(again).toEqual({ parties: 1, suppliers: 0, customers: 0 })
const n = await getPool().query<{ s: string; k: string }>(
`SELECT (SELECT count(*) FROM public.suppliers WHERE party_id = $1)::text AS s, (SELECT count(*) FROM public.customers WHERE party_id = $1)::text AS k`,
[id],
)
expect(n.rows[0]).toEqual({ s: '1', k: '1' })
})
it('types an EU party without org number as eu_business and a person party as an individual customer', async () => {
const c = await seedCompany()
const eu = await suggested(c.companyId, c.userId, 'Adobe Systems Software Ireland Ltd', { vat: 'IE6364992H' })
const person = await suggested(c.companyId, c.userId, 'Anna Andersson', { kind: 'person' })
await promote(c.companyId, c.userId, [
{ party_id: eu, roles: ['supplier'] },
{ party_id: person, roles: ['customer'] },
])
const s = await getPool().query<{ supplier_type: string }>(`SELECT supplier_type FROM public.suppliers WHERE party_id = $1`, [eu])
expect(s.rows[0]!.supplier_type).toBe('eu_business')
const k = await getPool().query<{ customer_type: string; org_number: string | null }>(`SELECT customer_type, org_number FROM public.customers WHERE party_id = $1`, [person])
expect(k.rows[0]).toEqual({ customer_type: 'individual', org_number: null })
})
it('undo archives the rows the promotion created and returns the party to the queue; a second undo is a no-op', async () => {
const c = await seedCompany()
const id = await suggested(c.companyId, c.userId, 'Loopia AB', { org: ORG })
await promote(c.companyId, c.userId, [{ party_id: id, roles: ['supplier'] }])
expect(await undo(c.companyId, c.userId, [id])).toBe(1)
const s = await getPool().query<{ archived: boolean; is_active: boolean }>(`SELECT archived_at IS NOT NULL AS archived, is_active FROM public.suppliers WHERE party_id = $1`, [id])
expect(s.rows[0]).toEqual({ archived: true, is_active: false })
const p = await getPool().query<{ status: string; reason: { occurrences: number } }>(`SELECT status, suggested_reason AS reason FROM public.parties WHERE id = $1`, [id])
expect(p.rows[0]!.status).toBe('suggested')
expect(p.rows[0]!.reason.occurrences).toBe(3)
expect(await undo(c.companyId, c.userId, [id])).toBe(0)
// Promoting again creates a fresh live supplier (the archived one no longer counts).
expect(await promote(c.companyId, c.userId, [{ party_id: id, roles: ['supplier'] }])).toEqual({ parties: 1, suppliers: 1, customers: 0 })
})
it('refuses an archived or foreign party, an empty role list, a spoofed user, and undo after 30 days', async () => {
const mine = await seedCompany()
const theirs = await seedCompany()
const foreign = await suggested(theirs.companyId, theirs.userId, 'Theirs AB')
const id = await suggested(mine.companyId, mine.userId, 'Mine AB')
await expect(promote(mine.companyId, mine.userId, [{ party_id: foreign, roles: ['supplier'] }])).rejects.toMatchObject({ code: '23503' })
await expect(promote(mine.companyId, mine.userId, [{ party_id: id, roles: [] }])).rejects.toMatchObject({ code: '22023' })
await expect(
withUserContext(mine.userId, (client) =>
client.query(`SELECT public.promote_parties($1, $2, $3::jsonb)`, [mine.companyId, theirs.userId, JSON.stringify([{ party_id: id, roles: ['supplier'] }])]),
),
).rejects.toMatchObject({ code: '42501' })
await getPool().query(`UPDATE public.parties SET archived_at = now() WHERE id = $1`, [id])
await expect(promote(mine.companyId, mine.userId, [{ party_id: id, roles: ['supplier'] }])).rejects.toMatchObject({ code: '23503' })
await getPool().query(`UPDATE public.parties SET archived_at = NULL WHERE id = $1`, [id])
await promote(mine.companyId, mine.userId, [{ party_id: id, roles: ['supplier'] }])
await getPool().query(`UPDATE public.party_decisions SET created_at = now() - interval '31 days' WHERE party_id = $1`, [id])
expect(await undo(mine.companyId, mine.userId, [id])).toBe(0)
})
})