Files
accounted/app/api/import/articles/parse/route.ts
T
Jakob Wennberg f266c386f3 chore: repo-wide bloat sweep, remove dead code and fold duplicate helpers (#2150)
* chore: repo-wide bloat sweep, remove dead code and fold duplicate helpers

Remove 33 dead files, ~270 unreferenced exports/types, 13 dead i18n
namespaces and 4 unused dependencies; fold byte-identical helper copies
into one canonical home each (lib/utils chunk/sleep/utcDateStamp,
lib/dates/iso, lib/invariants/uuid, lib/xml/escape, lib/reports/sru/format,
lib/pdf/number-text, lib/browser/panel-request, lib/api/v1/body +
v1ValidationError rolled out to ~55 v1 routes, booking-template schemas).

No behaviour change: v1 bodies and status codes, MCP tool schemas, DB
writes and money math are untouched. Naive ore rounding was deliberately
not swapped for roundOre; see DECISIONS.md 2026-09-02 for the full list
of things left alone on purpose.

tsc, lint, 19588 unit tests and check:guards green; antipattern baseline
ratcheted (naive-ore-round 622 -> 620, hand-rolled-invariant 115 -> 113).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* test(transactions): import RawTransaction from @/types after the ingest re-export removal

CI's type ratchet (check:types, full tsconfig) caught the one test file
that still imported the type through lib/transactions/ingest.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-02 11:51:16 +02:00

131 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'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
import { normalizeNameKey as nameKey } from '@/lib/import/shared/column-utils'
const ALLOWED_EXTENSIONS = ['.xlsx', '.xls', '.csv', '.ods']
const MAX_FILE_SIZE = 10 * 1024 * 1024 // 10 MB
/**
* 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 ? getUserErrorMessage(err) : 'unknown' },
})
}
},
)