Added logger and tests

This commit is contained in:
Emil
2026-02-24 10:02:13 +01:00
parent cfc4cb6849
commit 9b92883d30
16 changed files with 104 additions and 44 deletions
@@ -114,7 +114,8 @@ describe('Supplier Invoice Core Handler', () => {
})
expect(consoleSpy).toHaveBeenCalledWith(
'[supplier-invoice-handler] Failed to create registration journal entry:',
'[supplier-invoice-handler]',
'Failed to create registration journal entry:',
expect.any(Error)
)
@@ -2,8 +2,11 @@ import { eventBus } from '@/lib/events/bus'
import type { EventPayload } from '@/lib/events/types'
import { createClient } from '@/lib/supabase/server'
import { createSupplierInvoiceRegistrationEntry } from '@/lib/bookkeeping/supplier-invoice-entries'
import { createLogger } from '@/lib/logger'
import type { SupplierInvoiceItem } from '@/types'
const log = createLogger('supplier-invoice-handler')
/**
* Core event handler: creates a registration journal entry when a supplier
* invoice is confirmed (accrual method only).
@@ -36,7 +39,7 @@ async function handleSupplierInvoiceConfirmed(
.order('sort_order')
if (itemsError || !items || items.length === 0) {
console.error('[supplier-invoice-handler] Failed to fetch invoice items:', itemsError)
log.error('Failed to fetch invoice items:', itemsError)
return
}
@@ -64,7 +67,7 @@ async function handleSupplierInvoiceConfirmed(
.eq('id', supplierInvoice.id)
}
} catch (err) {
console.error('[supplier-invoice-handler] Failed to create registration journal entry:', err)
log.error('Failed to create registration journal entry:', err)
}
}
+7 -4
View File
@@ -1,6 +1,7 @@
import { createJournalEntry, findFiscalPeriod } from './engine'
import { generateSalesVatLines, generateReverseChargeLines } from './vat-entries'
import { getVatTreatmentForRate } from '@/lib/invoices/vat-rules'
import { createLogger } from '@/lib/logger'
import type {
CreateJournalEntryInput,
CreateJournalEntryLineInput,
@@ -11,6 +12,8 @@ import type {
VatTreatment,
} from '@/types'
const log = createLogger('invoice-entries')
/**
* Group invoice items by VAT rate and generate per-rate revenue + VAT lines.
* Returns credit lines only (revenue + VAT). The caller adds the debit side.
@@ -115,7 +118,7 @@ export async function createInvoiceJournalEntry(
): Promise<JournalEntry | null> {
const fiscalPeriodId = await findFiscalPeriod(userId, invoice.invoice_date)
if (!fiscalPeriodId) {
console.warn('No open fiscal period found for invoice date:', invoice.invoice_date)
log.warn('No open fiscal period found for invoice date:', invoice.invoice_date)
return null
}
@@ -180,7 +183,7 @@ export async function createInvoicePaymentJournalEntry(
): Promise<JournalEntry | null> {
const fiscalPeriodId = await findFiscalPeriod(userId, paymentDate)
if (!fiscalPeriodId) {
console.warn('No open fiscal period found for payment date:', paymentDate)
log.warn('No open fiscal period found for payment date:', paymentDate)
return null
}
@@ -226,7 +229,7 @@ export async function createCreditNoteJournalEntry(
): Promise<JournalEntry | null> {
const fiscalPeriodId = await findFiscalPeriod(userId, creditNote.invoice_date)
if (!fiscalPeriodId) {
console.warn('No open fiscal period found for credit note date:', creditNote.invoice_date)
log.warn('No open fiscal period found for credit note date:', creditNote.invoice_date)
return null
}
@@ -305,7 +308,7 @@ export async function createInvoiceCashEntry(
): Promise<JournalEntry | null> {
const fiscalPeriodId = await findFiscalPeriod(userId, paymentDate)
if (!fiscalPeriodId) {
console.warn('No open fiscal period found for payment date:', paymentDate)
log.warn('No open fiscal period found for payment date:', paymentDate)
return null
}
+1 -1
View File
@@ -224,6 +224,6 @@ export async function saveUserMappingRule(
})
if (error) {
console.error('Failed to save user mapping rule:', error)
// Silently fail — saving learned rules is non-critical
}
}
+7 -4
View File
@@ -1,5 +1,6 @@
import { createJournalEntry, findFiscalPeriod } from './engine'
import { generateReverseChargeLines } from './vat-entries'
import { createLogger } from '@/lib/logger'
import type {
CreateJournalEntryInput,
CreateJournalEntryLineInput,
@@ -8,6 +9,8 @@ import type {
SupplierInvoiceItem,
} from '@/types'
const log = createLogger('supplier-invoice-entries')
/**
* Create journal entry when a supplier invoice is registered (accrual method)
*
@@ -30,7 +33,7 @@ export async function createSupplierInvoiceRegistrationEntry(
): Promise<JournalEntry | null> {
const fiscalPeriodId = await findFiscalPeriod(userId, invoice.invoice_date)
if (!fiscalPeriodId) {
console.warn('No open fiscal period found for invoice date:', invoice.invoice_date)
log.warn('No open fiscal period found for invoice date:', invoice.invoice_date)
return null
}
@@ -112,7 +115,7 @@ export async function createSupplierInvoicePaymentEntry(
): Promise<JournalEntry | null> {
const fiscalPeriodId = await findFiscalPeriod(userId, paymentDate)
if (!fiscalPeriodId) {
console.warn('No open fiscal period found for payment date:', paymentDate)
log.warn('No open fiscal period found for payment date:', paymentDate)
return null
}
@@ -204,7 +207,7 @@ export async function createSupplierInvoiceCashEntry(
): Promise<JournalEntry | null> {
const fiscalPeriodId = await findFiscalPeriod(userId, paymentDate)
if (!fiscalPeriodId) {
console.warn('No open fiscal period found for payment date:', paymentDate)
log.warn('No open fiscal period found for payment date:', paymentDate)
return null
}
@@ -278,7 +281,7 @@ export async function createSupplierCreditNoteEntry(
): Promise<JournalEntry | null> {
const fiscalPeriodId = await findFiscalPeriod(userId, creditNote.invoice_date)
if (!fiscalPeriodId) {
console.warn('No open fiscal period found for credit note date:', creditNote.invoice_date)
log.warn('No open fiscal period found for credit note date:', creditNote.invoice_date)
return null
}
+4 -1
View File
@@ -1,5 +1,6 @@
import { createJournalEntry, findFiscalPeriod } from './engine'
import { generateInputVatLine, generateReverseChargeLines, extractNetAmount, extractVatAmount } from './vat-entries'
import { createLogger } from '@/lib/logger'
import type {
CreateJournalEntryInput,
CreateJournalEntryLineInput,
@@ -8,6 +9,8 @@ import type {
Transaction,
} from '@/types'
const log = createLogger('transaction-entries')
/**
* Create a journal entry from a bank transaction using mapping engine result
*
@@ -47,7 +50,7 @@ export async function createTransactionJournalEntry(
const fiscalPeriodId = await findFiscalPeriod(userId, transaction.date)
if (!fiscalPeriodId) {
console.warn('No open fiscal period found for transaction date:', transaction.date)
log.warn('No open fiscal period found for transaction date:', transaction.date)
return null
}