- Added API endpoints for tag management (create, read, update, delete). - Implemented entity tagging functionality to associate tags with various entities. - Created workflow management for tag-triggered actions. - Developed frontend views for tag administration using FastAPI and Jinja2. - Designed HTML template for tag management interface with Bootstrap styling. - Added JavaScript for tag picker component with keyboard shortcuts and dynamic tag filtering. - Created database migration scripts for tags, entity_tags, and tag_workflows tables. - Included default tags for initial setup in the database.
85 lines
2.7 KiB
PL/PgSQL
85 lines
2.7 KiB
PL/PgSQL
-- Link tmodule_customers til Hub customers baseret på navn match
|
|
-- Dette script kører automatisk ved opstart og kan også køres manuelt
|
|
|
|
-- Drop eksisterende funktion hvis den findes
|
|
DROP FUNCTION IF EXISTS link_tmodule_customers_to_hub();
|
|
|
|
-- Funktion til at linke kunder baseret på navn
|
|
CREATE OR REPLACE FUNCTION link_tmodule_customers_to_hub()
|
|
RETURNS TABLE (
|
|
tmodule_id INTEGER,
|
|
tmodule_name TEXT,
|
|
hub_id INTEGER,
|
|
hub_name TEXT,
|
|
action TEXT
|
|
) AS $$
|
|
BEGIN
|
|
RETURN QUERY
|
|
WITH matches AS (
|
|
-- Find eksakte navn matches
|
|
SELECT
|
|
tc.id as tmodule_id,
|
|
tc.name::TEXT as tmodule_name,
|
|
c.id as hub_id,
|
|
c.name::TEXT as hub_name,
|
|
'exact_match'::TEXT as action
|
|
FROM tmodule_customers tc
|
|
JOIN customers c ON LOWER(TRIM(tc.name)) = LOWER(TRIM(c.name))
|
|
WHERE tc.hub_customer_id IS NULL
|
|
),
|
|
updates AS (
|
|
-- Opdater tmodule_customers med hub_customer_id
|
|
UPDATE tmodule_customers tc
|
|
SET hub_customer_id = m.hub_id,
|
|
updated_at = CURRENT_TIMESTAMP
|
|
FROM matches m
|
|
WHERE tc.id = m.tmodule_id
|
|
RETURNING tc.id, tc.name::TEXT, m.hub_id, m.hub_name, m.action
|
|
)
|
|
SELECT * FROM updates;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
-- Kør linking
|
|
SELECT * FROM link_tmodule_customers_to_hub();
|
|
|
|
-- Trigger til automatisk linking ved insert/update af tmodule_customers
|
|
CREATE OR REPLACE FUNCTION auto_link_tmodule_customer()
|
|
RETURNS TRIGGER AS $$
|
|
DECLARE
|
|
matched_hub_id INTEGER;
|
|
BEGIN
|
|
-- Hvis hub_customer_id allerede er sat, skip
|
|
IF NEW.hub_customer_id IS NOT NULL THEN
|
|
RETURN NEW;
|
|
END IF;
|
|
|
|
-- Find matching hub customer baseret på navn
|
|
SELECT id INTO matched_hub_id
|
|
FROM customers
|
|
WHERE LOWER(TRIM(name)) = LOWER(TRIM(NEW.name))
|
|
LIMIT 1;
|
|
|
|
-- Hvis match fundet, sæt hub_customer_id
|
|
IF matched_hub_id IS NOT NULL THEN
|
|
NEW.hub_customer_id := matched_hub_id;
|
|
END IF;
|
|
|
|
RETURN NEW;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
-- Drop trigger hvis den eksisterer
|
|
DROP TRIGGER IF EXISTS trigger_auto_link_tmodule_customer ON tmodule_customers;
|
|
|
|
-- Opret trigger der kører før INSERT eller UPDATE
|
|
CREATE TRIGGER trigger_auto_link_tmodule_customer
|
|
BEFORE INSERT OR UPDATE OF name
|
|
ON tmodule_customers
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION auto_link_tmodule_customer();
|
|
|
|
COMMENT ON FUNCTION link_tmodule_customers_to_hub() IS 'Linker eksisterende tmodule_customers til Hub customers baseret på navn match';
|
|
COMMENT ON FUNCTION auto_link_tmodule_customer() IS 'Automatisk linker nye/opdaterede tmodule_customers til Hub customers';
|
|
COMMENT ON TRIGGER trigger_auto_link_tmodule_customer ON tmodule_customers IS 'Auto-linker tmodule customers til Hub customers ved navn match';
|