@@ -424,7 +425,7 @@ export default function DashboardNav({ companyName, entityType, uncategorizedTra
}}
>
- Logga ut
+ {isSandbox ? 'Avsluta sandbox' : 'Logga ut'}
diff --git a/components/dashboard/SandboxBanner.tsx b/components/dashboard/SandboxBanner.tsx
new file mode 100644
index 00000000..431d1a1c
--- /dev/null
+++ b/components/dashboard/SandboxBanner.tsx
@@ -0,0 +1,40 @@
+'use client'
+
+import { useState } from 'react'
+import { useRouter } from 'next/navigation'
+import { X } from 'lucide-react'
+import { createClient } from '@/lib/supabase/client'
+
+export function SandboxBanner() {
+ const [dismissed, setDismissed] = useState(false)
+ const router = useRouter()
+
+ if (dismissed) return null
+
+ async function handleCreateAccount() {
+ const supabase = createClient()
+ await supabase.auth.signOut()
+ router.push('/register')
+ }
+
+ return (
+
+
+ Sandlådemiljö — dina data raderas automatiskt efter 24 timmar
+
+
+
+
+ )
+}
diff --git a/lib/supabase/middleware.ts b/lib/supabase/middleware.ts
index 853ee693..77cf7197 100644
--- a/lib/supabase/middleware.ts
+++ b/lib/supabase/middleware.ts
@@ -54,7 +54,8 @@ export async function updateSession(request: NextRequest) {
pathname.startsWith('/login') ||
pathname.startsWith('/register') ||
pathname.startsWith('/auth') ||
- pathname.startsWith('/reset-password')
+ pathname.startsWith('/reset-password') ||
+ pathname.startsWith('/sandbox')
) {
// If user is logged in and trying to access auth pages, redirect to dashboard or onboarding
if (user) {
diff --git a/supabase/migrations/20240101000018_audit_triggers.sql b/supabase/migrations/20240101000018_audit_triggers.sql
index aedde3c1..b5a333c1 100644
--- a/supabase/migrations/20240101000018_audit_triggers.sql
+++ b/supabase/migrations/20240101000018_audit_triggers.sql
@@ -110,7 +110,4 @@ CREATE TRIGGER audit_company_settings
AFTER INSERT OR UPDATE OR DELETE ON public.company_settings
FOR EACH ROW EXECUTE FUNCTION public.write_audit_log();
--- tax_codes
-CREATE TRIGGER audit_tax_codes
- AFTER INSERT OR UPDATE OR DELETE ON public.tax_codes
- FOR EACH ROW EXECUTE FUNCTION public.write_audit_log();
+-- tax_codes trigger removed: table never created (migration 012 is a placeholder)
diff --git a/supabase/migrations/20260304191528_set_search_path_on_functions.sql b/supabase/migrations/20260304191528_set_search_path_on_functions.sql
index 7f5ce249..a4669975 100644
--- a/supabase/migrations/20260304191528_set_search_path_on_functions.sql
+++ b/supabase/migrations/20260304191528_set_search_path_on_functions.sql
@@ -6,7 +6,7 @@ ALTER FUNCTION public.audit_log_immutable() SET search_path = public;
ALTER FUNCTION public.block_document_deletion() SET search_path = public;
ALTER FUNCTION public.calculate_retention_expiry() SET search_path = public;
ALTER FUNCTION public.check_journal_entry_balance() SET search_path = public;
-ALTER FUNCTION public.create_invoice_with_items(p_invoice jsonb, p_items jsonb) SET search_path = public;
+-- create_invoice_with_items removed: function was never created
ALTER FUNCTION public.detect_voucher_gaps(p_user_id uuid, p_fiscal_period_id uuid, p_series text) SET search_path = public;
ALTER FUNCTION public.enforce_journal_entry_immutability() SET search_path = public;
ALTER FUNCTION public.enforce_journal_entry_line_immutability() SET search_path = public;
@@ -14,15 +14,15 @@ ALTER FUNCTION public.enforce_opening_balance_immutability() SET search_path = p
ALTER FUNCTION public.enforce_period_lock() SET search_path = public;
ALTER FUNCTION public.enforce_period_lock_documents() SET search_path = public;
ALTER FUNCTION public.enforce_retention_journal_entries() SET search_path = public;
-ALTER FUNCTION public.generate_invoice_number(p_user_id uuid) SET search_path = public;
+-- generate_invoice_number removed: created in later migration (20260306) with search_path already set
ALTER FUNCTION public.get_next_arrival_number(p_user_id uuid) SET search_path = public;
ALTER FUNCTION public.get_unlinked_1930_lines(p_user_id uuid, p_date_from date, p_date_to date) SET search_path = public;
ALTER FUNCTION public.handle_new_user() SET search_path = public;
ALTER FUNCTION public.next_voucher_number(p_user_id uuid, p_fiscal_period_id uuid, p_series text) SET search_path = public;
-ALTER FUNCTION public.seed_asset_categories(p_user_id uuid) SET search_path = public;
+-- seed_asset_categories removed: function was never created
ALTER FUNCTION public.seed_chart_of_accounts(p_user_id uuid, p_entity_type text) SET search_path = public;
ALTER FUNCTION public.set_committed_at() SET search_path = public;
ALTER FUNCTION public.update_overdue_supplier_invoices() SET search_path = public;
-ALTER FUNCTION public.update_reconciliation_session_counts() SET search_path = public;
+-- update_reconciliation_session_counts removed: function was never created
ALTER FUNCTION public.update_updated_at_column() SET search_path = public;
ALTER FUNCTION public.write_audit_log() SET search_path = public;
diff --git a/supabase/migrations/20260311120000_sandbox_support.sql b/supabase/migrations/20260311120000_sandbox_support.sql
new file mode 100644
index 00000000..51634787
--- /dev/null
+++ b/supabase/migrations/20260311120000_sandbox_support.sql
@@ -0,0 +1,289 @@
+-- Sandbox support: is_sandbox column, enforcement trigger bypasses, cleanup functions
+
+-- =============================================================================
+-- 1a. Add is_sandbox column to company_settings
+-- =============================================================================
+
+ALTER TABLE public.company_settings
+ ADD COLUMN is_sandbox boolean NOT NULL DEFAULT false;
+
+CREATE INDEX idx_company_settings_sandbox
+ ON public.company_settings (is_sandbox)
+ WHERE is_sandbox = true;
+
+-- =============================================================================
+-- 1b. Update enforcement triggers to skip for sandbox users
+-- =============================================================================
+
+-- enforce_journal_entry_immutability — add sandbox bypass
+CREATE OR REPLACE FUNCTION public.enforce_journal_entry_immutability()
+RETURNS trigger
+LANGUAGE plpgsql
+SET search_path = public
+AS $$
+BEGIN
+ -- Skip enforcement for sandbox users
+ IF EXISTS (
+ SELECT 1 FROM public.company_settings
+ WHERE user_id = COALESCE(OLD.user_id, NEW.user_id) AND is_sandbox = true
+ ) THEN
+ IF TG_OP = 'DELETE' THEN
+ RETURN OLD;
+ END IF;
+ RETURN NEW;
+ END IF;
+
+ IF TG_OP = 'DELETE' THEN
+ -- Allow deleting drafts
+ IF OLD.status = 'draft' THEN
+ RETURN OLD;
+ END IF;
+ RAISE EXCEPTION 'Cannot delete a % journal entry (id: %)', OLD.status, OLD.id;
+ END IF;
+
+ -- TG_OP = 'UPDATE'
+ -- Allow: draft → draft (editing a draft)
+ IF OLD.status = 'draft' AND NEW.status = 'draft' THEN
+ RETURN NEW;
+ END IF;
+
+ -- Allow: draft → posted (committing)
+ IF OLD.status = 'draft' AND NEW.status = 'posted' THEN
+ RETURN NEW;
+ END IF;
+
+ -- Allow: posted → reversed (storno reversal)
+ IF OLD.status = 'posted' AND NEW.status = 'reversed' THEN
+ -- Only allow setting reversed_by_id during this transition
+ IF NEW.description != OLD.description
+ OR NEW.entry_date != OLD.entry_date
+ OR NEW.fiscal_period_id != OLD.fiscal_period_id
+ OR NEW.voucher_number != OLD.voucher_number THEN
+ RAISE EXCEPTION 'Cannot modify fields of a posted entry during reversal (id: %)', OLD.id;
+ END IF;
+ RETURN NEW;
+ END IF;
+
+ -- Block all other transitions
+ RAISE EXCEPTION 'Cannot modify a % journal entry (id: %). Committed entries are immutable per Bokföringslagen.',
+ OLD.status, OLD.id;
+END;
+$$;
+
+-- enforce_journal_entry_line_immutability — add sandbox bypass
+CREATE OR REPLACE FUNCTION public.enforce_journal_entry_line_immutability()
+RETURNS trigger
+LANGUAGE plpgsql
+SET search_path = public
+AS $$
+DECLARE
+ v_status text;
+ v_user_id uuid;
+BEGIN
+ -- Get the parent entry status and user_id
+ SELECT status, user_id INTO v_status, v_user_id
+ FROM public.journal_entries
+ WHERE id = COALESCE(OLD.journal_entry_id, NEW.journal_entry_id);
+
+ -- Skip enforcement for sandbox users
+ IF EXISTS (
+ SELECT 1 FROM public.company_settings
+ WHERE user_id = v_user_id AND is_sandbox = true
+ ) THEN
+ IF TG_OP = 'DELETE' THEN
+ RETURN OLD;
+ END IF;
+ RETURN NEW;
+ END IF;
+
+ -- Allow modifications to lines of draft entries
+ IF v_status = 'draft' THEN
+ IF TG_OP = 'DELETE' THEN
+ RETURN OLD;
+ END IF;
+ RETURN NEW;
+ END IF;
+
+ -- Block modifications to lines of posted/reversed entries
+ RAISE EXCEPTION 'Cannot % lines of a % journal entry. Committed entries are immutable per Bokföringslagen.',
+ TG_OP, v_status;
+END;
+$$;
+
+-- enforce_retention_journal_entries — add sandbox bypass (SECURITY DEFINER)
+CREATE OR REPLACE FUNCTION public.enforce_retention_journal_entries()
+RETURNS trigger
+LANGUAGE plpgsql
+SECURITY DEFINER
+SET search_path = public
+AS $$
+DECLARE
+ v_retention_expires date;
+BEGIN
+ -- Skip enforcement for sandbox users
+ IF EXISTS (
+ SELECT 1 FROM public.company_settings
+ WHERE user_id = OLD.user_id AND is_sandbox = true
+ ) THEN
+ RETURN OLD;
+ END IF;
+
+ SELECT fp.retention_expires_at INTO v_retention_expires
+ FROM public.fiscal_periods fp
+ WHERE fp.id = OLD.fiscal_period_id;
+
+ IF v_retention_expires IS NOT NULL AND v_retention_expires > CURRENT_DATE THEN
+ INSERT INTO public.audit_log (user_id, action, table_name, record_id, description)
+ VALUES (OLD.user_id, 'RETENTION_BLOCK', 'journal_entries', OLD.id,
+ 'Attempted deletion within retention period (expires ' || v_retention_expires || ')');
+
+ RAISE EXCEPTION 'Cannot delete journal entry within 7-year retention period (expires %)',
+ v_retention_expires;
+ END IF;
+
+ RETURN OLD;
+END;
+$$;
+
+-- block_document_deletion — add sandbox bypass (SECURITY DEFINER)
+CREATE OR REPLACE FUNCTION public.block_document_deletion()
+RETURNS trigger
+LANGUAGE plpgsql
+SECURITY DEFINER
+SET search_path = public
+AS $$
+DECLARE
+ v_entry_status text;
+ v_retention_expires date;
+BEGIN
+ -- Skip enforcement for sandbox users
+ IF EXISTS (
+ SELECT 1 FROM public.company_settings
+ WHERE user_id = OLD.user_id AND is_sandbox = true
+ ) THEN
+ RETURN OLD;
+ END IF;
+
+ -- Check if linked to a committed journal entry
+ IF OLD.journal_entry_id IS NOT NULL THEN
+ SELECT je.status INTO v_entry_status
+ FROM public.journal_entries je
+ WHERE je.id = OLD.journal_entry_id;
+
+ IF v_entry_status IN ('posted', 'reversed') THEN
+ -- Log the blocked attempt
+ INSERT INTO public.audit_log (user_id, action, table_name, record_id, description)
+ VALUES (OLD.user_id, 'DOCUMENT_DELETE_BLOCKED', 'document_attachments', OLD.id,
+ 'Attempted deletion of document linked to ' || v_entry_status || ' journal entry ' || OLD.journal_entry_id);
+
+ RAISE EXCEPTION 'Cannot delete document linked to a % journal entry (Bokföringslagen)',
+ v_entry_status;
+ END IF;
+ END IF;
+
+ -- Check retention window
+ IF OLD.journal_entry_id IS NOT NULL THEN
+ SELECT fp.retention_expires_at INTO v_retention_expires
+ FROM public.journal_entries je
+ JOIN public.fiscal_periods fp ON fp.id = je.fiscal_period_id
+ WHERE je.id = OLD.journal_entry_id;
+
+ IF v_retention_expires IS NOT NULL AND v_retention_expires > CURRENT_DATE THEN
+ INSERT INTO public.audit_log (user_id, action, table_name, record_id, description)
+ VALUES (OLD.user_id, 'RETENTION_BLOCK', 'document_attachments', OLD.id,
+ 'Attempted deletion within retention period (expires ' || v_retention_expires || ')');
+
+ RAISE EXCEPTION 'Cannot delete document within 7-year retention period (expires %)',
+ v_retention_expires;
+ END IF;
+ END IF;
+
+ RETURN OLD;
+END;
+$$;
+
+-- =============================================================================
+-- 1c. cleanup_sandbox_user(p_user_id uuid)
+-- =============================================================================
+
+CREATE OR REPLACE FUNCTION public.cleanup_sandbox_user(p_user_id uuid)
+RETURNS integer
+LANGUAGE plpgsql
+SECURITY DEFINER
+SET search_path = public
+AS $$
+DECLARE
+ v_is_sandbox boolean;
+ v_deleted integer := 0;
+BEGIN
+ -- Verify this is a sandbox user
+ SELECT is_sandbox INTO v_is_sandbox
+ FROM public.company_settings
+ WHERE user_id = p_user_id;
+
+ IF v_is_sandbox IS NOT TRUE THEN
+ RAISE EXCEPTION 'User % is not a sandbox user', p_user_id;
+ END IF;
+
+ -- Clear RESTRICT FKs on document_attachments
+ UPDATE public.document_attachments
+ SET journal_entry_id = NULL, journal_entry_line_id = NULL
+ WHERE user_id = p_user_id;
+
+ DELETE FROM public.document_attachments WHERE user_id = p_user_id;
+
+ -- Delete journal entry lines (child of journal_entries)
+ DELETE FROM public.journal_entry_lines
+ WHERE journal_entry_id IN (
+ SELECT id FROM public.journal_entries WHERE user_id = p_user_id
+ );
+
+ -- Delete journal entries (triggers bypass for sandbox)
+ DELETE FROM public.journal_entries WHERE user_id = p_user_id;
+
+ -- Delete supplier invoices before suppliers cascade
+ DELETE FROM public.supplier_invoices WHERE user_id = p_user_id;
+
+ -- Delete from auth.users — cascades everything else
+ DELETE FROM auth.users WHERE id = p_user_id;
+ GET DIAGNOSTICS v_deleted = ROW_COUNT;
+
+ RETURN v_deleted;
+END;
+$$;
+
+-- =============================================================================
+-- 1d. cleanup_expired_sandbox_users(p_max_age_hours int)
+-- =============================================================================
+
+CREATE OR REPLACE FUNCTION public.cleanup_expired_sandbox_users(p_max_age_hours int DEFAULT 24)
+RETURNS integer
+LANGUAGE plpgsql
+SECURITY DEFINER
+SET search_path = public
+AS $$
+DECLARE
+ v_user_id uuid;
+ v_total integer := 0;
+BEGIN
+ FOR v_user_id IN
+ SELECT cs.user_id
+ FROM public.company_settings cs
+ WHERE cs.is_sandbox = true
+ AND cs.created_at < now() - interval '1 hour' * p_max_age_hours
+ LOOP
+ BEGIN
+ PERFORM public.cleanup_sandbox_user(v_user_id);
+ v_total := v_total + 1;
+ EXCEPTION WHEN OTHERS THEN
+ RAISE WARNING 'Failed to clean up sandbox user %: %', v_user_id, SQLERRM;
+ END;
+ END LOOP;
+
+ RETURN v_total;
+END;
+$$;
+
+-- Grant execute to service_role
+GRANT EXECUTE ON FUNCTION public.cleanup_sandbox_user(uuid) TO service_role;
+GRANT EXECUTE ON FUNCTION public.cleanup_expired_sandbox_users(int) TO service_role;
diff --git a/tests/helpers.ts b/tests/helpers.ts
index 8b7a6e95..cd053b4a 100644
--- a/tests/helpers.ts
+++ b/tests/helpers.ts
@@ -449,6 +449,7 @@ export function makeCompanySettings(
onboarding_step: 6,
onboarding_complete: true,
sector_slug: null,
+ is_sandbox: false,
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
...overrides,
diff --git a/types/index.ts b/types/index.ts
index 19446e9f..ff8c9e21 100644
--- a/types/index.ts
+++ b/types/index.ts
@@ -138,6 +138,9 @@ export interface CompanySettings {
// Sector
sector_slug: string | null
+ // Sandbox
+ is_sandbox: boolean
+
// Timestamps
created_at: string
updated_at: string
diff --git a/vercel.json b/vercel.json
index 13fd32a7..00f0f4ce 100644
--- a/vercel.json
+++ b/vercel.json
@@ -19,6 +19,10 @@
{
"path": "/api/documents/verify/cron",
"schedule": "0 3 * * 0"
+ },
+ {
+ "path": "/api/sandbox/cleanup/cron",
+ "schedule": "0 4 * * *"
}
]
}