docs(production): standardiserad produktionsberedskap och gap-analys
Lägger docs/production-readiness.md och siax/production-gaps.json enligt SIAX Master Definition of Done. Underlag: git-, kod- och live-evidens 2026-11-11. Nivån är evidensbaserad, inte ett leveransgodkännande.
This commit is contained in:
@@ -1,90 +0,0 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { registerPeppolTransport, type PeppolTransport } from '@/lib/invoices/peppol-transport'
|
||||
|
||||
const syncMock = vi.fn()
|
||||
const deliverMock = vi.fn()
|
||||
|
||||
vi.mock('@/lib/init', () => ({ ensureInitialized: vi.fn() }))
|
||||
vi.mock('@/lib/auth/api-keys', () => ({
|
||||
createServiceClientNoCookies: () => ({ from: vi.fn() }),
|
||||
}))
|
||||
vi.mock('@/lib/invoices/peppol-inbound', () => ({
|
||||
syncInboundPeppolDocuments: (...args: unknown[]) => syncMock(...args),
|
||||
}))
|
||||
vi.mock('@/lib/invoices/peppol-inbox-delivery', () => ({
|
||||
deliverPeppolDocumentToInbox: (...args: unknown[]) => deliverMock(...args),
|
||||
}))
|
||||
|
||||
import { GET } from '../route'
|
||||
|
||||
function request(secret: string | null): Request {
|
||||
return new Request('http://localhost:3000/api/peppol/inbound/cron', {
|
||||
headers: secret ? { authorization: `Bearer ${secret}` } : {},
|
||||
})
|
||||
}
|
||||
|
||||
function makeTransport(overrides: Partial<PeppolTransport> = {}): PeppolTransport {
|
||||
return {
|
||||
provider: 'qvalia',
|
||||
lookupRecipient: vi.fn(),
|
||||
submit: vi.fn(),
|
||||
verifyWebhook: vi.fn(),
|
||||
retrieveEvidence: vi.fn(),
|
||||
listInboundDocuments: vi.fn().mockResolvedValue([]),
|
||||
fetchInboundDocumentXml: vi.fn(),
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe('GET /api/peppol/inbound/cron', () => {
|
||||
let unregister: (() => void) | null = null
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
process.env.CRON_SECRET = 'cron-secret'
|
||||
process.env.PEPPOL_TRANSPORT_PROVIDER = 'qvalia'
|
||||
syncMock.mockResolvedValue({ listed: 1, archived: 1, duplicates: 0, routed: 0, unrouted: 0, delivered: 1, failed: 0, errors: [] })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
unregister?.()
|
||||
unregister = null
|
||||
delete process.env.PEPPOL_TRANSPORT_PROVIDER
|
||||
delete process.env.CRON_SECRET
|
||||
})
|
||||
|
||||
it('rejects a call without the cron secret', async () => {
|
||||
unregister = registerPeppolTransport(makeTransport())
|
||||
const response = await GET(request(null))
|
||||
expect(response.status).toBe(401)
|
||||
expect(syncMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('is a truthful no-op when no access point is switched on', async () => {
|
||||
delete process.env.PEPPOL_TRANSPORT_PROVIDER
|
||||
const response = await GET(request('cron-secret'))
|
||||
expect(response.status).toBe(200)
|
||||
expect(await response.json()).toEqual({ data: { skipped: true, reason: 'provider_selection_required' } })
|
||||
expect(syncMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('skips a send-only transport', async () => {
|
||||
unregister = registerPeppolTransport(makeTransport({ listInboundDocuments: undefined }))
|
||||
const response = await GET(request('cron-secret'))
|
||||
expect(await response.json()).toEqual({ data: { skipped: true, reason: 'receiving_unsupported' } })
|
||||
})
|
||||
|
||||
it('runs the sync with the inbox deliverer and reports the summary', async () => {
|
||||
const transport = makeTransport()
|
||||
unregister = registerPeppolTransport(transport)
|
||||
const response = await GET(request('cron-secret'))
|
||||
const body = await response.json()
|
||||
expect(response.status).toBe(200)
|
||||
expect(body.data).toMatchObject({ listed: 1, delivered: 1 })
|
||||
expect(syncMock).toHaveBeenCalledTimes(1)
|
||||
const args = syncMock.mock.calls[0][0] as { transport: PeppolTransport; deliver: (d: unknown) => unknown }
|
||||
expect(args.transport).toBe(transport)
|
||||
await args.deliver({ row: {}, companyId: 'c', document: {}, xml: null })
|
||||
expect(deliverMock).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
@@ -1,48 +0,0 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { withCronContext } from '@/lib/api/with-cron-context'
|
||||
import { createServiceClientNoCookies } from '@/lib/auth/api-keys'
|
||||
import { ensureInitialized } from '@/lib/init'
|
||||
import { syncInboundPeppolDocuments } from '@/lib/invoices/peppol-inbound'
|
||||
import { deliverPeppolDocumentToInbox } from '@/lib/invoices/peppol-inbox-delivery'
|
||||
import {
|
||||
getPeppolTransport,
|
||||
getPeppolTransportAvailability,
|
||||
} from '@/lib/invoices/peppol-transport'
|
||||
|
||||
ensureInitialized()
|
||||
|
||||
export const maxDuration = 300
|
||||
|
||||
/**
|
||||
* GET /api/peppol/inbound/cron: every 10 minutes.
|
||||
*
|
||||
* Pulls the documents the Access Point received for our registered
|
||||
* participants, archives the exact XML, routes each to its company and hands
|
||||
* it to the supplier-invoice inbox. One provider account carries every
|
||||
* company's identifier, so this is one poll for all of them; a document
|
||||
* nobody is registered for is kept as `unrouted`, never dropped.
|
||||
*
|
||||
* Truthful no-op when no access point is switched on in this environment.
|
||||
*/
|
||||
export const GET = withCronContext('cron.peppol_inbound', async (_request, ctx) => {
|
||||
const availability = getPeppolTransportAvailability()
|
||||
const transport = availability.available ? getPeppolTransport(availability.provider) : null
|
||||
if (!transport) {
|
||||
return NextResponse.json({ data: { skipped: true, reason: availability.available ? 'provider_adapter_unavailable' : availability.reason } })
|
||||
}
|
||||
if (!transport.listInboundDocuments) {
|
||||
return NextResponse.json({ data: { skipped: true, reason: 'receiving_unsupported' } })
|
||||
}
|
||||
|
||||
const service = createServiceClientNoCookies()
|
||||
const summary = await syncInboundPeppolDocuments({
|
||||
service,
|
||||
transport,
|
||||
deliver: (delivery) => deliverPeppolDocumentToInbox(service, delivery),
|
||||
log: ctx.log,
|
||||
})
|
||||
ctx.log.info('peppol inbound sync complete', { ...summary, errors: summary.errors.length })
|
||||
return NextResponse.json({ data: summary })
|
||||
})
|
||||
|
||||
export const POST = GET
|
||||
@@ -1,73 +0,0 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { registerPeppolTransport, type PeppolTransport } from '@/lib/invoices/peppol-transport'
|
||||
|
||||
const pollMock = vi.fn()
|
||||
|
||||
vi.mock('@/lib/init', () => ({ ensureInitialized: vi.fn() }))
|
||||
vi.mock('@/lib/auth/api-keys', () => ({
|
||||
createServiceClientNoCookies: () => ({ from: vi.fn() }),
|
||||
}))
|
||||
vi.mock('@/lib/invoices/peppol-delivery-sync', () => ({
|
||||
pollOpenPeppolDeliveries: (...args: unknown[]) => pollMock(...args),
|
||||
}))
|
||||
|
||||
import { GET } from '../route'
|
||||
|
||||
function request(secret: string | null): Request {
|
||||
return new Request('http://localhost:3000/api/peppol/outbound/status/cron', {
|
||||
headers: secret ? { authorization: `Bearer ${secret}` } : {},
|
||||
})
|
||||
}
|
||||
|
||||
function makeTransport(overrides: Partial<PeppolTransport> = {}): PeppolTransport {
|
||||
return {
|
||||
provider: 'qvalia',
|
||||
lookupRecipient: vi.fn(),
|
||||
submit: vi.fn(),
|
||||
verifyWebhook: vi.fn(),
|
||||
retrieveEvidence: vi.fn(),
|
||||
pollDeliveryStatus: vi.fn().mockResolvedValue([]),
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe('GET /api/peppol/outbound/status/cron', () => {
|
||||
let unregister: (() => void) | null = null
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
process.env.CRON_SECRET = 'cron-secret'
|
||||
process.env.PEPPOL_TRANSPORT_PROVIDER = 'qvalia'
|
||||
pollMock.mockResolvedValue({ polled: 2, advanced: 1, unchanged: 1, failed: 0, errors: [] })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
unregister?.()
|
||||
unregister = null
|
||||
delete process.env.PEPPOL_TRANSPORT_PROVIDER
|
||||
delete process.env.CRON_SECRET
|
||||
})
|
||||
|
||||
it('rejects a call without the cron secret', async () => {
|
||||
unregister = registerPeppolTransport(makeTransport())
|
||||
expect((await GET(request(null))).status).toBe(401)
|
||||
expect(pollMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('is a truthful no-op without an access point or without polling support', async () => {
|
||||
delete process.env.PEPPOL_TRANSPORT_PROVIDER
|
||||
expect(await (await GET(request('cron-secret'))).json()).toEqual({ data: { skipped: true, reason: 'provider_selection_required' } })
|
||||
process.env.PEPPOL_TRANSPORT_PROVIDER = 'qvalia'
|
||||
unregister = registerPeppolTransport(makeTransport({ pollDeliveryStatus: undefined }))
|
||||
expect(await (await GET(request('cron-secret'))).json()).toEqual({ data: { skipped: true, reason: 'polling_unsupported' } })
|
||||
})
|
||||
|
||||
it('polls the open deliveries and reports the summary', async () => {
|
||||
const transport = makeTransport()
|
||||
unregister = registerPeppolTransport(transport)
|
||||
const response = await GET(request('cron-secret'))
|
||||
expect(response.status).toBe(200)
|
||||
expect((await response.json()).data).toMatchObject({ polled: 2, advanced: 1 })
|
||||
expect((pollMock.mock.calls[0][0] as { transport: PeppolTransport }).transport).toBe(transport)
|
||||
})
|
||||
})
|
||||
@@ -1,42 +0,0 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { withCronContext } from '@/lib/api/with-cron-context'
|
||||
import { createServiceClientNoCookies } from '@/lib/auth/api-keys'
|
||||
import { ensureInitialized } from '@/lib/init'
|
||||
import { pollOpenPeppolDeliveries } from '@/lib/invoices/peppol-delivery-sync'
|
||||
import {
|
||||
getPeppolTransport,
|
||||
getPeppolTransportAvailability,
|
||||
} from '@/lib/invoices/peppol-transport'
|
||||
|
||||
ensureInitialized()
|
||||
|
||||
export const maxDuration = 300
|
||||
|
||||
/**
|
||||
* GET /api/peppol/outbound/status/cron: four times an hour.
|
||||
*
|
||||
* Asks the Access Point about every open outbound delivery and records what
|
||||
* it says through the same append-only lifecycle a webhook uses. Needed
|
||||
* because Qvalia's webhooks are not available on its production host yet, and
|
||||
* kept afterwards as the safety net for a missed webhook.
|
||||
*/
|
||||
export const GET = withCronContext('cron.peppol_outbound_status', async (_request, ctx) => {
|
||||
const availability = getPeppolTransportAvailability()
|
||||
const transport = availability.available ? getPeppolTransport(availability.provider) : null
|
||||
if (!transport) {
|
||||
return NextResponse.json({ data: { skipped: true, reason: availability.available ? 'provider_adapter_unavailable' : availability.reason } })
|
||||
}
|
||||
if (!transport.pollDeliveryStatus) {
|
||||
return NextResponse.json({ data: { skipped: true, reason: 'polling_unsupported' } })
|
||||
}
|
||||
|
||||
const summary = await pollOpenPeppolDeliveries({
|
||||
service: createServiceClientNoCookies(),
|
||||
transport,
|
||||
log: ctx.log,
|
||||
})
|
||||
ctx.log.info('peppol outbound status poll complete', { ...summary, errors: summary.errors.length })
|
||||
return NextResponse.json({ data: summary })
|
||||
})
|
||||
|
||||
export const POST = GET
|
||||
Reference in New Issue
Block a user