* fix(transactions): paginate the ingest dedup maps past the 1000-row cap
buildExistingTransactionMaps issued un-paginated selects for the booked and
unbooked dedup maps, so PostgREST silently truncated each at 1000 rows: a
re-import over a wide date range in an active company deduped against a
partial map and inserted everything past the cap as duplicates. Both queries
now go through fetchAllRows with a stable .order('id') for range paging.
Also exports the function and its types for the upcoming read-only duplicate
preview, which must share the exact stored-row universe execute-side ingest
dedups against.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* feat(import): add read-only duplicate preview endpoint for bank files
New POST /api/import/bank-file/check-duplicates (withRouteContext + Zod,
transactions capped at 20000) computes external_ids with the exact
generateExternalId(tx, format, index) derivation execute uses and runs
previewDuplicates: Layer-1 id collisions plus the Layer-2 text bridge with
counting semantics and the currency guard, against the same stored-row maps
ingest builds (buildExistingTransactionMaps). The result is advisory; execute
stays authoritative and mirrors/settlement-account guards are documented
preview/execute differences.
A dedicated endpoint because the generic_csv path re-parses client-side and
never re-hits /parse. Also removes the dead existing_transaction_count field
from the parse response (a raw date-range count consumed by nothing).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* feat(import): surface duplicate rows in the bank-file import wizard
Overlapping bank imports used to dedup silently: the wizard promised
'Importera N transaktioner', ingest skipped the twins, and the user saw fewer
rows than parsed with zero explanation. The wizard now calls check-duplicates
after a successful parse AND inside handleColumnMappingConfirm (the
generic_csv path never re-hits parse), and:
- BankFilePreviewStep: warning card in the AlertTriangle pattern ('{count}
rader finns redan', skipped automatically) plus a 'Finns redan' badge on
flagged rows in the 50-row table
- BankFileConfirmStep: repeats the summary card (generic path skips preview)
and the CTA counts 'Importera {parsed - duplicates} transaktioner'
- BankFileResultStep: renders result.duplicates when > 0, closing the loop
ingest.ts documents as unrendered
Execute semantics unchanged: all rows are sent, ingest skips; the preview is
advisory and never promises an exact final number. New strings in both
messages/sv.json and messages/en.json next to the import_psd2 anchors.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
---------
Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
93 lines
3.4 KiB
TypeScript
93 lines
3.4 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { parseBankFile, generateFileHash, detectFileFormat } from '@/lib/import/bank-file/parser'
|
|
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'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
/**
|
|
* POST /api/import/bank-file/parse
|
|
*
|
|
* Accepts a bank file (CSV/XML) via FormData, auto-detects format, and returns
|
|
* a parsed transactions preview with duplicate detection.
|
|
*/
|
|
export const POST = withRouteContext(
|
|
'bank_file.parse',
|
|
async (request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
const formData = await request.formData()
|
|
const file = formData.get('file') as File | null
|
|
const formatOverride = formData.get('format') as BankFileFormatId | null
|
|
|
|
if (!file) {
|
|
return errorResponseFromCode('BANK_FILE_NO_FILE', log, { requestId })
|
|
}
|
|
|
|
if (file.size > 10 * 1024 * 1024) {
|
|
return errorResponseFromCode('BANK_FILE_TOO_LARGE', log, {
|
|
requestId,
|
|
details: { sizeMb: +(file.size / 1024 / 1024).toFixed(1) },
|
|
})
|
|
}
|
|
|
|
const opLog = log.child({ filename: file.name, sizeBytes: file.size })
|
|
|
|
try {
|
|
const arrayBuffer = await file.arrayBuffer()
|
|
const content = decodeFileContent(arrayBuffer)
|
|
const fileHash = generateFileHash(content)
|
|
|
|
const { data: existingImport } = await supabase
|
|
.from('bank_file_imports')
|
|
.select('id, status, imported_count, created_at')
|
|
.eq('company_id', companyId)
|
|
.eq('file_hash', fileHash)
|
|
.single()
|
|
|
|
if (existingImport && existingImport.status === 'completed') {
|
|
return errorResponseFromCode('BANK_FILE_DUPLICATE', opLog, {
|
|
requestId,
|
|
details: {
|
|
importId: existingImport.id,
|
|
importedCount: existingImport.imported_count,
|
|
importedAt: existingImport.created_at,
|
|
},
|
|
})
|
|
}
|
|
|
|
const detectedFormat = formatOverride
|
|
? null
|
|
: detectFileFormat(content, file.name)
|
|
|
|
const parseResult = parseBankFile(content, file.name, formatOverride || undefined)
|
|
|
|
// Per-row duplicate detection deliberately does NOT live here: the
|
|
// wizard calls POST /api/import/bank-file/check-duplicates after every
|
|
// (re-)parse, including the client-side generic_csv re-parse that never
|
|
// hits this route. The old existing_transaction_count field (a raw count
|
|
// of ALL transactions in the date range) was consumed by nothing and has
|
|
// been removed.
|
|
return NextResponse.json({
|
|
data: {
|
|
parse_result: parseResult,
|
|
detected_format: detectedFormat?.id || formatOverride || null,
|
|
detected_format_name: detectedFormat?.name || parseResult.format_name,
|
|
file_hash: fileHash,
|
|
filename: file.name,
|
|
headers: parseResult.format === 'generic_csv'
|
|
? content.split('\n')[0]?.split(',').map((h) => h.trim()) || []
|
|
: null,
|
|
},
|
|
})
|
|
} catch (err) {
|
|
opLog.error('bank file parse failed', err as Error)
|
|
return errorResponseFromCode('BANK_FILE_PARSE_FAILED', opLog, {
|
|
requestId,
|
|
details: { reason: err instanceof Error ? getUserErrorMessage(err) : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
)
|