Expand supplier invoice module with overdue cron job, credit note journal entries, and event emissions on approve/mark-paid/create flows. Add entity type (EF/AB) awareness to transaction categorization UI and category mapping logic. Add comprehensive tests for supplier-invoice-entries, transaction-entries, and expanded API route coverage. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
63 lines
1.5 KiB
TypeScript
63 lines
1.5 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { eventBus } from '@/lib/events'
|
|
import { ensureInitialized } from '@/lib/init'
|
|
import type { SupplierInvoice } 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 { data: invoice } = await supabase
|
|
.from('supplier_invoices')
|
|
.select('*')
|
|
.eq('id', id)
|
|
.eq('user_id', user.id)
|
|
.single()
|
|
|
|
if (!invoice) {
|
|
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
|
}
|
|
|
|
if (invoice.status !== 'registered') {
|
|
return NextResponse.json(
|
|
{ error: 'Kan bara godkänna registrerade fakturor' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const { data, error } = await supabase
|
|
.from('supplier_invoices')
|
|
.update({ status: 'approved' })
|
|
.eq('id', id)
|
|
.eq('user_id', user.id)
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
try {
|
|
await eventBus.emit({
|
|
type: 'supplier_invoice.approved',
|
|
payload: { supplierInvoice: data as SupplierInvoice, userId: user.id },
|
|
})
|
|
} catch {
|
|
// Non-blocking
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
}
|