* fix(bookkeeping): harden correction account changes * feat(tax): enhance tax deadline generation with new settings and filing methods - Added new company settings: tax_turnover_over_40m, vat_has_eu_trade, vat_filing_method, periodisk_sammanstallning_enabled, and periodisk_sammanstallning_filing_method. - Updated deadline generation logic to accommodate new settings affecting VAT and employer declaration deadlines. - Implemented tests for new functionality, ensuring that completed obligations are preserved and not replaced by new pending rows. - Introduced a cron job to backfill missing tax deadlines for companies with settings but no upcoming deadlines. - Updated API routes for generating tax deadlines and handling cron jobs. - Modified database schema to include new columns for tax filing profiles and constraints for filing methods. * fix(invoices): record credit note reconciliation guard * fix(tax): correct automatic deadline settings * fix(tax): key AGI deadline to VAT taxable base and add storforetag payment deadline The 26th filing day for the skattedeklaration (AGI and VAT together) hinges on one statutory measure, a VAT taxable base above SEK 40 million (SFL 26 kap.), not a separate employer turnover. Drop employer_turnover_over_40m and derive the AGI schedule from vat_registered plus vat_taxable_base_over_40m, so a non-VAT-reporting employer is never shown the 26th when its binding date is the 12th. Also: - add a skatteinbetalning deadline row (12th, 17 January) for storforetag, whose deducted tax and employer contributions are due before the 26th filing date - normalize legally incoherent over-40m flag combinations to the earlier small-company schedule in a follow-up migration - replace hardcoded 27 December dates with the banking-day adjustment - extend the 40m help text to cover the SKV-decided early filing election and the payment-still-on-the-12th rule - document the regeneration race repaired by the daily backfill cron Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(migrations): add AGI and VAT filing logic with employer column removal * feat(settings): implement VAT registration logic and update related flags; enhance deadline handling --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
107 lines
3.4 KiB
TypeScript
107 lines
3.4 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { NextResponse } from 'next/server'
|
|
import { createMockRequest, createQueuedMockSupabase, parseJsonResponse } from '@/tests/helpers'
|
|
|
|
const { supabase, enqueue, reset } = createQueuedMockSupabase()
|
|
const mocks = vi.hoisted(() => ({
|
|
regenerate: vi.fn(),
|
|
}))
|
|
|
|
const requireAuthMock = vi.fn()
|
|
vi.mock('@/lib/auth/require-auth', () => ({
|
|
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
|
|
}))
|
|
|
|
vi.mock('@/lib/company/context', () => ({
|
|
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
}))
|
|
|
|
const requireWriteMock = vi.fn()
|
|
vi.mock('@/lib/auth/require-write', () => ({
|
|
requireWritePermission: (...args: unknown[]) => requireWriteMock(...args),
|
|
}))
|
|
|
|
vi.mock('@/lib/tax/deadline-generator', () => ({
|
|
DEADLINE_SETTINGS_SELECT: 'company_id, entity_type',
|
|
regenerateTaxDeadlinesForUser: mocks.regenerate,
|
|
toDeadlineSettings: vi.fn((settings: Record<string, unknown>) => settings),
|
|
}))
|
|
|
|
import { POST } from '../route'
|
|
|
|
function request(): Request {
|
|
return createMockRequest('/api/tax-deadlines/generate', { method: 'POST' })
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
reset()
|
|
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase, error: null })
|
|
requireWriteMock.mockResolvedValue({ ok: true })
|
|
mocks.regenerate.mockResolvedValue({ created: 12, deleted: 0 })
|
|
})
|
|
|
|
describe('POST /api/tax-deadlines/generate', () => {
|
|
it('returns 401 when unauthenticated', async () => {
|
|
requireAuthMock.mockResolvedValue({
|
|
user: null,
|
|
supabase,
|
|
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
|
|
})
|
|
|
|
const response = await POST(request(), { params: Promise.resolve({}) })
|
|
|
|
expect(response.status).toBe(401)
|
|
expect(mocks.regenerate).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 403 without write permission', async () => {
|
|
requireWriteMock.mockResolvedValue({
|
|
ok: false,
|
|
response: NextResponse.json({ error: 'Forbidden' }, { status: 403 }),
|
|
})
|
|
|
|
const response = await POST(request(), { params: Promise.resolve({}) })
|
|
|
|
expect(response.status).toBe(403)
|
|
expect(mocks.regenerate).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 404 when company settings are missing', async () => {
|
|
enqueue({ data: null, error: { code: 'PGRST116', message: 'No rows returned' } })
|
|
|
|
const response = await POST(request(), { params: Promise.resolve({}) })
|
|
|
|
expect(response.status).toBe(404)
|
|
})
|
|
|
|
it('regenerates the current company deadlines', async () => {
|
|
enqueue({ data: { company_id: 'company-1', entity_type: 'aktiebolag' } })
|
|
|
|
const response = await POST(request(), { params: Promise.resolve({}) })
|
|
const { status, body } = await parseJsonResponse<{
|
|
success: boolean
|
|
created: number
|
|
deleted: number
|
|
}>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(body).toEqual({ success: true, created: 12, deleted: 0 })
|
|
expect(mocks.regenerate).toHaveBeenCalledWith(
|
|
supabase,
|
|
'company-1',
|
|
expect.objectContaining({ entity_type: 'aktiebolag' }),
|
|
)
|
|
})
|
|
|
|
it('returns 500 when generation fails', async () => {
|
|
enqueue({ data: { company_id: 'company-1', entity_type: 'aktiebolag' } })
|
|
mocks.regenerate.mockRejectedValueOnce(new Error('insert failed'))
|
|
|
|
const response = await POST(request(), { params: Promise.resolve({}) })
|
|
|
|
expect(response.status).toBe(500)
|
|
})
|
|
})
|