Files
accounted/lib/invoices/__tests__/peppol-registration.test.ts
T
f93152c397 feat(peppol): receive e-invoices via Qvalia: registration, inbound archive, inbox delivery (#1789)
* feat(peppol): receive e-invoices via Qvalia: registration, inbound archive, inbox delivery

Second Peppol slice (#546). Qvalia confirmed that sending needs no
per-company account, so receiving keeps the consolidated partner account:
each company publishes its 0007:orgnr on our account and inbound documents
are routed by the AccountingCustomerParty endpoint.

- PeppolTransport grows optional receiving methods (registerRecipient,
  unregisterRecipient, listInboundDocuments, fetchInboundDocumentXml); the
  Qvalia adapter implements them (PUT/DELETE /peppol/{id}, readinvoices /
  readcreditnotes, exact XML fetch).
- lib/invoices/peppol-inbound-ubl.ts reads the provider's UBL-JSON
  (xml2js-style prefixed keys, verified against Qvalia's real inbound test
  invoice, kept as a fixture) into a neutral document: parties, payment
  means with SE:BANKGIRO/SE:PLUSGIRO/IBAN, totals, VAT subtotals, lines,
  embedded attachments, credit notes.
- Migration 20260821170000: peppol_registrations (one live row per company
  and participant), peppol_inbound_documents (exact XML immutable and
  undeletable, routed once), invoice_inbox_items.source gains 'peppol' with a
  per-channel dedupe index; pg-real test covers RLS, uniqueness, immutability
  and routing.
- POST/DELETE/GET /api/settings/peppol + "E-faktura via Peppol" switch in
  Settings > Fakturering; personnummer-based companies are refused until 0088
  GLN exists; sandbox refused.
- GET /api/peppol/inbound/cron every 10 minutes: archive, route, deliver.
  lib/invoices/peppol-inbox-delivery.ts archives the XML as a WORM document
  (upload_source e_invoice, extractionOwner none), an embedded PDF when
  present, and creates the inbox row with the extraction filled from the UBL
  (confidence 1, no model pass), matching the supplier by org number. The
  existing inbox review/convert flow takes over.
- document-service accepts application/xml for the archive; inbox list shows
  a Peppol icon.

Refs #546

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TqFpxeWqbpR7bcwUJLRERQ

* test(peppol): archive contract, pg fixture and phantom-column ceiling for the receiving tables

The two new tables are räkenskapsinformation and join MASTER_DATA_DUMP_TABLES;
the pg fixture for a deregistered row now carries deregistered_at as the
status-shape constraint requires; the archive insert is an inline literal and
the one generic processing-state updater is accounted for in the ceiling.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TqFpxeWqbpR7bcwUJLRERQ

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-21 16:56:32 +02:00

174 lines
7.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { SupabaseClient } from '@supabase/supabase-js'
import { createQueuedMockSupabase } from '@/tests/helpers'
import {
PEPPOL_BIS_BILLING_CREDIT_NOTE_DOCUMENT_TYPE_ID,
PEPPOL_RECEIVING_DOCUMENT_TYPES,
deregisterCompanyFromPeppolReceiving,
preparePeppolParticipant,
registerCompanyForPeppolReceiving,
} from '@/lib/invoices/peppol-registration'
import { PEPPOL_BIS_BILLING_INVOICE_DOCUMENT_TYPE_ID } from '@/lib/invoices/peppol-bis-billing'
import { PeppolTransportError, type PeppolTransport } from '@/lib/invoices/peppol-transport'
const { supabase: mockService, enqueue, reset, calls } = createQueuedMockSupabase()
const service = mockService as unknown as SupabaseClient
const settings = {
org_number: '559538-6219',
company_name: 'Arcim Technology AB',
vat_number: 'SE559538621901',
city: 'Stockholm',
country: 'SE',
}
function makeTransport(overrides: Partial<PeppolTransport> = {}): PeppolTransport {
return {
provider: 'qvalia',
lookupRecipient: vi.fn(),
submit: vi.fn(),
verifyWebhook: vi.fn(),
retrieveEvidence: vi.fn(),
registerRecipient: vi.fn().mockResolvedValue({
status: 'registered',
participant: { scheme: '0007', identifier: '5595386219' },
providerAccountReference: 'SE5595386219',
raw: {},
}),
unregisterRecipient: vi.fn().mockResolvedValue(undefined),
...overrides,
}
}
const registeredRow = {
id: 'reg-1',
company_id: 'company-1',
user_id: 'user-1',
provider: 'qvalia',
provider_account_reference: 'SE5595386219',
participant_scheme: '0007',
participant_identifier: '5595386219',
status: 'registered',
business_card: {},
document_types: PEPPOL_RECEIVING_DOCUMENT_TYPES,
registered_at: '2026-08-21T16:00:00.000Z',
deregistered_at: null,
last_error: null,
created_at: '2026-08-21T15:59:00.000Z',
updated_at: '2026-08-21T16:00:00.000Z',
}
describe('preparePeppolParticipant', () => {
it('derives 0007 + organisation number and the business card', () => {
expect(preparePeppolParticipant(settings)).toEqual({
ok: true,
participant: { scheme: '0007', identifier: '5595386219' },
businessCard: {
companyName: 'Arcim Technology AB',
countryCode: 'SE',
geographicalInformation: 'Stockholm',
vatNumber: 'SE559538621901',
orgNumber: '5595386219',
},
})
expect(preparePeppolParticipant({ ...settings, org_number: '16559538-6219' })).toMatchObject({
ok: true,
participant: { identifier: '5595386219' },
})
})
it('refuses missing numbers, personnummer and missing names', () => {
expect(preparePeppolParticipant({ ...settings, org_number: null })).toEqual({ ok: false, code: 'PEPPOL_REGISTRATION_ORG_NUMBER_REQUIRED' })
expect(preparePeppolParticipant({ ...settings, org_number: '198001011234' })).toEqual({ ok: false, code: 'PEPPOL_REGISTRATION_ORG_NUMBER_REQUIRED' })
expect(preparePeppolParticipant({ ...settings, org_number: '8001011234' })).toEqual({ ok: false, code: 'PEPPOL_REGISTRATION_PERSONAL_NUMBER' })
expect(preparePeppolParticipant({ ...settings, company_name: ' ' })).toEqual({ ok: false, code: 'PEPPOL_REGISTRATION_COMPANY_NAME_REQUIRED' })
})
})
describe('registerCompanyForPeppolReceiving', () => {
beforeEach(() => {
vi.clearAllMocks()
reset()
})
it('advertises Invoice and CreditNote for BIS Billing 3', () => {
expect(PEPPOL_RECEIVING_DOCUMENT_TYPES.map((t) => t.documentTypeId)).toEqual([
PEPPOL_BIS_BILLING_INVOICE_DOCUMENT_TYPE_ID,
PEPPOL_BIS_BILLING_CREDIT_NOTE_DOCUMENT_TYPE_ID,
])
})
it('writes pending, publishes through the transport, then finalizes as registered', async () => {
const transport = makeTransport()
enqueue({ data: [], error: null }) // existing registrations
enqueue({ data: { id: 'reg-1' }, error: null }) // insert pending
enqueue({ data: registeredRow, error: null }) // finalize update
const result = await registerCompanyForPeppolReceiving({
service, companyId: 'company-1', userId: 'user-1', transport, settings,
})
expect(result).toEqual({ ok: true, registration: registeredRow })
expect(transport.registerRecipient).toHaveBeenCalledWith({
participant: { scheme: '0007', identifier: '5595386219' },
businessCard: expect.objectContaining({ companyName: 'Arcim Technology AB', orgNumber: '5595386219' }),
documentTypes: PEPPOL_RECEIVING_DOCUMENT_TYPES,
})
const inserted = calls.find((c) => c.method === 'insert')
expect(inserted?.args[0]).toMatchObject({ status: 'pending', participant_identifier: '5595386219', company_id: 'company-1' })
const finalized = calls.filter((c) => c.method === 'update').at(-1)
expect(finalized?.args[0]).toMatchObject({ status: 'registered', provider_account_reference: 'SE5595386219', last_error: null })
})
it('records a failed registration with the provider reason and reports it', async () => {
const transport = makeTransport({
registerRecipient: vi.fn().mockRejectedValue(new PeppolTransportError('Qvalia answered 500', { retryable: true, detail: 'smp down' })),
})
enqueue({ data: [], error: null })
enqueue({ data: { id: 'reg-1' }, error: null })
enqueue({ data: null, error: null }) // failure update
const result = await registerCompanyForPeppolReceiving({
service, companyId: 'company-1', userId: 'user-1', transport, settings,
})
expect(result).toEqual({ ok: false, code: 'PEPPOL_REGISTRATION_FAILED', detail: 'smp down' })
const failed = calls.filter((c) => c.method === 'update').at(-1)
expect(failed?.args[0]).toMatchObject({ status: 'failed', last_error: 'Qvalia answered 500: smp down' })
})
it('stops before the network on a personnummer and on a send-only transport', async () => {
const transport = makeTransport()
expect(await registerCompanyForPeppolReceiving({
service, companyId: 'company-1', userId: 'user-1', transport, settings: { ...settings, org_number: '8001011234' },
})).toEqual({ ok: false, code: 'PEPPOL_REGISTRATION_PERSONAL_NUMBER' })
expect(transport.registerRecipient).not.toHaveBeenCalled()
const sendOnly = makeTransport({ registerRecipient: undefined })
expect(await registerCompanyForPeppolReceiving({
service, companyId: 'company-1', userId: 'user-1', transport: sendOnly, settings,
})).toEqual({ ok: false, code: 'PEPPOL_RECEIVING_UNSUPPORTED' })
})
})
describe('deregisterCompanyFromPeppolReceiving', () => {
beforeEach(() => {
vi.clearAllMocks()
reset()
})
it('withdraws the live identifier and marks the row deregistered', async () => {
const transport = makeTransport()
enqueue({ data: [registeredRow], error: null })
enqueue({ data: { ...registeredRow, status: 'deregistered', deregistered_at: '2026-08-21T17:00:00.000Z' }, error: null })
const result = await deregisterCompanyFromPeppolReceiving({ service, companyId: 'company-1', transport })
expect(result.ok).toBe(true)
expect(transport.unregisterRecipient).toHaveBeenCalledWith({ scheme: '0007', identifier: '5595386219' })
})
it('reports not found when nothing is live', async () => {
enqueue({ data: [{ ...registeredRow, status: 'deregistered' }], error: null })
expect(await deregisterCompanyFromPeppolReceiving({ service, companyId: 'company-1', transport: makeTransport() }))
.toEqual({ ok: false, code: 'PEPPOL_REGISTRATION_NOT_FOUND' })
})
})