e11f70b347
* refactor: optimize page loading and data fetching * fix: resolve recurring production runtime errors * feat: add MCP company and customer updates * fix: handle year-end tax adjustments * feat: harden annual report compliance * fix: expand invoice logo and font support * fix: sanitize API route error responses * fix: sanitize user-facing error messages * feat: persist onboarding and tax assessment notices * fix: reduce cloud backup audit churn * feat: refine invoice editor layout * fix: show saved tax adjustments in INK2 * fix: complete annual report API mappings * docs: record operational safeguards and decisions * fix: harden annual report review findings * fix: adjust column span for description based on VAT registration * New css class name
115 lines
4.2 KiB
TypeScript
115 lines
4.2 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { parseSuppliersFile } from '@/lib/import/suppliers/parser'
|
|
import { normalizeOrgNumber, normalizeEmail } from '@/lib/import/shared/column-utils'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import type {
|
|
AnnotatedSupplierRow,
|
|
SupplierImportParseResult,
|
|
DetectedSupplierColumns,
|
|
} from '@/lib/import/suppliers/types'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
const ALLOWED_EXTENSIONS = ['.xlsx', '.xls', '.csv', '.ods']
|
|
const MAX_FILE_SIZE = 10 * 1024 * 1024 // 10 MB
|
|
|
|
export const POST = withRouteContext(
|
|
'register_import.suppliers.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: DetectedSupplierColumns | undefined
|
|
if (columnOverridesRaw) {
|
|
try {
|
|
columnOverrides = JSON.parse(columnOverridesRaw)
|
|
} catch {
|
|
return errorResponseFromCode('REG_IMPORT_INVALID_COLUMN_OVERRIDES', opLog, { requestId })
|
|
}
|
|
}
|
|
|
|
try {
|
|
const buffer = await file.arrayBuffer()
|
|
const parsed = parseSuppliersFile(buffer, file.name, columnOverrides)
|
|
|
|
const existing = await fetchAllRows(({ from, to }) =>
|
|
supabase
|
|
.from('suppliers')
|
|
.select('id, name, org_number, email')
|
|
.eq('company_id', companyId)
|
|
.range(from, to),
|
|
)
|
|
|
|
const byOrg = new Map<string, { id: string; name: string }>()
|
|
const byEmail = new Map<string, { id: string; name: string }>()
|
|
for (const s of existing) {
|
|
const org = normalizeOrgNumber(s.org_number)
|
|
if (org) byOrg.set(org, { id: s.id, name: s.name })
|
|
const email = normalizeEmail(s.email)
|
|
if (email) byEmail.set(email, { id: s.id, name: s.name })
|
|
}
|
|
|
|
let duplicateCount = 0
|
|
const annotated: AnnotatedSupplierRow[] = parsed.rows.map((r) => {
|
|
const orgKey = normalizeOrgNumber(r.org_number)
|
|
const emailKey = normalizeEmail(r.email)
|
|
let match: AnnotatedSupplierRow['duplicate_match'] = null
|
|
if (orgKey && byOrg.has(orgKey)) {
|
|
const e = byOrg.get(orgKey)!
|
|
match = { supplier_id: e.id, matched_by: 'org_number', existing_name: e.name }
|
|
} else if (emailKey && byEmail.has(emailKey)) {
|
|
const e = byEmail.get(emailKey)!
|
|
match = { supplier_id: e.id, matched_by: 'email', existing_name: e.name }
|
|
}
|
|
if (match) duplicateCount++
|
|
return { ...r, duplicate_match: match }
|
|
})
|
|
|
|
const result: SupplierImportParseResult = {
|
|
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('supplier import parse failed', err as Error)
|
|
return errorResponseFromCode('REG_IMPORT_PARSE_FAILED', opLog, {
|
|
requestId,
|
|
details: { reason: err instanceof Error ? getUserErrorMessage(err) : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
)
|