diff --git a/app/api/calendar/feed/[token]/route.ts b/app/api/calendar/feed/[token]/route.ts index d703277d..b2198f0b 100644 --- a/app/api/calendar/feed/[token]/route.ts +++ b/app/api/calendar/feed/[token]/route.ts @@ -97,19 +97,18 @@ export async function GET( const startStr = startDate.toISOString().split('T')[0] const endStr = endDate.toISOString().split('T')[0] - // Fetch relevant data based on feed options + // Fetch relevant data based on feed options. Deadlines are always + // fetched: include_tax_deadlines only hides SYSTEM rows (the generator + // filters by source), while user-created deadlines always appear. const [deadlinesResult, invoicesResult] = await Promise.all([ - // Deadlines - feed.include_tax_deadlines - ? supabase - .from('deadlines') - .select('*') - .eq('company_id', feed.company_id) - .is('dismissed_at', null) - .gte('due_date', startStr) - .lte('due_date', endStr) - .order('due_date') - : { data: [] }, + supabase + .from('deadlines') + .select('*') + .eq('company_id', feed.company_id) + .is('dismissed_at', null) + .gte('due_date', startStr) + .lte('due_date', endStr) + .order('due_date'), // Invoices feed.include_invoices diff --git a/app/api/sandbox/seed/route.ts b/app/api/sandbox/seed/route.ts index 333a6755..b0a93e6f 100644 --- a/app/api/sandbox/seed/route.ts +++ b/app/api/sandbox/seed/route.ts @@ -637,11 +637,14 @@ export async function POST(request: Request) { { user_id: userId, company_id: companyId, - title: 'Momsdeklaration Q1 2026', + title: `Momsdeklaration Q1 ${currentYear}`, due_date: toDateStr(momsDeadline), deadline_type: 'tax', priority: 'important', - tax_deadline_type: 'moms', + // Current generator types: the bare 'moms'/'inkomstdeklaration' + // types were retired and seeding them recreates legacy rows the + // cleanup migration removed. + tax_deadline_type: 'moms_quarterly', tax_period: `${currentYear}-Q1`, source: 'system', status: 'upcoming', @@ -650,11 +653,12 @@ export async function POST(request: Request) { { user_id: userId, company_id: companyId, - title: 'Inkomstdeklaration 2025', + title: `Inkomstdeklaration ${currentYear - 1}`, due_date: `${currentYear}-05-02`, deadline_type: 'tax', priority: 'critical', - tax_deadline_type: 'inkomstdeklaration', + // Sandbox companies are enskild firma (see p_entity_type above). + tax_deadline_type: 'inkomstdeklaration_ef', tax_period: `${currentYear - 1}`, source: 'system', status: 'upcoming', diff --git a/lib/calendar/__tests__/ics-generator.test.ts b/lib/calendar/__tests__/ics-generator.test.ts new file mode 100644 index 00000000..94537b57 --- /dev/null +++ b/lib/calendar/__tests__/ics-generator.test.ts @@ -0,0 +1,70 @@ +import { describe, it, expect } from 'vitest' +import { generateCalendarFeed } from '../ics-generator' +import type { Deadline } from '@/types' + +function makeDeadline(overrides: Partial): Deadline { + return { + id: 'd-1', + user_id: 'user-1', + company_id: 'company-1', + title: 'Deadline', + due_date: '2027-03-12', + due_time: null, + deadline_type: 'other', + priority: 'normal', + is_completed: false, + completed_at: null, + customer_id: null, + is_auto_generated: false, + notes: null, + created_at: '2026-01-01T00:00:00Z', + updated_at: '2026-01-01T00:00:00Z', + tax_deadline_type: null, + tax_period: null, + source: 'user', + reminder_offsets: null, + status: 'upcoming', + status_changed_at: '2026-01-01T00:00:00Z', + dismissed_at: null, + linked_report_type: null, + linked_report_period: null, + ...overrides, + } as Deadline +} + +const SYSTEM_DEADLINE = makeDeadline({ + id: 'sys-1', + title: 'Momsdeklaration Q1 2027', + deadline_type: 'tax', + tax_deadline_type: 'moms_quarterly', + tax_period: '2027-Q1', + source: 'system', +}) + +const USER_DEADLINE = makeDeadline({ + id: 'usr-1', + title: 'Skicka avtal till kunden', + source: 'user', +}) + +describe('generateCalendarFeed deadline filtering', () => { + it('includes system and user deadlines when tax deadlines are on', async () => { + const ics = await generateCalendarFeed( + { deadlines: [SYSTEM_DEADLINE, USER_DEADLINE], invoices: [] }, + { includeTaxDeadlines: true, includeInvoices: false }, + ) + expect(ics).toContain('Momsdeklaration Q1 2027') + expect(ics).toContain('Skicka avtal till kunden') + }) + + it('keeps user-created deadlines when tax deadlines are off', async () => { + // The flag governs system-generated rows only; hiding the user's own + // manual deadlines with it was a bug. + const ics = await generateCalendarFeed( + { deadlines: [SYSTEM_DEADLINE, USER_DEADLINE], invoices: [] }, + { includeTaxDeadlines: false, includeInvoices: false }, + ) + expect(ics).not.toContain('Momsdeklaration Q1 2027') + expect(ics).toContain('Skicka avtal till kunden') + }) +}) diff --git a/lib/calendar/ics-generator.ts b/lib/calendar/ics-generator.ts index 2448a82c..d651d520 100644 --- a/lib/calendar/ics-generator.ts +++ b/lib/calendar/ics-generator.ts @@ -144,14 +144,19 @@ export function generateCalendarFeed( ): Promise { const events: EventAttributes[] = [] - if (options.includeTaxDeadlines) { - const taxDeadlines = data.deadlines.filter((d) => d.deadline_type === 'tax') - events.push(...generateDeadlineEvents(taxDeadlines)) + // The include_tax_deadlines flag governs SYSTEM-generated deadlines only: + // the user's own manual deadlines always appear in the feed. The previous + // nesting hid every deadline, including user-created ones, when the flag + // was off. + const visibleDeadlines = options.includeTaxDeadlines + ? data.deadlines + : data.deadlines.filter((d) => d.source !== 'system') - // Include non-tax deadlines too - const otherDeadlines = data.deadlines.filter((d) => d.deadline_type !== 'tax') - events.push(...generateDeadlineEvents(otherDeadlines)) - } + const taxDeadlines = visibleDeadlines.filter((d) => d.deadline_type === 'tax') + events.push(...generateDeadlineEvents(taxDeadlines)) + + const otherDeadlines = visibleDeadlines.filter((d) => d.deadline_type !== 'tax') + events.push(...generateDeadlineEvents(otherDeadlines)) if (options.includeInvoices) { events.push(...generateInvoiceEvents(data.invoices)) diff --git a/supabase/migrations/20260717153000_cleanup_legacy_deadline_types.sql b/supabase/migrations/20260717153000_cleanup_legacy_deadline_types.sql new file mode 100644 index 00000000..d7ab7521 --- /dev/null +++ b/supabase/migrations/20260717153000_cleanup_legacy_deadline_types.sql @@ -0,0 +1,12 @@ +-- Clean up rows of retired tax deadline types (issue #1028). +-- +-- The bare 'moms' and 'inkomstdeklaration' types were replaced by +-- moms_monthly/quarterly/yearly and inkomstdeklaration_ef/_ab long ago, but +-- their rows lingered (one per company from early seeding), polluting +-- history views and per-type analytics. Completed rows are kept as filing +-- history. The sandbox seed route stops inserting these types in the same +-- change, so they do not come back. +DELETE FROM public.deadlines +WHERE source = 'system' + AND tax_deadline_type IN ('moms', 'inkomstdeklaration') + AND is_completed = false;