* fix(reminder-processor): filter out credited invoices in overdue reminders * feat: implement linking of transactions to journal entries - Added POST endpoint for linking a bank transaction to an existing journal entry without creating new bookkeeping. - Implemented validation for required fields and error handling for various scenarios (e.g., missing journal_entry_id, transaction already linked, journal entry not found). - Created tests for the new endpoint to cover various cases including successful linking, error responses, and invoice handling. - Introduced duplicate payment detection logic to prevent double-booking of bank receipts. - Added a new component for correction affordance in the UI to facilitate user corrections on journal entries. * feat(invoice-matching): enhance force matching with expected journal entry validation
112 lines
3.1 KiB
TypeScript
112 lines
3.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
|
|
const chainCalls: Array<{ method: string; args: unknown[] }> = []
|
|
|
|
vi.mock('@supabase/ssr', () => {
|
|
const buildChain = (): unknown =>
|
|
new Proxy(
|
|
{},
|
|
{
|
|
get(_t, prop) {
|
|
if (prop === 'then') {
|
|
return (resolve: (v: unknown) => void) =>
|
|
resolve({ data: [], error: null, count: null })
|
|
}
|
|
return (...args: unknown[]) => {
|
|
chainCalls.push({ method: String(prop), args })
|
|
return buildChain()
|
|
}
|
|
},
|
|
},
|
|
)
|
|
|
|
return {
|
|
createServerClient: vi.fn(() => ({
|
|
from: vi.fn(() => buildChain()),
|
|
rpc: vi.fn(() => buildChain()),
|
|
})),
|
|
}
|
|
})
|
|
|
|
vi.mock('@/lib/email/service', () => ({
|
|
getEmailService: () => ({
|
|
sendEmail: vi.fn().mockResolvedValue({ success: true }),
|
|
}),
|
|
}))
|
|
|
|
import {
|
|
processOverdueReminders,
|
|
determineReminderLevel,
|
|
calculateDaysOverdue,
|
|
} from '../reminder-processor'
|
|
|
|
describe('determineReminderLevel', () => {
|
|
it('returns null below the level-1 threshold', () => {
|
|
expect(determineReminderLevel(10, [])).toBeNull()
|
|
})
|
|
|
|
it('returns 1 at 15 days overdue', () => {
|
|
expect(determineReminderLevel(15, [])).toBe(1)
|
|
})
|
|
|
|
it('returns 2 at 30 days when level 1 already sent', () => {
|
|
expect(determineReminderLevel(30, [1])).toBe(2)
|
|
})
|
|
|
|
it('returns 3 at 45 days when 1 and 2 already sent', () => {
|
|
expect(determineReminderLevel(45, [1, 2])).toBe(3)
|
|
})
|
|
|
|
it('returns null when all levels have been sent', () => {
|
|
expect(determineReminderLevel(60, [1, 2, 3])).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('calculateDaysOverdue', () => {
|
|
it('returns a positive number for a past due date', () => {
|
|
const tenDaysAgo = new Date()
|
|
tenDaysAgo.setDate(tenDaysAgo.getDate() - 10)
|
|
const days = calculateDaysOverdue(tenDaysAgo.toISOString().split('T')[0])
|
|
expect(days).toBeGreaterThanOrEqual(9)
|
|
expect(days).toBeLessThanOrEqual(10)
|
|
})
|
|
})
|
|
|
|
describe('processOverdueReminders — credit-note filter', () => {
|
|
beforeEach(() => {
|
|
chainCalls.length = 0
|
|
})
|
|
|
|
it('excludes credit notes via .is("credited_invoice_id", null)', async () => {
|
|
await processOverdueReminders()
|
|
|
|
const isCall = chainCalls.find(
|
|
(c) => c.method === 'is' && c.args[0] === 'credited_invoice_id',
|
|
)
|
|
|
|
expect(
|
|
isCall,
|
|
'overdue-invoice query must filter out credit notes — credit notes have a negative total and must never trigger a payment reminder (e.g. KR-F2026002)',
|
|
).toBeDefined()
|
|
expect(isCall?.args[1]).toBeNull()
|
|
})
|
|
|
|
it('combines the credit-note filter with status=sent and due_date cutoff', async () => {
|
|
await processOverdueReminders()
|
|
|
|
const eqStatus = chainCalls.find(
|
|
(c) => c.method === 'eq' && c.args[0] === 'status',
|
|
)
|
|
const isCreditedNull = chainCalls.find(
|
|
(c) => c.method === 'is' && c.args[0] === 'credited_invoice_id',
|
|
)
|
|
const lteDueDate = chainCalls.find(
|
|
(c) => c.method === 'lte' && c.args[0] === 'due_date',
|
|
)
|
|
|
|
expect(eqStatus?.args[1]).toBe('sent')
|
|
expect(isCreditedNull?.args[1]).toBeNull()
|
|
expect(lteDueDate).toBeDefined()
|
|
})
|
|
})
|