* feat(bookkeeping): page-size selector + First/Last pagination on verifikationslista Closes #738. The voucher list (verifikationslista) had a hardcoded page size of 20 and only Previous/Next buttons. Adds: - Page-size selector: 20 / 50 / 100 / Alla. Persisted per company in localStorage (same convention as the sort order and FiscalYearSelector) and hydrated in an effect so the first fetch already uses the saved size. "Alla" loads everything in the current scope and hides the pager. - Pagination footer: First / Previous / Next / Last icon buttons, a page indicator, and a "Visar 1–20 av N" result-range label. - Server: clamp limit to [1, 100000] and offset to >=0 so "Alla" sends a bounded large limit (defense in depth, ASVS V1.2.5). Sorting asc/desc on date and voucher already existed in the filter dialog; amount-column sorting is intentionally out of scope (journal_entries has no stored total — the voucher amount is summed client-side from debit lines — so ordering by it needs a schema change). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * refactor(bookkeeping): keep page-size selector reachable + clarify offset clamp Addresses PR review feedback: - Pagination footer now shows whenever a non-default page size (50/100/Alla) is active, not only when count > 20. A user who picks a larger size and then filters the list below 20 rows can still switch the size back. Default-20 users are unchanged — no selector under 21 rows. Empty results stay hidden. - offset clamp reads `rawOffset >= 0` instead of `> 0` (behaviour identical; clearer intent). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
261 lines
8.6 KiB
TypeScript
261 lines
8.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import {
|
|
createMockRequest,
|
|
parseJsonResponse,
|
|
createQueuedMockSupabase,
|
|
makeJournalEntry,
|
|
} from '@/tests/helpers'
|
|
|
|
// Mock dependencies
|
|
const { supabase: mockSupabase, enqueue, reset } = createQueuedMockSupabase()
|
|
vi.mock('@/lib/supabase/server', () => ({
|
|
createClient: () => Promise.resolve(mockSupabase),
|
|
}))
|
|
|
|
vi.mock('@/lib/init', () => ({
|
|
ensureInitialized: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/company/context', () => ({
|
|
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
}))
|
|
|
|
vi.mock('@/lib/auth/require-write', () => ({
|
|
requireWritePermission: vi.fn().mockResolvedValue({ ok: true }),
|
|
}))
|
|
|
|
const mockCreateJournalEntry = vi.fn()
|
|
vi.mock('@/lib/bookkeeping/engine', () => ({
|
|
createJournalEntry: (...args: unknown[]) => mockCreateJournalEntry(...args),
|
|
}))
|
|
|
|
import { GET, POST } from '../route'
|
|
|
|
describe('GET /api/bookkeeping/journal-entries', () => {
|
|
const mockUser = { id: 'user-1', email: 'test@test.se' }
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
reset()
|
|
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
|
|
})
|
|
|
|
it('returns 401 when not authenticated', async () => {
|
|
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null } })
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries')
|
|
const response = await GET(request)
|
|
const { status, body } = await parseJsonResponse(response)
|
|
|
|
expect(status).toBe(401)
|
|
expect(body).toEqual({ error: 'Unauthorized' })
|
|
})
|
|
|
|
it('returns entries list', async () => {
|
|
const entries = [makeJournalEntry(), makeJournalEntry()]
|
|
enqueue({ data: entries, error: null, count: 2 })
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries')
|
|
const response = await GET(request)
|
|
const { status, body } = await parseJsonResponse<{ data: unknown[]; count: number }>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(body.data).toEqual(entries)
|
|
expect(body.count).toBe(2)
|
|
})
|
|
|
|
it('passes filters to query', async () => {
|
|
enqueue({ data: [], error: null, count: 0 })
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries', {
|
|
searchParams: {
|
|
period_id: 'period-1',
|
|
status: 'posted',
|
|
date_from: '2024-01-01',
|
|
date_to: '2024-12-31',
|
|
limit: '10',
|
|
offset: '5',
|
|
// Strict period filtering — exercises the PostgREST path, not the RPC.
|
|
include_related: 'false',
|
|
},
|
|
})
|
|
const response = await GET(request)
|
|
const { status } = await parseJsonResponse(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(mockSupabase.from).toHaveBeenCalledWith('journal_entries')
|
|
})
|
|
|
|
it('uses RPC with include_related when period_id is set', async () => {
|
|
const rpcRows = [
|
|
{
|
|
entry: { ...makeJournalEntry({ id: 'je-1' }), out_of_period: false },
|
|
total_count: 2,
|
|
},
|
|
{
|
|
entry: { ...makeJournalEntry({ id: 'je-2' }), out_of_period: true },
|
|
total_count: 2,
|
|
},
|
|
]
|
|
enqueue({ data: rpcRows, error: null })
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries', {
|
|
searchParams: { period_id: 'period-1' },
|
|
})
|
|
const response = await GET(request)
|
|
const { status, body } = await parseJsonResponse<{
|
|
data: Array<{ id: string; out_of_period?: boolean }>
|
|
count: number
|
|
}>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(mockSupabase.rpc).toHaveBeenCalledWith(
|
|
'list_fiscal_period_entries_with_related',
|
|
expect.objectContaining({
|
|
p_company_id: 'company-1',
|
|
p_period_id: 'period-1',
|
|
p_include_related: true,
|
|
})
|
|
)
|
|
expect(body.data).toHaveLength(2)
|
|
expect(body.data[1].out_of_period).toBe(true)
|
|
expect(body.count).toBe(2)
|
|
})
|
|
|
|
it('forwards explicit ?status=cancelled to the RPC', async () => {
|
|
enqueue({ data: [], error: null })
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries', {
|
|
searchParams: { period_id: 'period-1', status: 'cancelled' },
|
|
})
|
|
await GET(request)
|
|
|
|
// The RPC itself hides cancelled entries unless p_status='cancelled' is
|
|
// passed explicitly (see migration 20260428153500). The behavior of the
|
|
// hide-by-default logic lives in SQL and is covered by pg-real tests.
|
|
expect(mockSupabase.rpc).toHaveBeenCalledWith(
|
|
'list_fiscal_period_entries_with_related',
|
|
expect.objectContaining({ p_status: 'cancelled' })
|
|
)
|
|
})
|
|
|
|
it('uses the direct query path (not the RPC) when a search term is set', async () => {
|
|
enqueue({ data: [], error: null, count: 0 })
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries', {
|
|
searchParams: { period_id: 'period-1', search: 'luftfyllning' },
|
|
})
|
|
const response = await GET(request)
|
|
const { status } = await parseJsonResponse(response)
|
|
|
|
expect(status).toBe(200)
|
|
// Free-text search needs an ILIKE the include_related RPC can't express, so
|
|
// the route must fall through to the direct PostgREST query.
|
|
expect(mockSupabase.from).toHaveBeenCalledWith('journal_entries')
|
|
expect(mockSupabase.rpc).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('accepts a large limit (the "Alla" page size) and a negative offset without erroring', async () => {
|
|
enqueue({ data: [], error: null, count: 0 })
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries', {
|
|
// 'Alla' sends a large limit; the route clamps it to MAX_LIMIT. A negative
|
|
// offset is floored to 0. Both are bounded server-side (ASVS V1.2.5).
|
|
searchParams: { limit: '999999', offset: '-5', include_related: 'false' },
|
|
})
|
|
const response = await GET(request)
|
|
const { status } = await parseJsonResponse(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(mockSupabase.from).toHaveBeenCalledWith('journal_entries')
|
|
})
|
|
|
|
it('returns 500 on database error', async () => {
|
|
enqueue({ data: null, error: { message: 'DB error' } })
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries')
|
|
const response = await GET(request)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(response)
|
|
|
|
expect(status).toBe(500)
|
|
expect(body.error).toBe('DB error')
|
|
})
|
|
})
|
|
|
|
const VALID_UUID = '550e8400-e29b-41d4-a716-446655440000'
|
|
|
|
describe('POST /api/bookkeeping/journal-entries', () => {
|
|
const mockUser = { id: 'user-1', email: 'test@test.se' }
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
reset()
|
|
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
|
|
})
|
|
|
|
it('returns 401 when not authenticated', async () => {
|
|
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null } })
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries', {
|
|
method: 'POST',
|
|
body: {},
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse(response)
|
|
|
|
expect(status).toBe(401)
|
|
expect(body).toEqual({ error: 'Unauthorized' })
|
|
})
|
|
|
|
it('creates journal entry and returns it', async () => {
|
|
const entry = makeJournalEntry()
|
|
mockCreateJournalEntry.mockResolvedValue(entry)
|
|
|
|
const input = {
|
|
fiscal_period_id: VALID_UUID,
|
|
entry_date: '2024-06-15',
|
|
description: 'Test entry',
|
|
source_type: 'manual',
|
|
lines: [
|
|
{ account_number: '1930', debit_amount: 1000, credit_amount: 0 },
|
|
{ account_number: '3001', debit_amount: 0, credit_amount: 1000 },
|
|
],
|
|
}
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries', {
|
|
method: 'POST',
|
|
body: input,
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ data: unknown }>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(body.data).toEqual(entry)
|
|
expect(mockCreateJournalEntry).toHaveBeenCalledWith(expect.anything(), 'company-1', 'user-1', input)
|
|
})
|
|
|
|
it('returns 400 when engine throws', async () => {
|
|
mockCreateJournalEntry.mockRejectedValue(new Error('Unbalanced entry'))
|
|
|
|
const request = createMockRequest('/api/bookkeeping/journal-entries', {
|
|
method: 'POST',
|
|
body: {
|
|
fiscal_period_id: VALID_UUID,
|
|
entry_date: '2024-06-15',
|
|
description: 'Bad entry',
|
|
source_type: 'manual',
|
|
lines: [
|
|
{ account_number: '1930', debit_amount: 1000, credit_amount: 0 },
|
|
{ account_number: '3001', debit_amount: 0, credit_amount: 500 },
|
|
],
|
|
},
|
|
})
|
|
const response = await POST(request)
|
|
const { status, body } = await parseJsonResponse<{ error: string }>(response)
|
|
|
|
expect(status).toBe(400)
|
|
expect(body.error).toBe('Unbalanced entry')
|
|
})
|
|
})
|