Files
accounted/app/api/tax-deadlines/cron/__tests__/route.test.ts
T
MattssonandClaude Fable 5 a5e37d3510 Fix/build (#1041)
* 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>
2026-07-17 00:52:57 +02:00

97 lines
2.9 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { NextResponse } from 'next/server'
const mocks = vi.hoisted(() => ({
createClient: vi.fn(() => ({ kind: 'service-client' })),
annual: vi.fn(),
backfill: vi.fn(),
}))
vi.mock('@supabase/supabase-js', () => ({
createClient: mocks.createClient,
}))
vi.mock('@/lib/auth/cron', () => ({
verifyCronSecret: vi.fn(() => null),
}))
vi.mock('@/lib/tax/deadline-generator', () => ({
generateNewYearDeadlines: mocks.annual,
backfillMissingTaxDeadlines: mocks.backfill,
}))
import { verifyCronSecret } from '@/lib/auth/cron'
import { GET } from '../route'
const originalUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const originalServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY
function cronRequest(): Request {
return new Request('http://localhost:3000/api/tax-deadlines/cron')
}
beforeEach(() => {
vi.clearAllMocks()
vi.useFakeTimers()
vi.setSystemTime(new Date('2026-07-16T00:00:00.000Z'))
process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://example.supabase.co'
process.env.SUPABASE_SERVICE_ROLE_KEY = 'service-key'
mocks.annual.mockResolvedValue({ usersProcessed: 0, totalCreated: 0 })
mocks.backfill.mockResolvedValue({
companiesScanned: 10,
companiesRepaired: 2,
totalCreated: 24,
})
})
afterEach(() => {
vi.useRealTimers()
process.env.NEXT_PUBLIC_SUPABASE_URL = originalUrl
process.env.SUPABASE_SERVICE_ROLE_KEY = originalServiceKey
})
describe('GET /api/tax-deadlines/cron', () => {
it('repairs companies with missing deadlines on the daily run', async () => {
const response = await GET(cronRequest())
expect(response.status).toBe(200)
await expect(response.json()).resolves.toMatchObject({
success: true,
isAnnualRun: false,
companiesScanned: 10,
companiesRepaired: 2,
totalCreated: 24,
})
expect(mocks.backfill).toHaveBeenCalledOnce()
expect(mocks.annual).not.toHaveBeenCalled()
})
it('extends every company horizon before running recovery on January 2', async () => {
vi.setSystemTime(new Date('2027-01-02T00:00:00.000Z'))
mocks.annual.mockResolvedValue({ usersProcessed: 8, totalCreated: 80 })
const response = await GET(cronRequest())
const body = await response.json()
expect(body).toMatchObject({
isAnnualRun: true,
usersProcessed: 8,
totalCreated: 104,
})
expect(mocks.annual).toHaveBeenCalledOnce()
expect(mocks.backfill).toHaveBeenCalledOnce()
})
it('returns 401 without creating a database client when cron auth fails', async () => {
vi.mocked(verifyCronSecret).mockReturnValueOnce(
NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
)
const response = await GET(cronRequest())
expect(response.status).toBe(401)
expect(mocks.createClient).not.toHaveBeenCalled()
expect(mocks.backfill).not.toHaveBeenCalled()
})
})