e1f13f870a
* 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>
256 lines
9.7 KiB
TypeScript
256 lines
9.7 KiB
TypeScript
'use client'
|
|
|
|
import { useTranslations } from 'next-intl'
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Badge } from '@/components/ui/badge'
|
|
import {
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableHead,
|
|
TableHeader,
|
|
TableRow,
|
|
} from '@/components/ui/table'
|
|
import {
|
|
ArrowLeft,
|
|
ArrowRight,
|
|
AlertTriangle,
|
|
Calendar,
|
|
TrendingUp,
|
|
TrendingDown,
|
|
FileText,
|
|
} from 'lucide-react'
|
|
import { formatCurrency } from '@/lib/utils'
|
|
import { summarizeByCurrency } from '@/lib/import/bank-file/currency-summary'
|
|
import type { BankFileParseResult, BankFileDuplicateInfo } from '@/lib/import/bank-file/types'
|
|
|
|
interface BankFilePreviewStepProps {
|
|
parseResult: BankFileParseResult
|
|
duplicateInfo?: BankFileDuplicateInfo | null
|
|
onContinue: () => void
|
|
onBack: () => void
|
|
}
|
|
|
|
export default function BankFilePreviewStep({
|
|
parseResult,
|
|
duplicateInfo,
|
|
onContinue,
|
|
onBack,
|
|
}: BankFilePreviewStepProps) {
|
|
const t = useTranslations('transactions')
|
|
const { transactions, stats, issues, date_from, date_to } = parseResult
|
|
const errors = issues.filter((i) => i.severity === 'error')
|
|
const hasIssues = errors.length > 0
|
|
const warnings = issues.filter((i) => i.severity === 'warning')
|
|
// Wise/camt.053 files can mix currencies per row: the parser-level totals
|
|
// sum across currencies, so income/expenses are grouped per currency here.
|
|
const currencyTotals = summarizeByCurrency(transactions)
|
|
const duplicateCount = duplicateInfo?.duplicate_count ?? 0
|
|
// Table rows render transactions.slice(0, 50), so the slice index IS the
|
|
// original array index the server flagged.
|
|
const duplicateRowSet = new Set(duplicateInfo?.duplicate_row_indexes ?? [])
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
{/* Summary cards */}
|
|
<div className="grid grid-cols-2 gap-4 md:grid-cols-4">
|
|
<Card>
|
|
<CardContent className="pt-6">
|
|
<div className="flex items-center gap-2 text-muted-foreground mb-1">
|
|
<FileText className="h-4 w-4" />
|
|
<span className="text-sm">Transaktioner</span>
|
|
</div>
|
|
<p className="text-2xl font-display tabular-nums">{stats.parsed_rows}</p>
|
|
{stats.skipped_rows > 0 && (
|
|
<p className="text-xs text-muted-foreground mt-1">
|
|
{stats.skipped_rows} rader hoppades över
|
|
</p>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card>
|
|
<CardContent className="pt-6">
|
|
<div className="flex items-center gap-2 text-muted-foreground mb-1">
|
|
<Calendar className="h-4 w-4" />
|
|
<span className="text-sm">Period</span>
|
|
</div>
|
|
<p className="text-sm font-medium">
|
|
{date_from || '-'} till {date_to || '-'}
|
|
</p>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card>
|
|
<CardContent className="pt-6">
|
|
<div className="flex items-center gap-2 text-muted-foreground mb-1">
|
|
<TrendingUp className="h-4 w-4" />
|
|
<span className="text-sm">Inkomster</span>
|
|
</div>
|
|
{(currencyTotals.length ? currencyTotals : [{ currency: 'SEK', total_income: 0, total_expenses: 0 }]).map((row) => (
|
|
<p key={row.currency} className="text-lg font-display tabular-nums">
|
|
{formatCurrency(row.total_income, row.currency)}
|
|
</p>
|
|
))}
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<Card>
|
|
<CardContent className="pt-6">
|
|
<div className="flex items-center gap-2 text-muted-foreground mb-1">
|
|
<TrendingDown className="h-4 w-4" />
|
|
<span className="text-sm">Utgifter</span>
|
|
</div>
|
|
{(currencyTotals.length ? currencyTotals : [{ currency: 'SEK', total_income: 0, total_expenses: 0 }]).map((row) => (
|
|
<p key={row.currency} className="text-lg font-display tabular-nums">
|
|
{formatCurrency(row.total_expenses, row.currency)}
|
|
</p>
|
|
))}
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
|
|
{/* Duplicate rows already in bookkeeping: advisory, ingest skips them */}
|
|
{duplicateCount > 0 && (
|
|
<Card>
|
|
<CardHeader className="py-3">
|
|
<CardTitle className="text-sm flex items-center gap-2 text-warning">
|
|
<AlertTriangle className="h-4 w-4" />
|
|
{t('import_duplicate_rows_title', { count: duplicateCount })}
|
|
</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="pt-0">
|
|
<p className="text-xs text-muted-foreground">
|
|
{t('import_duplicate_rows_body')}
|
|
</p>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
|
|
{/* Warnings */}
|
|
{warnings.length > 0 && (
|
|
<Card>
|
|
<CardHeader className="py-3">
|
|
<CardTitle className="text-sm flex items-center gap-2 text-warning">
|
|
<AlertTriangle className="h-4 w-4" />
|
|
{warnings.length} varning{warnings.length !== 1 ? 'ar' : ''}
|
|
</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="pt-0">
|
|
<div className="space-y-1 max-h-32 overflow-y-auto">
|
|
{warnings.slice(0, 10).map((issue, i) => (
|
|
<p key={i} className="text-xs text-muted-foreground">
|
|
Rad {issue.row}: {issue.message}
|
|
</p>
|
|
))}
|
|
{warnings.length > 10 && (
|
|
<p className="text-xs text-muted-foreground font-medium">
|
|
...och {warnings.length - 10} till
|
|
</p>
|
|
)}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
|
|
{/* Transaction preview table */}
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle className="text-base">Transaktioner</CardTitle>
|
|
<CardDescription>
|
|
Förhandsgranskning av de {Math.min(transactions.length, 50)} första transaktionerna
|
|
</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="rounded-md border max-h-96 overflow-x-auto overflow-y-auto">
|
|
<Table>
|
|
<TableHeader>
|
|
<TableRow>
|
|
<TableHead className="w-28">Datum</TableHead>
|
|
<TableHead>Beskrivning</TableHead>
|
|
<TableHead className="text-right w-32">Belopp</TableHead>
|
|
{transactions.some((t) => t.balance != null) && (
|
|
<TableHead className="text-right w-32">Saldo</TableHead>
|
|
)}
|
|
{transactions.some((t) => t.reference) && (
|
|
<TableHead className="w-32">Referens</TableHead>
|
|
)}
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody>
|
|
{transactions.slice(0, 50).map((tx, i) => (
|
|
<TableRow key={i}>
|
|
<TableCell className="font-mono text-sm">{tx.date}</TableCell>
|
|
<TableCell className="text-sm">
|
|
{tx.description}
|
|
{duplicateRowSet.has(i) && (
|
|
<Badge variant="secondary" className="ml-2">
|
|
{t('import_duplicate_row_badge')}
|
|
</Badge>
|
|
)}
|
|
</TableCell>
|
|
<TableCell
|
|
className="text-right font-mono text-sm"
|
|
>
|
|
{formatCurrency(tx.amount, tx.currency || 'SEK')}
|
|
</TableCell>
|
|
{transactions.some((t) => t.balance != null) && (
|
|
<TableCell className="text-right font-mono text-sm text-muted-foreground">
|
|
{tx.balance != null ? formatCurrency(tx.balance, tx.currency || 'SEK') : '-'}
|
|
</TableCell>
|
|
)}
|
|
{transactions.some((t) => t.reference) && (
|
|
<TableCell className="font-mono text-sm text-muted-foreground">
|
|
{tx.reference ? tx.reference : '-'}
|
|
</TableCell>
|
|
)}
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
{transactions.length > 50 && (
|
|
<p className="text-sm text-muted-foreground mt-2 text-center">
|
|
Visar 50 av {transactions.length} transaktioner
|
|
</p>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Error blocking continuation */}
|
|
{hasIssues && (
|
|
<Card className="border-destructive">
|
|
<CardContent className="pt-6">
|
|
<div className="flex gap-3">
|
|
<AlertTriangle className="h-5 w-5 text-destructive flex-shrink-0 mt-0.5" />
|
|
<div className="min-w-0 space-y-2">
|
|
<p className="font-medium text-destructive">Filen innehåller fel som förhindrar import</p>
|
|
<div className="max-h-32 space-y-1 overflow-y-auto">
|
|
{errors.map((issue, i) => (
|
|
<p key={i} className="text-xs text-muted-foreground">
|
|
Rad {issue.row}: {issue.message}
|
|
</p>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
)}
|
|
|
|
{/* Navigation */}
|
|
<div className="flex flex-col-reverse gap-3 sm:flex-row sm:justify-between">
|
|
<Button variant="outline" className="min-h-11" onClick={onBack}>
|
|
<ArrowLeft className="mr-2 h-4 w-4" />
|
|
Tillbaka
|
|
</Button>
|
|
<Button className="min-h-11" onClick={onContinue} disabled={hasIssues || transactions.length === 0}>
|
|
Fortsätt
|
|
<ArrowRight className="ml-2 h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|