Feat/verification attachments (#371)
* feat(attachments): enhance document preview functionality for images and PDFs * feat(reconciliation): enhance transaction matching logic and clarify reconciliation process
This commit is contained in:
@@ -31,13 +31,21 @@ function isImageType(type: string | null): boolean {
|
||||
return type?.startsWith('image/') ?? false
|
||||
}
|
||||
|
||||
function isPdfType(type: string | null): boolean {
|
||||
return type === 'application/pdf'
|
||||
}
|
||||
|
||||
function isPreviewable(type: string | null): boolean {
|
||||
return isImageType(type) || isPdfType(type)
|
||||
}
|
||||
|
||||
export default function JournalEntryAttachments({
|
||||
journalEntryId,
|
||||
onCountChange,
|
||||
}: JournalEntryAttachmentsProps) {
|
||||
const [documents, setDocuments] = useState<DocumentRecord[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [expandedImage, setExpandedImage] = useState<string | null>(null)
|
||||
const [expandedDoc, setExpandedDoc] = useState<string | null>(null)
|
||||
const [showUpload, setShowUpload] = useState(false)
|
||||
const [uploadFiles, setUploadFiles] = useState<UploadedFile[]>([])
|
||||
|
||||
@@ -87,8 +95,8 @@ export default function JournalEntryAttachments({
|
||||
}
|
||||
|
||||
const handlePreviewToggle = async (doc: DocumentRecord) => {
|
||||
if (expandedImage === doc.id) {
|
||||
setExpandedImage(null)
|
||||
if (expandedDoc === doc.id) {
|
||||
setExpandedDoc(null)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -108,7 +116,7 @@ export default function JournalEntryAttachments({
|
||||
}
|
||||
}
|
||||
|
||||
setExpandedImage(doc.id)
|
||||
setExpandedDoc(doc.id)
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
@@ -158,12 +166,12 @@ export default function JournalEntryAttachments({
|
||||
{documents.map((doc) => (
|
||||
<div key={doc.id}>
|
||||
<div className="flex items-center gap-2 text-sm py-1.5 px-2 rounded bg-muted/50">
|
||||
{isImageType(doc.mime_type) ? (
|
||||
{isPreviewable(doc.mime_type) ? (
|
||||
<button
|
||||
onClick={() => handlePreviewToggle(doc)}
|
||||
className="shrink-0 hover:text-primary transition-colors"
|
||||
>
|
||||
{expandedImage === doc.id ? (
|
||||
{expandedDoc === doc.id ? (
|
||||
<ChevronUp className="h-4 w-4" />
|
||||
) : (
|
||||
<ChevronDown className="h-4 w-4" />
|
||||
@@ -173,8 +181,12 @@ export default function JournalEntryAttachments({
|
||||
<FileText className="h-4 w-4 text-muted-foreground shrink-0" />
|
||||
)}
|
||||
|
||||
{isImageType(doc.mime_type) && expandedImage !== doc.id && (
|
||||
<ImageIcon className="h-4 w-4 text-muted-foreground shrink-0" />
|
||||
{isPreviewable(doc.mime_type) && expandedDoc !== doc.id && (
|
||||
isImageType(doc.mime_type) ? (
|
||||
<ImageIcon className="h-4 w-4 text-muted-foreground shrink-0" />
|
||||
) : (
|
||||
<FileText className="h-4 w-4 text-muted-foreground shrink-0" />
|
||||
)
|
||||
)}
|
||||
|
||||
<span className="truncate flex-1">{doc.file_name}</span>
|
||||
@@ -194,7 +206,7 @@ export default function JournalEntryAttachments({
|
||||
</div>
|
||||
|
||||
{/* Image preview */}
|
||||
{expandedImage === doc.id && doc.download_url && (
|
||||
{expandedDoc === doc.id && doc.download_url && isImageType(doc.mime_type) && (
|
||||
<div className="px-2 py-2">
|
||||
<img
|
||||
src={doc.download_url}
|
||||
@@ -203,6 +215,17 @@ export default function JournalEntryAttachments({
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* PDF preview */}
|
||||
{expandedDoc === doc.id && doc.download_url && isPdfType(doc.mime_type) && (
|
||||
<div className="px-2 py-2">
|
||||
<iframe
|
||||
src={doc.download_url}
|
||||
title={doc.file_name}
|
||||
className="w-full h-[60vh] rounded-lg border"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
+17
-14
@@ -1,25 +1,28 @@
|
||||
/**
|
||||
* Agent-inkorg feature flag.
|
||||
*
|
||||
* The AI bookkeeping agent isn't ready for general availability in production.
|
||||
* This helper gates the whole feature — sidebar link, page, API routes, and
|
||||
* orchestrator event handlers — behind either:
|
||||
*
|
||||
* 1. NODE_ENV === 'development' (local dev: always on)
|
||||
* 2. NEXT_PUBLIC_AGENT_INBOX_ENABLED=true (opt-in for staging/prod QA)
|
||||
*
|
||||
* The escape hatch lets us flip the feature on for a specific Vercel
|
||||
* deployment (staging) without a code change, and keeps prod deployments
|
||||
* safely dark until we explicitly enable it.
|
||||
*
|
||||
* Mirrors the pattern used for Salary in components/dashboard/DashboardNav.tsx.
|
||||
* The AI bookkeeping agent isn't ready for general availability. It is
|
||||
* strictly local-dev only — sidebar link, page, API routes, and orchestrator
|
||||
* event handlers all return 404 / are hidden on any deployed (Vercel) build.
|
||||
*/
|
||||
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
export function isAgentInboxEnabled(): boolean {
|
||||
if (process.env.NODE_ENV === 'development') return true
|
||||
return process.env.NEXT_PUBLIC_AGENT_INBOX_ENABLED === 'true'
|
||||
return process.env.NODE_ENV === 'development'
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto-booking of bank transactions during ingest.
|
||||
*
|
||||
* Mapping-rule-driven creation of journal entries on import is a future
|
||||
* feature. It must NEVER run on the deployed Vercel production build —
|
||||
* users have to explicitly book each transaction. Allowed only in local
|
||||
* dev (and in the test environment so the auto-book pipeline stays under
|
||||
* test coverage). No env-var escape hatch.
|
||||
*/
|
||||
export function isAutoBookEnabled(): boolean {
|
||||
return process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -73,9 +73,9 @@ describe('tryReconcileTransaction', () => {
|
||||
})
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Pass 2: Exact amount + reference match
|
||||
// Pass 2: Exact amount + OCR/reference match (within ±90 days)
|
||||
// ------------------------------------------------------------------
|
||||
it('matches on exact amount with reference match', () => {
|
||||
it('matches on exact amount with OCR reference match within 90 days', () => {
|
||||
const tx = makeTransaction({
|
||||
amount: 3500,
|
||||
date: '2024-06-20',
|
||||
@@ -95,6 +95,47 @@ describe('tryReconcileTransaction', () => {
|
||||
expect(result!.confidence).toBe(0.90)
|
||||
})
|
||||
|
||||
// Regression: viktor@frnzn.com — recurring monthly bank fee from 2026 was
|
||||
// wrongly reconciled to a 2024 SIE-imported voucher because description +
|
||||
// amount collided. auto_reference must require a real OCR token AND a
|
||||
// bounded date window — description alone, no date check, is not enough.
|
||||
it('does NOT match recurring charge across years on description alone', () => {
|
||||
const tx = makeTransaction({
|
||||
amount: -149,
|
||||
date: '2026-01-31',
|
||||
currency: 'SEK',
|
||||
description: 'Månadsavgift Baspaket',
|
||||
reference: null,
|
||||
})
|
||||
const line = makeGLLine({
|
||||
credit_amount: 149,
|
||||
entry_date: '2024-03-31',
|
||||
entry_description: 'Bankavgifter Månadsavgift Baspaket',
|
||||
})
|
||||
|
||||
const result = tryReconcileTransaction(tx, [line])
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('does NOT match on OCR reference when dates are >90 days apart', () => {
|
||||
const tx = makeTransaction({
|
||||
amount: 3500,
|
||||
date: '2026-06-20',
|
||||
currency: 'SEK',
|
||||
reference: '12345678',
|
||||
})
|
||||
const line = makeGLLine({
|
||||
debit_amount: 3500,
|
||||
entry_date: '2024-06-10',
|
||||
entry_description: 'Payment ref 12345678',
|
||||
})
|
||||
|
||||
const result = tryReconcileTransaction(tx, [line])
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Pass 3: Exact amount + date within ±3 days
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
@@ -69,7 +69,6 @@ export function tryReconcileTransaction(
|
||||
|
||||
const txAmount = transaction.amount
|
||||
const txDate = transaction.date
|
||||
const txDescription = (transaction.description || '').toLowerCase()
|
||||
const txReference = (transaction.reference || '').toLowerCase()
|
||||
|
||||
let bestMatch: ReconciliationMatch | null = null
|
||||
@@ -82,7 +81,12 @@ export function tryReconcileTransaction(
|
||||
const fuzzyAmountMatches = Math.abs(Math.abs(txAmount) - Math.abs(lineAmount)) <= 0.01
|
||||
const exactDateMatch = txDate === line.entry_date
|
||||
const dateWithinRange = isDateWithinRange(txDate, line.entry_date, 3)
|
||||
const referenceMatch = hasReferenceMatch(txDescription, txReference, line)
|
||||
// Reference matches require BOTH a real OCR/reference token AND a bounded
|
||||
// date window. Never description-only — that collides on recurring monthly
|
||||
// charges (same description, same amount, different year). Never cross-year.
|
||||
const referenceMatch =
|
||||
hasOcrReferenceMatch(txReference, line) &&
|
||||
isDateWithinRange(txDate, line.entry_date, 90)
|
||||
|
||||
let method: ReconciliationMethod | null = null
|
||||
let confidence = 0
|
||||
@@ -92,7 +96,7 @@ export function tryReconcileTransaction(
|
||||
method = 'auto_exact'
|
||||
confidence = 0.95
|
||||
}
|
||||
// Pass 2: Exact amount + reference match
|
||||
// Pass 2: Exact amount + OCR/reference match within ±90 days
|
||||
else if (amountMatches && referenceMatch) {
|
||||
method = 'auto_reference'
|
||||
confidence = 0.90
|
||||
@@ -484,6 +488,19 @@ function isDirectionCompatible(txAmount: number, line: UnlinkedGLLine): boolean
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* OCR/reference-number match. Requires a non-trivial reference token (≥4 chars)
|
||||
* on the transaction that appears in the GL line/entry description. Description
|
||||
* substring matching is intentionally NOT done here — that collided on recurring
|
||||
* monthly charges across years (same description, same amount, different year).
|
||||
*/
|
||||
function hasOcrReferenceMatch(txReference: string, line: UnlinkedGLLine): boolean {
|
||||
if (!txReference || txReference.length < 4) return false
|
||||
const lineDesc = (line.line_description || '').toLowerCase()
|
||||
const entryDesc = (line.entry_description || '').toLowerCase()
|
||||
return lineDesc.includes(txReference) || entryDesc.includes(txReference)
|
||||
}
|
||||
|
||||
/** Check if two dates are within ±dayRange of each other */
|
||||
function isDateWithinRange(date1: string, date2: string, dayRange: number): boolean {
|
||||
const d1 = new Date(date1)
|
||||
@@ -493,32 +510,6 @@ function isDateWithinRange(date1: string, date2: string, dayRange: number): bool
|
||||
return diffDays <= dayRange
|
||||
}
|
||||
|
||||
/** Check if transaction description/reference matches the GL line description */
|
||||
function hasReferenceMatch(
|
||||
txDescription: string,
|
||||
txReference: string,
|
||||
line: UnlinkedGLLine
|
||||
): boolean {
|
||||
const lineDesc = (line.line_description || '').toLowerCase()
|
||||
const entryDesc = (line.entry_description || '').toLowerCase()
|
||||
|
||||
if (!txReference && !txDescription) return false
|
||||
|
||||
// Check OCR/reference number match
|
||||
if (txReference && txReference.length >= 4) {
|
||||
if (lineDesc.includes(txReference) || entryDesc.includes(txReference)) return true
|
||||
}
|
||||
|
||||
// Check description overlap (at least 8 chars matching substring)
|
||||
if (txDescription && txDescription.length >= 8) {
|
||||
if (lineDesc.includes(txDescription) || entryDesc.includes(txDescription)) return true
|
||||
if (txDescription.includes(lineDesc) && lineDesc.length >= 8) return true
|
||||
if (txDescription.includes(entryDesc) && entryDesc.length >= 8) return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Greedy matching: run 4-pass matching, each pass at a specific confidence level.
|
||||
* Track used GL lines and transactions to prevent double-matching.
|
||||
|
||||
@@ -28,13 +28,6 @@ vi.mock('@/lib/invoices/invoice-matching', () => ({
|
||||
getBestInvoiceMatch: (...args: unknown[]) => mockGetBestInvoiceMatch(...args),
|
||||
}))
|
||||
|
||||
const mockTryReconcileTransaction = vi.fn()
|
||||
const mockFetchUnlinkedGLLines = vi.fn()
|
||||
vi.mock('@/lib/reconciliation/bank-reconciliation', () => ({
|
||||
tryReconcileTransaction: (...args: unknown[]) => mockTryReconcileTransaction(...args),
|
||||
fetchUnlinkedGLLines: (...args: unknown[]) => mockFetchUnlinkedGLLines(...args),
|
||||
}))
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Queue-based Supabase mock
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -125,9 +118,6 @@ function makeMappingResult(overrides: Record<string, unknown> = {}) {
|
||||
describe('ingestTransactions', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
// Default: no GL lines for reconciliation
|
||||
mockFetchUnlinkedGLLines.mockResolvedValue([])
|
||||
mockTryReconcileTransaction.mockReturnValue(null)
|
||||
})
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
@@ -571,9 +561,12 @@ describe('ingestTransactions', () => {
|
||||
})
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Reconciliation: matched transactions skip auto-categorization
|
||||
// Imports never auto-link to existing journal entries.
|
||||
// Reconciliation must be an explicit user action (manualLink / runReconciliation).
|
||||
// Regression: viktor@frnzn.com — bank txns from 2026 were silently linked
|
||||
// to SIE-imported vouchers, surfacing them as "bokförda" without action.
|
||||
// -----------------------------------------------------------------------
|
||||
it('reconciles transactions against GL lines and skips auto-categorization', async () => {
|
||||
it('never auto-reconciles imported transactions to existing GL lines', async () => {
|
||||
const { supabase, enqueue } = createQueueMockSupabase()
|
||||
const raw = makeRaw({ amount: -500, external_id: 'ext-recon' })
|
||||
const inserted = makeTransaction({
|
||||
@@ -583,75 +576,6 @@ describe('ingestTransactions', () => {
|
||||
currency: 'SEK',
|
||||
})
|
||||
|
||||
const glLine = {
|
||||
line_id: 'line-1',
|
||||
journal_entry_id: 'je-1',
|
||||
debit_amount: 0,
|
||||
credit_amount: 500,
|
||||
line_description: null,
|
||||
entry_date: '2024-06-15',
|
||||
voucher_number: 1,
|
||||
voucher_series: 'A',
|
||||
entry_description: 'Test entry',
|
||||
source_type: 'import',
|
||||
}
|
||||
|
||||
// Pre-fetch returns GL lines
|
||||
mockFetchUnlinkedGLLines.mockResolvedValue([glLine])
|
||||
// tryReconcileTransaction returns a match
|
||||
mockTryReconcileTransaction.mockReturnValue({
|
||||
transaction: inserted,
|
||||
glLine,
|
||||
method: 'auto_exact',
|
||||
confidence: 0.95,
|
||||
})
|
||||
|
||||
// Booked transaction map query
|
||||
enqueue({ data: [], error: null })
|
||||
// Unbooked bank-synced transaction map query
|
||||
enqueue({ data: [], error: null })
|
||||
// Supplier invoices fetch
|
||||
enqueue({ data: [], error: null })
|
||||
// Batch external_id dedup query (no matches)
|
||||
enqueue({ data: [], error: null })
|
||||
// Insert
|
||||
enqueue({ data: inserted, error: null })
|
||||
// Reconciliation update
|
||||
enqueue({ data: null, error: null })
|
||||
|
||||
const result = await ingestTransactions(supabase as never, COMPANY_ID, USER_ID, [raw])
|
||||
|
||||
expect(result.imported).toBe(1)
|
||||
expect(result.reconciled).toBe(1)
|
||||
// Should NOT have attempted auto-categorization
|
||||
expect(mockEvaluateMappingRules).not.toHaveBeenCalled()
|
||||
expect(mockGetBestInvoiceMatch).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Reconciliation: falls through when no GL matches
|
||||
// -----------------------------------------------------------------------
|
||||
it('falls through to auto-categorization when reconciliation finds no match', async () => {
|
||||
const { supabase, enqueue } = createQueueMockSupabase()
|
||||
const raw = makeRaw({ amount: -200 })
|
||||
const inserted = makeTransaction({ id: 'tx-no-recon', amount: -200 })
|
||||
|
||||
mockFetchUnlinkedGLLines.mockResolvedValue([
|
||||
{
|
||||
line_id: 'line-other',
|
||||
journal_entry_id: 'je-other',
|
||||
debit_amount: 999,
|
||||
credit_amount: 0,
|
||||
entry_date: '2024-01-01',
|
||||
voucher_number: 1,
|
||||
voucher_series: 'A',
|
||||
entry_description: 'Unrelated',
|
||||
source_type: 'import',
|
||||
line_description: null,
|
||||
},
|
||||
])
|
||||
mockTryReconcileTransaction.mockReturnValue(null)
|
||||
|
||||
// Booked transaction map query
|
||||
enqueue({ data: [], error: null })
|
||||
// Unbooked bank-synced transaction map query
|
||||
@@ -669,38 +593,6 @@ describe('ingestTransactions', () => {
|
||||
|
||||
expect(result.imported).toBe(1)
|
||||
expect(result.reconciled).toBe(0)
|
||||
// Should have fallen through to auto-categorization
|
||||
expect(mockEvaluateMappingRules).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Reconciliation: error is non-critical
|
||||
// -----------------------------------------------------------------------
|
||||
it('continues when reconciliation throws an error', async () => {
|
||||
const { supabase, enqueue } = createQueueMockSupabase()
|
||||
const raw = makeRaw({ amount: -300 })
|
||||
const inserted = makeTransaction({ id: 'tx-recon-err', amount: -300 })
|
||||
|
||||
mockFetchUnlinkedGLLines.mockRejectedValue(new Error('RPC error'))
|
||||
|
||||
// Booked transaction map query
|
||||
enqueue({ data: [], error: null })
|
||||
// Unbooked bank-synced transaction map query
|
||||
enqueue({ data: [], error: null })
|
||||
// Supplier invoices fetch
|
||||
enqueue({ data: [], error: null })
|
||||
// Batch external_id dedup query (no matches)
|
||||
enqueue({ data: [], error: null })
|
||||
// Insert
|
||||
enqueue({ data: inserted, error: null })
|
||||
|
||||
mockEvaluateMappingRules.mockResolvedValue(makeMappingResult({ confidence: 0.5 }))
|
||||
|
||||
const result = await ingestTransactions(supabase as never, COMPANY_ID, USER_ID, [raw])
|
||||
|
||||
expect(result.imported).toBe(1)
|
||||
expect(result.reconciled).toBe(0)
|
||||
expect(result.errors).toBe(0)
|
||||
})
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
@@ -735,8 +627,6 @@ describe('ingestTransactions', () => {
|
||||
expect(result.auto_categorized).toBe(0)
|
||||
expect(result.auto_matched_invoices).toBe(0)
|
||||
// Should NOT have attempted any post-insert operations
|
||||
expect(mockFetchUnlinkedGLLines).not.toHaveBeenCalled()
|
||||
expect(mockTryReconcileTransaction).not.toHaveBeenCalled()
|
||||
expect(mockGetBestInvoiceMatch).not.toHaveBeenCalled()
|
||||
expect(mockEvaluateMappingRules).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
+14
-42
@@ -1,14 +1,13 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js'
|
||||
import { evaluateMappingRules } from '@/lib/bookkeeping/mapping-engine'
|
||||
import { createTransactionJournalEntry } from '@/lib/bookkeeping/transaction-entries'
|
||||
import { isAutoBookEnabled } from '@/lib/ai/feature-flag'
|
||||
import { upsertCounterpartyTemplate } from '@/lib/bookkeeping/counterparty-templates'
|
||||
import { getBestInvoiceMatch } from '@/lib/invoices/invoice-matching'
|
||||
import { findSupplierInvoiceMatch } from '@/lib/invoices/supplier-invoice-matching'
|
||||
import { tryReconcileTransaction, fetchUnlinkedGLLines } from '@/lib/reconciliation/bank-reconciliation'
|
||||
import { fetchMultipleRates } from '@/lib/currency/riksbanken'
|
||||
import { logMatchEvent } from '@/lib/invoices/match-log'
|
||||
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
||||
import type { UnlinkedGLLine } from '@/lib/reconciliation/bank-reconciliation'
|
||||
import type { Transaction, RawTransaction, IngestResult, IngestOptions, SupplierInvoice, Currency, ExchangeRate } from '@/types'
|
||||
|
||||
// Re-export types for backward compatibility
|
||||
@@ -142,20 +141,12 @@ export async function ingestTransactions(
|
||||
return aiFlowEnabledCache
|
||||
}
|
||||
|
||||
// When rawInsertOnly is set (viewer imports), skip pre-fetching GL lines,
|
||||
// supplier invoices, and exchange rates — they are not used.
|
||||
let glLinePool: UnlinkedGLLine[] = []
|
||||
// When rawInsertOnly is set (viewer imports), skip pre-fetching supplier
|
||||
// invoices and exchange rates — they are not used.
|
||||
let unpaidSupplierInvoices: SupplierInvoice[] = []
|
||||
let exchangeRates = new Map<Currency, ExchangeRate>()
|
||||
|
||||
if (!options?.rawInsertOnly) {
|
||||
// Pre-fetch unlinked GL lines for reconciliation (non-critical)
|
||||
try {
|
||||
glLinePool = await fetchUnlinkedGLLines(supabase, companyId, undefined, undefined, options?.settlementAccount)
|
||||
} catch {
|
||||
// Non-critical — reconciliation will be skipped
|
||||
}
|
||||
|
||||
// Pre-fetch unpaid supplier invoices for expense matching (non-critical)
|
||||
try {
|
||||
unpaidSupplierInvoices = await fetchAllRows<SupplierInvoice>(({ from, to }) =>
|
||||
@@ -275,32 +266,13 @@ export async function ingestTransactions(
|
||||
result.imported++
|
||||
result.transaction_ids.push(newTransaction.id)
|
||||
|
||||
// rawInsertOnly: skip reconciliation, invoice matching, and auto-categorization
|
||||
// rawInsertOnly: skip invoice matching, and auto-categorization
|
||||
if (options?.rawInsertOnly) continue
|
||||
|
||||
// 2.5. Try reconciliation against pre-fetched unlinked GL lines
|
||||
if (glLinePool.length > 0) {
|
||||
try {
|
||||
const match = tryReconcileTransaction(newTransaction as Transaction, glLinePool)
|
||||
if (match) {
|
||||
await supabase
|
||||
.from('transactions')
|
||||
.update({
|
||||
journal_entry_id: match.glLine.journal_entry_id,
|
||||
reconciliation_method: match.method,
|
||||
is_business: true,
|
||||
})
|
||||
.eq('id', newTransaction.id)
|
||||
|
||||
// Remove matched GL line from pool to prevent double-matching
|
||||
glLinePool = glLinePool.filter((l) => l.line_id !== match.glLine.line_id)
|
||||
result.reconciled++
|
||||
continue // Skip invoice matching and auto-categorization
|
||||
}
|
||||
} catch {
|
||||
// Non-critical — fall through to normal flow
|
||||
}
|
||||
}
|
||||
// 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) {
|
||||
@@ -391,12 +363,12 @@ export async function ingestTransactions(
|
||||
}
|
||||
|
||||
// 4. Evaluate mapping rules for auto-categorization
|
||||
// Skipped when SIE-imported entries overlap the sync range — prevents
|
||||
// double-booking. Reconciliation (step 2.5) still links transactions to
|
||||
// existing GL lines; only the "create new journal entry" path is suppressed.
|
||||
// Also skipped when the company has opted into the AI agent flow — every
|
||||
// uncategorized transaction must become a proposal, not a silent post.
|
||||
if (!options?.skipAutoCategorization && !(await isAiFlowEnabled())) {
|
||||
// Production-disabled: auto-booking only runs in local dev (isAutoBookEnabled).
|
||||
// Users must explicitly book each transaction on the deployed app.
|
||||
// Also skipped when the company has opted into the AI agent flow (proposals)
|
||||
// and when SIE-imported entries overlap the sync range (prevents double-book).
|
||||
// Reconciliation (step 2.5) still links transactions to existing GL lines.
|
||||
if (isAutoBookEnabled() && !options?.skipAutoCategorization && !(await isAiFlowEnabled())) {
|
||||
try {
|
||||
const mappingResult = await evaluateMappingRules(
|
||||
supabase,
|
||||
|
||||
Reference in New Issue
Block a user