-- ═══════════════════════════════════════════════════════════════════════════ -- AAMOS Admin v2 — Notifications & Reports Schema -- ═══════════════════════════════════════════════════════════════════════════ -- ── Notifications ────────────────────────────────────────────────────────── CREATE TABLE IF NOT EXISTS notifications ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), type TEXT NOT NULL, -- 'lead'|'invoice'|'payment'|'ticket'|'employee'|'contract'|'system' title TEXT NOT NULL, message TEXT NOT NULL, entity_type TEXT, -- 'customer'|'invoice'|'ticket'|'lead'|'employee'|'contract' entity_id UUID, priority TEXT NOT NULL DEFAULT 'normal', -- 'low'|'normal'|'high'|'urgent' read_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE INDEX IF NOT EXISTS idx_notifications_read ON notifications (read_at); CREATE INDEX IF NOT EXISTS idx_notifications_type ON notifications (type); CREATE INDEX IF NOT EXISTS idx_notifications_created ON notifications (created_at DESC); -- ── Notification Settings ────────────────────────────────────────────────── CREATE TABLE IF NOT EXISTS notification_settings ( id INTEGER PRIMARY KEY DEFAULT 1, settings JSONB NOT NULL DEFAULT '{ "email_notifications": true, "push_notifications": true, "notify_on_new_lead": true, "notify_on_new_invoice": true, "notify_on_overdue_invoice": true, "notify_on_new_ticket": true, "notify_on_ticket_assigned": true, "notify_on_new_employee": true, "notify_on_contract_ending": true, "daily_summary": true, "weekly_summary": true }', updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); INSERT INTO notification_settings (id) VALUES (1) ON CONFLICT (id) DO NOTHING; -- ── Module Settings ──────────────────────────────────────────────────────── CREATE TABLE IF NOT EXISTS module_settings ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), module TEXT NOT NULL UNIQUE, -- 'crm'|'billing'|'ledger'|'support'|'hr'|'mail'|'automations' settings JSONB NOT NULL DEFAULT '{}', updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); -- Insert default settings for each module INSERT INTO module_settings (module, settings) VALUES ('crm', '{ "auto_convert_leads": false, "lead_assignment": "manual", "default_customer_type": "company", "require_org_nr": true }'), ('billing', '{ "default_payment_terms": 30, "default_vat_rate": 25, "invoice_prefix": "F-", "reminder_fee": 60, "interest_rate": 8 }'), ('ledger', '{ "fiscal_year_start": "01-01", "base_currency": "SEK", "auto_reconcile": false, "require_approval": true }'), ('support', '{ "auto_assign": false, "default_priority": "medium", "sla_response_hours": 24, "sla_resolution_hours": 72 }'), ('hr', '{ "probation_months": 6, "vacation_days": 25, "sick_days": 14, "notice_period_months": 3 }'), ('mail', '{ "auto_create_leads": true, "forward_to_support": false, "archive_after_days": 90 }'), ('automations', '{ "max_retries": 3, "retry_interval_minutes": 60, "log_retention_days": 30 }') ON CONFLICT (module) DO NOTHING; -- ── Scheduled Reports ────────────────────────────────────────────────────── CREATE TABLE IF NOT EXISTS scheduled_reports ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name TEXT NOT NULL, report_type TEXT NOT NULL, frequency TEXT NOT NULL, -- 'daily'|'weekly'|'monthly'|'quarterly' recipients TEXT[] NOT NULL DEFAULT '{}', last_run_at TIMESTAMPTZ, next_run_at TIMESTAMPTZ, is_active BOOLEAN NOT NULL DEFAULT TRUE, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); -- ── Report History ───────────────────────────────────────────────────────── CREATE TABLE IF NOT EXISTS report_history ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), report_name TEXT NOT NULL, report_type TEXT NOT NULL, parameters JSONB NOT NULL DEFAULT '{}', result_url TEXT, status TEXT NOT NULL DEFAULT 'completed', -- 'running'|'completed'|'failed' error_message TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), completed_at TIMESTAMPTZ ); CREATE INDEX IF NOT EXISTS idx_report_history_type ON report_history (report_type); CREATE INDEX IF NOT EXISTS idx_report_history_created ON report_history (created_at DESC);