bmc_hub/migrations/081_better_email_templates.sql
Christian 56d6d45aa2 feat(sag): Add Varekøb & Salg module with database migration and frontend template
- Created a new SQL migration for the sag_salgsvarer table to manage sales and purchase items.
- Implemented a new HTML template for the Varekøb & Salg module, including summary cards and tables for sales and purchases.
- Added JavaScript functions for loading and rendering order data dynamically.
- Introduced a new backend search module for customers, contacts, hardware, and locations with autocomplete functionality.
- Developed an email templates API for managing system and customer-specific email templates.
- Created multiple migrations for Nextcloud instances, cache, audit logs, email templates, sag comments, hardware locations, and billing methods.
- Enhanced the sag module with solutions, order lines, work types, and 2FA support for user authentication.
2026-02-02 20:23:56 +01:00

97 lines
3.7 KiB
SQL

-- Migration: 081_better_email_templates
-- Created: 2026-02-01
-- Create email_templates table
CREATE TABLE IF NOT EXISTS email_templates (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
slug VARCHAR(255) NOT NULL,
subject VARCHAR(255) NOT NULL,
body TEXT NOT NULL,
category VARCHAR(50) DEFAULT 'general',
description TEXT,
variables JSONB DEFAULT '{}',
is_system BOOLEAN DEFAULT FALSE,
customer_id INTEGER REFERENCES customers(id) ON DELETE CASCADE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
UNIQUE(slug, customer_id) -- A slug must be unique per customer (or global if customer_id is null)
);
-- Index for faster lookups
CREATE INDEX idx_email_templates_slug ON email_templates(slug);
CREATE INDEX idx_email_templates_customer ON email_templates(customer_id);
-- Migrate existing settings if they exist
DO $$
DECLARE
internal_subj TEXT;
internal_body TEXT;
external_subj TEXT;
external_body TEXT;
nc_subj TEXT;
nc_body TEXT;
BEGIN
-- Intern besked
SELECT value INTO internal_subj FROM settings WHERE key = 'email_template_internal_subject';
SELECT value INTO internal_body FROM settings WHERE key = 'email_template_internal_body';
IF internal_subj IS NOT NULL AND internal_body IS NOT NULL THEN
INSERT INTO email_templates (name, slug, subject, body, category, description, variables, is_system)
VALUES (
'Intern besked',
'internal_message',
internal_subj,
internal_body,
'internal',
'Standard skabelon til interne beskeder',
'{"message": "Selve beskeden"}'::jsonb,
TRUE
) ON CONFLICT DO NOTHING;
END IF;
-- Ekstern besked
SELECT value INTO external_subj FROM settings WHERE key = 'email_template_external_subject';
SELECT value INTO external_body FROM settings WHERE key = 'email_template_external_body';
IF external_subj IS NOT NULL AND external_body IS NOT NULL THEN
INSERT INTO email_templates (name, slug, subject, body, category, description, variables, is_system)
VALUES (
'Generel kundebesked',
'external_message',
external_subj,
external_body,
'general',
'Standard skabelon til emails sendt til kunder',
'{"customer_name": "Navnet på kunden", "message": "Selve beskeden"}'::jsonb,
TRUE
) ON CONFLICT DO NOTHING;
END IF;
-- Nextcloud velkomst
SELECT value INTO nc_subj FROM settings WHERE key = 'nextcloud_user_welcome_subject';
SELECT value INTO nc_body FROM settings WHERE key = 'nextcloud_user_welcome_body';
IF nc_subj IS NOT NULL AND nc_body IS NOT NULL THEN
INSERT INTO email_templates (name, slug, subject, body, category, description, variables, is_system)
VALUES (
'Nextcloud Velkomst',
'nextcloud_welcome',
nc_subj,
nc_body,
'nextcloud',
'Sendes til nye brugere når deres konto oprettes',
'{"name": "Modtagerens navn", "url": "URL til Nextcloud instans", "username": "Brugernavn", "password": "Det autogenererede password"}'::jsonb,
TRUE
) ON CONFLICT DO NOTHING;
END IF;
END $$;
-- Cleanup old settings (optional, keeping them for backward compatibility if needed, but usually safe to remove if code is updated simultaneously)
-- DELETE FROM settings WHERE key IN (
-- 'email_template_internal_subject', 'email_template_internal_body',
-- 'email_template_external_subject', 'email_template_external_body',
-- 'nextcloud_user_welcome_subject', 'nextcloud_user_welcome_body'
-- );