@realtimex/email-automator 2.26.0 → 2.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,140 +0,0 @@
1
- -- Migration: Fix rule categories - ensure all user rules get categories from templates
2
- -- Updates existing rules and fixes the installation functions
3
-
4
- -- Step 1: Add category column to rules table if it doesn't exist
5
- ALTER TABLE public.rules
6
- ADD COLUMN IF NOT EXISTS category VARCHAR(50);
7
-
8
- -- Step 2: Update existing user rules to get categories from their templates
9
- UPDATE public.rules r
10
- SET category = rt.category
11
- FROM public.rule_templates rt
12
- WHERE r.rule_template_id = rt.rule_id
13
- AND r.is_system_managed = true
14
- AND (r.category IS NULL OR r.category != rt.category);
15
-
16
- -- Step 3: Update the backfill function to include category
17
- CREATE OR REPLACE FUNCTION public.install_all_rules_for_user(target_user_id UUID)
18
- RETURNS void AS $$
19
- DECLARE
20
- existing_rules_count INTEGER;
21
- BEGIN
22
- -- Check if user already has rules installed
23
- SELECT COUNT(*) INTO existing_rules_count
24
- FROM public.rules
25
- WHERE user_id = target_user_id AND is_system_managed = true;
26
-
27
- IF existing_rules_count > 0 THEN
28
- RAISE NOTICE 'User % already has % system-managed rules installed', target_user_id, existing_rules_count;
29
- RETURN;
30
- END IF;
31
-
32
- -- Copy ALL rules from templates with category
33
- INSERT INTO public.rules (
34
- user_id,
35
- name,
36
- intent,
37
- condition,
38
- action,
39
- actions,
40
- is_enabled,
41
- pack,
42
- rule_template_id,
43
- is_system_managed,
44
- category,
45
- created_at
46
- )
47
- SELECT
48
- target_user_id,
49
- rt.name,
50
- rt.intent,
51
- rt.condition,
52
- rt.actions[1],
53
- rt.actions,
54
- rt.is_enabled_by_default,
55
- rt.pack_id,
56
- rt.rule_id,
57
- true,
58
- rt.category,
59
- NOW()
60
- FROM public.rule_templates rt
61
- ORDER BY rt.pack_id, rt.sort_order;
62
-
63
- RAISE NOTICE 'All rules installed for user %', target_user_id;
64
- END;
65
- $$ LANGUAGE plpgsql SECURITY DEFINER;
66
-
67
- -- Step 4: Update the trigger function to include category
68
- CREATE OR REPLACE FUNCTION public.handle_new_user()
69
- RETURNS TRIGGER AS $$
70
- BEGIN
71
- -- Wrap everything in exception handler to prevent user creation failure
72
- BEGIN
73
- -- 1. Create user_settings record with defaults
74
- INSERT INTO public.user_settings (
75
- user_id,
76
- llm_provider,
77
- llm_model,
78
- user_role,
79
- onboarding_completed,
80
- created_at,
81
- updated_at
82
- ) VALUES (
83
- NEW.id,
84
- 'realtimexai',
85
- 'gpt-4o-mini',
86
- NULL,
87
- FALSE,
88
- NOW(),
89
- NOW()
90
- )
91
- ON CONFLICT (user_id) DO NOTHING;
92
-
93
- -- 2. Install ALL rules from templates with category
94
- IF EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'rule_templates') THEN
95
- INSERT INTO public.rules (
96
- user_id,
97
- name,
98
- intent,
99
- condition,
100
- action,
101
- actions,
102
- is_enabled,
103
- pack,
104
- rule_template_id,
105
- is_system_managed,
106
- category,
107
- created_at
108
- )
109
- SELECT
110
- NEW.id,
111
- rt.name,
112
- rt.intent,
113
- rt.condition,
114
- rt.actions[1],
115
- rt.actions,
116
- rt.is_enabled_by_default,
117
- rt.pack_id,
118
- rt.rule_id,
119
- true,
120
- rt.category,
121
- NOW()
122
- FROM public.rule_templates rt
123
- ORDER BY rt.pack_id, rt.sort_order;
124
- END IF;
125
-
126
- EXCEPTION
127
- WHEN OTHERS THEN
128
- -- Log error but don't fail user creation
129
- RAISE WARNING 'Failed to initialize user data for %: % (SQLSTATE: %)', NEW.id, SQLERRM, SQLSTATE;
130
- END;
131
-
132
- RETURN NEW;
133
- END;
134
- $$ LANGUAGE plpgsql SECURITY DEFINER;
135
-
136
- -- Step 5: Add index for category lookups on rules table
137
- CREATE INDEX IF NOT EXISTS idx_rules_category ON public.rules(category);
138
-
139
- -- Comments
140
- COMMENT ON COLUMN public.rules.category IS 'Rule category for UI grouping: email_organization, priority_alerts, development, sales_business, operations';