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>
This commit is contained in:
co-authored by
Claude Fable 5.1
Jakob Wennberg
parent
e2d38b0ab3
commit
b996da60ee
@@ -151,15 +151,10 @@ describe('parties substrate (pg)', () => {
|
||||
VALUES ($1, $2, $3, 'pg test: the party backfill must skip archived companies', '{}'::jsonb, '{}'::jsonb)`,
|
||||
[frozen.companyId, replacement.companyId, frozen.userId],
|
||||
)
|
||||
const partyId = await getPool().query<{ id: string }>(
|
||||
`SELECT public.ensure_party($1, $2, 'Frozen Supplier', NULL, 'company', 'backfill') AS id`,
|
||||
[replacement.companyId, replacement.userId],
|
||||
)
|
||||
await expect(
|
||||
getPool().query(`UPDATE public.suppliers SET party_id = $1 WHERE id = $2`, [partyId.rows[0]!.id, rows[0]!.id]),
|
||||
).rejects.toMatchObject({ code: 'P0001' })
|
||||
const before = await getPool().query<{ party_id: string | null }>(`SELECT party_id FROM public.suppliers WHERE id = $1`, [rows[0]!.id])
|
||||
await expect(getPool().query(`UPDATE public.suppliers SET party_id = NULL WHERE id = $1`, [rows[0]!.id])).rejects.toMatchObject({ code: 'P0001' })
|
||||
const still = await getPool().query<{ party_id: string | null }>(`SELECT party_id FROM public.suppliers WHERE id = $1`, [rows[0]!.id])
|
||||
expect(still.rows[0]!.party_id).toBeNull()
|
||||
expect(still.rows[0]!.party_id).toBe(before.rows[0]!.party_id)
|
||||
})
|
||||
|
||||
it('ensure_party refuses a p_user_id that is not the authenticated caller', async () => {
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
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)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,110 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { getPool, withUserContext } from './setup'
|
||||
import { seedCompany } from './fixtures'
|
||||
|
||||
const ORG = '5564300142'
|
||||
|
||||
async function party(id: string) {
|
||||
const { rows } = await getPool().query<{ id: string; display_name: string; org_number: string | null; kind: string; status: string }>(
|
||||
`SELECT id, display_name, org_number, kind, status FROM public.parties WHERE id = $1`,
|
||||
[id],
|
||||
)
|
||||
return rows[0] ?? null
|
||||
}
|
||||
|
||||
describe('link_party_on_role_write (pg)', () => {
|
||||
it('gives a new supplier a confirmed party keyed on its org number, and reuses it for a customer with the same org', async () => {
|
||||
const c = await seedCompany()
|
||||
const s = await getPool().query<{ party_id: string }>(
|
||||
`INSERT INTO public.suppliers (company_id, user_id, name, org_number) VALUES ($1, $2, 'Beijer Byggmaterial AB', '556430-0142') RETURNING party_id`,
|
||||
[c.companyId, c.userId],
|
||||
)
|
||||
expect(s.rows[0]!.party_id).toBeTruthy()
|
||||
const p = await party(s.rows[0]!.party_id)
|
||||
expect(p).toMatchObject({ display_name: 'Beijer Byggmaterial AB', org_number: ORG, kind: 'company', status: 'confirmed' })
|
||||
|
||||
const k = await getPool().query<{ party_id: string }>(
|
||||
`INSERT INTO public.customers (company_id, user_id, name, org_number, customer_type) VALUES ($1, $2, 'Beijer Bygg', $3, 'swedish_business') RETURNING party_id`,
|
||||
[c.companyId, c.userId, ORG],
|
||||
)
|
||||
expect(k.rows[0]!.party_id).toBe(s.rows[0]!.party_id)
|
||||
const n = await getPool().query<{ n: string }>(`SELECT count(*)::text AS n FROM public.parties WHERE company_id = $1`, [c.companyId])
|
||||
expect(n.rows[0]!.n).toBe('1')
|
||||
})
|
||||
|
||||
it('never merges on name: two suppliers without org numbers become two parties', async () => {
|
||||
const c = await seedCompany()
|
||||
await getPool().query(`INSERT INTO public.suppliers (company_id, user_id, name) VALUES ($1, $2, 'Fortnox')`, [c.companyId, c.userId])
|
||||
await getPool().query(`INSERT INTO public.suppliers (company_id, user_id, name) VALUES ($1, $2, 'Fortnox')`, [c.companyId, c.userId])
|
||||
const n = await getPool().query<{ n: string }>(`SELECT count(*)::text AS n FROM public.parties WHERE company_id = $1`, [c.companyId])
|
||||
expect(n.rows[0]!.n).toBe('2')
|
||||
})
|
||||
|
||||
it('creates a person party without any number for a private customer', async () => {
|
||||
const c = await seedCompany()
|
||||
const k = await getPool().query<{ party_id: string }>(
|
||||
`INSERT INTO public.customers (company_id, user_id, name, customer_type, personal_number, org_number)
|
||||
VALUES ($1, $2, 'Anna Andersson', 'individual', repeat('ab', 40), '19800101-1234') RETURNING party_id`,
|
||||
[c.companyId, c.userId],
|
||||
)
|
||||
const p = await party(k.rows[0]!.party_id)
|
||||
expect(p).toMatchObject({ display_name: 'Anna Andersson', org_number: null, kind: 'person' })
|
||||
const leak = await getPool().query<{ n: string }>(
|
||||
`SELECT count(*)::text AS n FROM public.parties WHERE company_id = $1 AND (org_number IS NOT NULL OR display_name ~ '[0-9]{6}')`,
|
||||
[c.companyId],
|
||||
)
|
||||
expect(leak.rows[0]!.n).toBe('0')
|
||||
})
|
||||
|
||||
it('re-keys when the org number changes to another live party, and teaches a party its org number', async () => {
|
||||
const c = await seedCompany()
|
||||
const a = await getPool().query<{ id: string; party_id: string }>(
|
||||
`INSERT INTO public.suppliers (company_id, user_id, name) VALUES ($1, $2, 'Loopia') RETURNING id, party_id`,
|
||||
[c.companyId, c.userId],
|
||||
)
|
||||
expect((await party(a.rows[0]!.party_id))!.org_number).toBeNull()
|
||||
// The party learns the org number.
|
||||
await getPool().query(`UPDATE public.suppliers SET org_number = '556666-1012' WHERE id = $1`, [a.rows[0]!.id])
|
||||
const after = await getPool().query<{ party_id: string }>(`SELECT party_id FROM public.suppliers WHERE id = $1`, [a.rows[0]!.id])
|
||||
expect(after.rows[0]!.party_id).toBe(a.rows[0]!.party_id)
|
||||
expect((await party(a.rows[0]!.party_id))!.org_number).toBe('5566661012')
|
||||
// A different org number that belongs to another live party moves the link.
|
||||
const b = await getPool().query<{ party_id: string }>(
|
||||
`INSERT INTO public.suppliers (company_id, user_id, name, org_number) VALUES ($1, $2, 'Beijer', $3) RETURNING party_id`,
|
||||
[c.companyId, c.userId, ORG],
|
||||
)
|
||||
await getPool().query(`UPDATE public.suppliers SET org_number = $2 WHERE id = $1`, [a.rows[0]!.id, ORG])
|
||||
const moved = await getPool().query<{ party_id: string }>(`SELECT party_id FROM public.suppliers WHERE id = $1`, [a.rows[0]!.id])
|
||||
expect(moved.rows[0]!.party_id).toBe(b.rows[0]!.party_id)
|
||||
})
|
||||
|
||||
it('leaves a nameless row unlinked and relinks a row whose party_id was cleared', async () => {
|
||||
const c = await seedCompany()
|
||||
const s = await getPool().query<{ id: string; party_id: string | null }>(
|
||||
`INSERT INTO public.suppliers (company_id, user_id, name) VALUES ($1, $2, '') RETURNING id, party_id`,
|
||||
[c.companyId, c.userId],
|
||||
)
|
||||
expect(s.rows[0]!.party_id).toBeNull()
|
||||
const t = await getPool().query<{ id: string; party_id: string }>(
|
||||
`INSERT INTO public.suppliers (company_id, user_id, name) VALUES ($1, $2, 'Telia') RETURNING id, party_id`,
|
||||
[c.companyId, c.userId],
|
||||
)
|
||||
await getPool().query(`UPDATE public.suppliers SET party_id = NULL WHERE id = $1`, [t.rows[0]!.id])
|
||||
const relinked = await getPool().query<{ party_id: string | null }>(`SELECT party_id FROM public.suppliers WHERE id = $1`, [t.rows[0]!.id])
|
||||
expect(relinked.rows[0]!.party_id).toBeTruthy()
|
||||
})
|
||||
|
||||
it('works for an authenticated member writing a supplier owned by a colleague', async () => {
|
||||
const c = await seedCompany()
|
||||
const { rows } = await getPool().query<{ id: string }>(`SELECT id FROM auth.users WHERE id <> $1 LIMIT 1`, [c.userId])
|
||||
const colleague = rows[0]?.id ?? c.userId
|
||||
const inserted = await withUserContext(c.userId, async (client) => {
|
||||
const r = await client.query<{ party_id: string | null }>(
|
||||
`INSERT INTO public.suppliers (company_id, user_id, name, org_number) VALUES ($1, $2, 'Skellefteå Plåt AB', $3) RETURNING party_id`,
|
||||
[c.companyId, colleague, ORG],
|
||||
)
|
||||
return r.rows[0]!.party_id
|
||||
})
|
||||
expect(inserted).toBeTruthy()
|
||||
})
|
||||
})
|
||||
@@ -88,6 +88,20 @@ describe('get_ledger_key_evidence (pg)', () => {
|
||||
expect(loopia!.plusgiro).toEqual([])
|
||||
})
|
||||
|
||||
it('keeps hard keys for a company that has no org number of its own', async () => {
|
||||
const c = await seedCompany()
|
||||
await getPool().query(`UPDATE public.companies SET org_number = NULL WHERE id = $1`, [c.companyId])
|
||||
const e = await insertPostedJournalEntry({ userId: c.userId, companyId: c.companyId, fiscalPeriodId: c.fiscalPeriodId, sourceType: 'import', description: 'Levfakt Loopia AB', lines: expense('6540', 100) })
|
||||
await linkDocument({ ...c, journalEntryId: e, supplier: { name: 'Loopia AB', orgNumber: '556666-1012', vatNumber: null, bankgiro: '5317-0900', plusgiro: null } })
|
||||
const rows = await evidence(c.companyId, c.userId)
|
||||
const loopia = rows.find((r) => r.key === 'loopia')
|
||||
expect(loopia).toBeDefined()
|
||||
expect(loopia!.self_docs).toBe(0)
|
||||
expect(loopia!.orgs).toEqual([{ org: LOOPIA_ORG, n: 1 }])
|
||||
expect(loopia!.names).toEqual([{ name: 'Loopia AB', n: 1 }])
|
||||
expect(loopia!.bankgiro.map((b) => b.value)).toEqual(['53170900'])
|
||||
})
|
||||
|
||||
it('is invisible across companies', async () => {
|
||||
const mine = await seedCompany()
|
||||
const theirs = await seedCompany()
|
||||
@@ -252,3 +266,61 @@ describe('decide_parties (pg)', () => {
|
||||
expect(still.rows[0]!.status).toBe('suggested')
|
||||
})
|
||||
})
|
||||
|
||||
describe('undo_party_decisions (pg)', () => {
|
||||
async function undoDecisions(companyId: string, userId: string, ids: string[]): Promise<number> {
|
||||
const { rows } = await getPool().query<{ n: number }>(`SELECT public.undo_party_decisions($1, $2, $3::uuid[]) AS n`, [companyId, userId, ids])
|
||||
return rows[0]!.n
|
||||
}
|
||||
|
||||
it('reverses a confirm (reason restored) and a dismiss, logs undo, and refuses a second undo', async () => {
|
||||
const c = await seedCompany()
|
||||
await apply(c.companyId, c.userId, [
|
||||
{ key: 'loopia', display_name: 'Loopia AB', reason: { attach: 'new', occurrences: 3 } },
|
||||
{ key: 'noise', display_name: 'Noise', reason: { attach: 'new', occurrences: 1 } },
|
||||
])
|
||||
const ids = await getPool().query<{ id: string; display_name: string }>(`SELECT id, display_name FROM public.parties WHERE company_id = $1`, [c.companyId])
|
||||
const byName = Object.fromEntries(ids.rows.map((r) => [r.display_name, r.id]))
|
||||
expect(await decide(c.companyId, c.userId, [byName['Loopia AB']!], 'confirm')).toBe(1)
|
||||
expect(await decide(c.companyId, c.userId, [byName['Noise']!], 'dismiss')).toBe(1)
|
||||
const snap = await getPool().query<{ before: { suggested_reason: { occurrences: number } } }>(
|
||||
`SELECT before FROM public.party_decisions WHERE party_id = $1 AND kind = 'confirm'`,
|
||||
[byName['Loopia AB']],
|
||||
)
|
||||
expect(snap.rows[0]!.before.suggested_reason.occurrences).toBe(3)
|
||||
|
||||
expect(await undoDecisions(c.companyId, c.userId, [byName['Loopia AB']!, byName['Noise']!])).toBe(2)
|
||||
const state = await getPool().query<{ display_name: string; status: string; archived: boolean; reason: { occurrences: number } | null }>(
|
||||
`SELECT display_name, status, archived_at IS NOT NULL AS archived, suggested_reason AS reason FROM public.parties WHERE company_id = $1 ORDER BY display_name`,
|
||||
[c.companyId],
|
||||
)
|
||||
expect(state.rows).toEqual([
|
||||
{ display_name: 'Loopia AB', status: 'suggested', archived: false, reason: { attach: 'new', occurrences: 3 } },
|
||||
{ display_name: 'Noise', status: 'suggested', archived: false, reason: { attach: 'new', occurrences: 1 } },
|
||||
])
|
||||
const kinds = await getPool().query<{ kind: string; n: string }>(
|
||||
`SELECT kind, count(*)::text AS n FROM public.party_decisions WHERE company_id = $1 GROUP BY kind ORDER BY kind`,
|
||||
[c.companyId],
|
||||
)
|
||||
expect(kinds.rows).toEqual([
|
||||
{ kind: 'confirm', n: '1' },
|
||||
{ kind: 'dismiss', n: '1' },
|
||||
{ kind: 'undo', n: '2' },
|
||||
])
|
||||
// The latest decision is now the undo itself: nothing left to reverse.
|
||||
expect(await undoDecisions(c.companyId, c.userId, [byName['Loopia AB']!])).toBe(0)
|
||||
})
|
||||
|
||||
it('refuses after 30 days and ignores other companies', async () => {
|
||||
const c = await seedCompany()
|
||||
const other = await seedCompany()
|
||||
await apply(c.companyId, c.userId, [{ key: 'loopia', display_name: 'Loopia AB', reason: { attach: 'new' } }])
|
||||
const { rows } = await getPool().query<{ id: string }>(`SELECT id FROM public.parties WHERE company_id = $1`, [c.companyId])
|
||||
await decide(c.companyId, c.userId, [rows[0]!.id], 'confirm')
|
||||
expect(await undoDecisions(other.companyId, other.userId, [rows[0]!.id])).toBe(0)
|
||||
await getPool().query(`UPDATE public.party_decisions SET created_at = now() - interval '31 days' WHERE party_id = $1`, [rows[0]!.id])
|
||||
expect(await undoDecisions(c.companyId, c.userId, [rows[0]!.id])).toBe(0)
|
||||
const still = await getPool().query<{ status: string }>(`SELECT status FROM public.parties WHERE id = $1`, [rows[0]!.id])
|
||||
expect(still.rows[0]!.status).toBe('confirmed')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user