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>
277 lines
10 KiB
TypeScript
277 lines
10 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect } from 'react'
|
|
import { useTranslations } from 'next-intl'
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Label } from '@/components/ui/label'
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
|
import {
|
|
ArrowLeft,
|
|
Loader2,
|
|
Play,
|
|
FileText,
|
|
Link2,
|
|
Calendar,
|
|
Landmark,
|
|
AlertTriangle,
|
|
} from 'lucide-react'
|
|
import { formatCurrency } from '@/lib/utils'
|
|
import { summarizeByCurrency } from '@/lib/import/bank-file/currency-summary'
|
|
import { createClient } from '@/lib/supabase/client'
|
|
import { useCompany } from '@/contexts/CompanyContext'
|
|
import type { BankFileParseResult, BankFileDuplicateInfo } from '@/lib/import/bank-file/types'
|
|
|
|
interface BankAccount {
|
|
account_number: string
|
|
account_name: string
|
|
}
|
|
|
|
interface BankFileConfirmStepProps {
|
|
parseResult: BankFileParseResult
|
|
duplicateInfo?: BankFileDuplicateInfo | null
|
|
onExecute: (options: { skip_duplicates: boolean; auto_categorize: boolean; settlement_account?: string }) => void
|
|
onBack: () => void
|
|
isLoading: boolean
|
|
}
|
|
|
|
export default function BankFileConfirmStep({
|
|
parseResult,
|
|
duplicateInfo,
|
|
onExecute,
|
|
onBack,
|
|
isLoading,
|
|
}: BankFileConfirmStepProps) {
|
|
const t = useTranslations('transactions')
|
|
const { transactions, stats, date_from, date_to, issues } = parseResult
|
|
const refsCount = transactions.filter((tx) => tx.reference).length
|
|
const warnings = issues.filter((i) => i.severity === 'warning')
|
|
// Same per-currency grouping as the preview step: parser-level totals sum
|
|
// across currencies, which misleads on Wise/camt.053 multi-currency files.
|
|
const currencyTotals = summarizeByCurrency(transactions)
|
|
// Advisory: clamp so a stale preview can never produce a negative CTA
|
|
// count. Execute stays authoritative; the copy says rows are skipped
|
|
// automatically rather than promising an exact final number.
|
|
const duplicateCount = Math.min(Math.max(duplicateInfo?.duplicate_count ?? 0, 0), stats.parsed_rows)
|
|
|
|
const [bankAccounts, setBankAccounts] = useState<BankAccount[]>([])
|
|
const [selectedAccount, setSelectedAccount] = useState('1930')
|
|
const { company } = useCompany()
|
|
|
|
useEffect(() => {
|
|
if (!company?.id) return
|
|
async function fetchBankAccounts(companyId: string) {
|
|
const supabase = createClient()
|
|
const { data } = await supabase
|
|
.from('chart_of_accounts')
|
|
.select('account_number, account_name')
|
|
.eq('company_id', companyId)
|
|
.eq('is_active', true)
|
|
.gte('account_number', '1900')
|
|
.lte('account_number', '1999')
|
|
.order('account_number')
|
|
|
|
if (data && data.length > 0) {
|
|
setBankAccounts(data)
|
|
// Default to 1930 if available, otherwise first account
|
|
const has1930 = data.some(a => a.account_number === '1930')
|
|
if (!has1930) setSelectedAccount(data[0].account_number)
|
|
}
|
|
}
|
|
fetchBankAccounts(company.id)
|
|
}, [company?.id])
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="flex flex-col items-center justify-center py-24 space-y-6">
|
|
<div className="relative">
|
|
<Loader2 className="h-12 w-12 animate-spin text-primary" />
|
|
</div>
|
|
<div className="text-center space-y-2">
|
|
<p className="text-lg font-medium">Importerar transaktioner...</p>
|
|
<p className="text-sm text-muted-foreground">
|
|
{stats.parsed_rows} transaktioner bearbetas
|
|
</p>
|
|
</div>
|
|
<div className="w-48 h-1 bg-muted rounded-full overflow-hidden">
|
|
<div className="h-full bg-primary rounded-full animate-pulse" style={{ width: '60%' }} />
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
{/* Summary */}
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Bekräfta import</CardTitle>
|
|
<CardDescription>
|
|
Granska sammanfattningen och importera transaktionerna.
|
|
</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="space-y-6">
|
|
{/* Stats grid */}
|
|
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
|
|
<div className="p-4 bg-muted/50 rounded-lg">
|
|
<div className="flex items-center gap-2 text-muted-foreground mb-1">
|
|
<FileText className="h-4 w-4" />
|
|
<span className="text-xs">Transaktioner</span>
|
|
</div>
|
|
<p className="text-xl 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>
|
|
)}
|
|
</div>
|
|
|
|
<div className="p-4 bg-muted/50 rounded-lg">
|
|
<div className="flex items-center gap-2 text-muted-foreground mb-1">
|
|
<Calendar className="h-4 w-4" />
|
|
<span className="text-xs">Period</span>
|
|
</div>
|
|
<p className="text-sm font-medium">
|
|
{date_from}: {date_to}
|
|
</p>
|
|
</div>
|
|
|
|
<div className="p-4 bg-muted/50 rounded-lg">
|
|
<div className="flex items-center gap-2 text-muted-foreground mb-1">
|
|
<span className="text-xs">Inkomster</span>
|
|
</div>
|
|
{(currencyTotals.length ? currencyTotals : [{ currency: 'SEK', total_income: 0, total_expenses: 0 }]).map((row) => (
|
|
<p key={row.currency} className="text-xl font-display tabular-nums">
|
|
{formatCurrency(row.total_income, row.currency)}
|
|
</p>
|
|
))}
|
|
</div>
|
|
|
|
<div className="p-4 bg-muted/50 rounded-lg">
|
|
<div className="flex items-center gap-2 text-muted-foreground mb-1">
|
|
<span className="text-xs">Utgifter</span>
|
|
</div>
|
|
{(currencyTotals.length ? currencyTotals : [{ currency: 'SEK', total_income: 0, total_expenses: 0 }]).map((row) => (
|
|
<p key={row.currency} className="text-xl font-display tabular-nums">
|
|
{formatCurrency(row.total_expenses, row.currency)}
|
|
</p>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Bank account selector */}
|
|
{bankAccounts.length > 1 && (
|
|
<div className="space-y-2">
|
|
<Label className="flex items-center gap-2">
|
|
<Landmark className="h-4 w-4 text-muted-foreground" />
|
|
Bankkonto
|
|
</Label>
|
|
<Select value={selectedAccount} onValueChange={setSelectedAccount}>
|
|
<SelectTrigger className="w-full sm:w-72">
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{bankAccounts.map((account) => (
|
|
<SelectItem key={account.account_number} value={account.account_number}>
|
|
<span className="font-mono">{account.account_number}</span>
|
|
{' '}
|
|
{account.account_name}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
<p className="text-xs text-muted-foreground">
|
|
Välj vilket bankkonto transaktionerna ska bokföras mot.
|
|
</p>
|
|
</div>
|
|
)}
|
|
|
|
{/* Additional info */}
|
|
{refsCount > 0 && (
|
|
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
|
<Link2 className="h-3 w-3" />
|
|
{refsCount} med OCR/referens
|
|
</div>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Duplicate rows: repeated here because the generic_csv path skips the
|
|
preview step where the same card is shown. Advisory: ingest skips
|
|
them automatically at execute. */}
|
|
{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>
|
|
<CardDescription>
|
|
{t('import_duplicate_rows_body')}
|
|
</CardDescription>
|
|
</CardHeader>
|
|
</Card>
|
|
)}
|
|
|
|
{/* Skipped rows: surfaced here because the manual-mapping path skips the
|
|
preview step where these warnings would otherwise be shown. */}
|
|
{warnings.length > 0 && (
|
|
<Card>
|
|
<CardHeader className="py-3">
|
|
<CardTitle className="text-sm flex items-center gap-2">
|
|
<AlertTriangle className="h-4 w-4 text-warning" />
|
|
{warnings.length} {warnings.length === 1 ? 'rad' : 'rader'} hoppades över
|
|
</CardTitle>
|
|
<CardDescription>
|
|
Dessa rader kunde inte läsas och importeras inte. Kontrollera att inga transaktioner saknas.
|
|
</CardDescription>
|
|
</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>
|
|
)}
|
|
|
|
{/* Actions */}
|
|
<div className="flex flex-col-reverse gap-3 sm:flex-row sm:justify-between">
|
|
<Button variant="outline" className="min-h-11" onClick={onBack} disabled={isLoading}>
|
|
<ArrowLeft className="mr-2 h-4 w-4" />
|
|
Tillbaka
|
|
</Button>
|
|
<Button
|
|
className="min-h-11"
|
|
onClick={() => onExecute({
|
|
skip_duplicates: true,
|
|
auto_categorize: false,
|
|
settlement_account: selectedAccount !== '1930' ? selectedAccount : undefined,
|
|
})}
|
|
disabled={isLoading}
|
|
>
|
|
{isLoading ? (
|
|
<>
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
|
Importerar...
|
|
</>
|
|
) : (
|
|
<>
|
|
<Play className="mr-2 h-4 w-4" />
|
|
Importera {stats.parsed_rows - duplicateCount} transaktioner
|
|
</>
|
|
)}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|