* fix(salary): align pain.001 salary file with the Swedish domestic bank dialect Verified against the Swedish Common Interpretation of ISO 20022 (Bankforeningen, Common Payment Types in Sweden, Appendix 1 Example 4: Salaries) and Nordea Corporate Access pain.001 examples v2.6 (2026-06-22), and XSD-validated against the official pain.001.001.03 schema: - drop SvcLvl SEPA (SEPA credit transfers are EUR-only; omitting SvcLvl gets the domestic NURG default) - drop RmtInf (not allowed for SALA salary payments; the beneficiary statement text comes from the Dataclearing LON code) - address employees domestically: clearing as CdtrAgt ClrSysMmbId SESBA, account WITHOUT clearing as CdtrAcct Othr with SchmeNm BBAN - share the clearing/account split (Swedbank 5-digit shift, Nordea personkonto prefix dedup) between the LB and pain.001 generators via splitDomesticBankAccount, fixing pain.001 duplicating the personkonto clearing - clamp MsgId/PmtInfId/InstrId/EndToEndId to Max35Text with the per-tx counter surviving truncation; carry the org number on Dbtr - return 400 from the pain001 route on an invalid clearing instead of emitting a broken file Also includes two unrelated decision-log lines from the parallel revisor-review session (DECISIONS.md is a shared append-only log). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(nav): surface the year-end chain in the sidebar Add Periodiseringar, Arsredovisning (aktiebolag only) and Inkomstdeklaration (INK2 for AB, NE-bilaga for EF) to the Skatt & bokslut group, in workflow order. Entity gating via a new entityOnly flag on NavItem; isActive carve-outs extended so exactly one row lights up for the new routes. Driven by an external revisor review that concluded these features did not exist because none of them were reachable from the nav. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(stripe): Stripe Connect integration behind config gate Connect OAuth per company (only the acct_ id is stored), automatic single-use Payment Links on invoice send, deterministic payment settlement against 1686 (BAS moved acquirer receivables 1580 -> 1686), payout booking with reverse-charge fees (6570 + 4535/4598 + 2645/2614), and a 15-minute sync cron. Non-deterministic events land as needs_review, never guessed at. Fully dark without STRIPE_CONNECT_CLIENT_ID: connect returns 503, the send hook and cron no-op, and the settings page shows 'Kommer snart' (hosted) until the Connect platform is verified. Self-hosted keeps the honest not-configured message. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(deadlines): add shared completeTaxDeadline and fix dead AGI deadline auto-complete generate-declaration.ts has updated non-existent columns (type/period/ status) since inception, so the arbetsgivardeklaration deadline was never auto-completed. Replace with a shared helper targeting the real schema (tax_deadline_type/tax_period/is_completed), also used by the kvittens crons and moms handlers in the follow-up commit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(rot-rut): import Skatteverket beslutsfil and record decisions on payout requests Parse the beslutsfil JSON from Skatteverkets rot/rut e-tjanst and record godkant belopp on the matching begaran: matched by stored skv_referensnummer first, then exact name among active undecided requests; arenden by fakturanummer then personnummer, exactly-one or the beslut errors (all-or-nothing). Never auto-settles: recording the beslut and booking the payout are separate acts. Exposed as an API route and the gnubok_import_rot_rut_beslut MCP tool. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(skatteverket): system auth for background reads, one-click VAT submit, kvittens notifications Hybrid auth program: system CCG (org certificate) for background reads while personal BankID stays for interactive submissions, since SKV per-flow refresh tokens live 65 min and crons structurally cannot run on them. All system-auth code sits behind SKATTEVERKET_SYSTEM_AUTH_MODE (default off) with a stub transport until the Expisoft cert and CCG avtal land; auth resolution is centralized in resolve-auth.ts. Also in this change: - One-click VAT submit chaining kontrollera -> utkast -> las server-side with a stage discriminator; step-by-step buttons demoted to the overflow menu. - Kvittens crons (AGI + new VAT schedule) with email-only notifications, deduped in notification_log under the new skv_kvittens type. - Ombud grant probe + verification UI in the connect panel, and a dashboard promo card for unconnected companies. - skatteverket_company_connections table with pg-real coverage. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(salary): auto-settle AGI tax payment from skattekonto and surface SKV reconnect on the tax card The "Skatt att betala" card only cleared via the manual mark-paid button on the run detail page; the promised automatic flip from the Skattekonto sync was never implemented, so paid periods stayed red. - settleAgiTaxPayments: during every skattekonto sync, a booked "Arbetsgivardeklaration YYYYMM" debit row settles the matching agi_declarations.tax_paid_at, but only when the amount equals the declared total to the ore and the account is not in deficit (deterministic; drift or deficit falls back to manual). - Salary overview card: reconnect hint when the SKV token needs re-consent (link to /settings/tax, silent when the extension is off), plus an inline "Markera som betald" button reusing the existing endpoint and salary_payments strings. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Add cloud backup scheduling and alerting features - Implement unit tests for scheduling logic in `schedule.test.ts`, covering various scenarios for determining if a backup schedule is due. - Create a new module `backup-alert.ts` to handle failure alerts for cloud backup auto-sync, including email notifications for reauthentication and repeated failures. - Introduce `schedule.ts` to manage scheduling logic, including handling local time zones and converting between local and UTC hours. - Add CSV report generation functions in `archive-csv.ts` for trial balance, income statement, balance sheet, and general ledger, ensuring compatibility with Swedish Excel formats. - Create a README generator for the archive structure in `archive-readme.ts`, providing clear documentation for users accessing backup files. - Implement tests for CSV report generation in `archive-csv.test.ts`, ensuring correct formatting and content. - Establish a full-archive coverage contract test in `full-archive-coverage.pg.test.ts` to ensure all company-scoped tables are properly classified for backup. * fix(stripe): correct invoice clearing reference and improve type safety in sync logic * fix(invoices): narrow accountingMethod before resolveInvoicePaymentSourceType settleInvoicePayment takes accountingMethod as a raw settings string, but resolveInvoicePaymentSourceType requires the 'accrual' | 'cash' union. Normalize at the call site (anything but 'cash' books as accrual), matching the existing useCashEntry semantics. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: address CodeRabbit review findings and nitpicks on PR #1004 Review findings: - backup settings redirect: always force view=export over incoming params - AGI/VAT kvittens crons: isolate best-effort post-submit calls, check the signed-state persist error, guard recovery calls in catch blocks so one company cannot abort the rest; surface grant_revoked in the run summary - kvittens notifications: atomic claim-first dedup with a partial unique index; map non-uuid reference keys to deterministic uuids - grant probe: record the actual 2xx status; mTLS transport: handle response-stream errors - stripe: amount-aware idempotency keys for payment links; emit stripe.disconnected on upstream revocations - ROT/RUT beslut import: mutate in-memory request state after apply, move item + header writes into an atomic apply_rot_rut_beslut RPC, add rot_rut_payout to JournalEntrySourceTypeSchema - migrations: use NOT VALID + VALIDATE CONSTRAINT for CHECK constraints on journal_entries, notification_log and rot_rut_payout_requests - cloud backup: hour_utc-only schedule updates clear stale hour_local Nitpicks: - stripe sync: enforce the cron time budget inside per-connection event processing with idempotent cursor progress; maybeSingle for settings; honest partial-customer DTO shared with the settlement boundary - shared applyPaymentLinkToInvoice helper for both invoice send routes, v1 docblock documents step 6b and PAYMENT_LINK_FAILED - settings panel: drop redundant decodeURIComponent - cloud backup: document worst-case archive memory headroom Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
674 lines
24 KiB
TypeScript
674 lines
24 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import JSZip from 'jszip'
|
|
import {
|
|
generateFullArchive,
|
|
generateBaseDataArchive,
|
|
estimateArchiveSize,
|
|
MASTER_DATA_DUMP_TABLES,
|
|
} from '../full-archive-export'
|
|
import { createQueuedMockSupabase } from '@/tests/helpers'
|
|
import { getAuditLog } from '@/lib/core/audit/audit-service'
|
|
|
|
vi.mock('../sie-export', () => ({
|
|
generateSIEExport: vi.fn().mockResolvedValue('#FLAGGA 0\n#PROGRAM "ERPBase"'),
|
|
}))
|
|
|
|
vi.mock('../trial-balance', () => ({
|
|
generateTrialBalance: vi.fn().mockResolvedValue({
|
|
rows: [], totalDebit: 0, totalCredit: 0, isBalanced: true,
|
|
}),
|
|
}))
|
|
|
|
vi.mock('../income-statement', () => ({
|
|
generateIncomeStatement: vi.fn().mockResolvedValue({
|
|
revenue_sections: [], total_revenue: 0,
|
|
expense_sections: [], total_expenses: 0,
|
|
financial_sections: [], total_financial: 0,
|
|
net_result: 0, period: { start: '2024-01-01', end: '2024-12-31' },
|
|
}),
|
|
}))
|
|
|
|
vi.mock('../balance-sheet', () => ({
|
|
generateBalanceSheet: vi.fn().mockResolvedValue({
|
|
asset_sections: [], equity_liability_sections: [],
|
|
total_assets: 0, total_equity_liabilities: 0,
|
|
period: { start: '2024-01-01', end: '2024-12-31' },
|
|
}),
|
|
}))
|
|
|
|
vi.mock('../general-ledger', () => ({
|
|
generateGeneralLedger: vi.fn().mockResolvedValue({
|
|
accounts: [], period: { start: '2024-01-01', end: '2024-12-31' },
|
|
}),
|
|
}))
|
|
|
|
vi.mock('../journal-register', () => ({
|
|
generateJournalRegister: vi.fn().mockResolvedValue({
|
|
entries: [], total_entries: 0, total_debit: 0, total_credit: 0,
|
|
period: { start: '2024-01-01', end: '2024-12-31' },
|
|
}),
|
|
}))
|
|
|
|
vi.mock('../vat-declaration', () => ({
|
|
calculateVatDeclaration: vi.fn().mockResolvedValue({
|
|
period: { type: 'yearly', year: 2024, period: 1, start: '2024-01-01', end: '2024-12-31' },
|
|
rutor: {
|
|
ruta05: 0, ruta06: 0, ruta07: 0,
|
|
ruta10: 0, ruta11: 0, ruta12: 0,
|
|
ruta39: 0, ruta40: 0, ruta48: 0, ruta49: 0,
|
|
},
|
|
invoiceCount: 0, transactionCount: 0,
|
|
breakdown: {
|
|
invoices: { ruta05: 0, ruta06: 0, ruta07: 0, ruta10: 0, ruta11: 0, ruta12: 0, ruta39: 0, ruta40: 0, base25: 0, base12: 0, base6: 0 },
|
|
transactions: { ruta48: 0 },
|
|
receipts: { ruta48: 0 },
|
|
},
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/lib/core/audit/audit-service', () => ({
|
|
getAuditLog: vi.fn().mockResolvedValue({ data: [], count: 0 }),
|
|
}))
|
|
|
|
const mockGetAuditLog = vi.mocked(getAuditLog)
|
|
|
|
const COMPANY_ROW = {
|
|
company_name: 'Test AB',
|
|
org_number: '5566778899',
|
|
moms_period: 'quarterly',
|
|
}
|
|
|
|
const PERIOD_2024 = {
|
|
id: 'period-2024',
|
|
period_start: '2024-01-01',
|
|
period_end: '2024-12-31',
|
|
opening_balance_entry_id: null,
|
|
}
|
|
|
|
const PERIOD_2023 = {
|
|
id: 'period-2023',
|
|
period_start: '2023-01-01',
|
|
period_end: '2023-12-31',
|
|
opening_balance_entry_id: null,
|
|
}
|
|
|
|
describe('generateFullArchive', () => {
|
|
let supabase: ReturnType<typeof createQueuedMockSupabase>['supabase']
|
|
let enqueueMany: ReturnType<typeof createQueuedMockSupabase>['enqueueMany']
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockGetAuditLog.mockResolvedValue({ data: [], count: 0 })
|
|
const mock = createQueuedMockSupabase()
|
|
supabase = mock.supabase
|
|
enqueueMany = mock.enqueueMany
|
|
})
|
|
|
|
describe('scope: period', () => {
|
|
it('generates a ZIP with expected file structure', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW }, // company_settings
|
|
{ data: PERIOD_2024 }, // fiscal_periods (single)
|
|
{ data: [] }, // document_attachments
|
|
])
|
|
|
|
const buffer = await generateFullArchive(supabase as any, 'company-1', {
|
|
scope: 'period',
|
|
period_id: PERIOD_2024.id,
|
|
})
|
|
|
|
const zip = await JSZip.loadAsync(buffer)
|
|
|
|
expect(zip.file('bokforing.se')).not.toBeNull()
|
|
expect(zip.file('rapporter/saldobalans.json')).not.toBeNull()
|
|
expect(zip.file('rapporter/resultatrakning.json')).not.toBeNull()
|
|
expect(zip.file('rapporter/balansrakning.json')).not.toBeNull()
|
|
expect(zip.file('rapporter/huvudbok.json')).not.toBeNull()
|
|
expect(zip.file('rapporter/grundbok.json')).not.toBeNull()
|
|
expect(zip.file('rapporter/momsdeklaration.json')).not.toBeNull()
|
|
expect(zip.file('dokument/manifest.json')).not.toBeNull()
|
|
expect(zip.file('revision/behandlingshistorik.json')).not.toBeNull()
|
|
expect(zip.file('revision/systemdokumentation.json')).not.toBeNull()
|
|
// Human-readable layer: CSV twins + the Swedish README.
|
|
expect(zip.file('rapporter/saldobalans.csv')).not.toBeNull()
|
|
expect(zip.file('rapporter/resultatrakning.csv')).not.toBeNull()
|
|
expect(zip.file('rapporter/balansrakning.csv')).not.toBeNull()
|
|
expect(zip.file('rapporter/huvudbok.csv')).not.toBeNull()
|
|
const readme = zip.file('LÄSMIG.txt')
|
|
expect(readme).not.toBeNull()
|
|
const readmeText = await readme!.async('text')
|
|
expect(readmeText).toContain('Test AB')
|
|
expect(readmeText).toContain('Räkenskapsår')
|
|
})
|
|
|
|
it('handles missing documents gracefully', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW },
|
|
{ data: PERIOD_2024 },
|
|
{
|
|
data: [
|
|
{
|
|
id: 'doc-1',
|
|
file_name: 'receipt.pdf',
|
|
storage_path: 'documents/user-1/receipt.pdf',
|
|
journal_entry_id: 'entry-1',
|
|
journal_entries: {
|
|
voucher_number: 17,
|
|
voucher_series: 'A',
|
|
entry_date: '2024-03-15',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{ data: [{ id: 'entry-1', fiscal_period_id: PERIOD_2024.id }] },
|
|
])
|
|
|
|
supabase.storage.from = vi.fn().mockReturnValue({
|
|
download: vi.fn().mockResolvedValue({
|
|
data: null,
|
|
error: { message: 'File not found' },
|
|
}),
|
|
})
|
|
|
|
const buffer = await generateFullArchive(supabase as any, 'company-1', {
|
|
scope: 'period',
|
|
period_id: PERIOD_2024.id,
|
|
})
|
|
|
|
const zip = await JSZip.loadAsync(buffer)
|
|
const manifestFile = zip.file('dokument/manifest.json')
|
|
expect(manifestFile).not.toBeNull()
|
|
|
|
const manifest = JSON.parse(await manifestFile!.async('text'))
|
|
expect(manifest).toHaveLength(1)
|
|
expect(manifest[0].status).toBe('error')
|
|
expect(manifest[0].error).toBe('File not found')
|
|
expect(manifest[0].fiscal_period_id).toBe(PERIOD_2024.id)
|
|
// New manifest fields populated even on error (path is computed before download)
|
|
expect(manifest[0].voucher_number).toBe('A17')
|
|
expect(manifest[0].entry_date).toBe('2024-03-15')
|
|
expect(manifest[0].zip_path).toBe('dokument/2024/A17_receipt.pdf')
|
|
})
|
|
|
|
it('skips documents when include_documents is false', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW },
|
|
{ data: PERIOD_2024 },
|
|
])
|
|
|
|
const buffer = await generateFullArchive(supabase as any, 'company-1', {
|
|
scope: 'period',
|
|
period_id: PERIOD_2024.id,
|
|
include_documents: false,
|
|
})
|
|
|
|
const zip = await JSZip.loadAsync(buffer)
|
|
|
|
expect(zip.file('dokument/manifest.json')).toBeNull()
|
|
expect(zip.file('bokforing.se')).not.toBeNull()
|
|
expect(zip.file('revision/behandlingshistorik.json')).not.toBeNull()
|
|
})
|
|
|
|
it('throws when fiscal period not found', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW },
|
|
{ data: null },
|
|
])
|
|
|
|
await expect(
|
|
generateFullArchive(supabase as any, 'company-1', {
|
|
scope: 'period',
|
|
period_id: 'nonexistent',
|
|
})
|
|
).rejects.toThrow('Fiscal period not found')
|
|
})
|
|
|
|
it('filters audit trail by period dates', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW },
|
|
{ data: PERIOD_2024 },
|
|
{ data: [] },
|
|
])
|
|
|
|
await generateFullArchive(supabase as any, 'company-1', {
|
|
scope: 'period',
|
|
period_id: PERIOD_2024.id,
|
|
})
|
|
|
|
expect(mockGetAuditLog).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
'company-1',
|
|
expect.objectContaining({
|
|
from_date: PERIOD_2024.period_start,
|
|
to_date: `${PERIOD_2024.period_end}T23:59:59.999Z`,
|
|
})
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('scope: all', () => {
|
|
it('generates per-period SIE files and report subfolders', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW },
|
|
{ data: [PERIOD_2023, PERIOD_2024] }, // fiscal_periods (list for fetchAllPeriods)
|
|
{ data: [] }, // document_attachments
|
|
])
|
|
|
|
const buffer = await generateFullArchive(supabase as any, 'company-1', {
|
|
scope: 'all',
|
|
})
|
|
|
|
const zip = await JSZip.loadAsync(buffer)
|
|
|
|
expect(zip.file('sie/2023-01-01_2023-12-31.se')).not.toBeNull()
|
|
expect(zip.file('sie/2024-01-01_2024-12-31.se')).not.toBeNull()
|
|
expect(zip.file('rapporter/2023-01-01_2023-12-31/saldobalans.json')).not.toBeNull()
|
|
expect(zip.file('rapporter/2024-01-01_2024-12-31/saldobalans.json')).not.toBeNull()
|
|
expect(zip.file('rapporter/2024-01-01_2024-12-31/saldobalans.csv')).not.toBeNull()
|
|
expect(zip.file('rapporter/2024-01-01_2024-12-31/huvudbok.csv')).not.toBeNull()
|
|
expect(zip.file('revision/behandlingshistorik.json')).not.toBeNull()
|
|
expect(zip.file('revision/systemdokumentation.json')).not.toBeNull()
|
|
const readmeText = await zip.file('LÄSMIG.txt')!.async('text')
|
|
expect(readmeText).toContain('Hela bokföringen')
|
|
// No root bokforing.se in all-mode
|
|
expect(zip.file('bokforing.se')).toBeNull()
|
|
})
|
|
|
|
it('does not filter audit trail by date in all-mode', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW },
|
|
{ data: [PERIOD_2024] },
|
|
{ data: [] },
|
|
])
|
|
|
|
await generateFullArchive(supabase as any, 'company-1', { scope: 'all' })
|
|
|
|
const call = mockGetAuditLog.mock.calls[0]
|
|
expect(call[2]).not.toHaveProperty('from_date')
|
|
expect(call[2]).not.toHaveProperty('to_date')
|
|
})
|
|
|
|
it('tags each document with its fiscal_period_id across periods', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW },
|
|
{ data: [PERIOD_2023, PERIOD_2024] },
|
|
{
|
|
data: [
|
|
{
|
|
id: 'doc-2023',
|
|
file_name: 'r23.pdf',
|
|
storage_path: 'p/r23.pdf',
|
|
journal_entry_id: 'e-2023',
|
|
journal_entries: { voucher_number: 7, voucher_series: 'A', entry_date: '2023-06-01' },
|
|
},
|
|
{
|
|
id: 'doc-2024',
|
|
file_name: 'r24.pdf',
|
|
storage_path: 'p/r24.pdf',
|
|
journal_entry_id: 'e-2024',
|
|
journal_entries: { voucher_number: 12, voucher_series: 'B', entry_date: '2024-08-20' },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
data: [
|
|
{ id: 'e-2023', fiscal_period_id: PERIOD_2023.id },
|
|
{ id: 'e-2024', fiscal_period_id: PERIOD_2024.id },
|
|
],
|
|
},
|
|
])
|
|
|
|
const buffer = await generateFullArchive(supabase as any, 'company-1', {
|
|
scope: 'all',
|
|
})
|
|
|
|
const zip = await JSZip.loadAsync(buffer)
|
|
const manifestFile = zip.file('dokument/manifest.json')
|
|
expect(manifestFile).not.toBeNull()
|
|
|
|
const manifest = JSON.parse(await manifestFile!.async('text'))
|
|
expect(manifest).toHaveLength(2)
|
|
const byId = Object.fromEntries(
|
|
(
|
|
manifest as Array<{
|
|
document_id: string
|
|
fiscal_period_id: string | null
|
|
voucher_number: string | null
|
|
zip_path: string | null
|
|
}>
|
|
).map((m) => [m.document_id, m])
|
|
)
|
|
expect(byId['doc-2023'].fiscal_period_id).toBe(PERIOD_2023.id)
|
|
expect(byId['doc-2024'].fiscal_period_id).toBe(PERIOD_2024.id)
|
|
expect(byId['doc-2023'].voucher_number).toBe('A7')
|
|
expect(byId['doc-2024'].voucher_number).toBe('B12')
|
|
expect(byId['doc-2023'].zip_path).toBe('dokument/2023/A7_r23.pdf')
|
|
expect(byId['doc-2024'].zip_path).toBe('dokument/2024/B12_r24.pdf')
|
|
|
|
// Files actually written under the new path
|
|
expect(zip.file('dokument/2023/A7_r23.pdf')).not.toBeNull()
|
|
expect(zip.file('dokument/2024/B12_r24.pdf')).not.toBeNull()
|
|
})
|
|
|
|
it('routes draft entries and orphans to dokument/_okopplade and disambiguates collisions', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW },
|
|
{ data: [PERIOD_2024] },
|
|
{
|
|
data: [
|
|
// True orphan: uploaded but never linked to any entry. Backups
|
|
// must still carry it (inbox items are räkenskapsinformation).
|
|
{
|
|
id: 'doc-orphan',
|
|
file_name: 'inbox.pdf',
|
|
storage_path: 'p/inbox.pdf',
|
|
journal_entry_id: null,
|
|
journal_entries: null,
|
|
},
|
|
// Draft entry: journal_entry_id present but voucher_number is null
|
|
{
|
|
id: 'doc-draft',
|
|
file_name: 'invoice.pdf',
|
|
storage_path: 'p/invoice.pdf',
|
|
journal_entry_id: 'e-draft',
|
|
journal_entries: { voucher_number: null, voucher_series: 'A', entry_date: null },
|
|
},
|
|
// Two posted docs that collide on the same voucher+filename
|
|
{
|
|
id: 'doc-collide-1234abcd-ee',
|
|
file_name: 'kvitto.pdf',
|
|
storage_path: 'p/kvitto-1.pdf',
|
|
journal_entry_id: 'e-posted',
|
|
journal_entries: { voucher_number: 5, voucher_series: 'A', entry_date: '2024-05-01' },
|
|
},
|
|
{
|
|
id: 'doc-collide-5678efgh-ff',
|
|
file_name: 'kvitto.pdf',
|
|
storage_path: 'p/kvitto-2.pdf',
|
|
journal_entry_id: 'e-posted',
|
|
journal_entries: { voucher_number: 5, voucher_series: 'A', entry_date: '2024-05-01' },
|
|
},
|
|
],
|
|
},
|
|
// entryIdToPeriodId map: draft and posted both resolve to PERIOD_2024
|
|
{
|
|
data: [
|
|
{ id: 'e-draft', fiscal_period_id: PERIOD_2024.id },
|
|
{ id: 'e-posted', fiscal_period_id: PERIOD_2024.id },
|
|
],
|
|
},
|
|
])
|
|
|
|
const buffer = await generateFullArchive(supabase as any, 'company-1', {
|
|
scope: 'all',
|
|
})
|
|
|
|
const zip = await JSZip.loadAsync(buffer)
|
|
const manifest = JSON.parse(await zip.file('dokument/manifest.json')!.async('text')) as Array<{
|
|
document_id: string
|
|
voucher_number: string | null
|
|
zip_path: string | null
|
|
}>
|
|
const byId = Object.fromEntries(manifest.map((m) => [m.document_id, m]))
|
|
|
|
// Unlinked orphan -> _okopplade, included in the backup
|
|
expect(byId['doc-orphan'].voucher_number).toBeNull()
|
|
expect(byId['doc-orphan'].zip_path).toBe('dokument/_okopplade/inbox.pdf')
|
|
|
|
// Draft -> _okopplade, no voucher prefix
|
|
expect(byId['doc-draft'].voucher_number).toBeNull()
|
|
expect(byId['doc-draft'].zip_path).toBe('dokument/_okopplade/invoice.pdf')
|
|
|
|
// First posted doc gets the canonical path
|
|
expect(byId['doc-collide-1234abcd-ee'].zip_path).toBe('dokument/2024/A5_kvitto.pdf')
|
|
// Second posted doc gets the id-suffix disambiguation before the extension
|
|
expect(byId['doc-collide-5678efgh-ff'].zip_path).toBe(
|
|
'dokument/2024/A5_kvitto_doc-coll.pdf'
|
|
)
|
|
|
|
// All files exist in the ZIP
|
|
expect(zip.file('dokument/_okopplade/inbox.pdf')).not.toBeNull()
|
|
expect(zip.file('dokument/_okopplade/invoice.pdf')).not.toBeNull()
|
|
expect(zip.file('dokument/2024/A5_kvitto.pdf')).not.toBeNull()
|
|
expect(zip.file('dokument/2024/A5_kvitto_doc-coll.pdf')).not.toBeNull()
|
|
})
|
|
|
|
it('throws when no fiscal periods exist', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW },
|
|
{ data: [] },
|
|
])
|
|
|
|
await expect(
|
|
generateFullArchive(supabase as any, 'company-1', { scope: 'all' })
|
|
).rejects.toThrow('No fiscal periods found')
|
|
})
|
|
|
|
it('includes imported SIE source files and master-data dumps in all-mode', async () => {
|
|
const importRow = {
|
|
id: 'import-1',
|
|
filename: 'original.se',
|
|
file_hash: 'abc123',
|
|
file_storage_path: 'company-1/import-1.se',
|
|
org_number: '5560000000',
|
|
company_name: 'Test AB',
|
|
sie_type: 4,
|
|
fiscal_year_start: '2024-01-01',
|
|
fiscal_year_end: '2024-12-31',
|
|
accounts_count: 42,
|
|
transactions_count: 120,
|
|
status: 'completed',
|
|
fiscal_period_id: PERIOD_2024.id,
|
|
imported_at: '2024-11-01T10:00:00Z',
|
|
created_at: '2024-11-01T09:55:00Z',
|
|
}
|
|
|
|
// Master-data dump runs sequentially over MASTER_DATA_DUMP_TABLES.
|
|
// Direct tables issue one query; via-tables issue a parent-id query and,
|
|
// when parents exist, one chunked child query.
|
|
const masterDataQueue = MASTER_DATA_DUMP_TABLES.flatMap((t): { data: unknown }[] => {
|
|
if (t.name === 'customers') {
|
|
return [{ data: [{ id: 'cust-1', name: 'Acme AB' }] }]
|
|
}
|
|
if (t.name === 'invoice_items') {
|
|
return [
|
|
{ data: [{ id: 'inv-1' }] }, // parent ids (invoices)
|
|
{ data: [{ id: 'item-1', invoice_id: 'inv-1', description: 'Konsulttid' }] },
|
|
]
|
|
}
|
|
if (t.via) return [{ data: [] }] // no parents -> no child query
|
|
if (t.name === 'company_settings') return [{ data: [COMPANY_ROW] }]
|
|
return [{ data: [] }]
|
|
})
|
|
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW }, // fetchCompany
|
|
{ data: [PERIOD_2024] }, // fetchAllPeriods
|
|
{ data: [] }, // document_attachments
|
|
{ data: [importRow] }, // sie_imports
|
|
{ data: [{ source_account: '9999', target_account: '1510' }] }, // sie_account_mappings
|
|
...masterDataQueue,
|
|
])
|
|
|
|
const buffer = await generateFullArchive(supabase as any, 'company-1', {
|
|
scope: 'all',
|
|
})
|
|
const zip = await JSZip.loadAsync(buffer)
|
|
|
|
const originalFile = zip.file('sie/original/import-1_original.se')
|
|
expect(originalFile).not.toBeNull()
|
|
|
|
const manifestFile = zip.file('sie/original/manifest.json')
|
|
expect(manifestFile).not.toBeNull()
|
|
const manifest = JSON.parse(await manifestFile!.async('text'))
|
|
expect(manifest[0].import_id).toBe('import-1')
|
|
expect(manifest[0].status).toBe('downloaded')
|
|
|
|
const imports = JSON.parse(await zip.file('sie/imports.json')!.async('text'))
|
|
expect(imports[0].filename).toBe('original.se')
|
|
expect(zip.file('sie/account_mappings.json')).not.toBeNull()
|
|
|
|
// Every table in the dump contract gets a file, even when empty.
|
|
for (const t of MASTER_DATA_DUMP_TABLES) {
|
|
expect(zip.file(`data/${t.file}`), `data/${t.file}`).not.toBeNull()
|
|
}
|
|
|
|
const customers = JSON.parse(await zip.file('data/customers.json')!.async('text'))
|
|
expect(customers).toEqual([{ id: 'cust-1', name: 'Acme AB' }])
|
|
|
|
// Child table fetched via parent ids (invoice_items has no company_id).
|
|
const items = JSON.parse(await zip.file('data/invoice_items.json')!.async('text'))
|
|
expect(items).toEqual([
|
|
{ id: 'item-1', invoice_id: 'inv-1', description: 'Konsulttid' },
|
|
])
|
|
})
|
|
|
|
it('skips raw SIE blobs when include_documents is false but keeps metadata', async () => {
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW },
|
|
{ data: [PERIOD_2024] },
|
|
{
|
|
data: [
|
|
{
|
|
id: 'import-1',
|
|
filename: 'x.se',
|
|
file_hash: 'h',
|
|
file_storage_path: 'company-1/import-1.se',
|
|
status: 'completed',
|
|
imported_at: '2024-11-01T10:00:00Z',
|
|
created_at: '2024-11-01T09:55:00Z',
|
|
},
|
|
],
|
|
}, // sie_imports
|
|
{ data: [] }, // sie_account_mappings
|
|
])
|
|
|
|
const buffer = await generateFullArchive(supabase as any, 'company-1', {
|
|
scope: 'all',
|
|
include_documents: false,
|
|
})
|
|
const zip = await JSZip.loadAsync(buffer)
|
|
|
|
expect(zip.file('sie/imports.json')).not.toBeNull()
|
|
expect(zip.file('sie/account_mappings.json')).not.toBeNull()
|
|
expect(zip.file('sie/original/import-1_x.se')).toBeNull()
|
|
expect(zip.file('sie/original/manifest.json')).toBeNull()
|
|
expect(zip.file('data/customers.json')).not.toBeNull()
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('generateBaseDataArchive', () => {
|
|
let supabase: ReturnType<typeof createQueuedMockSupabase>['supabase']
|
|
let enqueueMany: ReturnType<typeof createQueuedMockSupabase>['enqueueMany']
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockGetAuditLog.mockResolvedValue({ data: [], count: 0 })
|
|
const mock = createQueuedMockSupabase()
|
|
supabase = mock.supabase
|
|
enqueueMany = mock.enqueueMany
|
|
})
|
|
|
|
it('bundles unlinked documents, master data, SIE sources and the audit trail', async () => {
|
|
const masterDataQueue = MASTER_DATA_DUMP_TABLES.flatMap((t): { data: unknown }[] => {
|
|
if (t.name === 'customers') return [{ data: [{ id: 'cust-1', name: 'Acme AB' }] }]
|
|
if (t.via) return [{ data: [] }]
|
|
return [{ data: [] }]
|
|
})
|
|
|
|
enqueueMany([
|
|
{ data: COMPANY_ROW }, // fetchCompany
|
|
{ data: [PERIOD_2024] }, // fetchAllPeriods
|
|
{
|
|
data: [
|
|
// Orphan: goes into Grunddata.
|
|
{
|
|
id: 'doc-orphan',
|
|
file_name: 'inbox.pdf',
|
|
storage_path: 'p/inbox.pdf',
|
|
journal_entry_id: null,
|
|
journal_entries: null,
|
|
},
|
|
// Linked to a posted entry: belongs to the period archive, not here.
|
|
{
|
|
id: 'doc-linked',
|
|
file_name: 'kvitto.pdf',
|
|
storage_path: 'p/kvitto.pdf',
|
|
journal_entry_id: 'e-1',
|
|
journal_entries: { voucher_number: 5, voucher_series: 'A', entry_date: '2024-05-01' },
|
|
},
|
|
],
|
|
}, // document_attachments
|
|
{ data: [{ id: 'e-1', fiscal_period_id: PERIOD_2024.id }] }, // entry->period map
|
|
{ data: [] }, // sie_imports
|
|
{ data: [] }, // sie_account_mappings
|
|
...masterDataQueue,
|
|
])
|
|
|
|
const buffer = await generateBaseDataArchive(supabase as any, 'company-1')
|
|
const zip = await JSZip.loadAsync(buffer)
|
|
|
|
const manifest = JSON.parse(await zip.file('dokument/manifest.json')!.async('text'))
|
|
expect(manifest).toHaveLength(1)
|
|
expect(manifest[0].document_id).toBe('doc-orphan')
|
|
expect(zip.file('dokument/_okopplade/inbox.pdf')).not.toBeNull()
|
|
|
|
expect(zip.file('data/customers.json')).not.toBeNull()
|
|
expect(zip.file('sie/imports.json')).not.toBeNull()
|
|
expect(zip.file('revision/behandlingshistorik.json')).not.toBeNull()
|
|
expect(zip.file('revision/systemdokumentation.json')).not.toBeNull()
|
|
|
|
const readme = await zip.file('LÄSMIG.txt')!.async('text')
|
|
expect(readme).toContain('Grunddata')
|
|
// Period-scoped content stays out of Grunddata.
|
|
expect(zip.file('bokforing.se')).toBeNull()
|
|
expect(zip.file('rapporter/saldobalans.json')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('estimateArchiveSize', () => {
|
|
let supabase: ReturnType<typeof createQueuedMockSupabase>['supabase']
|
|
let enqueueMany: ReturnType<typeof createQueuedMockSupabase>['enqueueMany']
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
const mock = createQueuedMockSupabase()
|
|
supabase = mock.supabase
|
|
enqueueMany = mock.enqueueMany
|
|
})
|
|
|
|
it('sums document file_size_bytes in all-mode plus overhead', async () => {
|
|
enqueueMany([
|
|
{
|
|
data: [
|
|
{ file_size_bytes: 1_000_000, journal_entry_id: 'e1' },
|
|
{ file_size_bytes: 2_500_000, journal_entry_id: 'e2' },
|
|
],
|
|
count: 2,
|
|
},
|
|
])
|
|
|
|
const result = await estimateArchiveSize(supabase as any, 'company-1', 'all')
|
|
|
|
expect(result.document_bytes).toBe(3_500_000)
|
|
expect(result.document_count).toBe(2)
|
|
// overhead is +8 MB
|
|
expect(result.total_bytes).toBe(3_500_000 + 8 * 1024 * 1024)
|
|
})
|
|
|
|
it('returns overhead only when no documents in scope', async () => {
|
|
enqueueMany([
|
|
{ data: [], count: 0 }, // journal_entries for periodEntryIds
|
|
{ data: [], count: 0 }, // document_attachments
|
|
])
|
|
|
|
const result = await estimateArchiveSize(supabase as any, 'company-1', 'period', 'p-1')
|
|
|
|
expect(result.document_bytes).toBe(0)
|
|
expect(result.document_count).toBe(0)
|
|
expect(result.total_bytes).toBe(8 * 1024 * 1024)
|
|
})
|
|
})
|