Feat/change fiscal year (#300)

* feat: enhance journal entry handling with follow-up entries and related RPC

* fix: improve validation for journal entry lines to ensure proper submission criteria

* fix: enhance OAuth error handling and user feedback in Arcim migration process

* fix: add OAuth error translation for user-friendly feedback in Fortnox integration

* feat: add fiscal period editor and related API for entry count

* feat: enforce calendar year for individual firms and improve fiscal period validation
This commit is contained in:
Mattsson
2026-04-21 14:44:03 +02:00
committed by GitHub
parent b5df2fb292
commit 08991218ee
6 changed files with 492 additions and 3 deletions
@@ -0,0 +1,73 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
vi.mock('@/lib/supabase/server', () => ({
createClient: vi.fn(),
}))
vi.mock('@/lib/company/context', () => ({
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
}))
import { createClient } from '@/lib/supabase/server'
import { createQueuedMockSupabase } from '@/tests/helpers'
import { GET } from '../route'
function mkReq() {
return new Request('http://localhost/api/bookkeeping/fiscal-periods/period-1/entry-count')
}
function mkParams(id = 'period-1') {
return { params: Promise.resolve({ id }) }
}
describe('GET /api/bookkeeping/fiscal-periods/[id]/entry-count', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('returns 401 when unauthenticated', async () => {
const { supabase } = createQueuedMockSupabase()
supabase.auth.getUser.mockResolvedValue({ data: { user: null } })
vi.mocked(createClient).mockResolvedValue(supabase as never)
const res = await GET(mkReq(), mkParams())
expect(res.status).toBe(401)
})
it('returns 404 when period does not belong to company', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
supabase.auth.getUser.mockResolvedValue({ data: { user: { id: 'user-1' } } })
enqueue({ data: null, error: null })
vi.mocked(createClient).mockResolvedValue(supabase as never)
const res = await GET(mkReq(), mkParams())
expect(res.status).toBe(404)
})
it('returns posted count on happy path', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
supabase.auth.getUser.mockResolvedValue({ data: { user: { id: 'user-1' } } })
// fiscal_periods lookup
enqueue({ data: { id: 'period-1' }, error: null })
// journal_entries count
enqueue({ count: 3, error: null })
vi.mocked(createClient).mockResolvedValue(supabase as never)
const res = await GET(mkReq(), mkParams())
expect(res.status).toBe(200)
const body = await res.json()
expect(body.data.posted_count).toBe(3)
})
it('returns 0 when no posted entries exist', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
supabase.auth.getUser.mockResolvedValue({ data: { user: { id: 'user-1' } } })
enqueue({ data: { id: 'period-1' }, error: null })
enqueue({ count: 0, error: null })
vi.mocked(createClient).mockResolvedValue(supabase as never)
const res = await GET(mkReq(), mkParams())
expect(res.status).toBe(200)
const body = await res.json()
expect(body.data.posted_count).toBe(0)
})
})
@@ -0,0 +1,42 @@
import { createClient } from '@/lib/supabase/server'
import { NextResponse } from 'next/server'
import { requireCompanyId } from '@/lib/company/context'
export async function GET(
_request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params
const supabase = await createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const companyId = await requireCompanyId(supabase, user.id)
const { data: period, error: fetchError } = await supabase
.from('fiscal_periods')
.select('id')
.eq('id', id)
.eq('company_id', companyId)
.maybeSingle()
if (fetchError || !period) {
return NextResponse.json({ error: 'Räkenskapsår hittades inte' }, { status: 404 })
}
const { count, error: countError } = await supabase
.from('journal_entries')
.select('id', { count: 'exact', head: true })
.eq('company_id', companyId)
.eq('fiscal_period_id', id)
.in('status', ['posted', 'reversed'])
if (countError) {
return NextResponse.json({ error: countError.message }, { status: 500 })
}
return NextResponse.json({ data: { posted_count: count ?? 0 } })
}
@@ -1,7 +1,7 @@
import { createClient } from '@/lib/supabase/server'
import { NextResponse } from 'next/server'
import { validateBody } from '@/lib/api/validate'
import { validatePeriodDuration } from '@/lib/bookkeeping/validate-period-duration'
import { validatePeriodDuration, parseDateParts } from '@/lib/bookkeeping/validate-period-duration'
import { requireCompanyId } from '@/lib/company/context'
import { requireWritePermission } from '@/lib/auth/require-write'
import { z } from 'zod'
@@ -72,6 +72,24 @@ export async function PATCH(
const newStart = body.period_start || period.period_start
const newEnd = body.period_end || period.period_end
// Enskild firma must use calendar year per BFL 3 kap.
const { data: companyRow } = await supabase
.from('companies')
.select('entity_type')
.eq('id', companyId)
.single()
if (companyRow?.entity_type === 'enskild_firma') {
const s = parseDateParts(newStart)
const e = parseDateParts(newEnd)
if (s.month !== 1 || s.day !== 1 || e.month !== 12 || e.day !== 31) {
return NextResponse.json(
{ error: 'Enskild firma måste använda kalenderår (1 januari – 31 december) enligt BFL 3 kap.' },
{ status: 400 }
)
}
}
// First period for this company may start on any day (BFL 3 kap.)
const { count: earlierCount } = await supabase
.from('fiscal_periods')