* Refactor bookkeeping error handling and introduce new error classes - Introduced new error classes for better error categorization: - JournalEntryNotBalancedError - FiscalPeriodNotFoundError - EntryDateOutsideFiscalPeriodError - JournalEntryNotFoundError - CannotReverseNonPostedError - CannotCorrectNonPostedError - EntryAlreadyReversedError - CurrencyRevaluationAlreadyExistsError - InvalidMappingResultError - BookkeepingDatabaseError - Updated existing functions in engine.ts and transaction-entries.ts to throw specific errors instead of generic ones. - Enhanced error response handling in get-error-message.ts to provide localized messages for new error types. - Added unit tests for new error classes and error handling functions to ensure correctness and coverage. * feat(ai): implement AI proposal application and persistence - Add apply.ts to handle the application of AI proposals, including match and booking steps. - Introduce persist.ts for inserting and managing AI requests and proposals, ensuring unique constraints. - Create re-validate.ts for validating proposals before acceptance, checking for stale conditions. - Define database migrations for ai_requests and ai_proposals tables, including constraints and indexes. - Enhance journal_entries with AI provenance tracking, linking entries to AI proposals. - Update categorization_templates to distinguish AI-corrected templates. - Add company settings for toggling AI flow and managing backfill processes. - Extend processing_history to include AI-related events for better tracking. * feat: add uncategorized transactions API and UI for transaction selection - Implemented a new API endpoint for fetching uncategorized transactions with pagination and filtering options. - Created ChangeTransactionDialog component for selecting alternative transactions based on AI proposals. - Developed ReceiptDetailDialog to display detailed information about receipts, including upload functionality. - Added TransactionDetailDialog for viewing transaction details with links to the transaction list. - Introduced receipt quality assessment logic to evaluate extracted receipt data. - Implemented feature flagging for the AI bookkeeping agent to control availability in different environments. * feat: add manual receipt extraction dialog and integrate AWS Textract for expense analysis - Added ManualExtractDialog component for user input when AI fails to extract receipt data. - Implemented ReceiptsList component to manage and display uploaded receipts, including upload and rescan functionalities. - Introduced Textract integration for analyzing expenses, extracting fields like total, vendor, and date. - Updated package.json to include @aws-sdk/client-textract dependency. * fix(ai): handle livsmedel VAT transition (12% → 6%) in booking prompt and re-validate guard Add date-aware guidance to BOOKING_SYSTEM_PROMPT for the temporary livsmedel VAT cut (Prop. 2025/26:55, 2026-04-01 to 2027-12-31), with restaurang/servering carve-out at 12%. Add a re-validate safety net that rejects clearly-stale rate labels for grocery-chain merchants relative to the entry date. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
94 lines
3.6 KiB
TypeScript
94 lines
3.6 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import { ensureInitialized } from '@/lib/init'
|
|
import { requireCompanyId } from '@/lib/company/context'
|
|
import { gateAgentInbox } from '@/lib/ai/feature-flag'
|
|
|
|
ensureInitialized()
|
|
|
|
/**
|
|
* GET /api/transactions/uncategorized
|
|
*
|
|
* Paginated list of uncategorized expense transactions for a picker UI
|
|
* (e.g. agent-inkorg's "Byt transaktion" flow). Returns expenses only —
|
|
* amount < 0 — since match proposals always pair receipts to outgoing
|
|
* payments. Includes basic range filters so callers can narrow to matches
|
|
* within ±window of a target amount/date.
|
|
*
|
|
* Query params:
|
|
* search Free-text against description/merchant_name (ILIKE)
|
|
* amount_center Target amount (signed). Must be accompanied by amount_window.
|
|
* amount_window Half-window in SEK — e.g. 50 means amount_center ± 50.
|
|
* date_center Target ISO date. Must be accompanied by date_window.
|
|
* date_window Half-window in days — e.g. 30 means ±30 days.
|
|
* limit Max rows (1-50, default 20).
|
|
* offset Row offset for pagination (default 0).
|
|
*/
|
|
export async function GET(request: Request) {
|
|
const gate = gateAgentInbox()
|
|
if (gate) return gate
|
|
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
|
|
const companyId = await requireCompanyId(supabase, user.id)
|
|
const url = new URL(request.url)
|
|
|
|
const search = url.searchParams.get('search')?.trim() ?? ''
|
|
const amountCenterRaw = url.searchParams.get('amount_center')
|
|
const amountWindowRaw = url.searchParams.get('amount_window')
|
|
const dateCenterRaw = url.searchParams.get('date_center')
|
|
const dateWindowRaw = url.searchParams.get('date_window')
|
|
const limit = Math.min(Math.max(1, Number(url.searchParams.get('limit')) || 20), 50)
|
|
const offset = Math.max(0, Number(url.searchParams.get('offset')) || 0)
|
|
|
|
let query = supabase
|
|
.from('transactions')
|
|
.select('id, date, description, amount, currency, merchant_name, category, is_business', { count: 'exact' })
|
|
.eq('company_id', companyId)
|
|
.is('journal_entry_id', null)
|
|
.lt('amount', 0)
|
|
.order('date', { ascending: false })
|
|
.range(offset, offset + limit - 1)
|
|
|
|
if (search.length > 0) {
|
|
const escaped = search.replace(/[%_]/g, '\\$&')
|
|
query = query.or(`description.ilike.%${escaped}%,merchant_name.ilike.%${escaped}%`)
|
|
}
|
|
|
|
if (amountCenterRaw && amountWindowRaw) {
|
|
const center = Number(amountCenterRaw)
|
|
const window = Math.abs(Number(amountWindowRaw))
|
|
if (Number.isFinite(center) && Number.isFinite(window) && window > 0) {
|
|
query = query.gte('amount', center - window).lte('amount', center + window)
|
|
}
|
|
}
|
|
|
|
if (dateCenterRaw && dateWindowRaw) {
|
|
const windowDays = Math.abs(Number(dateWindowRaw))
|
|
if (Number.isFinite(windowDays) && windowDays > 0) {
|
|
const center = new Date(dateCenterRaw)
|
|
if (!Number.isNaN(center.getTime())) {
|
|
const msPerDay = 86_400_000
|
|
const from = new Date(center.getTime() - windowDays * msPerDay)
|
|
const to = new Date(center.getTime() + windowDays * msPerDay)
|
|
query = query.gte('date', from.toISOString().slice(0, 10))
|
|
query = query.lte('date', to.toISOString().slice(0, 10))
|
|
}
|
|
}
|
|
}
|
|
|
|
const { data, error, count } = await query
|
|
if (error) return NextResponse.json({ error: error.message }, { status: 500 })
|
|
|
|
return NextResponse.json({
|
|
data: {
|
|
transactions: data ?? [],
|
|
count: count ?? 0,
|
|
limit,
|
|
offset,
|
|
},
|
|
})
|
|
}
|