Add journal entry preview, human-readable account names, auto-apply VAT, fallback template suggestions, example prompts, invoice match comparison, and batch result feedback. Also includes user-description-match extension, describe/batch-describe API routes, improved AI categorization with multi- suggestion support, and template embedding search. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
99 lines
3.1 KiB
TypeScript
99 lines
3.1 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { ensureInitialized } from '@/lib/init'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { DescribeTransactionSchema } from '@/lib/api/schemas'
|
|
import { findSimilarTemplates } from '@/lib/bookkeeping/template-embeddings'
|
|
import type { Transaction, EntityType } from '@/types'
|
|
|
|
ensureInitialized()
|
|
|
|
export async function POST(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const supabase = await createClient()
|
|
const { id } = await params
|
|
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const validation = await validateBody(request, DescribeTransactionSchema)
|
|
if (!validation.success) return validation.response
|
|
const { description } = validation.data
|
|
|
|
// Fetch the transaction (validates ownership)
|
|
const { data: transaction, error: fetchError } = await supabase
|
|
.from('transactions')
|
|
.select('*')
|
|
.eq('id', id)
|
|
.eq('user_id', user.id)
|
|
.single()
|
|
|
|
if (fetchError || !transaction) {
|
|
return NextResponse.json({ error: 'Transaction not found' }, { status: 404 })
|
|
}
|
|
|
|
// Fetch entity type
|
|
const { data: settings } = await supabase
|
|
.from('company_settings')
|
|
.select('entity_type')
|
|
.eq('user_id', user.id)
|
|
.single()
|
|
|
|
const entityType: EntityType = (settings?.entity_type as EntityType) || 'enskild_firma'
|
|
|
|
// Run embedding search with user description dominating the query
|
|
const templates = await findSimilarTemplates(
|
|
transaction as Transaction,
|
|
entityType,
|
|
10,
|
|
description
|
|
)
|
|
|
|
// Flag if top confidence is too low
|
|
const needsMoreDetail = templates.length === 0 || templates[0].confidence < 0.55
|
|
|
|
// Count uncategorized sibling transactions from same merchant
|
|
let batchCandidateCount = 0
|
|
if (transaction.merchant_name) {
|
|
const { count } = await supabase
|
|
.from('transactions')
|
|
.select('id', { count: 'exact', head: true })
|
|
.eq('user_id', user.id)
|
|
.eq('merchant_name', transaction.merchant_name)
|
|
.is('journal_entry_id', null)
|
|
.neq('id', id)
|
|
|
|
batchCandidateCount = count || 0
|
|
}
|
|
|
|
return NextResponse.json({
|
|
data: {
|
|
templates: templates.map((m) => ({
|
|
template_id: m.template.id,
|
|
name_sv: m.template.name_sv,
|
|
name_en: m.template.name_en,
|
|
group: m.template.group,
|
|
debit_account: m.template.debit_account,
|
|
credit_account: m.template.credit_account,
|
|
confidence: m.confidence,
|
|
description_sv: m.template.description_sv,
|
|
vat_rate: m.template.vat_rate,
|
|
vat_treatment: m.template.vat_treatment,
|
|
deductibility: m.template.deductibility,
|
|
deductibility_note_sv: m.template.deductibility_note_sv || null,
|
|
special_rules_sv: m.template.special_rules_sv || null,
|
|
risk_level: m.template.risk_level,
|
|
})),
|
|
needs_more_detail: needsMoreDetail,
|
|
user_description: description,
|
|
batch_candidate_count: batchCandidateCount,
|
|
merchant_name: transaction.merchant_name,
|
|
},
|
|
})
|
|
}
|