Add/csv import options (#420)
* feat(import): add customer and supplier parsing functionality - Implemented customer file parsing in `lib/import/customers/parser.ts` with support for Excel and CSV formats. - Created types for detected customer columns and parsed customer rows in `lib/import/customers/types.ts`. - Added tests for customer classification logic in `lib/import/shared/__tests__/classify.test.ts`. - Developed classification functions for customers and suppliers in `lib/import/shared/classify.ts`. - Introduced shared column utility functions in `lib/import/shared/column-utils.ts`. - Implemented supplier file parsing in `lib/import/suppliers/parser.ts` with validation for various fields. - Created types for detected supplier columns and parsed supplier rows in `lib/import/suppliers/types.ts`. - Added tests for supplier column detection and parsing in `lib/import/suppliers/__tests__/column-detector.test.ts` and `lib/import/suppliers/__tests__/parser.test.ts`. * fix(labels): update 'Svenskt företag' to 'Svenskt företag eller organisation' for clarity * feat(import): refactor encoding handling for Swedish files and add tests for character preservation * feat(recapt): implement clearRecaptIdentity function and integrate into logout flow * feat(bookkeeping): implement copy functionality and next voucher sequence retrieval * feat(import): enhance customer and supplier import functionality with normalization and event handling
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { parseBankFile, generateFileHash, detectFileFormat } from '@/lib/import/bank-file/parser'
|
||||
import { decodeFileContent } from '@/lib/import/bank-file/encoding'
|
||||
import { decodeFileContent } from '@/lib/import/shared/encoding'
|
||||
import { withRouteContext } from '@/lib/api/with-route-context'
|
||||
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
||||
import type { BankFileFormatId } from '@/lib/import/bank-file/types'
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { ensureInitialized } from '@/lib/init'
|
||||
import { eventBus } from '@/lib/events'
|
||||
import { validateBody } from '@/lib/api/validate'
|
||||
import { CustomerImportExecuteSchema } from '@/lib/api/schemas'
|
||||
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 { Customer } from '@/types'
|
||||
import type { CustomerImportExecuteResult } from '@/lib/import/customers/types'
|
||||
|
||||
ensureInitialized()
|
||||
|
||||
interface ExistingCustomer {
|
||||
id: string
|
||||
name: string
|
||||
org_number: string | null
|
||||
email: string | null
|
||||
phone: string | null
|
||||
address_line1: string | null
|
||||
address_line2: string | null
|
||||
postal_code: string | null
|
||||
city: string | null
|
||||
country: string
|
||||
vat_number: string | null
|
||||
default_payment_terms: number
|
||||
notes: string | null
|
||||
customer_type: string
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/import/customers/execute
|
||||
*
|
||||
* Imports validated customer rows. Duplicates (matched by org_number or email)
|
||||
* are either updated (merge — only non-empty file fields overwrite) or skipped
|
||||
* based on `update_duplicates`.
|
||||
*/
|
||||
export const POST = withRouteContext(
|
||||
'register_import.customers.execute',
|
||||
async (request, ctx) => {
|
||||
const { user, supabase, companyId, log, requestId } = ctx
|
||||
|
||||
const result = await validateBody(request, CustomerImportExecuteSchema, {
|
||||
log,
|
||||
operation: 'register_import.customers.execute',
|
||||
})
|
||||
if (!result.success) return result.response
|
||||
|
||||
const { rows, update_duplicates } = result.data
|
||||
const opLog = log.child({ rowCount: rows.length, updateDuplicates: update_duplicates })
|
||||
|
||||
if (rows.length === 0) {
|
||||
return errorResponseFromCode('REG_IMPORT_NO_ROWS', opLog, { requestId })
|
||||
}
|
||||
|
||||
try {
|
||||
const existingRaw = await fetchAllRows(({ from, to }) =>
|
||||
supabase
|
||||
.from('customers')
|
||||
.select(
|
||||
'id, name, org_number, email, phone, address_line1, address_line2, ' +
|
||||
'postal_code, city, country, vat_number, default_payment_terms, notes, ' +
|
||||
'customer_type',
|
||||
)
|
||||
.eq('company_id', companyId)
|
||||
.range(from, to),
|
||||
)
|
||||
const existing = existingRaw as unknown as ExistingCustomer[]
|
||||
|
||||
const byOrg = new Map<string, ExistingCustomer>()
|
||||
const byEmail = new Map<string, ExistingCustomer>()
|
||||
for (const c of existing) {
|
||||
const org = normalizeOrgNumber(c.org_number)
|
||||
if (org) byOrg.set(org, c)
|
||||
const email = normalizeEmail(c.email)
|
||||
if (email) byEmail.set(email, c)
|
||||
}
|
||||
|
||||
const created: Customer[] = []
|
||||
const updated: Customer[] = []
|
||||
let skipped = 0
|
||||
const errors: { row_index: number; name: string; reason: string }[] = []
|
||||
|
||||
for (const row of rows) {
|
||||
const orgKey = normalizeOrgNumber(row.org_number)
|
||||
const emailKey = normalizeEmail(row.email)
|
||||
const match =
|
||||
(orgKey && byOrg.get(orgKey)) ||
|
||||
(emailKey && byEmail.get(emailKey)) ||
|
||||
null
|
||||
|
||||
if (match) {
|
||||
if (!update_duplicates) {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
// Merge mode: only overwrite fields where the file has a non-empty value.
|
||||
const merged: Record<string, unknown> = {}
|
||||
if (row.name) merged.name = row.name
|
||||
if (row.customer_type) merged.customer_type = row.customer_type
|
||||
if (row.org_number) merged.org_number = row.org_number
|
||||
if (row.email) merged.email = row.email
|
||||
if (row.phone) merged.phone = row.phone
|
||||
if (row.address_line1) merged.address_line1 = row.address_line1
|
||||
if (row.address_line2) merged.address_line2 = row.address_line2
|
||||
if (row.postal_code) merged.postal_code = row.postal_code
|
||||
if (row.city) merged.city = row.city
|
||||
if (row.country) merged.country = row.country
|
||||
if (row.vat_number) merged.vat_number = row.vat_number
|
||||
if (row.default_payment_terms) merged.default_payment_terms = row.default_payment_terms
|
||||
if (row.notes) merged.notes = row.notes
|
||||
|
||||
if (Object.keys(merged).length === 0) {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('customers')
|
||||
.update(merged)
|
||||
.eq('id', match.id)
|
||||
.eq('company_id', companyId)
|
||||
.select()
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
errors.push({ row_index: row.row_index, name: row.name, reason: error.message })
|
||||
continue
|
||||
}
|
||||
if (data) updated.push(data as Customer)
|
||||
continue
|
||||
}
|
||||
|
||||
// No match — create.
|
||||
const { data, error } = await supabase
|
||||
.from('customers')
|
||||
.insert({
|
||||
user_id: user.id,
|
||||
company_id: companyId,
|
||||
name: row.name,
|
||||
customer_type: row.customer_type,
|
||||
email: row.email,
|
||||
phone: row.phone,
|
||||
address_line1: row.address_line1,
|
||||
address_line2: row.address_line2,
|
||||
postal_code: row.postal_code,
|
||||
city: row.city,
|
||||
country: row.country || 'Sweden',
|
||||
org_number: row.org_number,
|
||||
vat_number: row.vat_number,
|
||||
default_payment_terms: row.default_payment_terms || 30,
|
||||
notes: row.notes,
|
||||
})
|
||||
.select()
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
// Treat unique-violation as a soft skip (race with concurrent import).
|
||||
if (error.code === '23505') {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
errors.push({ row_index: row.row_index, name: row.name, reason: error.message })
|
||||
continue
|
||||
}
|
||||
if (data) {
|
||||
created.push(data as Customer)
|
||||
// Track newly inserted org/email so subsequent rows in the same batch
|
||||
// dedup against them too.
|
||||
const newOrg = normalizeOrgNumber(data.org_number)
|
||||
if (newOrg) byOrg.set(newOrg, data as ExistingCustomer)
|
||||
const newEmail = normalizeEmail(data.email)
|
||||
if (newEmail) byEmail.set(newEmail, data as ExistingCustomer)
|
||||
}
|
||||
}
|
||||
|
||||
// Emit events for downstream listeners (non-blocking).
|
||||
for (const c of created) {
|
||||
await eventBus.emit({
|
||||
type: 'customer.created',
|
||||
payload: { customer: c, companyId: companyId!, userId: user.id },
|
||||
})
|
||||
}
|
||||
|
||||
const response: CustomerImportExecuteResult = {
|
||||
success: errors.length === 0,
|
||||
created: created.length,
|
||||
updated: updated.length,
|
||||
skipped,
|
||||
failed: errors.length,
|
||||
errors,
|
||||
}
|
||||
|
||||
opLog.info('customer import complete', response)
|
||||
|
||||
return NextResponse.json({ data: response })
|
||||
} catch (err) {
|
||||
opLog.error('customer import execute failed', err as Error)
|
||||
return errorResponseFromCode('REG_IMPORT_EXECUTE_FAILED', opLog, {
|
||||
requestId,
|
||||
details: { reason: err instanceof Error ? err.message : 'unknown' },
|
||||
})
|
||||
}
|
||||
},
|
||||
{ requireWrite: true },
|
||||
)
|
||||
@@ -0,0 +1,120 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { parseCustomersFile } from '@/lib/import/customers/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 {
|
||||
AnnotatedCustomerRow,
|
||||
CustomerImportParseResult,
|
||||
DetectedCustomerColumns,
|
||||
} from '@/lib/import/customers/types'
|
||||
|
||||
const ALLOWED_EXTENSIONS = ['.xlsx', '.xls', '.csv', '.ods']
|
||||
const MAX_FILE_SIZE = 10 * 1024 * 1024 // 10 MB
|
||||
|
||||
/**
|
||||
* POST /api/import/customers/parse
|
||||
*
|
||||
* Accepts an Excel/CSV file via FormData, auto-detects columns, parses rows,
|
||||
* and annotates each row with any duplicate-match against existing customers.
|
||||
*/
|
||||
export const POST = withRouteContext(
|
||||
'register_import.customers.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: DetectedCustomerColumns | 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 = parseCustomersFile(buffer, file.name, columnOverrides)
|
||||
|
||||
// Fetch existing customers for duplicate detection.
|
||||
const existing = await fetchAllRows(({ from, to }) =>
|
||||
supabase
|
||||
.from('customers')
|
||||
.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 c of existing) {
|
||||
const org = normalizeOrgNumber(c.org_number)
|
||||
if (org) byOrg.set(org, { id: c.id, name: c.name })
|
||||
const email = normalizeEmail(c.email)
|
||||
if (email) byEmail.set(email, { id: c.id, name: c.name })
|
||||
}
|
||||
|
||||
let duplicateCount = 0
|
||||
const annotated: AnnotatedCustomerRow[] = parsed.rows.map((r) => {
|
||||
const orgKey = normalizeOrgNumber(r.org_number)
|
||||
const emailKey = normalizeEmail(r.email)
|
||||
let match: AnnotatedCustomerRow['duplicate_match'] = null
|
||||
if (orgKey && byOrg.has(orgKey)) {
|
||||
const e = byOrg.get(orgKey)!
|
||||
match = { customer_id: e.id, matched_by: 'org_number', existing_name: e.name }
|
||||
} else if (emailKey && byEmail.has(emailKey)) {
|
||||
const e = byEmail.get(emailKey)!
|
||||
match = { customer_id: e.id, matched_by: 'email', existing_name: e.name }
|
||||
}
|
||||
if (match) duplicateCount++
|
||||
return { ...r, duplicate_match: match }
|
||||
})
|
||||
|
||||
const result: CustomerImportParseResult = {
|
||||
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('customer import parse failed', err as Error)
|
||||
return errorResponseFromCode('REG_IMPORT_PARSE_FAILED', opLog, {
|
||||
requestId,
|
||||
details: { reason: err instanceof Error ? err.message : 'unknown' },
|
||||
})
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,193 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { ensureInitialized } from '@/lib/init'
|
||||
import { eventBus } from '@/lib/events'
|
||||
import { validateBody } from '@/lib/api/validate'
|
||||
import { SupplierImportExecuteSchema } from '@/lib/api/schemas'
|
||||
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 { Supplier } from '@/types'
|
||||
import type { SupplierImportExecuteResult } from '@/lib/import/suppliers/types'
|
||||
|
||||
ensureInitialized()
|
||||
|
||||
interface ExistingSupplier {
|
||||
id: string
|
||||
name: string
|
||||
org_number: string | null
|
||||
email: string | null
|
||||
}
|
||||
|
||||
export const POST = withRouteContext(
|
||||
'register_import.suppliers.execute',
|
||||
async (request, ctx) => {
|
||||
const { user, supabase, companyId, log, requestId } = ctx
|
||||
|
||||
const result = await validateBody(request, SupplierImportExecuteSchema, {
|
||||
log,
|
||||
operation: 'register_import.suppliers.execute',
|
||||
})
|
||||
if (!result.success) return result.response
|
||||
|
||||
const { rows, update_duplicates } = result.data
|
||||
const opLog = log.child({ rowCount: rows.length, updateDuplicates: update_duplicates })
|
||||
|
||||
if (rows.length === 0) {
|
||||
return errorResponseFromCode('REG_IMPORT_NO_ROWS', opLog, { requestId })
|
||||
}
|
||||
|
||||
try {
|
||||
const existingRaw = await fetchAllRows(({ from, to }) =>
|
||||
supabase
|
||||
.from('suppliers')
|
||||
.select('id, name, org_number, email')
|
||||
.eq('company_id', companyId)
|
||||
.range(from, to),
|
||||
)
|
||||
const existing = existingRaw as unknown as ExistingSupplier[]
|
||||
|
||||
const byOrg = new Map<string, ExistingSupplier>()
|
||||
const byEmail = new Map<string, ExistingSupplier>()
|
||||
for (const s of existing) {
|
||||
const org = normalizeOrgNumber(s.org_number)
|
||||
if (org) byOrg.set(org, s)
|
||||
const email = normalizeEmail(s.email)
|
||||
if (email) byEmail.set(email, s)
|
||||
}
|
||||
|
||||
const created: Supplier[] = []
|
||||
const updated: Supplier[] = []
|
||||
let skipped = 0
|
||||
const errors: { row_index: number; name: string; reason: string }[] = []
|
||||
|
||||
for (const row of rows) {
|
||||
const orgKey = normalizeOrgNumber(row.org_number)
|
||||
const emailKey = normalizeEmail(row.email)
|
||||
const match =
|
||||
(orgKey && byOrg.get(orgKey)) ||
|
||||
(emailKey && byEmail.get(emailKey)) ||
|
||||
null
|
||||
|
||||
if (match) {
|
||||
if (!update_duplicates) {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
const merged: Record<string, unknown> = {}
|
||||
if (row.name) merged.name = row.name
|
||||
if (row.supplier_type) merged.supplier_type = row.supplier_type
|
||||
if (row.org_number) merged.org_number = row.org_number
|
||||
if (row.email) merged.email = row.email
|
||||
if (row.phone) merged.phone = row.phone
|
||||
if (row.address_line1) merged.address_line1 = row.address_line1
|
||||
if (row.address_line2) merged.address_line2 = row.address_line2
|
||||
if (row.postal_code) merged.postal_code = row.postal_code
|
||||
if (row.city) merged.city = row.city
|
||||
if (row.country) merged.country = row.country
|
||||
if (row.vat_number) merged.vat_number = row.vat_number
|
||||
if (row.bankgiro) merged.bankgiro = row.bankgiro
|
||||
if (row.plusgiro) merged.plusgiro = row.plusgiro
|
||||
if (row.bank_account) merged.bank_account = row.bank_account
|
||||
if (row.iban) merged.iban = row.iban
|
||||
if (row.bic) merged.bic = row.bic
|
||||
if (row.default_payment_terms) merged.default_payment_terms = row.default_payment_terms
|
||||
if (row.default_currency) merged.default_currency = row.default_currency
|
||||
if (row.notes) merged.notes = row.notes
|
||||
|
||||
if (Object.keys(merged).length === 0) {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('suppliers')
|
||||
.update(merged)
|
||||
.eq('id', match.id)
|
||||
.eq('company_id', companyId)
|
||||
.select()
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
errors.push({ row_index: row.row_index, name: row.name, reason: error.message })
|
||||
continue
|
||||
}
|
||||
if (data) updated.push(data as Supplier)
|
||||
continue
|
||||
}
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('suppliers')
|
||||
.insert({
|
||||
user_id: user.id,
|
||||
company_id: companyId,
|
||||
name: row.name,
|
||||
supplier_type: row.supplier_type,
|
||||
email: row.email,
|
||||
phone: row.phone,
|
||||
address_line1: row.address_line1,
|
||||
address_line2: row.address_line2,
|
||||
postal_code: row.postal_code,
|
||||
city: row.city,
|
||||
country: row.country || 'SE',
|
||||
org_number: row.org_number,
|
||||
vat_number: row.vat_number,
|
||||
bankgiro: row.bankgiro,
|
||||
plusgiro: row.plusgiro,
|
||||
bank_account: row.bank_account,
|
||||
iban: row.iban,
|
||||
bic: row.bic,
|
||||
default_payment_terms: row.default_payment_terms || 30,
|
||||
default_currency: row.default_currency || 'SEK',
|
||||
notes: row.notes,
|
||||
})
|
||||
.select()
|
||||
.single()
|
||||
|
||||
if (error) {
|
||||
if (error.code === '23505') {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
errors.push({ row_index: row.row_index, name: row.name, reason: error.message })
|
||||
continue
|
||||
}
|
||||
if (data) {
|
||||
created.push(data as Supplier)
|
||||
const newOrg = normalizeOrgNumber(data.org_number)
|
||||
if (newOrg) byOrg.set(newOrg, data as ExistingSupplier)
|
||||
const newEmail = normalizeEmail(data.email)
|
||||
if (newEmail) byEmail.set(newEmail, data as ExistingSupplier)
|
||||
}
|
||||
}
|
||||
|
||||
for (const s of created) {
|
||||
await eventBus.emit({
|
||||
type: 'supplier.created',
|
||||
payload: { supplier: s, companyId, userId: user.id },
|
||||
})
|
||||
}
|
||||
|
||||
const response: SupplierImportExecuteResult = {
|
||||
success: errors.length === 0,
|
||||
created: created.length,
|
||||
updated: updated.length,
|
||||
skipped,
|
||||
failed: errors.length,
|
||||
errors,
|
||||
}
|
||||
|
||||
opLog.info('supplier import complete', response)
|
||||
|
||||
return NextResponse.json({ data: response })
|
||||
} catch (err) {
|
||||
opLog.error('supplier import execute failed', err as Error)
|
||||
return errorResponseFromCode('REG_IMPORT_EXECUTE_FAILED', opLog, {
|
||||
requestId,
|
||||
details: { reason: err instanceof Error ? err.message : 'unknown' },
|
||||
})
|
||||
}
|
||||
},
|
||||
{ requireWrite: true },
|
||||
)
|
||||
@@ -0,0 +1,113 @@
|
||||
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'
|
||||
|
||||
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 ? err.message : 'unknown' },
|
||||
})
|
||||
}
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user