* feat(branding): add BrandingService with default-preserving env layer Introduce lib/branding/service.ts mirroring lib/email/service.ts. Defaults match current gnubok values exactly, so production behaviour is unchanged unless an env var (NEXT_PUBLIC_BRANDING_*, BRANDING_*) or extension override (via registerBrandingService) is set. Resolution order: defaults < env vars < extension override. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(branding): route root layout, manifest, and PWA assets through branding service - app/layout.tsx now reads title, description, themeColor, and apple-touch-icon from getBranding() instead of hardcoded values. - public/manifest.json replaced by dynamic app/manifest.ts so PWA name, short_name, description, theme_color, background_color, and icon paths are resolved at request time. The manifest now serves at /manifest.webmanifest (Next.js convention for the metadata file route). The previous /manifest.json URL is no longer populated; nothing in core references it after this commit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(branding): route email service and templates through branding service - resend-service.ts: From line uses getBranding().appName instead of hardcoded "Gnubok" in both the with-fromName and bare cases. - invite-templates.ts: subject, HTML header, body, plain text, and the team-invite variants all read from branding (sentence case in prose, uppercased for the styled <p> header). - consent-notification-templates.ts: signature fallback (companyName || branding) for both HTML and plain text variants. Defaults preserve the exact current strings ("Gnubok", "GNUBOK", "gnubok" in their respective contexts) so no email content changes for production. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(branding): route OAuth consent page through branding service The MCP OAuth consent page rendered for Claude Desktop / Claude.ai connector flows now reads the app name from getBranding() for both the HTML <title> and the body copy. Default still produces "gnubok" in lowercase prose, matching current behaviour. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(branding): route auth, dashboard, and onboarding text through branding service Replace user-visible "gnubok" / "Gnubok" references with calls to getBranding(). Touches: - Auth pages (login, register, mfa/enroll): logo src/alt, MFA TOTP friendlyName. - Onboarding (companies/new, invite, sandbox, WelcomeOnboarding, Step2CompanyDetails, NewUserChecklist, BankIdCompanyPicker, ArcimMigrationWorkspace): logo, headings, error/help text. - Dashboard fallback (companyName="gnubok") and settings (backup copy, ApiKeysPanel MCP connector name + login note, CompanyDangerZone, retention-notice). - API routes (support contact subject prefix, enable-banking consent email companyName fallback, AI inbox receipt-request appUrl, pain001 messageId prefix). - MCP server "open the gnubok web app" review message. - Salary/reports filings (AGI Programnamn, KU10 Programnamn, payslip footer, full-archive system metadata, SRU #PROGRAM line). Internal identifiers (cookie names gnubok-company-id / gnubok-invite-token, API key prefix gnubok_sk_, invite token prefix gnubok_inv_, MCP tool names, npm package gnubok-mcp, GNUBOK_API_KEY env name) are deliberately left unchanged — they're stable contracts that whitelabels must not break. Defaults match current behaviour exactly. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(branding): support legal page field-level swaps for entity and contact Privacy and DPA pages now interpolate appName, legalEntity, and privacyEmail from the branding service instead of hardcoding "Gnubok", "Arcim", and "privacy@gnubok.se". Page metadata uses generateMetadata() so titles also reflect the brand. lib/support.ts now falls back to getBranding().supportEmail when SUPPORT_RECIPIENT_EMAIL is unset, so a single BRANDING_SUPPORT_EMAIL env var configures both the support form recipient and the displayed support address. Whitelabels with a different legal jurisdiction or entirely different DPA text should override the page route from an extension. Phase 1 intentionally only supports field-level swaps. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * docs(branding): add WHITELABEL.md and example branding extension WHITELABEL.md: fork checklist, env var reference, the "do not change" list (cookies, API key prefixes, invite token prefixes, MCP tool names, gnubok-mcp npm package, GNUBOK_API_KEY env name), out-of-scope items, the upstream sync workflow YAML to copy into a fork, conflict avoidance guidance, and a verification checklist. extensions/general/_example-branding/: copy-paste starter extension with index.ts (commented placeholder values for registerBrandingService), manifest.json, and README.md. Disabled by default (not added to extensions.config.json); whitelabels cp the folder, edit, and enable. sectors.test.ts: bumped expected extension count 12 -> 13 to account for the new starter extension on disk. The generated registry is unchanged because the example is disabled. The sync workflow YAML is documented inline in WHITELABEL.md rather than checked in as a workflow file. It's only meaningful in a fork -- gnubok itself has nothing to sync from. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(branding): address PR review — lazy support email + escape brand in HTML/XML Three issues from code review: P1 — lib/support.ts: SUPPORT_RECIPIENT_EMAIL was a module-level const, evaluated at import time before extensions register branding overrides via ensureInitialized(). Convert to getSupportRecipientEmail() lazy accessor; update the only caller in app/api/support/contact/route.ts. Extension-supplied supportEmail values now route correctly. P2 — app/api/mcp-oauth/authorize/route.ts: appName was interpolated into the consent page HTML without escapeHtml(), inconsistent with the existing escaping of companyName. Wrap appName.toLowerCase() in escapeHtml() at use sites in <title> and the body paragraph. P2 — lib/salary/agi/xml-generator.ts and lib/salary/ku/ku10-generator.ts: appName placed inside <gem:Programnamn> / <Programnamn> XML elements without escapeXml(), the helper already used for other admin-controlled fields in the same files. Wrap accordingly to prevent malformed XML if a brand name contains XML reserved characters. All admin-controlled inputs only — no user-exploitable path. Defense in depth, not a known incident. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(branding): security follow-up — lazy metadata, SRU/email header sanitization Self-audit after the PR review surfaced four more concerns. Fixes them with the same defense-in-depth posture as the prior review fixes. 1. app/layout.tsx — same eager-evaluation class as P1 support.ts. The module-level `const branding = getBranding()` froze branding before extensions registered, so extension-based overrides for title, description, themeColor, and apple-touch-icon silently never applied. - Convert to generateMetadata() / generateViewport() (lazy, run per request, see extension-registered overrides). - Inline getBranding() inside RootLayout for the apple-touch-icon href so it picks up overrides too. - Add ensureInitialized() at module level so extensions are loaded before the first metadata call. Mirrors the API route pattern. 2. app/manifest.ts — same class. The dynamic manifest function reads getBranding() per request, but if the manifest is requested before any other module has triggered ensureInitialized(), extensions are still unloaded. Add ensureInitialized() at module level. 3. lib/reports/ink2/sru-generator.ts — appName interpolated into the SRU `#PROGRAM` directive without sanitization. SRU's reserved char is `#` (directive marker) and CRLF injects new directives. Wrap in the existing sanitizeString() helper to match the pattern used for other admin-controlled fields in this file (#NAMN, #ADRESS, etc.). 4. extensions/general/email/lib/resend-service.ts — appName and the user-controlled fromName both flow into the From header. Resend's API does its own validation, but defense in depth: strip CRLF and angle brackets via a small sanitizeHeaderPart() helper before building the header string. fromName was a pre-existing surface; appName is new with this whitelabel work. All four are admin-controlled inputs (env vars or extension code), not user-exploitable. No known incidents — defense in depth, and correctness for extension-based whitelabels. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
739 lines
24 KiB
TypeScript
739 lines
24 KiB
TypeScript
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import JSZip from 'jszip'
|
|
import { generateSIEExport } from './sie-export'
|
|
import { generateTrialBalance } from './trial-balance'
|
|
import { generateIncomeStatement } from './income-statement'
|
|
import { generateBalanceSheet } from './balance-sheet'
|
|
import { generateGeneralLedger } from './general-ledger'
|
|
import { generateJournalRegister } from './journal-register'
|
|
import { calculateVatDeclaration } from './vat-declaration'
|
|
import { getAuditLog } from '@/lib/core/audit/audit-service'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
import { getBranding } from '@/lib/branding/service'
|
|
import type { AuditLogEntry } from '@/types'
|
|
|
|
export type FullArchiveOptions =
|
|
| { scope: 'period'; period_id: string; include_documents?: boolean }
|
|
| { scope: 'all'; include_documents?: boolean }
|
|
|
|
export type ArchiveScope = FullArchiveOptions['scope']
|
|
|
|
interface DocumentManifestEntry {
|
|
document_id: string
|
|
file_name: string
|
|
storage_path: string
|
|
sha256_hash: string
|
|
journal_entry_id: string | null
|
|
fiscal_period_id: string | null
|
|
version: number
|
|
digitization_date: string | null
|
|
upload_source: string | null
|
|
mime_type: string | null
|
|
file_size_bytes: number | null
|
|
status: 'downloaded' | 'missing' | 'error'
|
|
error?: string
|
|
}
|
|
|
|
interface FiscalPeriodRow {
|
|
id: string
|
|
period_start: string
|
|
period_end: string
|
|
opening_balance_entry_id: string | null
|
|
}
|
|
|
|
interface CompanyInfo {
|
|
company_name: string | null
|
|
trade_name: string | null
|
|
org_number: string | null
|
|
moms_period: string | null
|
|
}
|
|
|
|
interface DocumentRow {
|
|
id: string
|
|
file_name: string
|
|
storage_path: string
|
|
journal_entry_id: string | null
|
|
sha256_hash: string
|
|
version: number
|
|
digitization_date: string | null
|
|
upload_source: string | null
|
|
mime_type: string | null
|
|
file_size_bytes: number | null
|
|
}
|
|
|
|
interface PeriodReports {
|
|
trialBalance: unknown
|
|
incomeStatement: unknown
|
|
balanceSheet: unknown
|
|
generalLedger: unknown
|
|
journalRegister: unknown
|
|
vatDeclaration: unknown | null
|
|
}
|
|
|
|
const REPORT_CONCURRENCY = 3
|
|
// 5 MB for SIE + reports + audit + system doc, +3 MB headroom for master-data
|
|
// JSON dumps and raw imported SIE files (the bucket caps each file at 50 MB,
|
|
// but typical SIE4 files are tens of KB so a few MB covers most companies).
|
|
const ARCHIVE_OVERHEAD_BYTES = 8 * 1024 * 1024
|
|
|
|
/**
|
|
* Generate a full archive ZIP for a company.
|
|
*
|
|
* `scope: 'period'` produces the single-period archive used by account/company
|
|
* deletion flows: `bokforing.se`, flat `rapporter/*.json`, `dokument/*`, and
|
|
* `revision/*`.
|
|
*
|
|
* `scope: 'all'` produces the "säkerhetsbackup" covering the entire company
|
|
* history: one SIE4 file per period under `sie/`, per-period `rapporter/`
|
|
* subfolders, a flat `dokument/` with manifest tagged by fiscal_period_id,
|
|
* and an unfiltered `revision/behandlingshistorik.json`.
|
|
*/
|
|
export async function generateFullArchive(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
options: FullArchiveOptions
|
|
): Promise<ArrayBuffer> {
|
|
const company = await fetchCompany(supabase, companyId)
|
|
const periods =
|
|
options.scope === 'all'
|
|
? await fetchAllPeriods(supabase, companyId)
|
|
: [await fetchSinglePeriod(supabase, companyId, options.period_id)]
|
|
|
|
if (periods.length === 0) {
|
|
throw new Error('No fiscal periods found')
|
|
}
|
|
|
|
const zip = new JSZip()
|
|
|
|
if (options.scope === 'all') {
|
|
const sieFolder = zip.folder('sie')!
|
|
const rapporterFolder = zip.folder('rapporter')!
|
|
|
|
for (let i = 0; i < periods.length; i += REPORT_CONCURRENCY) {
|
|
const batch = periods.slice(i, i + REPORT_CONCURRENCY)
|
|
await Promise.all(
|
|
batch.map(async (period) => {
|
|
const sie = await generateSIEExport(supabase, companyId, {
|
|
fiscal_period_id: period.id,
|
|
company_name: company.company_name || 'Unknown',
|
|
trade_name: company.trade_name,
|
|
org_number: company.org_number,
|
|
program_name: 'ERPBase',
|
|
})
|
|
sieFolder.file(`${periodLabel(period)}.se`, sie)
|
|
|
|
const reports = await generatePeriodReports(supabase, companyId, period)
|
|
const periodFolder = rapporterFolder.folder(periodLabel(period))!
|
|
writeReports(periodFolder, reports)
|
|
})
|
|
)
|
|
}
|
|
} else {
|
|
const period = periods[0]
|
|
const sie = await generateSIEExport(supabase, companyId, {
|
|
fiscal_period_id: period.id,
|
|
company_name: company.company_name || 'Unknown',
|
|
trade_name: company.trade_name,
|
|
org_number: company.org_number,
|
|
program_name: 'ERPBase',
|
|
})
|
|
zip.file('bokforing.se', sie)
|
|
|
|
const reports = await generatePeriodReports(supabase, companyId, period)
|
|
const rapporter = zip.folder('rapporter')!
|
|
writeReports(rapporter, reports)
|
|
}
|
|
|
|
if (options.include_documents !== false) {
|
|
await writeDocuments(zip, supabase, companyId, periods, options.scope)
|
|
}
|
|
|
|
if (options.scope === 'all') {
|
|
await writeSieSourceFiles(zip, supabase, companyId, options.include_documents !== false)
|
|
await writeMasterData(zip, supabase, companyId)
|
|
}
|
|
|
|
const revision = zip.folder('revision')!
|
|
|
|
const auditFilters =
|
|
options.scope === 'period'
|
|
? {
|
|
from_date: periods[0].period_start,
|
|
to_date: `${periods[0].period_end}T23:59:59.999Z`,
|
|
}
|
|
: {}
|
|
const auditEntries = await fetchAllAuditEntries(supabase, companyId, auditFilters)
|
|
revision.file('behandlingshistorik.json', JSON.stringify(auditEntries, null, 2))
|
|
|
|
const systemDoc = await buildSystemDoc(supabase, companyId, periods, options.scope)
|
|
revision.file('systemdokumentation.json', JSON.stringify(systemDoc, null, 2))
|
|
|
|
return zip.generateAsync({ type: 'arraybuffer' })
|
|
}
|
|
|
|
/**
|
|
* Estimate the uncompressed size of the archive in bytes.
|
|
*
|
|
* Sums `file_size_bytes` across all documents in scope plus a fixed overhead
|
|
* for SIE, reports, audit trail, and system documentation. Used by the API
|
|
* route to short-circuit generation when the payload would exceed the
|
|
* platform's response-size ceiling.
|
|
*/
|
|
export async function estimateArchiveSize(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
scope: ArchiveScope,
|
|
periodId?: string
|
|
): Promise<{ total_bytes: number; document_bytes: number; document_count: number }> {
|
|
let query = supabase
|
|
.from('document_attachments')
|
|
.select('file_size_bytes, journal_entry_id', { count: 'exact' })
|
|
.eq('company_id', companyId)
|
|
.not('journal_entry_id', 'is', null)
|
|
|
|
if (scope === 'period') {
|
|
if (!periodId) {
|
|
throw new Error('period_id is required for scope=period')
|
|
}
|
|
const periodEntryIds = await fetchAllRows<{ id: string }>(({ from, to }) =>
|
|
supabase
|
|
.from('journal_entries')
|
|
.select('id')
|
|
.eq('company_id', companyId)
|
|
.eq('fiscal_period_id', periodId)
|
|
.in('status', ['posted', 'reversed'])
|
|
.range(from, to)
|
|
)
|
|
const ids = periodEntryIds.map((e) => e.id)
|
|
if (ids.length === 0) {
|
|
return { total_bytes: ARCHIVE_OVERHEAD_BYTES, document_bytes: 0, document_count: 0 }
|
|
}
|
|
query = query.in('journal_entry_id', ids)
|
|
}
|
|
|
|
const { data, error } = await query
|
|
if (error) {
|
|
throw new Error(`Failed to estimate archive size: ${error.message}`)
|
|
}
|
|
|
|
const rows = (data as { file_size_bytes: number | null }[]) || []
|
|
const documentBytes = rows.reduce((sum, r) => sum + (Number(r.file_size_bytes) || 0), 0)
|
|
|
|
return {
|
|
total_bytes: documentBytes + ARCHIVE_OVERHEAD_BYTES,
|
|
document_bytes: documentBytes,
|
|
document_count: rows.length,
|
|
}
|
|
}
|
|
|
|
async function fetchCompany(supabase: SupabaseClient, companyId: string): Promise<CompanyInfo> {
|
|
const { data } = await supabase
|
|
.from('company_settings')
|
|
.select('company_name, trade_name, org_number, moms_period')
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!data) {
|
|
throw new Error('Company settings not found')
|
|
}
|
|
return data as CompanyInfo
|
|
}
|
|
|
|
async function fetchSinglePeriod(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
periodId: string
|
|
): Promise<FiscalPeriodRow> {
|
|
const { data } = await supabase
|
|
.from('fiscal_periods')
|
|
.select('id, period_start, period_end, opening_balance_entry_id')
|
|
.eq('id', periodId)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!data) {
|
|
throw new Error('Fiscal period not found')
|
|
}
|
|
return data as FiscalPeriodRow
|
|
}
|
|
|
|
async function fetchAllPeriods(
|
|
supabase: SupabaseClient,
|
|
companyId: string
|
|
): Promise<FiscalPeriodRow[]> {
|
|
const rows = await fetchAllRows<FiscalPeriodRow>(({ from, to }) =>
|
|
supabase
|
|
.from('fiscal_periods')
|
|
.select('id, period_start, period_end, opening_balance_entry_id')
|
|
.eq('company_id', companyId)
|
|
.order('period_start', { ascending: true })
|
|
.range(from, to)
|
|
)
|
|
return rows
|
|
}
|
|
|
|
async function generatePeriodReports(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
period: FiscalPeriodRow
|
|
): Promise<PeriodReports> {
|
|
const [trialBalance, incomeStatement, balanceSheet, generalLedger, journalRegister] =
|
|
await Promise.all([
|
|
generateTrialBalance(supabase, companyId, period.id),
|
|
generateIncomeStatement(supabase, companyId, period.id),
|
|
generateBalanceSheet(supabase, companyId, period.id),
|
|
generateGeneralLedger(supabase, companyId, period.id),
|
|
generateJournalRegister(supabase, companyId, period.id),
|
|
])
|
|
|
|
let vatDeclaration: unknown = null
|
|
try {
|
|
const startDate = new Date(period.period_start)
|
|
vatDeclaration = await calculateVatDeclaration(
|
|
supabase,
|
|
companyId,
|
|
'yearly',
|
|
startDate.getFullYear(),
|
|
1
|
|
)
|
|
} catch {
|
|
// VAT declaration may fail if no relevant entries exist — skip gracefully
|
|
}
|
|
|
|
return { trialBalance, incomeStatement, balanceSheet, generalLedger, journalRegister, vatDeclaration }
|
|
}
|
|
|
|
function writeReports(folder: JSZip, reports: PeriodReports): void {
|
|
folder.file('saldobalans.json', JSON.stringify(reports.trialBalance, null, 2))
|
|
folder.file('resultatrakning.json', JSON.stringify(reports.incomeStatement, null, 2))
|
|
folder.file('balansrakning.json', JSON.stringify(reports.balanceSheet, null, 2))
|
|
folder.file('huvudbok.json', JSON.stringify(reports.generalLedger, null, 2))
|
|
folder.file('grundbok.json', JSON.stringify(reports.journalRegister, null, 2))
|
|
if (reports.vatDeclaration) {
|
|
folder.file('momsdeklaration.json', JSON.stringify(reports.vatDeclaration, null, 2))
|
|
}
|
|
}
|
|
|
|
async function writeDocuments(
|
|
zip: JSZip,
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
periods: FiscalPeriodRow[],
|
|
scope: ArchiveScope
|
|
): Promise<void> {
|
|
const dokument = zip.folder('dokument')!
|
|
const manifest: DocumentManifestEntry[] = []
|
|
|
|
try {
|
|
const documents = await fetchAllRows<DocumentRow>(({ from, to }) =>
|
|
supabase
|
|
.from('document_attachments')
|
|
.select(
|
|
'id, file_name, storage_path, journal_entry_id, sha256_hash, version, digitization_date, upload_source, mime_type, file_size_bytes'
|
|
)
|
|
.eq('company_id', companyId)
|
|
.not('journal_entry_id', 'is', null)
|
|
.range(from, to)
|
|
)
|
|
|
|
if (documents.length > 0) {
|
|
const entryIdToPeriodId = await buildEntryToPeriodMap(supabase, companyId, periods, scope)
|
|
|
|
const inScopeDocuments =
|
|
scope === 'period'
|
|
? documents.filter((d) => d.journal_entry_id && entryIdToPeriodId.has(d.journal_entry_id))
|
|
: documents.filter((d) => d.journal_entry_id) // all-mode: keep every linked doc
|
|
|
|
for (const doc of inScopeDocuments) {
|
|
const fiscalPeriodId = doc.journal_entry_id
|
|
? entryIdToPeriodId.get(doc.journal_entry_id) ?? null
|
|
: null
|
|
|
|
const baseManifest = {
|
|
document_id: doc.id,
|
|
file_name: doc.file_name,
|
|
storage_path: doc.storage_path,
|
|
sha256_hash: doc.sha256_hash,
|
|
journal_entry_id: doc.journal_entry_id,
|
|
fiscal_period_id: fiscalPeriodId,
|
|
version: doc.version,
|
|
digitization_date: doc.digitization_date,
|
|
upload_source: doc.upload_source,
|
|
mime_type: doc.mime_type,
|
|
file_size_bytes: doc.file_size_bytes,
|
|
}
|
|
|
|
try {
|
|
const { data: fileData, error } = await supabase.storage
|
|
.from('documents')
|
|
.download(doc.storage_path)
|
|
|
|
if (error || !fileData) {
|
|
manifest.push({
|
|
...baseManifest,
|
|
status: 'error',
|
|
error: error?.message || 'Download returned no data',
|
|
})
|
|
continue
|
|
}
|
|
|
|
const buffer = await fileData.arrayBuffer()
|
|
const zipFileName = `${doc.id}_${doc.file_name}`
|
|
dokument.file(zipFileName, buffer)
|
|
manifest.push({ ...baseManifest, status: 'downloaded' })
|
|
} catch (err) {
|
|
manifest.push({
|
|
...baseManifest,
|
|
status: 'error',
|
|
error: err instanceof Error ? err.message : 'Unknown error',
|
|
})
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
// Document fetch failed — archive will still contain reports and audit trail
|
|
}
|
|
|
|
dokument.file('manifest.json', JSON.stringify(manifest, null, 2))
|
|
}
|
|
|
|
interface SieImportRow {
|
|
id: string
|
|
filename: string | null
|
|
file_hash: string | null
|
|
file_storage_path: string | null
|
|
org_number: string | null
|
|
company_name: string | null
|
|
sie_type: number | null
|
|
fiscal_year_start: string | null
|
|
fiscal_year_end: string | null
|
|
accounts_count: number | null
|
|
transactions_count: number | null
|
|
status: string | null
|
|
fiscal_period_id: string | null
|
|
imported_at: string | null
|
|
created_at: string | null
|
|
}
|
|
|
|
interface SieSourceManifestEntry {
|
|
import_id: string
|
|
filename: string | null
|
|
storage_path: string | null
|
|
sha256_hash: string | null
|
|
sie_type: number | null
|
|
fiscal_year_start: string | null
|
|
fiscal_year_end: string | null
|
|
imported_at: string | null
|
|
status: 'downloaded' | 'missing' | 'skipped'
|
|
zip_file_name: string | null
|
|
error?: string
|
|
}
|
|
|
|
/**
|
|
* Copy raw imported SIE files from the `sie-files` storage bucket into the
|
|
* archive under `sie/original/`. Preserves the byte-identical source that the
|
|
* user uploaded (vs the `sie/<period>.se` files which gnubok re-generates from
|
|
* the current journal entries).
|
|
*
|
|
* `sie/imports.json` and `sie/account_mappings.json` are written regardless of
|
|
* `includeFiles` — they're small and critical for reconstructing the import
|
|
* history. Blob download is gated behind `includeFiles` since the files can be
|
|
* large and share the documents opt-out.
|
|
*/
|
|
async function writeSieSourceFiles(
|
|
zip: JSZip,
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
includeFiles: boolean
|
|
): Promise<void> {
|
|
const sieFolder = zip.folder('sie')!
|
|
|
|
try {
|
|
const imports = await fetchAllRows<SieImportRow>(({ from, to }) =>
|
|
supabase
|
|
.from('sie_imports')
|
|
.select(
|
|
'id, filename, file_hash, file_storage_path, org_number, company_name, sie_type, fiscal_year_start, fiscal_year_end, accounts_count, transactions_count, status, fiscal_period_id, imported_at, created_at'
|
|
)
|
|
.eq('company_id', companyId)
|
|
.order('created_at', { ascending: true })
|
|
.range(from, to)
|
|
)
|
|
|
|
sieFolder.file('imports.json', JSON.stringify(imports, null, 2))
|
|
|
|
const manifest: SieSourceManifestEntry[] = []
|
|
|
|
if (includeFiles && imports.length > 0) {
|
|
const originalFolder = sieFolder.folder('original')!
|
|
|
|
for (const imp of imports) {
|
|
if (!imp.file_storage_path) {
|
|
manifest.push({
|
|
import_id: imp.id,
|
|
filename: imp.filename,
|
|
storage_path: null,
|
|
sha256_hash: imp.file_hash,
|
|
sie_type: imp.sie_type,
|
|
fiscal_year_start: imp.fiscal_year_start,
|
|
fiscal_year_end: imp.fiscal_year_end,
|
|
imported_at: imp.imported_at,
|
|
status: 'skipped',
|
|
zip_file_name: null,
|
|
error: 'No storage path on record',
|
|
})
|
|
continue
|
|
}
|
|
|
|
const zipFileName = `${imp.id}_${sanitizeFileName(imp.filename || `${imp.id}.se`)}`
|
|
|
|
try {
|
|
const { data: fileData, error } = await supabase.storage
|
|
.from('sie-files')
|
|
.download(imp.file_storage_path)
|
|
|
|
if (error || !fileData) {
|
|
manifest.push({
|
|
import_id: imp.id,
|
|
filename: imp.filename,
|
|
storage_path: imp.file_storage_path,
|
|
sha256_hash: imp.file_hash,
|
|
sie_type: imp.sie_type,
|
|
fiscal_year_start: imp.fiscal_year_start,
|
|
fiscal_year_end: imp.fiscal_year_end,
|
|
imported_at: imp.imported_at,
|
|
status: 'missing',
|
|
zip_file_name: null,
|
|
error: error?.message || 'Download returned no data',
|
|
})
|
|
continue
|
|
}
|
|
|
|
const buffer = await fileData.arrayBuffer()
|
|
originalFolder.file(zipFileName, buffer)
|
|
manifest.push({
|
|
import_id: imp.id,
|
|
filename: imp.filename,
|
|
storage_path: imp.file_storage_path,
|
|
sha256_hash: imp.file_hash,
|
|
sie_type: imp.sie_type,
|
|
fiscal_year_start: imp.fiscal_year_start,
|
|
fiscal_year_end: imp.fiscal_year_end,
|
|
imported_at: imp.imported_at,
|
|
status: 'downloaded',
|
|
zip_file_name: zipFileName,
|
|
})
|
|
} catch (err) {
|
|
manifest.push({
|
|
import_id: imp.id,
|
|
filename: imp.filename,
|
|
storage_path: imp.file_storage_path,
|
|
sha256_hash: imp.file_hash,
|
|
sie_type: imp.sie_type,
|
|
fiscal_year_start: imp.fiscal_year_start,
|
|
fiscal_year_end: imp.fiscal_year_end,
|
|
imported_at: imp.imported_at,
|
|
status: 'missing',
|
|
zip_file_name: null,
|
|
error: err instanceof Error ? err.message : 'Unknown error',
|
|
})
|
|
}
|
|
}
|
|
|
|
originalFolder.file('manifest.json', JSON.stringify(manifest, null, 2))
|
|
}
|
|
|
|
const mappings = await fetchAllRows<Record<string, unknown>>(({ from, to }) =>
|
|
supabase
|
|
.from('sie_account_mappings')
|
|
.select('*')
|
|
.eq('company_id', companyId)
|
|
.range(from, to)
|
|
)
|
|
sieFolder.file('account_mappings.json', JSON.stringify(mappings, null, 2))
|
|
} catch {
|
|
// SIE metadata fetch failed — archive will still contain the re-generated SIE files
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Dump structured master data as JSON under `data/`. These records are implicit
|
|
* in the SIE export (as journal entries) but not recoverable as domain objects
|
|
* without this dump — critical for disaster recovery of a company's state.
|
|
*/
|
|
async function writeMasterData(
|
|
zip: JSZip,
|
|
supabase: SupabaseClient,
|
|
companyId: string
|
|
): Promise<void> {
|
|
const data = zip.folder('data')!
|
|
|
|
const tables: Array<{ name: string; file: string; orderBy?: string }> = [
|
|
{ name: 'customers', file: 'customers.json', orderBy: 'created_at' },
|
|
{ name: 'suppliers', file: 'suppliers.json', orderBy: 'created_at' },
|
|
{ name: 'invoices', file: 'invoices.json', orderBy: 'invoice_date' },
|
|
{ name: 'invoice_items', file: 'invoice_items.json' },
|
|
{ name: 'invoice_payments', file: 'invoice_payments.json', orderBy: 'payment_date' },
|
|
{ name: 'supplier_invoices', file: 'supplier_invoices.json', orderBy: 'invoice_date' },
|
|
{ name: 'supplier_invoice_items', file: 'supplier_invoice_items.json' },
|
|
{ name: 'receipts', file: 'receipts.json', orderBy: 'receipt_date' },
|
|
{ name: 'receipt_line_items', file: 'receipt_line_items.json' },
|
|
{ name: 'transactions', file: 'transactions.json', orderBy: 'booking_date' },
|
|
{ name: 'mapping_rules', file: 'mapping_rules.json' },
|
|
{ name: 'categorization_templates', file: 'categorization_templates.json' },
|
|
{ name: 'bank_file_imports', file: 'bank_file_imports.json', orderBy: 'created_at' },
|
|
{ name: 'company_settings', file: 'company_settings.json' },
|
|
]
|
|
|
|
await Promise.all(
|
|
tables.map(async (t) => {
|
|
try {
|
|
const rows = await fetchAllRows<Record<string, unknown>>(({ from, to }) => {
|
|
let q = supabase.from(t.name).select('*').eq('company_id', companyId)
|
|
if (t.orderBy) {
|
|
q = q.order(t.orderBy, { ascending: true })
|
|
}
|
|
return q.range(from, to)
|
|
})
|
|
data.file(t.file, JSON.stringify(rows, null, 2))
|
|
} catch (err) {
|
|
data.file(
|
|
t.file,
|
|
JSON.stringify(
|
|
{ error: err instanceof Error ? err.message : 'Fetch failed', rows: [] },
|
|
null,
|
|
2
|
|
)
|
|
)
|
|
}
|
|
})
|
|
)
|
|
}
|
|
|
|
function sanitizeFileName(name: string): string {
|
|
return name.replace(/[\\/:*?"<>|]/g, '_').slice(0, 120)
|
|
}
|
|
|
|
async function buildEntryToPeriodMap(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
periods: FiscalPeriodRow[],
|
|
scope: ArchiveScope
|
|
): Promise<Map<string, string>> {
|
|
const map = new Map<string, string>()
|
|
const periodIds = periods.map((p) => p.id)
|
|
if (periodIds.length === 0) return map
|
|
|
|
let query = supabase
|
|
.from('journal_entries')
|
|
.select('id, fiscal_period_id')
|
|
.eq('company_id', companyId)
|
|
.in('status', ['posted', 'reversed'])
|
|
|
|
if (scope === 'period') {
|
|
query = query.eq('fiscal_period_id', periodIds[0])
|
|
} else {
|
|
query = query.in('fiscal_period_id', periodIds)
|
|
}
|
|
|
|
const entries = await fetchAllRows<{ id: string; fiscal_period_id: string }>(({ from, to }) =>
|
|
query.range(from, to)
|
|
)
|
|
|
|
for (const entry of entries) {
|
|
map.set(entry.id, entry.fiscal_period_id)
|
|
}
|
|
return map
|
|
}
|
|
|
|
async function fetchAllAuditEntries(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
filters: { from_date?: string; to_date?: string }
|
|
): Promise<AuditLogEntry[]> {
|
|
const all: AuditLogEntry[] = []
|
|
let page = 1
|
|
const pageSize = 500
|
|
|
|
while (true) {
|
|
const result = await getAuditLog(supabase, companyId, { ...filters, page, pageSize })
|
|
all.push(...result.data)
|
|
if (all.length >= result.count || result.data.length < pageSize) {
|
|
break
|
|
}
|
|
page++
|
|
}
|
|
return all
|
|
}
|
|
|
|
async function buildSystemDoc(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
periods: FiscalPeriodRow[],
|
|
scope: ArchiveScope
|
|
): Promise<Record<string, unknown>> {
|
|
let voucherSeriesQuery = supabase
|
|
.from('voucher_sequences')
|
|
.select('voucher_series, last_number, fiscal_period_id')
|
|
.eq('company_id', companyId)
|
|
|
|
if (scope === 'period') {
|
|
voucherSeriesQuery = voucherSeriesQuery.eq('fiscal_period_id', periods[0].id)
|
|
}
|
|
|
|
const [accountsResult, voucherSeriesResult] = await Promise.all([
|
|
supabase
|
|
.from('chart_of_accounts')
|
|
.select('account_number, account_name, account_type, is_active')
|
|
.eq('company_id', companyId)
|
|
.order('account_number'),
|
|
voucherSeriesQuery,
|
|
])
|
|
|
|
const branding = getBranding()
|
|
return {
|
|
system: {
|
|
name: branding.appName.toLowerCase(),
|
|
description: 'Bokforingssystem for enskild firma och aktiebolag',
|
|
url: branding.appUrl,
|
|
},
|
|
kontoplan: {
|
|
standard: 'BAS 2026',
|
|
accounts: accountsResult.data || [],
|
|
},
|
|
verifikationsserier: (voucherSeriesResult.data || []).map(
|
|
(vs: { voucher_series: string; last_number: number; fiscal_period_id?: string }) => ({
|
|
serie: vs.voucher_series,
|
|
senaste_nummer: vs.last_number,
|
|
fiscal_period_id: vs.fiscal_period_id ?? null,
|
|
})
|
|
),
|
|
behorighetskontroll: {
|
|
description: 'Rollbaserad atkomstkontroll med owner/admin/member/viewer',
|
|
mfa_stod: true,
|
|
rls_aktiv: true,
|
|
},
|
|
arkivering: {
|
|
lagringstid_ar: 7,
|
|
format: 'WORM (Write Once, Read Many)',
|
|
integritetskontroll: 'SHA-256 hashning vid uppladdning, regelbunden verifiering',
|
|
lagringsplats: 'Supabase Storage (krypterad)',
|
|
},
|
|
integrationer: {
|
|
bank: 'Enable Banking (PSD2)',
|
|
email: 'Resend',
|
|
export_format: 'SIE4',
|
|
},
|
|
generated_at: new Date().toISOString(),
|
|
fiscal_periods: periods.map((p) => ({
|
|
id: p.id,
|
|
start: p.period_start,
|
|
end: p.period_end,
|
|
})),
|
|
}
|
|
}
|
|
|
|
function periodLabel(period: FiscalPeriodRow): string {
|
|
return `${period.period_start}_${period.period_end}`
|
|
}
|