f24b26a139
* fix(security): gate replace_sie_import behind owner/admin membership The RPC was SECURITY DEFINER with EXECUTE granted to PUBLIC and anon, no company_members lookup, no auth.uid() reference and no unauthorized raise, while setting gnubok.allow_delete to disarm the BFL immutability and retention triggers. Any caller holding a company_id and an import id could hard delete another tenant's verifikationer. Confirmed live in production. Applies the same fail closed owner/admin guard that undo_sie_import already carries (migration 20260624120000), resolving the actor from COALESCE(p_user_id, auth.uid()) so it denies when the role is NULL, then revokes EXECUTE from PUBLIC and anon. search_path and the raised statement_timeout are restated, since CREATE OR REPLACE drops settings that are not repeated. userId is a required parameter on replaceSIEImport: the service client has a NULL auth.uid(), so a caller without an explicit actor now fails to compile rather than hitting the closed gate at runtime. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(security): validate arcim OAuth callback state server side The callback route is skipAuth and decoded the state parameter as plain base64url JSON, trusting consentId and provider from it. A one time code was minted at flow start and never read. An unauthenticated attacker who learned a consent id could run an OAuth flow on their own provider account and post the callback with a forged state, landing their tokens on another tenant's consent, so the victim's next migration imported the attacker's ledger. State is now an opaque randomBytes(32) pointer to a provider_otc row, consumed by a single atomic UPDATE guarded on used_at IS NULL and expires_at, so a replay loses the row lock race and updates nothing. provider is read from provider_consents rather than trusted from the client. provider_otc already existed for exactly this purpose and was never wired up. Also scopes getConsent to an owning company, closing a cross tenant status oracle where the preview and migrate paths echoed a consent's status before the scoped check ran. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(security): scope documents storage to company_id (phase A) The documents bucket policies matched on auth.uid(), and upload keys were documents/{userId}/..., so company membership was never consulted. Removing a member revoked nothing: their session still authenticated and they kept direct Storage read access to every receipt, supplier invoice and bank statement they had uploaded. The same bug was fixed for sie-files in 20260416120000; this bucket was left behind. Phase A is additive. Company scoped policies are added alongside the uploader scoped ones, uploads move to documents/{companyId}/{userId}/..., and reads accept either layout so nothing breaks mid migration. Phase C, which drops the old policies, is gated on the backfill reporting zero remaining legacy prefix objects. The policy compares the company segment as text rather than casting to uuid the way sie-files does: this bucket holds keys whose second segment is not a uuid (MCP audit packages), and Postgres does not guarantee the bucket prefix qual runs before the cast, so a planner reordering would raise 22P02 and fail the whole query instead of filtering the row out. deleteDocument now removes both candidate keys. Removing only the stored pointer would leave a readable orphan copy of a document the user asked to erase. The backfill script is included but has never been run. It defaults to dry run, refuses .env.local by name, and verifies each copy is readable and SHA-256 identical before repointing the row. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(security): enforce events:read scope and membership on /api/events This was the only one of the three validateApiKey call sites with no downstream guard: v1 and the MCP server both check scope and re-verify company membership, this route did neither. An events:read scope existed and was documented as gating the endpoint but was never called, so a legacy key falling back to DEFAULT_SCOPES read the full log. The bound company id went straight from the api_keys row into a service role query, so a key whose user had been removed from the company kept reading. Adds the scope check before any database access, re-verifies company_members with archived_at IS NULL, honours test mode by stamping X-Gnubok-Mode instead of ignoring it, applies minimisePayload so the pull surface can never return a wider payload than the push surface, and replaces the three flat error strings with the canonical envelope. Test key reads are served rather than blocked: TEST_KEY_WRITE_BLOCKED is gated on mutations in with-api-v1, so a read gets the same treatment as every other v1 read endpoint. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * perf(bookkeeping): sweep remaining journal_entries!inner embeds A previous refactor removed this pattern from lib/reports and introduced fetchEntryLines, but the class was never swept. Seventeen sites remained and had become the top application consumer of production database time: measured across the resulting query shapes, 32,694 calls and 25,848 seconds of execution, mean 790ms, with shapes averaging 2.6s and 3.0s and maxing at 7,962ms against the 8s statement_timeout, which surfaced to users as 500s on the booking path. PostgREST compiles an embed with filters on the embedded side into a correlated INNER JOIN LATERAL with a parameterized LIMIT, which stops Postgres reordering the join, so each query walked the whole journal_entry_lines table across all tenants. Driving from the entries side instead turns that into two indexed round trips. Converted sites keep their existing shape: the helper reattaches the parent entry under the same key the embed produced. Several conversions also remove a latent silent truncation where an unpaginated query was capped at PostgREST's 1000 row ceiling. Two deliberate exceptions. The free text ilike legs of the MCP display query stay on the embed, because each is capped at legLimit and that cap drives the truncation contract the tool reports, while the helper is unbounded. The accounts route moves to the existing get_account_usage_counts RPC instead, since its embed was a head count and the helper returns rows. commitEntry's write path is untouched: the change there is confined to the read query of the pre-commit dimension rule check. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(api): anchor v1 list cursors on created_at Page two returned page one, forever, while still advertising a fresh next_cursor. The three routes sorted by and encoded a Postgres date column, which serializes as YYYY-MM-DD, but decodeDefaultCursor validates the cursor timestamp as full ISO-8601 and returned null, so the keyset filter was never applied and has_more never went false. An integrator syncing verifikat looped on the newest rows indefinitely. The transactions route already solved this and its comment names the trap; the fix was never ported. All three now order and encode on created_at with an id tie break, matching the transactions keyset predicate exactly. ISO_TIMESTAMP is deliberately left alone: relaxing it would silently change sort semantics on the route that currently works. Default ordering therefore moves from business date to insert order. Every business date is still on the row, and the invoices list gains date_from and date_to filters so a date range is still reachable; the other two already had them. The tests use an in-memory PostgREST that actually evaluates the filters, because the repo's pass-through mock cannot catch this class of bug: the bug is that the filter is never sent. They walk to exhaustion with a hard iteration cap, so an unterminated walk fails instead of hanging. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(api): separate dry run from commit in the idempotency hash The request hash was built from url.pathname, which excludes the query string, so a dry run and its commit hashed identically. Following the flow documented in dry-run.ts, re-issuing the request with the same Idempotency-Key returned the cached preview with Idempotent-Replayed set and wrote nothing, while reporting 200. An agent or integrator saw success for a write that never happened. dry_run is folded into the hash only when true, not as an unconditional boolean. Including it as false would change the hash of every ordinary write, and with a 24h idempotency TTL any key in flight across the deploy would fail the request_hash comparison and 409 on a legitimate retry. Both hash call sites now go through one shared helper so they cannot drift into a permanent cache miss, and dry run responses are no longer stored at all. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * ci: install the Bedrock SDK out of tree in the compliance review The Swedish accounting compliance gate had failed ten consecutive runs and so was posting nothing. With --no-package-lock npm discarded the lockfile and re-resolved the whole tree from package.json, floating @hookform/resolvers to 5.4.3, whose valibot ^1 peer conflicts with the pinned valibot 0.39.0. Installing into the parent of the checkout resolves only that one package, so an unrelated peer conflict can never take the gate down again. Node still finds it because ESM bare specifiers walk up parent node_modules; NODE_PATH would not have worked, as it is CommonJS only. --legacy-peer-deps was rejected because it masks future genuine peer conflicts and still reifies the full tree. The same step's SDK version is aligned from 0.31.0 back to the 0.29.1 that package.json and check:guards enforce after the streaming outage. That drift went unnoticed because the pin guard only inspects package.json and the lockfile, never workflow files. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * build(docker): generate crontabs from vercel.json vercel.json defines 16 cron jobs; both Docker crontabs carried 9, and were byte identical to each other. Self hosted deployments therefore never sent recurring invoices, never dispatched webhooks and never cleaned up idempotency keys. tax-deadlines also ran once a year on 2 January instead of daily, and documents/verify weekly instead of daily. Extension crons are included rather than excluded. The Dockerfile copies the whole tree before building, so every extension cron route is compiled into the image regardless of the enabled preset, and each returns 200 when its extension is unconfigured, so curl -sf logs no failure. Two such entries were already present in the crontab for extensions absent from the preset, which settles the intent. documents/verify is treated as drift rather than a self hosted concession: the weekly cadence was present in the hosted crontab too, and the run is capped at 200 documents walking a nulls-first queue, so weekly drains the integrity queue seven times slower on a check that exists for BFL retention. webhooks/dispatch keeps its per minute cadence, adding 1,440 requests a day on self hosted. A gentler tick would silently stretch the first retry, since the retry ladder opens at 60 seconds. SCHEDULE_OVERRIDES is the one line place to change that. A parity test asserts the path sets match minus a documented exclusion list, and ratchets three cron routes that are currently scheduled nowhere so they are named rather than silently rotting. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * chore(observability): add a provider agnostic error sink There is no error tracking in this codebase: logs go to console and Vercel retention and nowhere else, nothing alerts on the 16 cron jobs, and seven code comments across lib, app, components and extensions asserted that Sentry captures errors when Sentry is not a dependency. The two most recent bug fixes on this repo were both discovered by customer email. This adds the sink, not a vendor. No dependency is taken: the interface has a no-op default and a registration point, so behaviour is unchanged until an adapter is registered. Releases are tagged from the build id already inlined by next.config.ts. Redaction moved out of lib/logger.ts into a leaf module that both the logger and the sink import, so there is one denylist and no path from application data to a third party can skip the personnummer regex, including direct sink calls that bypass the logger. That matters here because these logs carry personnummer and financial data. verifyCronSecret now reports its own 401s, which covers all 16 jobs without touching a route file and catches the case where CRON_SECRET is rotated without updating the scheduler and every job silently 401s forever. The threshold is one failure rather than the backup alert's three: suppressing the first occurrence is precisely how an outage stays invisible. The seven misleading comments are corrected to describe what the code actually does, including the two cases that still are not covered: the client side one, since the sink is server side, and a warn level call that is not forwarded. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix: remediate the 2026-07-26 similar-sweep findings across all surfaces Resolves the ~150-finding sweep (dev_docs/similar-sweep-2026-07-26.md) with one agent per finding; every behavioural fix carries a regression test proven to fail at HEAD. Full status, corrections to the sweep, refusals and open decisions in dev_docs/similar-sweep-2026-07-26-remediation-status.md. Structural roots closed: - resolveSekAmountOrNull(): honest SEK resolution refuses instead of booking 1:1; four duplicated toSek closures now refuse via INVOICE_FX_RATE_MISSING - ledger-line-amount.ts: journal_entry_lines.currency labels the document, not the amount; SQL pre-filter decoy proven and fixed - sparse-patch.ts: .partial() does not strip .default() in Zod 4.4.3; the exploitable salary payslip-line PATCH and KPI preferences sinks fixed - tests/schema: migration-replay phantom-column guard (13k+ refs, closed CHECK sets, onConflict targets); found 28 real defects, all fixed, all four baselines now empty - three new ratchet guards: sek-labelled-amount, cross-extension-import, ungated-extension-route Highlights: lawful VAT-rate set on all seven invoice surfaces (ML 6 kap), RC input VAT mismatch wired on web + both MCP callers, missing-underlag resource delegates to the shared RPC predicate, push-notifications consent polarity fail-closed, deadlines undo honours requested state, silent-failure and read-side-fabrication classes fixed across settings/KPI/inbox/Stripe/ Arcim/kassaflodesanalys, error-envelope stringification fixed at 10+ sites with isSwedishUserMessage extended. Also includes the parallel session's MCP invoice tools (update_invoice, recurring schedules, invoice deliveries) which share files with the sweep work and are verified green together. 13 new migrations are NOT applied anywhere; they apply via branch merge. 20260726120000 backfills 1247 supplier-invoice rows. pg tests for new DDL are written but unrun (no local Postgres). Verified: 11088 tests / 881 files green, tsc 0 non-test errors, lint 0 errors, check:guards passing, MCP payload 57475/57500. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(migrations): rename replace_sie_import migration off main's 20260726090000 version origin/main shipped 20260726090000_agent_quota_rpc_caller_guard.sql; keeping our replace_sie_import migration on the same version would abort the Supabase apply with a schema_migrations_pkey duplicate at merge time. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(review): remediate pre-publish deep-review findings across all slices A 13-agent review of the full branch diff surfaced 1 critical, 5 high and ~45 further findings; this commit resolves them in one pass: - replace_sie_import / undo_sie_import: p_user_id honored only for service_role callers; any other caller is pinned to auth.uid() (impersonation gate bypass), authz raise errcode 42501 mapped to a Swedish 403 in the route, new caller-guard migration for undo - bulk_book_transactions refuses homogeneous non-SEK batches instead of writing foreign magnitudes into SEK ledger columns - credit-note cap trigger: company-match on credited_invoice_id, no cross-tenant figures in exception text - link_voucher RPCs resolve NULL invoice currency as SEK end to end - personal-number ciphertext CHECK split into NOT VALID + VALIDATE - same-currency foreign settlements clear 1510 at booking rate and book realized diff to 3960/7960; rate-less foreign write paths refuse - receivables revaluation covers partially_paid and outstanding amounts - period lock guard paginates candidates past the PostgREST 1000 cap - documents: service-client storage removals after authz, dual-layout reads in integrity cron and archive export, backfill delete-source sweep actually deletes with hash verification and shared-key grouping - invoice matching normalizes NULL/lowercase currencies (regression), duplicate candidates stop claiming amount matches they never ran - match-invoice aborts on any booking failure (no paid-without-verifikat) - refresh-exchange-rate reverts on concurrent booking (TOCTOU window) - KPI preferences upsert arbiter aligned to the company-scoped constraint - personnummer_last4 stripped from all salary responses incl. MCP tools - worked-hours batch restores destroyed rows on conflict and error paths - MCP: shared duplicate-claim builder (no more 'null kr'), short-circuit on tag_journal_lines overflow, auto_send schedules stage as high risk - observability sink redacts emails/IBANs/API keys and keeps redacted stacks in prod; assorted small guards (safe-return-to /@, dry_run=True, cursor helper off-by-one, OAuth state TTL 10 min, arcim saveMappings call removed) Full dispositions, deferred items and hand-verified accounting numbers are documented in the PR body and DECISIONS.md. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(personnummer): implement masking and encryption for personal numbers with tests * fix(review): address CI and compliance-bot findings for PR #1215 pg-real: the CI image's auth shim reads the legacy request.jwt.claim.role GUC, so both service-role simulations (runAsServiceRole and the invoice-delivery test's local helper) never satisfied auth.role() = 'service_role' and every legitimate p_user_id path failed closed; the shared helper now sets both GUC shapes plus SET LOCAL ROLE with a fail-loud sanity check, and the delivery test reuses it. The link-voucher migration had recreated both RPCs from pre-rewrite file text, reintroducing the NULL-unsafe membership pattern the null-safe-tenant-guards ratchet bans; both guards now use public.caller_is_company_member() with all currency changes preserved. Compliance bots: the customers export now emits the standard masked form instead of raw AES-256-GCM ciphertext in the Org-/personnummer column, and maskCustomerRow returns a non-round-trippable placeholder on decrypt failure instead of 500ing the list. MCP parity: gnubok_lock_period's staging pre-check now runs the exact countUnbookedInPeriod the commit path enforces (exported from period-service; local mirror deleted), and gnubok_agi_status resolves AGI state run-scoped so a correction run no longer renders as already filed. Declined with evidence: PR-Agent's opening-balances null-zeroing concern (all mergeable columns are NOT NULL with defaults per 20260713101000). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(review): address codex review findings on PR #1215 - restore 20260726140000 to its preview-recorded content and restate the NULL-safe tenant guard under 20260727130000: a recorded migration version never re-runs, so the in-place edit could not reach the preview branch - replace toFixed() with sv-SE two-decimal formatting in the ROT/RUT cap warning texts and update the pinned test expectations - drop the em dash in the fiscal-periods route comment - strip trailing whitespace in import-existing.test.ts Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(reports): raise timeout on real PDF render tests renderToBuffer does real @react-pdf layout work and exceeds the 5s default when the full suite saturates the CPU; tests pass in isolation. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1049 lines
49 KiB
TypeScript
1049 lines
49 KiB
TypeScript
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import { evaluateMappingRules } from '@/lib/bookkeeping/mapping-engine'
|
|
import { createTransactionJournalEntry } from '@/lib/bookkeeping/transaction-entries'
|
|
import { upsertCounterpartyTemplate } from '@/lib/bookkeeping/counterparty-templates'
|
|
import { getBestInvoiceMatch } from '@/lib/invoices/invoice-matching'
|
|
import { findSupplierInvoiceMatch } from '@/lib/invoices/supplier-invoice-matching'
|
|
import { fetchExchangeRate } from '@/lib/currency/riksbanken'
|
|
import { logMatchEvent } from '@/lib/invoices/match-log'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
import { contentBucketKey, descriptionsBridge, normalizeImportedDescription, shiftIsoDate } from '@/lib/transactions/external-id'
|
|
import { isImportedTransaction } from '@/lib/transactions/origin'
|
|
import { createLogger } from '@/lib/logger'
|
|
import type { Transaction, RawTransaction, IngestResult, IngestOptions, SupplierInvoice, Currency, ExchangeRate } from '@/types'
|
|
|
|
// Re-export types for backward compatibility
|
|
export type { RawTransaction, IngestResult } from '@/types'
|
|
|
|
/**
|
|
* Sentinel for a (date, öre) bucket whose incoming rows carry more than one
|
|
* currency: the booked-hand-entered mirror's per-bucket currency gate cannot be
|
|
* evaluated there, so the mirror is disabled for that bucket.
|
|
*/
|
|
const MIXED_CURRENCIES = Symbol('mixed-currencies')
|
|
|
|
/**
|
|
* One existing row in a content-dedup bucket: its normalized/lowercased
|
|
* description, the cash account it settled on (null for legacy rows that
|
|
* predate the cash_account_id backfill), the import channel it came from, and
|
|
* whether that channel is an external feed (vs a hand-entered row). `source` +
|
|
* `isImportFeed` drive the cross-channel mirror bridge (see
|
|
* `consumeBridgingTwin`); `cashAccountId` is the cross-account guard.
|
|
*/
|
|
type BucketEntry = {
|
|
/** Row id of the stored transaction; used to persist hand-mirror adoption. */
|
|
id: string | null
|
|
desc: string
|
|
cashAccountId: string | null
|
|
source: string | null
|
|
isImportFeed: boolean
|
|
/**
|
|
* ISO currency of the stored row ('SEK', 'USD', ...), null for rows without
|
|
* one. Guards EVERY content-dedup match path (text bridge, cross-channel
|
|
* mirror, booked-hand-entered mirror): the content bucket keys on (date, öre)
|
|
* only, so without this a stored 250,00 SEK row and an incoming 250,00 EUR
|
|
* row on the same date share a bucket and either could consume the other.
|
|
* See the currency guard in `consumeBridgingTwin`.
|
|
*/
|
|
currency: string | null
|
|
/**
|
|
* The stored row's `external_id`. Used ONLY by the shadow-mode same-feed
|
|
* scope-drift instrumentation (see ingestTransactions): a stored row is a
|
|
* "drift candidate" when its id is NOT among the incoming batch's ids, which
|
|
* is what distinguishes an IBAN-scope re-import from a normal sibling whose id
|
|
* Layer-1 already reconciles. Null for rows predating the column.
|
|
*/
|
|
externalId: string | null
|
|
}
|
|
|
|
/**
|
|
* Content-dedup bucket: a `{date}|{öre}` key mapped to the multiset of existing
|
|
* rows in that bucket. Matching is by `descriptionsBridge` (prefix-containment)
|
|
* gated by the account guard, consumed with COUNTING semantics (one entry is
|
|
* spliced out per deduped incoming row), so two genuinely-distinct
|
|
* same-(date,amount) transactions are never collapsed.
|
|
*/
|
|
type DescBucket = Map<string, BucketEntry[]>
|
|
|
|
interface ExistingTransactionMaps {
|
|
/**
|
|
* Booked transactions (any source): consumed by any incoming raw transaction.
|
|
* Hand-entered rows (import_source manual/mcp/null, no bank connection) in
|
|
* THIS map are also cross-channel-mirror candidates: a booked hand-entered
|
|
* row is the ledger asserting the movement already exists, so an incoming
|
|
* feed row for the same (date, öre, currency, account) in a count-symmetric
|
|
* bucket is the bank's copy of it, not new money (the MCP-then-bank-sync
|
|
* case: user bookkeeps by chat first, connects the bank later).
|
|
*/
|
|
booked: DescBucket
|
|
/**
|
|
* Unbooked rows from ANY external import feed (Enable Banking PSD2 sync,
|
|
* bank-file CSV/CAMT import): consumed by any incoming raw transaction
|
|
* regardless of source. Catches the cross-channel re-import: the same bank
|
|
* account pulled once via PSD2 and once via a CSV/CAMT file upload (in either
|
|
* order), plus PSD2 reconnect duplicates whose external_id regenerated.
|
|
* Hand-entered rows (import_source manual/mcp/null) are deliberately
|
|
* excluded HERE: an UNBOOKED hand-entered row is just a staged intent (e.g.
|
|
* an MCP draft awaiting approval), not ledger evidence, so it must never
|
|
* consume an incoming import.
|
|
*/
|
|
unbookedImported: DescBucket
|
|
}
|
|
|
|
/** Push a row into its (date, öre) bucket, normalizing the description. */
|
|
function addToBucket(
|
|
bucket: DescBucket,
|
|
id: string | null,
|
|
date: string,
|
|
amount: number | string,
|
|
description: string,
|
|
cashAccountId: string | null,
|
|
source: string | null,
|
|
isImportFeed: boolean,
|
|
currency: string | null,
|
|
externalId: string | null,
|
|
): void {
|
|
const key = contentBucketKey(date, amount)
|
|
const entry: BucketEntry = {
|
|
id,
|
|
desc: description.toLowerCase().trim(),
|
|
cashAccountId,
|
|
source,
|
|
isImportFeed,
|
|
currency,
|
|
externalId,
|
|
}
|
|
const entries = bucket.get(key)
|
|
if (entries) entries.push(entry)
|
|
else bucket.set(key, [entry])
|
|
}
|
|
|
|
async function buildExistingTransactionMaps(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
rawTransactions: RawTransaction[]
|
|
): Promise<ExistingTransactionMaps> {
|
|
const booked: DescBucket = new Map()
|
|
const unbookedImported: DescBucket = new Map()
|
|
if (rawTransactions.length === 0) return { booked, unbookedImported }
|
|
|
|
const dates = rawTransactions.map((t) => t.date).sort()
|
|
const dateFrom = dates[0]
|
|
const dateTo = dates[dates.length - 1]
|
|
|
|
try {
|
|
const { data: bookedRows } = await supabase
|
|
.from('transactions')
|
|
.select('id, date, amount, original_description, description, cash_account_id, import_source, bank_connection_id, currency, external_id')
|
|
.eq('company_id', companyId)
|
|
.not('journal_entry_id', 'is', null)
|
|
.gte('date', dateFrom)
|
|
.lte('date', dateTo)
|
|
|
|
if (bookedRows) {
|
|
for (const tx of bookedRows) {
|
|
// Key off the immutable bank original, not the user-editable
|
|
// description: a title edit must never make the dedup bridge miss a
|
|
// genuine re-import. Falls back to description for rows predating the
|
|
// original_description column.
|
|
addToBucket(
|
|
booked,
|
|
tx.id ?? null,
|
|
tx.date,
|
|
tx.amount,
|
|
normalizeImportedDescription(tx.original_description ?? tx.description),
|
|
tx.cash_account_id ?? null,
|
|
tx.import_source ?? null,
|
|
isImportedTransaction({ import_source: tx.import_source, bank_connection_id: tx.bank_connection_id }),
|
|
tx.currency ?? null,
|
|
tx.external_id ?? null,
|
|
)
|
|
}
|
|
}
|
|
} catch {
|
|
// Non-critical: content-based dedup will be skipped
|
|
}
|
|
|
|
try {
|
|
// ALL unbooked import-feed rows, not just enable_banking. An unbooked CSV
|
|
// row must dedup an incoming PSD2 sync of the same account, and an unbooked
|
|
// PSD2 row must dedup an incoming CSV import. Feeds always set a non-null
|
|
// import_source outside the user-created allowlist (manual/mcp); null /
|
|
// manual / mcp are hand-entered and intentionally excluded.
|
|
const { data: unbookedRows } = await supabase
|
|
.from('transactions')
|
|
.select('id, date, amount, original_description, description, cash_account_id, import_source, bank_connection_id, currency, external_id')
|
|
.eq('company_id', companyId)
|
|
.is('journal_entry_id', null)
|
|
.not('import_source', 'is', null)
|
|
.neq('import_source', 'manual')
|
|
.neq('import_source', 'mcp')
|
|
.gte('date', dateFrom)
|
|
.lte('date', dateTo)
|
|
|
|
if (unbookedRows) {
|
|
for (const tx of unbookedRows) {
|
|
// See booked-map note: dedup on the immutable bank original so a
|
|
// user title edit cannot reopen the duplicate-import window.
|
|
addToBucket(
|
|
unbookedImported,
|
|
tx.id ?? null,
|
|
tx.date,
|
|
tx.amount,
|
|
normalizeImportedDescription(tx.original_description ?? tx.description),
|
|
tx.cash_account_id ?? null,
|
|
tx.import_source ?? null,
|
|
isImportedTransaction({ import_source: tx.import_source, bank_connection_id: tx.bank_connection_id }),
|
|
tx.currency ?? null,
|
|
tx.external_id ?? null,
|
|
)
|
|
}
|
|
}
|
|
} catch {
|
|
// Non-critical: reconnect dedup will be skipped
|
|
}
|
|
|
|
return { booked, unbookedImported }
|
|
}
|
|
|
|
/**
|
|
* Generic transaction ingestion pipeline.
|
|
*
|
|
* Handles:
|
|
* 1. Deduplication via external_id
|
|
* 1b. Content-based dedup (date+amount+description prefix) against already-booked
|
|
* transactions: catches cross-source duplicates, e.g. PSD2 row gets booked
|
|
* before the user later re-imports the same period via CSV.
|
|
* 1c. Content-based dedup against unbooked enable_banking rows: catches PSD2
|
|
* reconnect duplicates AND CSV imports overlapping an active PSD2 sync (the
|
|
* description-prefix component makes this safe to apply across sources).
|
|
* 2. Insert into transactions table
|
|
* 3. OCR/reference-based invoice matching (highest confidence)
|
|
* 4. Amount+customer fallback invoice matching
|
|
* 5. Mapping rule evaluation for auto-categorization
|
|
* 6. Auto-journal-entry creation for high-confidence matches
|
|
*
|
|
* Used by both bank file import and Enable Banking PSD2 sync.
|
|
*/
|
|
export async function ingestTransactions(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
userId: string,
|
|
rawTransactions: RawTransaction[],
|
|
options?: IngestOptions
|
|
): Promise<IngestResult> {
|
|
const result: IngestResult = {
|
|
imported: 0,
|
|
duplicates: 0,
|
|
reconciled: 0,
|
|
auto_categorized: 0,
|
|
auto_matched_invoices: 0,
|
|
errors: 0,
|
|
transaction_ids: [],
|
|
shadow_scope_drift_candidates: 0,
|
|
shadow_date_drift_candidates: 0,
|
|
}
|
|
|
|
const log = createLogger('transactions.ingest', { companyId })
|
|
// SHADOW-ONLY instrumentation for the same-feed scope-drift bridge (Hole A:
|
|
// Enable Banking returns the same account under a drifted IBAN, the
|
|
// IBAN-embedded external_id changes, Layer-1 dedup misses the re-import, and
|
|
// because both rows are the SAME feed the cross-channel mirror does not fire).
|
|
// When on, we LOG which rows an enforcing rule WOULD treat as re-imports and
|
|
// count them, but never change what gets inserted. Default on; set
|
|
// DEDUP_SCOPE_DRIFT_MODE=off to silence. There is deliberately NO 'enforce'
|
|
// branch yet: we validate on real fleet data first (see the plan).
|
|
const scopeDriftShadow = process.env.DEDUP_SCOPE_DRIFT_MODE !== 'off'
|
|
// SHADOW-ONLY instrumentation for the date-drift bridge: the content bridge
|
|
// buckets on EXACT (date, öre), so a booking date that drifts a day between
|
|
// syncs lands its twin in an ADJACENT bucket and every dedup layer misses it
|
|
// (this produced the observed EB↔EB and CSV↔EB 1-day-apart duplicates). When
|
|
// on, we LOG + COUNT which surviving rows a ±1-day-tolerant rule WOULD treat
|
|
// as re-imports, but never change what is inserted. Default on; set
|
|
// DEDUP_DATE_DRIFT_MODE=off to silence. No 'enforce' branch, same as
|
|
// scope-drift, we validate on real fleet data first.
|
|
const dateDriftShadow = process.env.DEDUP_DATE_DRIFT_MODE !== 'off'
|
|
|
|
// Pre-fetch existing transactions for content-based dedup (date+amount+
|
|
// description prefix, plus the cross-channel mirror below). Booked rows catch
|
|
// cross-source duplicates after they've been booked; unbooked import-feed rows
|
|
// catch the common case where a PSD2 row is still unbooked when the user
|
|
// re-imports the same period via CSV, or the reverse, a CSV import that
|
|
// predates the first PSD2 sync of the same account.
|
|
const existingMaps = await buildExistingTransactionMaps(supabase, companyId, rawTransactions)
|
|
|
|
// Every row in one ingest call shares an import_source (EB sync passes
|
|
// 'enable_banking', bank-file import passes 'csv_<format>'/'camt053'), so the
|
|
// first row's source identifies this batch's channel. We use it to find
|
|
// "cross-channel mirror" buckets: a (date, öre) bucket where the number of
|
|
// incoming rows EQUALS the number of stored rows from a DIFFERENT feed. That
|
|
// equality is the signal that the same set of real transactions is arriving
|
|
// once per channel (e.g. Nordea's CSV export and its PSD2 feed), where the
|
|
// per-row description is known-unreliable: CSV shows the payee, PSD2 the
|
|
// OCR/message, or vice versa. Only in those buckets do we dedup on
|
|
// (date, öre, account) without a description match (see consumeBridgingTwin).
|
|
// An asymmetric bucket keeps the description requirement, so a genuinely-new
|
|
// row is never collapsed into a different one.
|
|
//
|
|
// The same count-symmetry signal also runs against BOOKED hand-entered rows
|
|
// (import_source manual/mcp/null): a user who bookkeeps by chat/MCP first and
|
|
// connects the bank afterwards has already put the movement in the ledger,
|
|
// and the feed's copy of it must not re-appear as a duplicate. This mirror is
|
|
// gated harder than feed-vs-feed: the stored row must be BOOKED (staged/
|
|
// unbooked hand-entered rows never consume an import), its currency must not
|
|
// contradict the incoming row's, its cash account must be compatible, and the
|
|
// bucket counts must match exactly. Tracked in a SEPARATE count map (built
|
|
// further down, once the batch settlement account and the stored external_id
|
|
// set are known) so the feed-vs-feed mirror semantics are untouched: a
|
|
// hand-entered twin never breaks feed count symmetry.
|
|
const batchSource = rawTransactions[0]?.import_source ?? null
|
|
const batchIsImportFeed = isImportedTransaction({ import_source: batchSource })
|
|
const incomingByBucket = new Map<string, number>()
|
|
const crossSourceStoredByBucket = new Map<string, number>()
|
|
const incomingCurrencyByBucket = new Map<string, string | null | typeof MIXED_CURRENCIES>()
|
|
if (batchIsImportFeed) {
|
|
for (const raw of rawTransactions) {
|
|
const k = contentBucketKey(raw.date, raw.amount)
|
|
incomingByBucket.set(k, (incomingByBucket.get(k) ?? 0) + 1)
|
|
const cur = raw.currency ?? null
|
|
if (!incomingCurrencyByBucket.has(k)) incomingCurrencyByBucket.set(k, cur)
|
|
else if (incomingCurrencyByBucket.get(k) !== cur) incomingCurrencyByBucket.set(k, MIXED_CURRENCIES)
|
|
}
|
|
for (const bucket of [existingMaps.booked, existingMaps.unbookedImported]) {
|
|
for (const [k, entries] of bucket) {
|
|
for (const entry of entries) {
|
|
if (entry.isImportFeed && entry.source !== batchSource) {
|
|
crossSourceStoredByBucket.set(k, (crossSourceStoredByBucket.get(k) ?? 0) + 1)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// When rawInsertOnly is set (viewer imports), skip pre-fetching supplier
|
|
// invoices and exchange rates: they are not used.
|
|
let unpaidSupplierInvoices: SupplierInvoice[] = []
|
|
// Keyed by `${currency}|${date}` so each non-SEK transaction gets the
|
|
// rate that was valid on its own transaction date, not the import date.
|
|
const exchangeRatesByDate = new Map<string, ExchangeRate>()
|
|
|
|
if (!options?.rawInsertOnly) {
|
|
// Pre-fetch unpaid supplier invoices for expense matching (non-critical)
|
|
try {
|
|
unpaidSupplierInvoices = await fetchAllRows<SupplierInvoice>(({ from, to }) =>
|
|
supabase
|
|
.from('supplier_invoices')
|
|
.select('*, supplier:suppliers(*)')
|
|
.eq('company_id', companyId)
|
|
.in('status', ['registered', 'approved'])
|
|
.gt('remaining_amount', 0)
|
|
.range(from, to)
|
|
)
|
|
} catch {
|
|
// Non-critical: supplier invoice matching will be skipped
|
|
}
|
|
}
|
|
|
|
// Pre-fetch exchange rates for each unique (currency, date) pair in the
|
|
// batch. Riksbanken publishes a per-day rate; using one batched fetch with
|
|
// no date stamps every row at today's rate, which is wrong for historical
|
|
// imports (issue #442). fetchExchangeRate already falls back to the last
|
|
// 7 days when the requested day is a weekend/holiday.
|
|
//
|
|
// Concurrency is bounded: a 90-day first-sync backfill of a foreign-
|
|
// currency account used to fire every pair at Riksbanken simultaneously
|
|
// and got the whole batch rate-limited. Passing `supabase` gives
|
|
// fetchExchangeRate the persistent exchange_rates cache, so repeat dates
|
|
// cost a DB lookup instead of an API call.
|
|
if (!options?.rawInsertOnly) {
|
|
const uniquePairs = new Map<string, { currency: Currency; date: string }>()
|
|
for (const t of rawTransactions) {
|
|
if (t.currency && t.currency !== 'SEK' && t.date) {
|
|
const key = `${t.currency}|${t.date}`
|
|
if (!uniquePairs.has(key)) {
|
|
uniquePairs.set(key, { currency: t.currency as Currency, date: t.date })
|
|
}
|
|
}
|
|
}
|
|
|
|
if (uniquePairs.size > 0) {
|
|
const pairs = Array.from(uniquePairs.entries())
|
|
const RATE_FETCH_CONCURRENCY = 4
|
|
for (let i = 0; i < pairs.length; i += RATE_FETCH_CONCURRENCY) {
|
|
const chunk = pairs.slice(i, i + RATE_FETCH_CONCURRENCY)
|
|
const settled = await Promise.allSettled(
|
|
chunk.map(([, { currency, date }]) =>
|
|
fetchExchangeRate(currency, new Date(date), supabase)
|
|
)
|
|
)
|
|
for (let j = 0; j < chunk.length; j++) {
|
|
const [key] = chunk[j]
|
|
const outcome = settled[j]
|
|
if (outcome.status === 'fulfilled' && outcome.value) {
|
|
exchangeRatesByDate.set(key, outcome.value)
|
|
}
|
|
// A null/rejected outcome leaves the key unset: the transaction is
|
|
// inserted without amount_sek/exchange_rate and remains repairable
|
|
// via /api/transactions/[id]/refresh-exchange-rate. Rates are never
|
|
// made up, fetchExchangeRate's last resort is the most recent
|
|
// CACHED observation, not a hardcoded number.
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Pre-fetch existing external_ids in batches for dedup (avoids N+1 queries)
|
|
const existingExternalIds = new Set<string>()
|
|
const externalIds = rawTransactions.map(t => t.external_id)
|
|
for (let i = 0; i < externalIds.length; i += 500) {
|
|
const chunk = externalIds.slice(i, i + 500)
|
|
const { data } = await supabase
|
|
.from('transactions')
|
|
.select('external_id')
|
|
.eq('company_id', companyId)
|
|
.in('external_id', chunk)
|
|
data?.forEach(r => existingExternalIds.add(r.external_id))
|
|
}
|
|
|
|
// Resolve the cash account this batch settled on, once. Every row in one
|
|
// ingest call shares a settlement account: enable-banking calls this per
|
|
// account (settlementAccount = account.ledger_account), CSV import passes the
|
|
// single account the user picked. cash_accounts.ledger_account is unique per
|
|
// company, so this is a single-row lookup. Tolerate a miss: the row stays
|
|
// unbound (cash_account_id NULL) and reconciliation falls back to currency.
|
|
// We never auto-create a cash account here; that would race upsertFromPsd2's
|
|
// seed-promotion logic in lib/cash-accounts/service.ts.
|
|
let cashAccountId: string | null = null
|
|
if (options?.settlementAccount) {
|
|
const { data: ca } = await supabase
|
|
.from('cash_accounts')
|
|
.select('id')
|
|
.eq('company_id', companyId)
|
|
.eq('ledger_account', options.settlementAccount)
|
|
.maybeSingle()
|
|
cashAccountId = (ca?.id as string | undefined) ?? null
|
|
}
|
|
|
|
// ── Shadow-mode same-feed scope-drift precompute (measure only) ──────────
|
|
// Two per-(date, öre) bucket counts that, when EQUAL and non-zero, mark a
|
|
// bucket as a probable scope-drift mirror:
|
|
// - unmatchedIncomingByBucket: incoming rows whose external_id is NOT
|
|
// already stored (i.e. Layer-1 will not reconcile them, the ones that
|
|
// would otherwise insert as fresh rows).
|
|
// - driftCandidateStoredByBucket: stored rows from THIS SAME feed whose id
|
|
// the incoming batch does NOT carry (so they are "orphaned" by a drifted
|
|
// id), restricted to account-compatible rows. Account compatibility uses
|
|
// the batch settlement account (cash_account_id), which is keyed on the
|
|
// provider's STABLE account uid, not the drifting IBAN that broke the
|
|
// external_id (see lib/cash-accounts/service.ts upsertFromPsd2). So a
|
|
// genuinely different account on the same company is never a candidate.
|
|
// Equality is the safety signal (same as the cross-channel mirror): it means
|
|
// the same set of transactions re-arrived once, under new ids. An asymmetric
|
|
// bucket is left alone. Counts are pre-loop snapshots; the gate is evaluated
|
|
// per incoming row inside the loop.
|
|
const incomingIdSet = new Set(externalIds)
|
|
// Incoming rows Layer-1 will NOT reconcile (their external_id is not already
|
|
// stored): the honest per-bucket count of rows that will actually reach the
|
|
// content-dedup layer. Shared by the hand-entered mirror (enforcing) and the
|
|
// scope-drift shadow (measure-only): the coarse incomingByBucket would let a
|
|
// Layer-1 duplicate inflate the symmetry check.
|
|
const unmatchedIncomingByBucket = new Map<string, number>()
|
|
if (batchIsImportFeed) {
|
|
for (const raw of rawTransactions) {
|
|
if (!existingExternalIds.has(raw.external_id)) {
|
|
const k = contentBucketKey(raw.date, raw.amount)
|
|
unmatchedIncomingByBucket.set(k, (unmatchedIncomingByBucket.get(k) ?? 0) + 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ── Booked-hand-entered mirror candidates ────────────────────────────────
|
|
// Built HERE, after the batch settlement account (cashAccountId) and the
|
|
// stored external_id set are known, so the counts apply the SAME guards the
|
|
// consume step applies (account compatibility + currency). Counting entries
|
|
// the consume step would reject makes "symmetry" lie: an unconsumable twin
|
|
// could switch the mirror on and collapse a genuinely-new row. Hand-entered
|
|
// candidates exist ONLY in the booked map: the unbooked map's query excludes
|
|
// manual/mcp/null sources at the DB level.
|
|
const bookedHandEnteredByBucket = new Map<string, number>()
|
|
if (batchIsImportFeed) {
|
|
for (const [k, entries] of existingMaps.booked) {
|
|
const bucketCurrency = incomingCurrencyByBucket.get(k)
|
|
// No incoming rows in this bucket, or the incoming rows disagree on
|
|
// currency: the currency gate cannot be evaluated per-bucket, so the
|
|
// hand-entered mirror stays off there (conservative: row inserts).
|
|
if (bucketCurrency === undefined || bucketCurrency === MIXED_CURRENCIES) continue
|
|
for (const entry of entries) {
|
|
if (entry.isImportFeed) continue
|
|
const accountCompatible =
|
|
cashAccountId === null || entry.cashAccountId === null || entry.cashAccountId === cashAccountId
|
|
if (!accountCompatible) continue
|
|
if (entry.currency !== null && bucketCurrency !== null && entry.currency !== bucketCurrency) continue
|
|
bookedHandEnteredByBucket.set(k, (bookedHandEnteredByBucket.get(k) ?? 0) + 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
const driftCandidateStoredByBucket = new Map<string, number>()
|
|
if (batchIsImportFeed && scopeDriftShadow) {
|
|
for (const bucket of [existingMaps.booked, existingMaps.unbookedImported]) {
|
|
for (const [k, entries] of bucket) {
|
|
for (const entry of entries) {
|
|
const sameFeed = entry.isImportFeed && entry.source === batchSource
|
|
const accountCompatible =
|
|
cashAccountId === null ||
|
|
entry.cashAccountId === null ||
|
|
entry.cashAccountId === cashAccountId
|
|
const idOrphaned = entry.externalId !== null && !incomingIdSet.has(entry.externalId)
|
|
if (sameFeed && accountCompatible && idOrphaned) {
|
|
driftCandidateStoredByBucket.set(k, (driftCandidateStoredByBucket.get(k) ?? 0) + 1)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ── Shadow-mode date-drift precompute (measure only) ─────────────────────
|
|
// The content bridge matches only the EXACT (date, öre) bucket, so a twin
|
|
// whose booking date drifted a day is invisible to it. Snapshot the stored
|
|
// buckets BEFORE the dedup loop (a COPY of each bucket's entries, so Layer-2's
|
|
// splices don't mutate what the shadow reads), so each surviving row can look
|
|
// one day to either side for an account-compatible twin without disturbing
|
|
// real dedup. Window is ±1 day (the only gap observed); a named constant so
|
|
// widening to ±2 is one line if fleet data shows it.
|
|
const DATE_DRIFT_WINDOW_DAYS = 1
|
|
const storedByBucketForDrift = new Map<string, BucketEntry[]>()
|
|
if (batchIsImportFeed && dateDriftShadow) {
|
|
for (const bucket of [existingMaps.booked, existingMaps.unbookedImported]) {
|
|
for (const [k, entries] of bucket) {
|
|
const snapshot = storedByBucketForDrift.get(k)
|
|
if (snapshot) snapshot.push(...entries)
|
|
else storedByBucketForDrift.set(k, [...entries])
|
|
}
|
|
}
|
|
}
|
|
|
|
// Track already-matched invoice IDs within this ingestion batch
|
|
// to prevent suggesting the same invoice for multiple transactions
|
|
const matchedInvoiceIds = new Set<string>()
|
|
const matchedSupplierInvoiceIds = new Set<string>()
|
|
|
|
for (const raw of rawTransactions) {
|
|
// Normalize the source title once. Guarantees a non-empty, Swedish-first
|
|
// label for every import path (PSD2 sync + all bank-file CSV/CAMT parsers
|
|
// funnel into raw.description): catching both empty/whitespace titles and
|
|
// the legacy English 'Unknown' sentinel. This normalized value is stored as
|
|
// both description and original_description below; it's what the user sees
|
|
// and edits, and what the content-dedup key is built from.
|
|
const description = normalizeImportedDescription(raw.description)
|
|
|
|
// 1. Check for duplicates via external_id (batch pre-fetched)
|
|
if (existingExternalIds.has(raw.external_id)) {
|
|
result.duplicates++
|
|
continue
|
|
}
|
|
|
|
// 1b/1c. Content-dedup bridge: skip if an existing booked row (any source)
|
|
// OR an unbooked import-feed row shares this (date, öre) bucket and EITHER
|
|
// (a) a *bridging* description (prefix-containment, see descriptionsBridge),
|
|
// OR (b) the bucket is a cross-channel mirror (crossSourceMirror below),
|
|
// OR (c) the bucket is a booked-hand-entered mirror (handEnteredMirror).
|
|
// (a) catches re-imports the external_id check misses: old-format ids
|
|
// re-synced after the id scheme changed, and PSD2 description enrichment
|
|
// between syncs ("TIC" → "TIC BG … via internet"). (b) catches the same
|
|
// bank account imported via two channels whose descriptions don't bridge at
|
|
// all (Nordea CSV payee "TELENOR"/"Nordea" vs PSD2 OCR/message), which (a)
|
|
// alone cannot. (c) catches the feed delivering a movement the user already
|
|
// booked by hand (MCP/manual) under a free-form title that shares no text
|
|
// with the bank's ("Egen insättning …" vs "TRANSFER-123 Topped up").
|
|
// Booked first, then unbooked.
|
|
//
|
|
// Consumed with COUNTING semantics: each match splices one stored entry out
|
|
// of its bucket, so N stored twins dedup exactly N incoming and two
|
|
// genuinely-distinct same-(date,amount) transactions are kept apart. The
|
|
// text bridge is tried first (LONGEST bridging description wins, so a
|
|
// more-specific twin is matched before a generic one); the cross-channel
|
|
// mirror is the text-independent fallback.
|
|
//
|
|
// Account guard: when BOTH the incoming batch and a stored entry have a known
|
|
// cash_account_id, they must match, so a transaction on one bank account
|
|
// never deduplicates a genuinely-different one on another account of the same
|
|
// company (the content bucket is company-wide; only external_id embeds the
|
|
// account). A null on either side falls back to bridge-allowed, leaving
|
|
// single-account and legacy (un-backfilled) rows exactly as before. The guard
|
|
// applies to BOTH the text and the cross-channel-mirror path.
|
|
//
|
|
// Currency guard: same shape, same null-tolerance, applied to all three
|
|
// match paths. The bucket key carries no currency, so 250,00 EUR and
|
|
// 250,00 SEK on one date share a bucket; without this an incoming row in
|
|
// one currency consumes a stored row in another and the survivor is never
|
|
// bokförd. A null on either side stays compatible (legacy rows).
|
|
//
|
|
// crossSourceMirror: this (date, öre) bucket holds the same number of
|
|
// incoming rows as stored rows from a different feed → the same real
|
|
// transactions arriving once per channel. Only then is the description
|
|
// requirement dropped; an asymmetric bucket keeps it, so when the channels
|
|
// disagree on how many transactions a bucket holds we keep a visible
|
|
// (deletable) duplicate rather than risk collapsing a genuinely-new row.
|
|
//
|
|
// handEnteredMirror: the analogous signal against BOOKED hand-entered rows
|
|
// (manual/mcp/null source). A booked hand-entered row means the user
|
|
// already put this movement in the ledger before the feed delivered the
|
|
// bank's copy (bookkeep-by-chat first, connect the bank later). Symmetry
|
|
// compares the bucket's Layer-1-UNMATCHED incoming count (a row Layer-1
|
|
// reconciles never reaches this layer, so it must not inflate the count)
|
|
// against the guarded hand-entered candidate count, plus a per-entry
|
|
// currency gate in consumeBridgingTwin (the bucket key is only date+öre,
|
|
// and hand-entered rows often lack a cash_account_id for the account guard
|
|
// to bite on).
|
|
const bucketKey = contentBucketKey(raw.date, raw.amount)
|
|
const rawCurrency = raw.currency ?? null
|
|
const crossSourceMirror =
|
|
batchIsImportFeed &&
|
|
(crossSourceStoredByBucket.get(bucketKey) ?? 0) > 0 &&
|
|
incomingByBucket.get(bucketKey) === crossSourceStoredByBucket.get(bucketKey)
|
|
const handEnteredMirror =
|
|
batchIsImportFeed &&
|
|
(bookedHandEnteredByBucket.get(bucketKey) ?? 0) > 0 &&
|
|
unmatchedIncomingByBucket.get(bucketKey) === bookedHandEnteredByBucket.get(bucketKey)
|
|
const consumeBridgingTwin = (bucket: DescBucket): BucketEntry | null => {
|
|
const entries = bucket.get(bucketKey)
|
|
if (!entries || entries.length === 0) return null
|
|
let bestIdx = -1
|
|
let bestLen = -1
|
|
let crossIdx = -1
|
|
let handIdx = -1
|
|
for (let i = 0; i < entries.length; i++) {
|
|
const entry = entries[i]
|
|
const sameAccount =
|
|
cashAccountId === null || entry.cashAccountId === null || entry.cashAccountId === cashAccountId
|
|
if (!sameAccount) continue
|
|
// Currency guard, applies to ALL THREE match paths below. The bucket key
|
|
// is (date, öre) with no currency, so a stored 250,00 SEK row and an
|
|
// incoming 250,00 EUR row on the same date land in the SAME bucket; the
|
|
// text bridge (identical bank titles) or the cross-channel mirror would
|
|
// then consume one for the other and the survivor is silently never
|
|
// bokförd (BFL 5 kap: a real affärshändelse dropped without a trace).
|
|
// Two channels reporting the SAME movement always agree on the booked
|
|
// amount's currency, so a mismatch here means two different movements
|
|
// whose öre happen to coincide. Null on either side is compatible:
|
|
// rows predating the currency column, and feeds that send none, must
|
|
// dedup exactly as they did before this guard existed.
|
|
const sameCurrency =
|
|
entry.currency === null || rawCurrency === null || entry.currency === rawCurrency
|
|
if (!sameCurrency) continue
|
|
if (descriptionsBridge(description, entry.desc) && entry.desc.length > bestLen) {
|
|
bestIdx = i
|
|
bestLen = entry.desc.length
|
|
}
|
|
// Text-independent fallback: in a cross-channel mirror bucket a stored
|
|
// entry from a different feed is the same transaction even when the
|
|
// descriptions don't bridge. Remember the first eligible one.
|
|
if (crossIdx === -1 && crossSourceMirror && entry.isImportFeed && entry.source !== batchSource) {
|
|
crossIdx = i
|
|
}
|
|
// Booked-hand-entered fallback: hand-entered entries only exist in the
|
|
// booked map (the unbooked query excludes manual/mcp at the DB level),
|
|
// so !isImportFeed here already implies booked. The currency
|
|
// requirement this path used to carry on its own is now the loop-level
|
|
// `sameCurrency` guard above, which every match path shares.
|
|
if (handIdx === -1 && handEnteredMirror && !entry.isImportFeed) {
|
|
handIdx = i
|
|
}
|
|
}
|
|
const idx = bestIdx !== -1 ? bestIdx : crossIdx !== -1 ? crossIdx : handIdx
|
|
if (idx === -1) return null
|
|
const [consumed] = entries.splice(idx, 1)
|
|
return consumed
|
|
}
|
|
const consumedTwin =
|
|
consumeBridgingTwin(existingMaps.booked) ?? consumeBridgingTwin(existingMaps.unbookedImported)
|
|
if (consumedTwin) {
|
|
result.duplicates++
|
|
// Leave a trace of the drop. Layer-1 (external_id) duplicates are exact
|
|
// key collisions and need none, but this layer drops a row on a
|
|
// judgement call (text bridge / cross-channel mirror / hand mirror), and
|
|
// an incoming row dropped here is never inserted and therefore never
|
|
// bokförd. result.duplicates does count it, and that count reaches the
|
|
// API response plus bank_file_imports.duplicate_count, but no import UI
|
|
// currently RENDERS it (BankFileResultStep and the EB sync toast show
|
|
// `imported` only), and the count cannot distinguish this judgement call
|
|
// from an exact Layer-1 id collision anyway. So this log is the only
|
|
// per-row record of WHICH affärshändelse was dropped and against what;
|
|
// keep it until a UI surfaces skipped rows. Same field shape as the two
|
|
// shadow blocks below.
|
|
log.info('import dedup: content-bridge duplicate skipped', {
|
|
decision: 'content-bridge',
|
|
mode: 'enforced',
|
|
bucket: bucketKey,
|
|
incomingExternalId: raw.external_id,
|
|
incomingDescription: description,
|
|
incomingAmount: raw.amount,
|
|
incomingCurrency: rawCurrency,
|
|
incomingSource: raw.import_source ?? null,
|
|
cashAccountId,
|
|
matchedStoredId: consumedTwin.id,
|
|
matchedStoredExternalId: consumedTwin.externalId,
|
|
matchedStoredDescription: consumedTwin.desc,
|
|
matchedStoredCurrency: consumedTwin.currency,
|
|
matchedStoredCashAccountId: consumedTwin.cashAccountId,
|
|
})
|
|
// Persist the hand-mirror adoption: bind the account-unbound hand row to
|
|
// the account this feed batch settled on. Consumption is otherwise
|
|
// in-memory only, so without this ONE null-account hand row could
|
|
// consume one genuine feed row per sync call on EVERY account whose
|
|
// bucket happens to be date+öre+currency symmetric: permanent silent
|
|
// suppression across accounts. After the stamp, the account guard
|
|
// excludes this row from any other account's mirror. The `.is()` filter
|
|
// makes the write race-safe (never overwrites a concurrent binding),
|
|
// and a failure is non-critical: dedup already happened, the stamp only
|
|
// narrows future consumption.
|
|
if (
|
|
!consumedTwin.isImportFeed &&
|
|
consumedTwin.id !== null &&
|
|
consumedTwin.cashAccountId === null &&
|
|
cashAccountId !== null
|
|
) {
|
|
try {
|
|
const { error: stampError } = await supabase
|
|
.from('transactions')
|
|
.update({ cash_account_id: cashAccountId })
|
|
.eq('id', consumedTwin.id)
|
|
.is('cash_account_id', null)
|
|
if (stampError) {
|
|
log.warn('hand-mirror adoption stamp failed; row stays unbound', {
|
|
transactionId: consumedTwin.id,
|
|
cashAccountId,
|
|
error: stampError.message,
|
|
})
|
|
}
|
|
} catch (stampError) {
|
|
log.warn('hand-mirror adoption stamp failed; row stays unbound', {
|
|
transactionId: consumedTwin.id,
|
|
cashAccountId,
|
|
error: stampError instanceof Error ? stampError.message : String(stampError),
|
|
})
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
|
|
// SHADOW-ONLY: this row survived Layer-1 and Layer-2, so today it WILL
|
|
// insert. If its bucket is a symmetric same-feed scope-drift mirror (equal
|
|
// non-zero counts of unreconciled incoming rows and account-compatible
|
|
// same-feed drift candidates), an enforcing rule WOULD treat it as a
|
|
// re-import. We only record it (full content on both sides so every
|
|
// decision can be human-verified against real fleet data before any
|
|
// enforcement is switched on), then fall through and insert exactly as
|
|
// before. This block has NO effect on result.imported/duplicates.
|
|
if (scopeDriftShadow && batchIsImportFeed) {
|
|
const driftCount = driftCandidateStoredByBucket.get(bucketKey) ?? 0
|
|
const unmatchedCount = unmatchedIncomingByBucket.get(bucketKey) ?? 0
|
|
if (driftCount > 0 && unmatchedCount === driftCount) {
|
|
let matched: BucketEntry | undefined
|
|
for (const bucket of [existingMaps.booked, existingMaps.unbookedImported]) {
|
|
const entries = bucket.get(bucketKey)
|
|
if (!entries) continue
|
|
matched = entries.find(
|
|
(e) =>
|
|
e.isImportFeed &&
|
|
e.source === batchSource &&
|
|
e.externalId !== null &&
|
|
!incomingIdSet.has(e.externalId) &&
|
|
(cashAccountId === null ||
|
|
e.cashAccountId === null ||
|
|
e.cashAccountId === cashAccountId)
|
|
)
|
|
if (matched) break
|
|
}
|
|
if (matched) {
|
|
result.shadow_scope_drift_candidates =
|
|
(result.shadow_scope_drift_candidates ?? 0) + 1
|
|
log.info('import dedup shadow: same-feed scope-drift candidate', {
|
|
decision: 'same-feed-scope-drift',
|
|
mode: 'shadow',
|
|
bucket: bucketKey,
|
|
unmatchedIncoming: unmatchedCount,
|
|
driftCandidates: driftCount,
|
|
incomingExternalId: raw.external_id,
|
|
incomingDescription: description,
|
|
incomingAmount: raw.amount,
|
|
incomingSource: raw.import_source ?? null,
|
|
cashAccountId,
|
|
matchedStoredExternalId: matched.externalId,
|
|
matchedStoredDescription: matched.desc,
|
|
matchedStoredCashAccountId: matched.cashAccountId,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// SHADOW-ONLY: date-drift. This row survived Layer-1 + Layer-2 and WILL
|
|
// insert. The content bridge only matched its EXACT (date, öre) bucket, so a
|
|
// twin whose booking date drifted a day is invisible to it. Look ±1 day for
|
|
// an account-compatible stored twin that EITHER bridges by description
|
|
// (same/enriched title, the EB↔EB hotel/fee case) OR is a cross-feed
|
|
// count-symmetric mirror displaced by a day (the CSV↔EB case where the
|
|
// descriptions don't bridge). Record it for fleet validation, then insert
|
|
// unchanged: this block never affects result.imported/duplicates.
|
|
//
|
|
// Fail-safe date guard: measurement must NEVER abort a real import. raw.date
|
|
// is always ISO in practice, but a malformed value would make shiftIsoDate
|
|
// throw (new Date(NaN).toISOString()), so we skip detection rather than risk
|
|
// it. Any /^\d{4}-\d{2}-\d{2}$/ value is safe: Date.UTC normalizes
|
|
// out-of-range parts to a finite epoch, never NaN.
|
|
if (dateDriftShadow && batchIsImportFeed && /^\d{4}-\d{2}-\d{2}$/.test(raw.date)) {
|
|
let driftMatch: { entry: BucketEntry; gap: number; signal: 'desc' | 'cross-channel' } | undefined
|
|
const incomingHere = incomingByBucket.get(bucketKey) ?? 0
|
|
for (let delta = 1; delta <= DATE_DRIFT_WINDOW_DAYS && !driftMatch; delta++) {
|
|
for (const sign of [-1, 1] as const) {
|
|
const adjKey = contentBucketKey(shiftIsoDate(raw.date, sign * delta), raw.amount)
|
|
const entries = storedByBucketForDrift.get(adjKey)
|
|
if (!entries) continue
|
|
// Cross-feed count-symmetry across the drift: equal counts of incoming
|
|
// rows in THIS bucket and account-compatible cross-feed rows one day
|
|
// over, the cross-channel mirror, displaced by a date drift.
|
|
const adjCrossFeed = entries.filter(
|
|
(e) =>
|
|
e.isImportFeed &&
|
|
e.source !== batchSource &&
|
|
(cashAccountId === null || e.cashAccountId === null || e.cashAccountId === cashAccountId),
|
|
).length
|
|
const mirrorSymmetric = adjCrossFeed > 0 && incomingHere === adjCrossFeed
|
|
for (const entry of entries) {
|
|
const sameAccount =
|
|
cashAccountId === null || entry.cashAccountId === null || entry.cashAccountId === cashAccountId
|
|
if (!sameAccount) continue
|
|
if (descriptionsBridge(description, entry.desc)) {
|
|
driftMatch = { entry, gap: sign * delta, signal: 'desc' }
|
|
break
|
|
}
|
|
if (mirrorSymmetric && entry.isImportFeed && entry.source !== batchSource) {
|
|
driftMatch = { entry, gap: sign * delta, signal: 'cross-channel' }
|
|
break
|
|
}
|
|
}
|
|
if (driftMatch) break
|
|
}
|
|
}
|
|
if (driftMatch) {
|
|
result.shadow_date_drift_candidates = (result.shadow_date_drift_candidates ?? 0) + 1
|
|
log.info('import dedup shadow: date-drift candidate', {
|
|
decision: 'date-drift',
|
|
mode: 'shadow',
|
|
signal: driftMatch.signal,
|
|
dayGap: driftMatch.gap,
|
|
bucket: bucketKey,
|
|
incomingExternalId: raw.external_id,
|
|
incomingDescription: description,
|
|
incomingAmount: raw.amount,
|
|
incomingSource: raw.import_source ?? null,
|
|
cashAccountId,
|
|
matchedStoredExternalId: driftMatch.entry.externalId,
|
|
matchedStoredDescription: driftMatch.entry.desc,
|
|
matchedStoredCashAccountId: driftMatch.entry.cashAccountId,
|
|
})
|
|
}
|
|
}
|
|
|
|
// 2. Insert new transaction (with SEK conversion for foreign currencies)
|
|
const rateInfo = raw.currency && raw.currency !== 'SEK'
|
|
? exchangeRatesByDate.get(`${raw.currency}|${raw.date}`)
|
|
: undefined
|
|
const amountSek = rateInfo
|
|
? Math.round(raw.amount * rateInfo.rate * 100) / 100
|
|
: null
|
|
|
|
const { data: newTransaction, error: insertError } = await supabase
|
|
.from('transactions')
|
|
.insert({
|
|
company_id: companyId,
|
|
user_id: userId,
|
|
bank_connection_id: raw.bank_connection_id || null,
|
|
cash_account_id: cashAccountId,
|
|
external_id: raw.external_id,
|
|
date: raw.date,
|
|
description: description,
|
|
// Immutable bank/PSD2 original: captured once, never overwritten by a
|
|
// title edit. Equals description at insert; they diverge only if the
|
|
// user later edits the title.
|
|
original_description: description,
|
|
amount: raw.amount,
|
|
currency: raw.currency,
|
|
amount_sek: amountSek,
|
|
exchange_rate: rateInfo?.rate ?? null,
|
|
exchange_rate_date: rateInfo?.date ?? null,
|
|
category: 'uncategorized',
|
|
is_business: null,
|
|
mcc_code: raw.mcc_code || null,
|
|
merchant_name: raw.merchant_name || null,
|
|
reference: raw.reference || null,
|
|
import_source: raw.import_source || null,
|
|
counterparty_iban: raw.counterparty_iban || null,
|
|
counterparty_account: raw.counterparty_account || null,
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (insertError || !newTransaction) {
|
|
result.errors++
|
|
if (!result.first_error && insertError) {
|
|
result.first_error = {
|
|
message: insertError.message,
|
|
code: insertError.code ?? null,
|
|
details: insertError.details ?? null,
|
|
hint: insertError.hint ?? null,
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
|
|
result.imported++
|
|
result.transaction_ids.push(newTransaction.id)
|
|
|
|
// rawInsertOnly: skip invoice matching, and auto-categorization
|
|
if (options?.rawInsertOnly) continue
|
|
|
|
// Reconciliation against existing GL lines is intentionally NOT run on
|
|
// import: auto-linking made imported transactions appear "bokförda" to
|
|
// the user without any explicit action. Reconciliation is now a manual
|
|
// operation (BankReconciliationView / runReconciliation / manualLink).
|
|
|
|
// 3. For income transactions, try invoice matching
|
|
if (newTransaction.amount > 0) {
|
|
try {
|
|
// OCR/reference matching is handled inside getBestInvoiceMatch
|
|
// (which calls findMatchingInvoices, which now checks references)
|
|
const bestMatch = await getBestInvoiceMatch(
|
|
supabase,
|
|
companyId,
|
|
newTransaction as Transaction,
|
|
0.50
|
|
)
|
|
|
|
if (bestMatch && !matchedInvoiceIds.has(bestMatch.invoice.id)) {
|
|
await supabase
|
|
.from('transactions')
|
|
.update({ potential_invoice_id: bestMatch.invoice.id })
|
|
.eq('id', newTransaction.id)
|
|
|
|
logMatchEvent(supabase, userId, newTransaction.id, 'auto_suggested', {
|
|
invoiceId: bestMatch.invoice.id,
|
|
matchConfidence: bestMatch.confidence,
|
|
matchMethod: bestMatch.matchReason,
|
|
})
|
|
|
|
matchedInvoiceIds.add(bestMatch.invoice.id)
|
|
result.auto_matched_invoices++
|
|
// Skip mapping engine: transaction has an invoice match.
|
|
// Auto-categorization would create an orphaned journal entry
|
|
// that conflicts with the eventual invoice payment entry.
|
|
continue
|
|
}
|
|
} catch {
|
|
// Non-critical: continue processing
|
|
}
|
|
}
|
|
|
|
// 3b. For expense transactions, try supplier invoice matching
|
|
if (newTransaction.amount < 0 && unpaidSupplierInvoices.length > 0) {
|
|
try {
|
|
const match = findSupplierInvoiceMatch(
|
|
newTransaction as Transaction,
|
|
unpaidSupplierInvoices
|
|
)
|
|
|
|
if (match && !matchedSupplierInvoiceIds.has(match.supplierInvoice.id)) {
|
|
// ALWAYS a suggestion (potential_supplier_invoice_id), never a hard
|
|
// link. supplier_invoice_id is reserved for completed matches: the
|
|
// match route books the payment voucher when it sets it. A sync-time
|
|
// hard link booked nothing, left the invoice open, and then BLOCKED
|
|
// the match route (MATCH_SI_TX_ALREADY_LINKED), stranding the
|
|
// transaction with no path to a payment voucher.
|
|
await supabase
|
|
.from('transactions')
|
|
.update({ potential_supplier_invoice_id: match.supplierInvoice.id })
|
|
.eq('id', newTransaction.id)
|
|
|
|
logMatchEvent(supabase, userId, newTransaction.id, 'auto_suggested', {
|
|
supplierInvoiceId: match.supplierInvoice.id,
|
|
matchConfidence: match.confidence,
|
|
matchMethod: match.matchMethod,
|
|
})
|
|
|
|
if (match.confidence >= 0.85 && !match.ambiguous) {
|
|
// High-confidence unambiguous hit: drain the pool so the next
|
|
// transaction can't claim the same invoice, and skip the mapping
|
|
// engine: auto-categorization would create an orphaned journal
|
|
// entry that conflicts with the eventual payment booking.
|
|
unpaidSupplierInvoices = unpaidSupplierInvoices.filter(
|
|
inv => inv.id !== match.supplierInvoice.id
|
|
)
|
|
matchedSupplierInvoiceIds.add(match.supplierInvoice.id)
|
|
|
|
result.auto_matched_invoices++
|
|
continue
|
|
}
|
|
// Lower confidence (0.70-0.85) or ambiguous: tentative, do NOT
|
|
// drain the pool.
|
|
}
|
|
} catch {
|
|
// Non-critical: continue processing
|
|
}
|
|
}
|
|
|
|
// 4. Evaluate mapping rules for auto-categorization
|
|
// Production-disabled: auto-booking only runs in local dev (and tests).
|
|
// Users must explicitly book each transaction on the deployed app.
|
|
// Reconciliation (step 2.5) still links transactions to existing GL lines.
|
|
const autoBookEnabled = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'
|
|
if (autoBookEnabled && !options?.skipAutoCategorization) {
|
|
try {
|
|
const mappingResult = await evaluateMappingRules(
|
|
supabase,
|
|
companyId,
|
|
newTransaction as Transaction,
|
|
undefined,
|
|
options?.settlementAccount
|
|
)
|
|
|
|
if (mappingResult.confidence >= 0.8 && !mappingResult.requires_review) {
|
|
const journalEntry = await createTransactionJournalEntry(
|
|
supabase,
|
|
companyId,
|
|
userId,
|
|
newTransaction as Transaction,
|
|
mappingResult
|
|
)
|
|
|
|
if (journalEntry) {
|
|
await supabase
|
|
.from('transactions')
|
|
.update({
|
|
journal_entry_id: journalEntry.id,
|
|
is_business: !mappingResult.default_private,
|
|
})
|
|
.eq('id', newTransaction.id)
|
|
|
|
// Upsert counterparty template (auto-learned, lower confidence)
|
|
try {
|
|
await upsertCounterpartyTemplate(
|
|
supabase, companyId, newTransaction as Transaction,
|
|
mappingResult, 'auto_learned'
|
|
)
|
|
} catch {
|
|
// Non-critical
|
|
}
|
|
|
|
result.auto_categorized++
|
|
}
|
|
}
|
|
} catch {
|
|
// Non-critical: continue processing
|
|
}
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|