Files
accounted/lib/import/bank-file/formats/wise.ts
T
1dc85736d8 feat(import): add Wise (TransferWise) CSV import format (#1018)
* feat(import): add Wise (TransferWise) CSV import format

Wise exports a single multi-currency transaction history (one row per balance
movement). Add it as a bank-file format plugin so it flows through the existing
upload -> preview -> confirm -> execute wizard.

- lib/import/bank-file/formats/wise.ts: quote-aware parse (dates contain a
  space), Direction IN/OUT drives the sign, booked on the moved side (target
  for IN, source for OUT). Native currency preserved; SEK conversion is left to
  the downstream FX/booking pipeline (Riksbanken).
- Non-zero Wise fees become their own negative "Wise avgift" row (source and
  target), so the fee books separately and the balance ties out.
- Only COMPLETED rows import. external_id keys on the stable Wise ID
  (TRANSFER-/PLAN_ORDER-, -fee suffix for fee rows) via a new 'wise' branch in
  generateExternalId, so re-imports dedup exactly.
- Register the format (types, parser list), add it to the manual-format picker
  and the v1 /imports/bank format enum.

Tests cover detection, IN/OUT signing + currency, fee splitting, stable
external_id, and COMPLETED-only filtering.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Alexander Reinthal <email@reinthal.me>

* fix(import): harden Wise parser against malformed rows (CodeRabbit #1018)

- Strict amount parsing: reject "12abc"/"1,234" instead of parseFloat coercing
  them to 12/1 and silently corrupting the imported amount.
- Require Status to be exactly COMPLETED: a blank/missing status no longer
  slips through the completed-only filter.
- Fail hard on an unsupported Direction: a blank or non-IN/OUT value (e.g.
  NEUTRAL for a balance conversion) throws instead of being guessed as income;
  the parse route surfaces it as BANK_FILE_PARSE_FAILED. Proper conversion
  support is tracked in #1019.
- Never invent currencies: a missing movement currency skips the row with a
  warning (no SEK default), and a fee with no currency of its own is dropped
  with a warning rather than inheriting the movement currency.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Alexander Reinthal <email@reinthal.me>

---------

Signed-off-by: Alexander Reinthal <email@reinthal.me>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Jakob Wennberg <jakob.wennberg@gmail.com>
2026-07-16 16:22:32 +02:00

244 lines
10 KiB
TypeScript

/**
* Wise (TransferWise) transaction-history CSV parser.
*
* Wise exports a single multi-currency statement (one row per balance movement)
* with a header like:
* ID,Status,Direction,"Created on","Finished on","Source fee amount",
* "Source fee currency","Target fee amount","Target fee currency",
* "Source name","Source amount (after fees)","Source currency",
* "Target name","Target amount (after fees)","Target currency",
* "Exchange rate",Reference,Batch,"Created by",Category,Note
*
* Design notes:
* - Comma-delimited, `.` decimal, fields quoted (dates contain a space, so a
* quote-aware splitter is required: parseCSVLine).
* - Direction IN/OUT drives the sign. We book the row in the currency that
* actually moved on the balance: the target side for IN, the source side for
* OUT. Amounts stay in their native currency; SEK conversion happens
* downstream at booking time via the existing FX pipeline (Riksbanken), so
* this parser never converts.
* - Wise fees are a real cost, so a non-zero source/target fee becomes its OWN
* negative transaction ("Wise avgift") rather than being folded or dropped.
* - Only COMPLETED rows are imported; pending/cancelled/refunded rows are skipped.
* - The stable Wise ID (TRANSFER-…, PLAN_ORDER-…) is carried in `raw_line` so
* generateExternalId can key dedup on it instead of a row hash (fee rows get
* an `<id>-fee` / `<id>-tgtfee` suffix).
*/
import type { BankFileFormat, BankFileParseResult, ParsedBankTransaction, BankFileParseIssue } from '../types'
import { prepareContent } from '../../shared/encoding'
import { normalizeDate } from '../date-utils'
import { parseCSVLine } from './nordea'
/** Money rule: round to two decimals without toFixed. */
function round2(n: number): number {
return Math.round(n * 100) / 100
}
/**
* Parse a Wise amount ("2500.0", "520.00", "-2.20"). Wise amounts are plain
* decimals: optional sign, digits, optional `.` fraction, no thousands
* separator. Reject anything else: bare parseFloat would silently turn "12abc"
* into 12 and "1,234" into 1, corrupting the imported amount. NaN = reject.
*/
function parseWiseAmount(value: string | undefined): number {
if (!value) return NaN
const cleaned = value.trim()
if (!/^-?\d+(\.\d+)?$/.test(cleaned)) return NaN
return parseFloat(cleaned)
}
/** Wise datetimes are "YYYY-MM-DD HH:MM:SS"; keep the date part only. */
function wiseDate(value: string | undefined): string | null {
if (!value) return null
return normalizeDate(value.trim().split(/[ T]/)[0])
}
const HEADER_TOKENS = ['direction', 'source amount (after fees)', 'target amount (after fees)']
export const wiseFormat: BankFileFormat = {
id: 'wise',
name: 'Wise',
description: 'Wise (TransferWise) transaction history CSV, multi-currency',
fileExtensions: ['.csv'],
detect(content: string, _filename: string): boolean {
const firstLine = prepareContent(content).split('\n')[0]?.toLowerCase() || ''
// Wise's header is distinctive: the "(after fees)" amount columns plus a
// Direction column don't appear in any Swedish-bank export.
return firstLine.includes(',') && HEADER_TOKENS.every((t) => firstLine.includes(t))
},
parse(content: string): BankFileParseResult {
const prepared = prepareContent(content)
const lines = prepared.split('\n').filter((line) => line.trim() !== '')
const transactions: ParsedBankTransaction[] = []
const issues: BankFileParseIssue[] = []
let skippedRows = 0
const headers = parseCSVLine(lines[0] || '', ',').map((h) =>
h.trim().toLowerCase().replace(/^"|"$/g, ''),
)
const col = (name: string) => headers.findIndex((h) => h === name)
const idx = {
id: col('id'),
status: col('status'),
direction: col('direction'),
createdOn: col('created on'),
finishedOn: col('finished on'),
sourceFeeAmount: col('source fee amount'),
sourceFeeCurrency: col('source fee currency'),
targetFeeAmount: col('target fee amount'),
targetFeeCurrency: col('target fee currency'),
sourceName: col('source name'),
sourceAmount: col('source amount (after fees)'),
sourceCurrency: col('source currency'),
targetName: col('target name'),
targetAmount: col('target amount (after fees)'),
targetCurrency: col('target currency'),
reference: col('reference'),
category: col('category'),
note: col('note'),
}
if (idx.direction === -1 || idx.sourceAmount === -1 || idx.targetAmount === -1) {
issues.push({ row: 1, message: 'Could not identify required Wise columns', severity: 'error' })
return {
format: 'wise',
format_name: 'Wise',
transactions: [],
date_from: null,
date_to: null,
issues,
stats: { total_rows: 0, parsed_rows: 0, skipped_rows: 0, total_income: 0, total_expenses: 0 },
}
}
for (let i = 1; i < lines.length; i++) {
const fields = parseCSVLine(lines[i], ',').map((f) => f.trim().replace(/^"|"$/g, ''))
const at = (j: number) => (j >= 0 ? fields[j] ?? '' : '')
const wiseId = at(idx.id)
const status = at(idx.status).toUpperCase()
// Only settled movements affect the balance. A blank/missing status is
// NOT completed, so it must not slip through: require an exact match.
if (status !== 'COMPLETED') {
skippedRows++
continue
}
// Direction drives the sign. An unrecognized value (blank, or NEUTRAL for
// a balance conversion/cashback, or anything Wise adds later) must NOT be
// guessed: silently treating it as income mis-signs real money. Fail the
// whole import so it surfaces (the parse route turns this throw into
// BANK_FILE_PARSE_FAILED). Proper conversion handling is tracked in #1019.
const direction = at(idx.direction).toUpperCase()
if (direction !== 'IN' && direction !== 'OUT') {
throw new Error(
`Wise import: unsupported Direction "${at(idx.direction)}" on ${wiseId || `row ${i + 1}`}`,
)
}
const isOut = direction === 'OUT'
// Book the side that moved on the balance: target for IN, source for OUT.
// Never invent a currency: a missing/malformed one is a bad row, skip it.
const currency = (isOut ? at(idx.sourceCurrency) : at(idx.targetCurrency)).trim().toUpperCase()
if (!/^[A-Z]{3}$/.test(currency)) {
issues.push({ row: i + 1, message: `Missing/invalid currency on ${wiseId || 'row'}`, severity: 'warning' })
skippedRows++
continue
}
const rawAmount = parseWiseAmount(isOut ? at(idx.sourceAmount) : at(idx.targetAmount))
const date = wiseDate(at(idx.finishedOn)) || wiseDate(at(idx.createdOn))
if (!date) {
issues.push({ row: i + 1, message: `Invalid date on ${wiseId || 'row'}`, severity: 'warning' })
skippedRows++
continue
}
if (!Number.isFinite(rawAmount)) {
issues.push({ row: i + 1, message: `Invalid amount on ${wiseId || 'row'}`, severity: 'warning' })
skippedRows++
continue
}
const counterparty = (isOut ? at(idx.targetName) : at(idx.sourceName)).trim()
const note = at(idx.note).trim()
const reference = at(idx.reference).trim()
const category = at(idx.category).trim()
const description =
[counterparty, note].filter(Boolean).join(' - ') || reference || category || 'Wise-transaktion'
// Signed movement: OUT leaves the balance (negative), IN enters it.
const amount = round2(isOut ? -Math.abs(rawAmount) : Math.abs(rawAmount))
transactions.push({
date,
description,
amount,
currency,
balance: null,
reference: reference || null,
counterparty: counterparty || null,
// Stable Wise ID drives dedup (see generateExternalId).
raw_line: wiseId || undefined,
})
// Fees are a real cost: emit each non-zero fee as its own negative row so
// it lands in the inbox to categorize (e.g. 6570) and the balance ties out.
const feeSpecs: Array<{ amountCol: number; currencyCol: number; suffix: string }> = [
{ amountCol: idx.sourceFeeAmount, currencyCol: idx.sourceFeeCurrency, suffix: 'fee' },
{ amountCol: idx.targetFeeAmount, currencyCol: idx.targetFeeCurrency, suffix: 'tgtfee' },
]
for (const spec of feeSpecs) {
const feeRaw = at(spec.amountCol).trim()
if (!feeRaw) continue // No fee column value: normal, most rows.
const fee = parseWiseAmount(feeRaw)
if (fee === 0) continue // Explicit 0.00 fee: nothing to book, no warning.
if (!Number.isFinite(fee) || fee < 0) {
issues.push({ row: i + 1, message: `Invalid fee amount "${feeRaw}" on ${wiseId || 'row'}`, severity: 'warning' })
continue
}
// Never inherit the movement currency for the fee: a fee amount without
// its own currency is a bad row, so flag and skip it rather than guess.
const feeCurrency = at(spec.currencyCol).trim().toUpperCase()
if (!/^[A-Z]{3}$/.test(feeCurrency)) {
issues.push({ row: i + 1, message: `Fee on ${wiseId || 'row'} has no currency; skipped`, severity: 'warning' })
continue
}
transactions.push({
date,
description: `Wise avgift${counterparty ? ` (${counterparty})` : ''}`,
amount: round2(-Math.abs(fee)),
currency: feeCurrency,
balance: null,
reference: reference || null,
counterparty: 'Wise',
raw_line: wiseId ? `${wiseId}-${spec.suffix}` : undefined,
})
}
}
const dates = transactions.map((t) => t.date).sort()
return {
format: 'wise',
format_name: 'Wise',
transactions,
date_from: dates[0] || null,
date_to: dates[dates.length - 1] || null,
issues,
stats: {
total_rows: lines.length - 1,
parsed_rows: transactions.length,
skipped_rows: skippedRows,
total_income: round2(transactions.filter((t) => t.amount > 0).reduce((s, t) => s + t.amount, 0)),
total_expenses: round2(transactions.filter((t) => t.amount < 0).reduce((s, t) => s + t.amount, 0)),
},
}
},
}