Files
accounted/app/api/import/articles/parse/route.ts
T
Jakob Wennberg 2d6ddeafc5 feat(import/export): article import + register export (xlsx/csv) (#750)
* feat(import/export): article import + register export (xlsx/csv)

Add CSV/Excel import for the article register (artiklar), mirroring the
existing customer/supplier import pipeline, plus Excel + CSV export for
articles, customers and suppliers.

Import (lib/import/articles + app/api/import/articles):
- Column auto-detection tuned to Fortnox/Visma/Bokio export headers,
  Swedish-decimal price parsing, VAT snapped to {0,6,12,25}, type/unit
  normalization.
- Dedup by article number then name; 23505 soft-skip; auto-number
  backfill; revenue-account override kept only when active, otherwise
  dropped with a warning (never mutates the chart of accounts).
- New "Artiklar" flow in the /import hub.

Export (app/api/export/* + lib/export/register-export):
- Read-only xlsx (default) / csv (?format=csv, UTF-8 BOM) downloads.
- Headers chosen so files round-trip back through the importer.
- "Exportera" menu added to the articles, customers and suppliers pages.

Refs #746. Direct Fortnox/Visma API article fetch tracked in #749.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(import/export): address PR review — lint ratchet + export hardening

- xlsx-export: keep `SheetSpec<any>` on the eslint-disabled line (fixes the
  core-only lint ratchet regression: no-explicit-any 16 -> 15) and define
  UTF8_BOM as an explicit `` escape instead of a raw BOM character.
- export routes (articles/customers/suppliers): move the data queries inside
  the try/catch, add `Cache-Control: no-store`, and emit a `register exported`
  audit log line (entity, format, rowCount).
- articles parse route: validate `column_overrides` against a Zod schema before
  trusting it to drive the parser.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(import): drop öre-round pattern on article column-detector confidence

The confidence score is a 0-1 heuristic, not money, and is only compared
against the 0.8 skip-mapping threshold. Removing the Math.round(x*100)/100
form clears the core-only antipattern ratchet (naive-ore-round 660 -> 659).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(import): flag adjusted VAT rows in the article import edit step

Surface VAT snapping/defaulting per row, not just as a file-level warning:
the parser sets `vat_rate_adjusted`, the edit step highlights those rows'
VAT selector and shows a count banner, and confirming a rate clears the flag.
Addresses the Swedish-compliance review note that silent snapping could
otherwise store a wrong VAT rate at scale.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 14:38:44 +02:00

135 lines
4.8 KiB
TypeScript

import { NextResponse } from 'next/server'
import { parseArticlesFile } from '@/lib/import/articles/parser'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
import { withRouteContext } from '@/lib/api/with-route-context'
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
import { ArticleColumnOverridesSchema } from '@/lib/api/schemas'
import type {
AnnotatedArticleRow,
ArticleImportParseResult,
DetectedArticleColumns,
} from '@/lib/import/articles/types'
const ALLOWED_EXTENSIONS = ['.xlsx', '.xls', '.csv', '.ods']
const MAX_FILE_SIZE = 10 * 1024 * 1024 // 10 MB
/** Lowercased dedup key for matching an article by name. */
function nameKey(value: string | null): string | null {
if (!value) return null
return value.trim().toLowerCase() || null
}
/**
* POST /api/import/articles/parse
*
* Accepts an Excel/CSV file via FormData, auto-detects columns, parses rows,
* and annotates each row with any duplicate-match against existing articles
* (by article number first, then by name).
*/
export const POST = withRouteContext(
'register_import.articles.parse',
async (request, ctx) => {
const { supabase, companyId, log, requestId } = ctx
const formData = await request.formData()
const file = formData.get('file') as File | null
const columnOverridesRaw = formData.get('column_overrides') as string | null
if (!file) {
return errorResponseFromCode('REG_IMPORT_NO_FILE', log, { requestId })
}
if (file.size > MAX_FILE_SIZE) {
return errorResponseFromCode('REG_IMPORT_FILE_TOO_LARGE', log, {
requestId,
details: { sizeMb: +(file.size / 1024 / 1024).toFixed(1) },
})
}
const ext = '.' + file.name.split('.').pop()?.toLowerCase()
if (!ALLOWED_EXTENSIONS.includes(ext)) {
return errorResponseFromCode('REG_IMPORT_INVALID_FORMAT', log, {
requestId,
details: { extension: ext, allowed: ALLOWED_EXTENSIONS },
})
}
const opLog = log.child({ filename: file.name, sizeBytes: file.size })
let columnOverrides: DetectedArticleColumns | undefined
if (columnOverridesRaw) {
let raw: unknown
try {
raw = JSON.parse(columnOverridesRaw)
} catch {
return errorResponseFromCode('REG_IMPORT_INVALID_COLUMN_OVERRIDES', opLog, { requestId })
}
// Validate shape/indices before trusting it to drive the parser.
const parsed = ArticleColumnOverridesSchema.safeParse(raw)
if (!parsed.success) {
return errorResponseFromCode('REG_IMPORT_INVALID_COLUMN_OVERRIDES', opLog, { requestId })
}
columnOverrides = parsed.data
}
try {
const buffer = await file.arrayBuffer()
const parsed = parseArticlesFile(buffer, file.name, columnOverrides)
// Fetch existing articles for duplicate detection.
const existing = await fetchAllRows(({ from, to }) =>
supabase
.from('articles')
.select('id, name, article_number')
.eq('company_id', companyId)
.range(from, to),
)
const byNumber = new Map<string, { id: string; name: string }>()
const byName = new Map<string, { id: string; name: string }>()
for (const a of existing) {
if (a.article_number) byNumber.set(String(a.article_number), { id: a.id, name: a.name })
const nk = nameKey(a.name)
if (nk && !byName.has(nk)) byName.set(nk, { id: a.id, name: a.name })
}
let duplicateCount = 0
const annotated: AnnotatedArticleRow[] = parsed.rows.map((r) => {
let match: AnnotatedArticleRow['duplicate_match'] = null
if (r.article_number && byNumber.has(r.article_number)) {
const e = byNumber.get(r.article_number)!
match = { article_id: e.id, matched_by: 'article_number', existing_name: e.name }
} else {
const nk = nameKey(r.name)
if (nk && byName.has(nk)) {
const e = byName.get(nk)!
match = { article_id: e.id, matched_by: 'name', existing_name: e.name }
}
}
if (match) duplicateCount++
return { ...r, duplicate_match: match }
})
const result: ArticleImportParseResult = {
filename: parsed.filename,
sheet_name: parsed.sheet_name,
total_rows: annotated.length,
detected_columns: parsed.detected_columns,
headers: parsed.headers,
preview_rows: parsed.preview_rows,
rows: annotated,
duplicate_count: duplicateCount,
warnings: parsed.warnings,
}
return NextResponse.json({ data: result })
} catch (err) {
opLog.error('article import parse failed', err as Error)
return errorResponseFromCode('REG_IMPORT_PARSE_FAILED', opLog, {
requestId,
details: { reason: err instanceof Error ? err.message : 'unknown' },
})
}
},
)