@fayz-ai/plugin-automations 0.15.29 → 0.15.31
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.
- package/dist/AutomationDetailPage-UHY7KHQK.js +4 -0
- package/dist/AutomationDetailPage-UHY7KHQK.js.map +1 -0
- package/dist/AutomationsList-3UTTQSIX.js +478 -0
- package/dist/AutomationsList-3UTTQSIX.js.map +1 -0
- package/dist/FlowsPage-6SUXT2LP.js +509 -0
- package/dist/FlowsPage-6SUXT2LP.js.map +1 -0
- package/dist/chunk-63YAGMWV.js +258 -0
- package/dist/chunk-63YAGMWV.js.map +1 -0
- package/dist/chunk-TCSHZ3VF.js +764 -0
- package/dist/chunk-TCSHZ3VF.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -2974
- package/dist/index.js.map +1 -1
- package/dist/migrations/main.browser.js +9 -0
- package/dist/migrations/main.browser.js.map +1 -0
- package/dist/migrations/main.js +998 -0
- package/dist/migrations/main.js.map +1 -0
- package/dist/views/AutomationsList.d.ts.map +1 -1
- package/package.json +10 -4
|
@@ -0,0 +1,998 @@
|
|
|
1
|
+
// src/migrations/index.ts
|
|
2
|
+
var MIGRATION_000_BASELINE = `-- ============================================================================
|
|
3
|
+
-- plugins/plugin-automations/migrations/000_baseline.sql \u2014 what installing this provisions, as one file.
|
|
4
|
+
--
|
|
5
|
+
-- GENERATED by scripts/emit-unit-baselines.mjs (#338). Do not edit by hand:
|
|
6
|
+
-- change the schema, regenerate, and let scripts/check-chain-equivalence.mjs
|
|
7
|
+
-- prove the result still matches.
|
|
8
|
+
--
|
|
9
|
+
-- This replaced 4 migration files. They are kept, unapplied, in
|
|
10
|
+
-- migrations.archive/ \u2014 a year of decisions is worth reading even when it is no
|
|
11
|
+
-- longer worth replaying.
|
|
12
|
+
--
|
|
13
|
+
-- Object order is fixed rather than dependency-sorted: schemas, types,
|
|
14
|
+
-- sequences, tables, defaults, functions, constraints, views, indexes, foreign
|
|
15
|
+
-- keys, triggers, row security, policies, grants. Dependencies BETWEEN units
|
|
16
|
+
-- are carried by the order packages/db/chain.json declares.
|
|
17
|
+
-- ============================================================================
|
|
18
|
+
|
|
19
|
+
-- A LANGUAGE sql function binds its body at CREATE time, and 570 functions
|
|
20
|
+
-- sorted by name are not sorted by who calls whom. This is what lets them load
|
|
21
|
+
-- in any order; every body is still compiled on first call, so a genuinely
|
|
22
|
+
-- broken reference surfaces then rather than never. Session-scoped.
|
|
23
|
+
SET check_function_bodies = false;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
-- \u2500\u2500 default privileges: reset \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
28
|
+
-- Cleared BEFORE anything is created, so every object below gets the ACL the
|
|
29
|
+
-- dump describes rather than the image's defaults on top of it. Restored at
|
|
30
|
+
-- the end of this file.
|
|
31
|
+
|
|
32
|
+
DO $dp$ BEGIN
|
|
33
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON SEQUENCES FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
34
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
35
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON SEQUENCES FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
36
|
+
END $dp$;
|
|
37
|
+
|
|
38
|
+
DO $dp$ BEGIN
|
|
39
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON SEQUENCES FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
40
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
41
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON SEQUENCES FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
42
|
+
END $dp$;
|
|
43
|
+
|
|
44
|
+
DO $dp$ BEGIN
|
|
45
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON FUNCTIONS FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
46
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
47
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON FUNCTIONS FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
48
|
+
END $dp$;
|
|
49
|
+
|
|
50
|
+
DO $dp$ BEGIN
|
|
51
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON FUNCTIONS FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
52
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
53
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON FUNCTIONS FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
54
|
+
END $dp$;
|
|
55
|
+
|
|
56
|
+
DO $dp$ BEGIN
|
|
57
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON TABLES FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
58
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
59
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public REVOKE ALL ON TABLES FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
60
|
+
END $dp$;
|
|
61
|
+
|
|
62
|
+
DO $dp$ BEGIN
|
|
63
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON TABLES FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
64
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
65
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public REVOKE ALL ON TABLES FROM anon, authenticated, service_role, fayz_connector$stmt$;
|
|
66
|
+
END $dp$;
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
-- \u2500\u2500 table \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
70
|
+
|
|
71
|
+
CREATE TABLE public.plg_automations_provider_templates (
|
|
72
|
+
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
73
|
+
tenant_id uuid,
|
|
74
|
+
event_key text NOT NULL,
|
|
75
|
+
channel text DEFAULT 'whatsapp'::text NOT NULL,
|
|
76
|
+
provider text DEFAULT 'tyxter'::text NOT NULL,
|
|
77
|
+
provider_template_id text,
|
|
78
|
+
template_name text NOT NULL,
|
|
79
|
+
language text DEFAULT 'pt_BR'::text NOT NULL,
|
|
80
|
+
variable_map jsonb DEFAULT '[]'::jsonb NOT NULL,
|
|
81
|
+
actions_map jsonb DEFAULT '[]'::jsonb NOT NULL,
|
|
82
|
+
status text DEFAULT 'draft'::text NOT NULL,
|
|
83
|
+
rejection_reason text,
|
|
84
|
+
body_snapshot text,
|
|
85
|
+
submitted_at timestamp with time zone,
|
|
86
|
+
approved_at timestamp with time zone,
|
|
87
|
+
created_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
88
|
+
updated_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
89
|
+
CONSTRAINT plg_automations_provider_templates_status_check CHECK ((status = ANY (ARRAY['draft'::text, 'submitted'::text, 'approved'::text, 'rejected'::text, 'paused'::text, 'disabled'::text])))
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
CREATE TABLE public.plg_automations_templates (
|
|
93
|
+
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
94
|
+
tenant_id uuid NOT NULL,
|
|
95
|
+
event_key text NOT NULL,
|
|
96
|
+
channel text DEFAULT 'email'::text NOT NULL,
|
|
97
|
+
enabled boolean,
|
|
98
|
+
subject text,
|
|
99
|
+
body text,
|
|
100
|
+
name text,
|
|
101
|
+
created_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
102
|
+
updated_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
103
|
+
actions jsonb
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
-- \u2500\u2500 function \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
108
|
+
|
|
109
|
+
CREATE FUNCTION public.automations_set_updated_at() RETURNS trigger
|
|
110
|
+
LANGUAGE plpgsql
|
|
111
|
+
AS $$
|
|
112
|
+
BEGIN
|
|
113
|
+
NEW.updated_at = now();
|
|
114
|
+
RETURN NEW;
|
|
115
|
+
END $$;
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
-- \u2500\u2500 constraint \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
119
|
+
|
|
120
|
+
ALTER TABLE ONLY public.plg_automations_provider_templates
|
|
121
|
+
ADD CONSTRAINT plg_automations_provider_templates_pkey PRIMARY KEY (id);
|
|
122
|
+
|
|
123
|
+
ALTER TABLE ONLY public.plg_automations_templates
|
|
124
|
+
ADD CONSTRAINT plg_automations_templates_pkey PRIMARY KEY (id);
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
-- \u2500\u2500 index \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
128
|
+
|
|
129
|
+
CREATE INDEX idx_plg_automations_provider_templates_lookup ON public.plg_automations_provider_templates USING btree (event_key, channel, provider, status);
|
|
130
|
+
|
|
131
|
+
CREATE INDEX plg_automations_templates_tenant_idx ON public.plg_automations_templates USING btree (tenant_id);
|
|
132
|
+
|
|
133
|
+
CREATE UNIQUE INDEX plg_automations_templates_unique_idx ON public.plg_automations_templates USING btree (tenant_id, event_key, channel);
|
|
134
|
+
|
|
135
|
+
CREATE UNIQUE INDEX uq_plg_automations_provider_templates_owner ON public.plg_automations_provider_templates USING btree (COALESCE(tenant_id, '00000000-0000-0000-0000-000000000000'::uuid), event_key, channel, provider);
|
|
136
|
+
|
|
137
|
+
CREATE UNIQUE INDEX uq_plg_automations_provider_templates_provider_id ON public.plg_automations_provider_templates USING btree (provider, provider_template_id) WHERE (provider_template_id IS NOT NULL);
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
-- \u2500\u2500 fk constraint \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
141
|
+
|
|
142
|
+
ALTER TABLE ONLY public.plg_automations_provider_templates
|
|
143
|
+
ADD CONSTRAINT plg_automations_provider_templates_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
-- \u2500\u2500 trigger \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
147
|
+
|
|
148
|
+
CREATE TRIGGER plg_automations_provider_templates_set_updated_at BEFORE UPDATE ON public.plg_automations_provider_templates FOR EACH ROW EXECUTE FUNCTION public.automations_set_updated_at();
|
|
149
|
+
|
|
150
|
+
CREATE TRIGGER plg_automations_templates_set_updated_at BEFORE UPDATE ON public.plg_automations_templates FOR EACH ROW EXECUTE FUNCTION public.automations_set_updated_at();
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
-- \u2500\u2500 row security \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
154
|
+
|
|
155
|
+
ALTER TABLE public.plg_automations_provider_templates ENABLE ROW LEVEL SECURITY;
|
|
156
|
+
|
|
157
|
+
ALTER TABLE public.plg_automations_templates ENABLE ROW LEVEL SECURITY;
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
-- \u2500\u2500 policy \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
161
|
+
|
|
162
|
+
CREATE POLICY plg_automations_provider_templates_select ON public.plg_automations_provider_templates FOR SELECT TO authenticated USING (((tenant_id IS NULL) OR (tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids))));
|
|
163
|
+
|
|
164
|
+
CREATE POLICY plg_automations_templates_member_all ON public.plg_automations_templates TO authenticated USING ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids))) WITH CHECK ((tenant_id IN ( SELECT public.user_tenant_ids() AS user_tenant_ids)));
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
-- \u2500\u2500 acl \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
168
|
+
|
|
169
|
+
GRANT ALL ON FUNCTION public.automations_set_updated_at() TO authenticated;
|
|
170
|
+
GRANT ALL ON FUNCTION public.automations_set_updated_at() TO service_role;
|
|
171
|
+
|
|
172
|
+
GRANT MAINTAIN ON TABLE public.plg_automations_provider_templates TO anon;
|
|
173
|
+
GRANT ALL ON TABLE public.plg_automations_provider_templates TO authenticated;
|
|
174
|
+
GRANT ALL ON TABLE public.plg_automations_provider_templates TO service_role;
|
|
175
|
+
|
|
176
|
+
GRANT MAINTAIN ON TABLE public.plg_automations_templates TO anon;
|
|
177
|
+
GRANT ALL ON TABLE public.plg_automations_templates TO authenticated;
|
|
178
|
+
GRANT ALL ON TABLE public.plg_automations_templates TO service_role;
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
-- \u2500\u2500 comment \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
182
|
+
|
|
183
|
+
COMMENT ON COLUMN public.plg_automations_templates.actions IS 'Channel-neutral message actions: [{id: confirm|cancel|pay, label, kind: reply|payment}]. NULL = the catalog default; [] = deliberately no buttons.';
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
-- \u2500\u2500 default privileges: restore \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
187
|
+
-- Put back, so the NEXT thing installed inherits what the chain had.
|
|
188
|
+
|
|
189
|
+
DO $dp$ BEGIN
|
|
190
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres$stmt$;
|
|
191
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
192
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres$stmt$;
|
|
193
|
+
END $dp$;
|
|
194
|
+
|
|
195
|
+
DO $dp$ BEGIN
|
|
196
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO anon$stmt$;
|
|
197
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
198
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO anon$stmt$;
|
|
199
|
+
END $dp$;
|
|
200
|
+
|
|
201
|
+
DO $dp$ BEGIN
|
|
202
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO authenticated$stmt$;
|
|
203
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
204
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO authenticated$stmt$;
|
|
205
|
+
END $dp$;
|
|
206
|
+
|
|
207
|
+
DO $dp$ BEGIN
|
|
208
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role$stmt$;
|
|
209
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
210
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role$stmt$;
|
|
211
|
+
END $dp$;
|
|
212
|
+
|
|
213
|
+
DO $dp$ BEGIN
|
|
214
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres$stmt$;
|
|
215
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
216
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO postgres$stmt$;
|
|
217
|
+
END $dp$;
|
|
218
|
+
|
|
219
|
+
DO $dp$ BEGIN
|
|
220
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO anon$stmt$;
|
|
221
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
222
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO anon$stmt$;
|
|
223
|
+
END $dp$;
|
|
224
|
+
|
|
225
|
+
DO $dp$ BEGIN
|
|
226
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO authenticated$stmt$;
|
|
227
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
228
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO authenticated$stmt$;
|
|
229
|
+
END $dp$;
|
|
230
|
+
|
|
231
|
+
DO $dp$ BEGIN
|
|
232
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role$stmt$;
|
|
233
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
234
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON SEQUENCES TO service_role$stmt$;
|
|
235
|
+
END $dp$;
|
|
236
|
+
|
|
237
|
+
DO $dp$ BEGIN
|
|
238
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO postgres$stmt$;
|
|
239
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
240
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO postgres$stmt$;
|
|
241
|
+
END $dp$;
|
|
242
|
+
|
|
243
|
+
DO $dp$ BEGIN
|
|
244
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO authenticated$stmt$;
|
|
245
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
246
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO authenticated$stmt$;
|
|
247
|
+
END $dp$;
|
|
248
|
+
|
|
249
|
+
DO $dp$ BEGIN
|
|
250
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role$stmt$;
|
|
251
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
252
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role$stmt$;
|
|
253
|
+
END $dp$;
|
|
254
|
+
|
|
255
|
+
DO $dp$ BEGIN
|
|
256
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO postgres$stmt$;
|
|
257
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
258
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO postgres$stmt$;
|
|
259
|
+
END $dp$;
|
|
260
|
+
|
|
261
|
+
DO $dp$ BEGIN
|
|
262
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO anon$stmt$;
|
|
263
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
264
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO anon$stmt$;
|
|
265
|
+
END $dp$;
|
|
266
|
+
|
|
267
|
+
DO $dp$ BEGIN
|
|
268
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO authenticated$stmt$;
|
|
269
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
270
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO authenticated$stmt$;
|
|
271
|
+
END $dp$;
|
|
272
|
+
|
|
273
|
+
DO $dp$ BEGIN
|
|
274
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role$stmt$;
|
|
275
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
276
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON FUNCTIONS TO service_role$stmt$;
|
|
277
|
+
END $dp$;
|
|
278
|
+
|
|
279
|
+
DO $dp$ BEGIN
|
|
280
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO postgres$stmt$;
|
|
281
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
282
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO postgres$stmt$;
|
|
283
|
+
END $dp$;
|
|
284
|
+
|
|
285
|
+
DO $dp$ BEGIN
|
|
286
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT MAINTAIN ON TABLES TO anon$stmt$;
|
|
287
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
288
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT MAINTAIN ON TABLES TO anon$stmt$;
|
|
289
|
+
END $dp$;
|
|
290
|
+
|
|
291
|
+
DO $dp$ BEGIN
|
|
292
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO authenticated$stmt$;
|
|
293
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
294
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO authenticated$stmt$;
|
|
295
|
+
END $dp$;
|
|
296
|
+
|
|
297
|
+
DO $dp$ BEGIN
|
|
298
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO service_role$stmt$;
|
|
299
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
300
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA public GRANT ALL ON TABLES TO service_role$stmt$;
|
|
301
|
+
END $dp$;
|
|
302
|
+
|
|
303
|
+
DO $dp$ BEGIN
|
|
304
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO postgres$stmt$;
|
|
305
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
306
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO postgres$stmt$;
|
|
307
|
+
END $dp$;
|
|
308
|
+
|
|
309
|
+
DO $dp$ BEGIN
|
|
310
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO anon$stmt$;
|
|
311
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
312
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO anon$stmt$;
|
|
313
|
+
END $dp$;
|
|
314
|
+
|
|
315
|
+
DO $dp$ BEGIN
|
|
316
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO authenticated$stmt$;
|
|
317
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
318
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO authenticated$stmt$;
|
|
319
|
+
END $dp$;
|
|
320
|
+
|
|
321
|
+
DO $dp$ BEGIN
|
|
322
|
+
EXECUTE $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO service_role$stmt$;
|
|
323
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
324
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA public GRANT ALL ON TABLES TO service_role$stmt$;
|
|
325
|
+
END $dp$;
|
|
326
|
+
|
|
327
|
+
DO $dp$ BEGIN
|
|
328
|
+
EXECUTE $stmt$--
|
|
329
|
+
-- PostgreSQL database dump complete
|
|
330
|
+
--$stmt$;
|
|
331
|
+
EXCEPTION WHEN insufficient_privilege OR undefined_object THEN
|
|
332
|
+
RAISE NOTICE 'baseline: not ours to set \u2014 %', $stmt$--
|
|
333
|
+
-- PostgreSQL database dump complete
|
|
334
|
+
--$stmt$;
|
|
335
|
+
END $dp$;
|
|
336
|
+
`;
|
|
337
|
+
var MIGRATION_001_THE_FLOW_HAS_STEPS = `-- 001_the_flow_has_steps.sql
|
|
338
|
+
--
|
|
339
|
+
-- A CADENCE: the same lead touched on day 0, day 2 and day 5, by itself.
|
|
340
|
+
--
|
|
341
|
+
-- This plugin's README has promised "multi-step sequences, and enrollment
|
|
342
|
+
-- stats" since M1, and \`AutomationsList.tsx\` says plainly what shipped instead:
|
|
343
|
+
-- "um fluxo de um passo \xE9 o texto com o gatilho escrito na frente". One row per
|
|
344
|
+
-- (event, channel). Nothing waits, nothing remembers where a person got to, and
|
|
345
|
+
-- nothing stops when they reply.
|
|
346
|
+
--
|
|
347
|
+
-- WHAT THIS DOES NOT BUILD, BECAUSE IT ALREADY EXISTS.
|
|
348
|
+
--
|
|
349
|
+
-- * The clock. \`fayz_event_runner\` is a pg_cron job that already calls
|
|
350
|
+
-- \`event_runner_tick(200)\` every minute. This adds a fifth call to it
|
|
351
|
+
-- (packages/db 046), guarded like the other four.
|
|
352
|
+
-- * The delay. \`notifications_enqueue(..., p_scheduled_for, p_dedupe_key)\`
|
|
353
|
+
-- already means "send this at time T, and not twice". A cadence step is a
|
|
354
|
+
-- row in that queue with a computed \`scheduled_for\`.
|
|
355
|
+
-- * Consent, channel preference and quiet-channel fallback \u2014 all inside
|
|
356
|
+
-- \`notifications_resolve_channel\`, which plugin-notifications 002 just
|
|
357
|
+
-- taught to read the opt-out correctly. A cadence that dodged the queue
|
|
358
|
+
-- would dodge consent with it, which is the whole reason it does not.
|
|
359
|
+
-- * The claim-and-retry shape. \`marketing_run_due\` / \`marketing_campaign_run\`
|
|
360
|
+
-- already solve "advance a set of enrolled people on a tick" with
|
|
361
|
+
-- \`FOR UPDATE SKIP LOCKED\`. \`automations_flows_run_due\` is that function
|
|
362
|
+
-- with a step pointer.
|
|
363
|
+
--
|
|
364
|
+
-- THE THREE DECISIONS WORTH ARGUING WITH.
|
|
365
|
+
--
|
|
366
|
+
-- 1. THE DELAY IS AN OFFSET FROM THE PREVIOUS STEP, NOT FROM ENROLMENT.
|
|
367
|
+
-- "Two days after the call" is how a salesperson describes a cadence, and it
|
|
368
|
+
-- survives inserting a step in the middle. Absolute offsets from day 0 mean
|
|
369
|
+
-- every later step silently shifts when you add one.
|
|
370
|
+
--
|
|
371
|
+
-- 2. THE ENROLMENT CARRIES THE STEP POINTER, AND THE DEDUPE KEY CARRIES THE
|
|
372
|
+
-- POSITION. \`flow:<enrolment>:<position>\` means replaying a tick, or two
|
|
373
|
+
-- workers taking the same row, cannot send a step twice \u2014
|
|
374
|
+
-- \`notifications_enqueue\` refuses a repeated \`dedupe_key\` before it writes
|
|
375
|
+
-- anything. The SKIP LOCKED claim is the first defence; this is the one that
|
|
376
|
+
-- survives a crash between the enqueue and the pointer update.
|
|
377
|
+
--
|
|
378
|
+
-- 3. A FLOW IS BORN INACTIVE. \`is_active\` defaults false. A cadence that starts
|
|
379
|
+
-- sending the moment somebody saves a draft is a cadence that sends a draft.
|
|
380
|
+
--
|
|
381
|
+
-- WHAT IS STILL MISSING AFTER THIS, AND IS NOT PRETENDED OTHERWISE: e-mail and
|
|
382
|
+
-- SMS still resolve to \`notifications_send_to_log\`, which records and sends
|
|
383
|
+
-- nothing. A cadence on those channels will enqueue, drain, and deliver to a
|
|
384
|
+
-- log row. WhatsApp reaches a provider; in-app reaches the bell.
|
|
385
|
+
|
|
386
|
+
SET check_function_bodies = false;
|
|
387
|
+
|
|
388
|
+
-- \u2500\u2500 tables \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
389
|
+
|
|
390
|
+
CREATE TABLE IF NOT EXISTS public.plg_automations_flows (
|
|
391
|
+
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
392
|
+
tenant_id uuid NOT NULL,
|
|
393
|
+
unit_id uuid,
|
|
394
|
+
name text NOT NULL,
|
|
395
|
+
description text,
|
|
396
|
+
-- The fact that puts somebody in. Names an event from the aggregated plugin
|
|
397
|
+
-- catalog (\`crm.lead_created\`, \`crm.stage_changed\`, \u2026); it is text and not a
|
|
398
|
+
-- foreign key because the catalog is assembled at runtime from whichever
|
|
399
|
+
-- plugins an app mounts.
|
|
400
|
+
entry_event text NOT NULL,
|
|
401
|
+
-- The facts that take somebody out. Empty means the flow only ends by
|
|
402
|
+
-- running out of steps.
|
|
403
|
+
exit_events text[] DEFAULT '{}'::text[] NOT NULL,
|
|
404
|
+
is_active boolean DEFAULT false NOT NULL,
|
|
405
|
+
owner_id uuid,
|
|
406
|
+
created_at timestamptz DEFAULT now() NOT NULL,
|
|
407
|
+
updated_at timestamptz DEFAULT now() NOT NULL,
|
|
408
|
+
CONSTRAINT plg_automations_flows_pkey PRIMARY KEY (id)
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
CREATE TABLE IF NOT EXISTS public.plg_automations_flow_steps (
|
|
412
|
+
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
413
|
+
tenant_id uuid NOT NULL,
|
|
414
|
+
flow_id uuid NOT NULL,
|
|
415
|
+
-- Sparse on purpose (10, 20, 30\u2026): inserting between two steps should not
|
|
416
|
+
-- renumber the ones after it, and the enrolment's pointer names a position.
|
|
417
|
+
position integer NOT NULL,
|
|
418
|
+
-- Minutes to wait AFTER THE PREVIOUS STEP. The first step's delay is measured
|
|
419
|
+
-- from enrolment, so 0 means "right away".
|
|
420
|
+
delay_minutes integer DEFAULT 0 NOT NULL,
|
|
421
|
+
kind text DEFAULT 'message' NOT NULL,
|
|
422
|
+
-- kind='message': which template, and optionally force a channel. NULL
|
|
423
|
+
-- channel lets notifications_resolve_channel pick, which is what honours the
|
|
424
|
+
-- recipient's preference.
|
|
425
|
+
template_key text,
|
|
426
|
+
channel text,
|
|
427
|
+
-- kind='task': what the human is asked to do, and by when.
|
|
428
|
+
task_title text,
|
|
429
|
+
task_due_days integer,
|
|
430
|
+
created_at timestamptz DEFAULT now() NOT NULL,
|
|
431
|
+
updated_at timestamptz DEFAULT now() NOT NULL,
|
|
432
|
+
CONSTRAINT plg_automations_flow_steps_pkey PRIMARY KEY (id),
|
|
433
|
+
CONSTRAINT plg_automations_flow_steps_kind_check
|
|
434
|
+
CHECK (kind = ANY (ARRAY['message'::text, 'task'::text])),
|
|
435
|
+
CONSTRAINT plg_automations_flow_steps_delay_check CHECK (delay_minutes >= 0),
|
|
436
|
+
-- A message step with no template has nothing to send; a task step with no
|
|
437
|
+
-- title gives the human nothing to read. Either is a step that would fail at
|
|
438
|
+
-- 3am rather than at save time.
|
|
439
|
+
CONSTRAINT plg_automations_flow_steps_payload_check CHECK (
|
|
440
|
+
(kind = 'message' AND template_key IS NOT NULL)
|
|
441
|
+
OR (kind = 'task' AND task_title IS NOT NULL)
|
|
442
|
+
)
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
CREATE TABLE IF NOT EXISTS public.plg_automations_enrollments (
|
|
446
|
+
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
|
447
|
+
tenant_id uuid NOT NULL,
|
|
448
|
+
flow_id uuid NOT NULL,
|
|
449
|
+
person_id uuid NOT NULL,
|
|
450
|
+
-- What put them here, for the timeline: ('lead', <id>) or ('deal', <id>).
|
|
451
|
+
subject_type text,
|
|
452
|
+
subject_id uuid,
|
|
453
|
+
status text DEFAULT 'active' NOT NULL,
|
|
454
|
+
-- The position of the step that runs NEXT. NULL once nothing is left.
|
|
455
|
+
current_position integer,
|
|
456
|
+
next_run_at timestamptz DEFAULT now() NOT NULL,
|
|
457
|
+
entered_at timestamptz DEFAULT now() NOT NULL,
|
|
458
|
+
exited_at timestamptz,
|
|
459
|
+
exit_reason text,
|
|
460
|
+
created_at timestamptz DEFAULT now() NOT NULL,
|
|
461
|
+
updated_at timestamptz DEFAULT now() NOT NULL,
|
|
462
|
+
CONSTRAINT plg_automations_enrollments_pkey PRIMARY KEY (id),
|
|
463
|
+
CONSTRAINT plg_automations_enrollments_status_check
|
|
464
|
+
CHECK (status = ANY (ARRAY['active'::text, 'completed'::text, 'exited'::text]))
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
-- \u2500\u2500 constraint \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
468
|
+
|
|
469
|
+
ALTER TABLE public.plg_automations_flows
|
|
470
|
+
DROP CONSTRAINT IF EXISTS plg_automations_flows_tenant_fkey;
|
|
471
|
+
ALTER TABLE public.plg_automations_flows
|
|
472
|
+
ADD CONSTRAINT plg_automations_flows_tenant_fkey
|
|
473
|
+
FOREIGN KEY (tenant_id) REFERENCES public.tenants(id) ON DELETE CASCADE;
|
|
474
|
+
|
|
475
|
+
ALTER TABLE public.plg_automations_flow_steps
|
|
476
|
+
DROP CONSTRAINT IF EXISTS plg_automations_flow_steps_flow_fkey;
|
|
477
|
+
ALTER TABLE public.plg_automations_flow_steps
|
|
478
|
+
ADD CONSTRAINT plg_automations_flow_steps_flow_fkey
|
|
479
|
+
FOREIGN KEY (flow_id) REFERENCES public.plg_automations_flows(id) ON DELETE CASCADE;
|
|
480
|
+
|
|
481
|
+
ALTER TABLE public.plg_automations_enrollments
|
|
482
|
+
DROP CONSTRAINT IF EXISTS plg_automations_enrollments_flow_fkey;
|
|
483
|
+
ALTER TABLE public.plg_automations_enrollments
|
|
484
|
+
ADD CONSTRAINT plg_automations_enrollments_flow_fkey
|
|
485
|
+
FOREIGN KEY (flow_id) REFERENCES public.plg_automations_flows(id) ON DELETE CASCADE;
|
|
486
|
+
|
|
487
|
+
-- \u2500\u2500 index \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
488
|
+
|
|
489
|
+
CREATE UNIQUE INDEX IF NOT EXISTS plg_automations_flow_steps_position_uq
|
|
490
|
+
ON public.plg_automations_flow_steps (flow_id, position);
|
|
491
|
+
CREATE INDEX IF NOT EXISTS plg_automations_flows_tenant_idx
|
|
492
|
+
ON public.plg_automations_flows (tenant_id);
|
|
493
|
+
CREATE INDEX IF NOT EXISTS plg_automations_flows_entry_idx
|
|
494
|
+
ON public.plg_automations_flows (tenant_id, entry_event) WHERE is_active;
|
|
495
|
+
|
|
496
|
+
-- One live enrolment per person per flow. Re-entering a cadence somebody is
|
|
497
|
+
-- already in would double every remaining message.
|
|
498
|
+
CREATE UNIQUE INDEX IF NOT EXISTS plg_automations_enrollments_live_uq
|
|
499
|
+
ON public.plg_automations_enrollments (flow_id, person_id) WHERE status = 'active';
|
|
500
|
+
-- The claim index: the runner only ever asks for due, active rows.
|
|
501
|
+
CREATE INDEX IF NOT EXISTS plg_automations_enrollments_due_idx
|
|
502
|
+
ON public.plg_automations_enrollments (next_run_at) WHERE status = 'active';
|
|
503
|
+
CREATE INDEX IF NOT EXISTS plg_automations_enrollments_person_idx
|
|
504
|
+
ON public.plg_automations_enrollments (tenant_id, person_id);
|
|
505
|
+
|
|
506
|
+
-- \u2500\u2500 row security \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
507
|
+
|
|
508
|
+
-- ENABLE **and** FORCE. Without FORCE the table owner \u2014 which is the role that
|
|
509
|
+
-- runs migrations and every SECURITY DEFINER function here \u2014 bypasses these
|
|
510
|
+
-- policies entirely, and \`packages/db/test/pgtap/900_verification/001_posture.sql\`
|
|
511
|
+
-- (N14b) fails the whole pool for it.
|
|
512
|
+
ALTER TABLE public.plg_automations_flows ENABLE ROW LEVEL SECURITY;
|
|
513
|
+
ALTER TABLE public.plg_automations_flows FORCE ROW LEVEL SECURITY;
|
|
514
|
+
ALTER TABLE public.plg_automations_flow_steps ENABLE ROW LEVEL SECURITY;
|
|
515
|
+
ALTER TABLE public.plg_automations_flow_steps FORCE ROW LEVEL SECURITY;
|
|
516
|
+
ALTER TABLE public.plg_automations_enrollments ENABLE ROW LEVEL SECURITY;
|
|
517
|
+
ALTER TABLE public.plg_automations_enrollments FORCE ROW LEVEL SECURITY;
|
|
518
|
+
|
|
519
|
+
DROP POLICY IF EXISTS plg_automations_flows_authz_select ON public.plg_automations_flows;
|
|
520
|
+
CREATE POLICY plg_automations_flows_authz_select ON public.plg_automations_flows
|
|
521
|
+
FOR SELECT TO authenticated USING (
|
|
522
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
523
|
+
AND (SELECT app.has_permission('automations.read', NULL::uuid)));
|
|
524
|
+
|
|
525
|
+
DROP POLICY IF EXISTS plg_automations_flows_authz_insert ON public.plg_automations_flows;
|
|
526
|
+
CREATE POLICY plg_automations_flows_authz_insert ON public.plg_automations_flows
|
|
527
|
+
FOR INSERT TO authenticated WITH CHECK (
|
|
528
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
529
|
+
AND (SELECT app.has_permission('automations.edit', NULL::uuid)));
|
|
530
|
+
|
|
531
|
+
DROP POLICY IF EXISTS plg_automations_flows_authz_update ON public.plg_automations_flows;
|
|
532
|
+
CREATE POLICY plg_automations_flows_authz_update ON public.plg_automations_flows
|
|
533
|
+
FOR UPDATE TO authenticated USING (
|
|
534
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
535
|
+
AND (SELECT app.has_permission('automations.edit', NULL::uuid)))
|
|
536
|
+
WITH CHECK (
|
|
537
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
538
|
+
AND (SELECT app.has_permission('automations.edit', NULL::uuid)));
|
|
539
|
+
|
|
540
|
+
DROP POLICY IF EXISTS plg_automations_flows_authz_delete ON public.plg_automations_flows;
|
|
541
|
+
CREATE POLICY plg_automations_flows_authz_delete ON public.plg_automations_flows
|
|
542
|
+
FOR DELETE TO authenticated USING (
|
|
543
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
544
|
+
AND (SELECT app.has_permission('automations.edit', NULL::uuid)));
|
|
545
|
+
|
|
546
|
+
DROP POLICY IF EXISTS plg_automations_flow_steps_authz_select ON public.plg_automations_flow_steps;
|
|
547
|
+
CREATE POLICY plg_automations_flow_steps_authz_select ON public.plg_automations_flow_steps
|
|
548
|
+
FOR SELECT TO authenticated USING (
|
|
549
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
550
|
+
AND (SELECT app.has_permission('automations.read', NULL::uuid)));
|
|
551
|
+
|
|
552
|
+
DROP POLICY IF EXISTS plg_automations_flow_steps_authz_insert ON public.plg_automations_flow_steps;
|
|
553
|
+
-- The parent flow is checked, not just the stamped tenant. A tenant-only
|
|
554
|
+
-- WITH CHECK lets a caller stamp their OWN tenant_id on a step and hang it off
|
|
555
|
+
-- somebody else's flow \u2014 and the engine looks steps up by flow_id alone, so the
|
|
556
|
+
-- injected step runs in that tenant's cadence.
|
|
557
|
+
CREATE POLICY plg_automations_flow_steps_authz_insert ON public.plg_automations_flow_steps
|
|
558
|
+
FOR INSERT TO authenticated WITH CHECK (
|
|
559
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
560
|
+
AND (SELECT app.has_permission('automations.edit', NULL::uuid))
|
|
561
|
+
AND EXISTS (SELECT 1 FROM public.plg_automations_flows f
|
|
562
|
+
WHERE f.id = flow_id AND f.tenant_id = (SELECT app.current_tenant_id())));
|
|
563
|
+
|
|
564
|
+
DROP POLICY IF EXISTS plg_automations_flow_steps_authz_update ON public.plg_automations_flow_steps;
|
|
565
|
+
CREATE POLICY plg_automations_flow_steps_authz_update ON public.plg_automations_flow_steps
|
|
566
|
+
FOR UPDATE TO authenticated USING (
|
|
567
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
568
|
+
AND (SELECT app.has_permission('automations.edit', NULL::uuid)))
|
|
569
|
+
WITH CHECK (
|
|
570
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
571
|
+
AND (SELECT app.has_permission('automations.edit', NULL::uuid)));
|
|
572
|
+
|
|
573
|
+
DROP POLICY IF EXISTS plg_automations_flow_steps_authz_delete ON public.plg_automations_flow_steps;
|
|
574
|
+
CREATE POLICY plg_automations_flow_steps_authz_delete ON public.plg_automations_flow_steps
|
|
575
|
+
FOR DELETE TO authenticated USING (
|
|
576
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
577
|
+
AND (SELECT app.has_permission('automations.edit', NULL::uuid)));
|
|
578
|
+
|
|
579
|
+
-- Enrolments are READ-ONLY to the operator. They are written by the engine, and
|
|
580
|
+
-- a hand-edited step pointer is a cadence nobody can explain afterwards.
|
|
581
|
+
-- Leaving a flow is \`automations_flow_exit\`, not an UPDATE.
|
|
582
|
+
DROP POLICY IF EXISTS plg_automations_enrollments_authz_select ON public.plg_automations_enrollments;
|
|
583
|
+
CREATE POLICY plg_automations_enrollments_authz_select ON public.plg_automations_enrollments
|
|
584
|
+
FOR SELECT TO authenticated USING (
|
|
585
|
+
tenant_id = (SELECT app.current_tenant_id())
|
|
586
|
+
AND (SELECT app.has_permission('automations.read', NULL::uuid)));
|
|
587
|
+
|
|
588
|
+
GRANT SELECT, INSERT, UPDATE, DELETE ON public.plg_automations_flows TO authenticated;
|
|
589
|
+
GRANT ALL ON public.plg_automations_flows TO service_role;
|
|
590
|
+
GRANT SELECT, INSERT, UPDATE, DELETE ON public.plg_automations_flow_steps TO authenticated;
|
|
591
|
+
GRANT ALL ON public.plg_automations_flow_steps TO service_role;
|
|
592
|
+
GRANT SELECT ON public.plg_automations_enrollments TO authenticated;
|
|
593
|
+
GRANT ALL ON public.plg_automations_enrollments TO service_role;
|
|
594
|
+
|
|
595
|
+
INSERT INTO app.scaffold_registry
|
|
596
|
+
(table_schema, table_name, resource_type, plugin, owner_scoping, mode, previous_policies, audit_hook, scope, distributable)
|
|
597
|
+
VALUES
|
|
598
|
+
('public', 'plg_automations_flows', 'automations.flow', 'automations', false, 'enforce', ARRAY[]::text[], false, 'unit_optional', false),
|
|
599
|
+
('public', 'plg_automations_flow_steps', 'automations.flow_step', 'automations', false, 'enforce', ARRAY[]::text[], false, 'tenant', false),
|
|
600
|
+
('public', 'plg_automations_enrollments', 'automations.enrollment', 'automations', false, 'enforce', ARRAY[]::text[], false, 'tenant', false)
|
|
601
|
+
ON CONFLICT DO NOTHING;
|
|
602
|
+
|
|
603
|
+
-- \u2500\u2500 function \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
604
|
+
|
|
605
|
+
-- Put somebody into a flow. Returns the enrolment, or NULL when the flow is
|
|
606
|
+
-- inactive, has no steps, or the person is already in it.
|
|
607
|
+
CREATE OR REPLACE FUNCTION public.automations_flow_enroll(
|
|
608
|
+
p_tenant_id uuid, p_flow_id uuid, p_person_id uuid,
|
|
609
|
+
p_subject_type text DEFAULT NULL, p_subject_id uuid DEFAULT NULL
|
|
610
|
+
) RETURNS uuid
|
|
611
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
612
|
+
SET search_path TO ''
|
|
613
|
+
AS $$
|
|
614
|
+
DECLARE v_first record; v_id uuid;
|
|
615
|
+
BEGIN
|
|
616
|
+
SELECT s.position, s.delay_minutes INTO v_first
|
|
617
|
+
FROM public.plg_automations_flow_steps s
|
|
618
|
+
JOIN public.plg_automations_flows f ON f.id = s.flow_id
|
|
619
|
+
WHERE s.flow_id = p_flow_id AND f.tenant_id = p_tenant_id AND f.is_active
|
|
620
|
+
ORDER BY s.position
|
|
621
|
+
LIMIT 1;
|
|
622
|
+
|
|
623
|
+
-- An inactive flow, or one with no steps, enrols nobody. Silently: this is
|
|
624
|
+
-- called from an event handler, and raising would fail the whole delivery.
|
|
625
|
+
IF v_first.position IS NULL THEN RETURN NULL; END IF;
|
|
626
|
+
|
|
627
|
+
INSERT INTO public.plg_automations_enrollments
|
|
628
|
+
(tenant_id, flow_id, person_id, subject_type, subject_id, current_position, next_run_at)
|
|
629
|
+
VALUES
|
|
630
|
+
(p_tenant_id, p_flow_id, p_person_id, p_subject_type, p_subject_id,
|
|
631
|
+
v_first.position, now() + make_interval(mins => v_first.delay_minutes))
|
|
632
|
+
ON CONFLICT DO NOTHING
|
|
633
|
+
RETURNING id INTO v_id;
|
|
634
|
+
|
|
635
|
+
RETURN v_id;
|
|
636
|
+
END $$;
|
|
637
|
+
|
|
638
|
+
-- Take somebody out of every flow they are in. The reason is kept, because
|
|
639
|
+
-- "left because they replied" and "left because the deal was lost" are the two
|
|
640
|
+
-- halves of whether a cadence works.
|
|
641
|
+
CREATE OR REPLACE FUNCTION public.automations_flow_exit(
|
|
642
|
+
p_tenant_id uuid, p_person_id uuid, p_reason text, p_flow_ids uuid[] DEFAULT NULL
|
|
643
|
+
) RETURNS integer
|
|
644
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
645
|
+
SET search_path TO ''
|
|
646
|
+
AS $$
|
|
647
|
+
DECLARE v_n integer; v_gone uuid[];
|
|
648
|
+
BEGIN
|
|
649
|
+
-- SCOPED TO THE FLOWS THAT ASKED. \`p_flow_ids\` NULL still means "every flow",
|
|
650
|
+
-- which is what a manual "take this person out" wants; the event handler
|
|
651
|
+
-- passes the flows whose own \`exit_events\` name the fact.
|
|
652
|
+
--
|
|
653
|
+
-- Without the scope, one flow declaring \`crm.deal_won\` pulled the person out
|
|
654
|
+
-- of EVERY flow they were in and cancelled every queued step \u2014 including a
|
|
655
|
+
-- flow that declared no exits at all, whose comment says it "only ends by
|
|
656
|
+
-- running out of steps". Flow-level exit_events were meaningless the moment a
|
|
657
|
+
-- second flow existed.
|
|
658
|
+
WITH gone AS (
|
|
659
|
+
UPDATE public.plg_automations_enrollments
|
|
660
|
+
SET status = 'exited', exited_at = now(), exit_reason = p_reason,
|
|
661
|
+
current_position = NULL, updated_at = now()
|
|
662
|
+
WHERE tenant_id = p_tenant_id AND person_id = p_person_id AND status = 'active'
|
|
663
|
+
AND (p_flow_ids IS NULL OR flow_id = ANY (p_flow_ids))
|
|
664
|
+
RETURNING id
|
|
665
|
+
)
|
|
666
|
+
SELECT count(*)::integer, array_agg(id) INTO v_n, v_gone FROM gone;
|
|
667
|
+
|
|
668
|
+
-- A step already in the queue but not yet sent must not go out after the
|
|
669
|
+
-- person left. \`cancelled\` was in the status CHECK from the beginning and
|
|
670
|
+
-- nothing ever wrote it; this is what it was for.
|
|
671
|
+
--
|
|
672
|
+
-- Keyed by the ENROLMENTS that just left, not by \`flow:%\` \u2014 the wide pattern
|
|
673
|
+
-- cancelled the pending steps of flows the person is still in.
|
|
674
|
+
IF v_n > 0 AND to_regclass('public.plg_notifications_queue') IS NOT NULL THEN
|
|
675
|
+
UPDATE public.plg_notifications_queue q
|
|
676
|
+
SET status = 'cancelled', last_error = 'left the flow: ' || p_reason
|
|
677
|
+
WHERE q.tenant_id = p_tenant_id
|
|
678
|
+
AND q.person_id = p_person_id
|
|
679
|
+
AND q.status = 'pending'
|
|
680
|
+
AND split_part(q.dedupe_key, ':', 1) = 'flow'
|
|
681
|
+
AND split_part(q.dedupe_key, ':', 2) = ANY (SELECT unnest(v_gone)::text);
|
|
682
|
+
END IF;
|
|
683
|
+
|
|
684
|
+
RETURN v_n;
|
|
685
|
+
END $$;
|
|
686
|
+
|
|
687
|
+
-- The tick. Claims due enrolments, runs ONE step each, moves the pointer.
|
|
688
|
+
CREATE OR REPLACE FUNCTION public.automations_flows_run_due(p_limit integer DEFAULT 50)
|
|
689
|
+
RETURNS jsonb
|
|
690
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
691
|
+
SET search_path TO ''
|
|
692
|
+
AS $$
|
|
693
|
+
DECLARE
|
|
694
|
+
e record; v_step record; v_next record; v_error text;
|
|
695
|
+
v_ran integer := 0; v_done integer := 0; v_failed integer := 0;
|
|
696
|
+
BEGIN
|
|
697
|
+
-- JOINED to the flow, and \`f.is_active\` checked here rather than only in
|
|
698
|
+
-- \`automations_flow_enroll\`. Switching a flow off used to stop new entrants
|
|
699
|
+
-- and nothing else: everybody already mid-cadence kept receiving messages
|
|
700
|
+
-- from a flow the operator had just turned off, which is the opposite of what
|
|
701
|
+
-- the switch promises.
|
|
702
|
+
FOR e IN
|
|
703
|
+
SELECT en.* FROM public.plg_automations_enrollments en
|
|
704
|
+
JOIN public.plg_automations_flows f ON f.id = en.flow_id AND f.is_active
|
|
705
|
+
WHERE en.status = 'active' AND en.next_run_at <= now()
|
|
706
|
+
ORDER BY en.next_run_at
|
|
707
|
+
LIMIT greatest(p_limit, 0)
|
|
708
|
+
FOR UPDATE OF en SKIP LOCKED
|
|
709
|
+
LOOP
|
|
710
|
+
SELECT * INTO v_step FROM public.plg_automations_flow_steps s
|
|
711
|
+
WHERE s.flow_id = e.flow_id AND s.position = e.current_position;
|
|
712
|
+
|
|
713
|
+
-- The step was deleted while somebody stood on it. Move on rather than
|
|
714
|
+
-- spinning on a pointer that will never resolve.
|
|
715
|
+
IF v_step.id IS NULL THEN
|
|
716
|
+
SELECT * INTO v_next FROM public.plg_automations_flow_steps s
|
|
717
|
+
WHERE s.flow_id = e.flow_id AND s.position > coalesce(e.current_position, -1)
|
|
718
|
+
ORDER BY s.position LIMIT 1;
|
|
719
|
+
ELSE
|
|
720
|
+
BEGIN
|
|
721
|
+
IF v_step.kind = 'message' THEN
|
|
722
|
+
PERFORM public.notifications_enqueue(
|
|
723
|
+
e.tenant_id, v_step.template_key, e.person_id,
|
|
724
|
+
jsonb_build_object('flow_id', e.flow_id, 'enrollment_id', e.id, 'step', v_step.position),
|
|
725
|
+
'flow:' || e.id::text || ':' || v_step.position::text,
|
|
726
|
+
now(),
|
|
727
|
+
v_step.channel
|
|
728
|
+
);
|
|
729
|
+
ELSIF v_step.kind = 'task' AND to_regclass('public.tasks') IS NOT NULL THEN
|
|
730
|
+
-- \`tasks_source_uniq (tenant_id, source, source_key)\` makes this
|
|
731
|
+
-- idempotent for free \u2014 the same step cannot raise two tasks.
|
|
732
|
+
INSERT INTO public.tasks
|
|
733
|
+
(tenant_id, title, description, status, due_date, source, source_key)
|
|
734
|
+
VALUES
|
|
735
|
+
(e.tenant_id, v_step.task_title,
|
|
736
|
+
'Automation flow step ' || v_step.position::text,
|
|
737
|
+
'todo',
|
|
738
|
+
CASE WHEN v_step.task_due_days IS NOT NULL
|
|
739
|
+
THEN (now() + make_interval(days => v_step.task_due_days))::date END,
|
|
740
|
+
'automation:flow', e.id::text || ':' || v_step.position::text)
|
|
741
|
+
ON CONFLICT DO NOTHING;
|
|
742
|
+
END IF;
|
|
743
|
+
v_ran := v_ran + 1;
|
|
744
|
+
EXCEPTION WHEN OTHERS THEN
|
|
745
|
+
-- One bad step must not stop the tick for everybody else, so the
|
|
746
|
+
-- enrolment still leaves this step: a template that no longer exists
|
|
747
|
+
-- would otherwise wedge the person forever.
|
|
748
|
+
--
|
|
749
|
+
-- But it does NOT read as a finished cadence. The first cut let the
|
|
750
|
+
-- loop fall through to \`completed\`, and because a failed step never
|
|
751
|
+
-- produces a queue row, \`v_automations_flow_steps\` \u2014 which counts queue
|
|
752
|
+
-- statuses \u2014 could not show it either. The screen built to answer
|
|
753
|
+
-- "where do people stop" reported the person as having finished.
|
|
754
|
+
v_failed := v_failed + 1;
|
|
755
|
+
v_error := SQLERRM;
|
|
756
|
+
RAISE NOTICE 'automations_flows_run_due: step % of enrolment % failed (%)',
|
|
757
|
+
v_step.position, e.id, SQLERRM;
|
|
758
|
+
END;
|
|
759
|
+
|
|
760
|
+
SELECT * INTO v_next FROM public.plg_automations_flow_steps s
|
|
761
|
+
WHERE s.flow_id = e.flow_id AND s.position > v_step.position
|
|
762
|
+
ORDER BY s.position LIMIT 1;
|
|
763
|
+
END IF;
|
|
764
|
+
|
|
765
|
+
IF v_next.id IS NULL THEN
|
|
766
|
+
UPDATE public.plg_automations_enrollments
|
|
767
|
+
SET status = 'completed', current_position = NULL,
|
|
768
|
+
exited_at = now(), updated_at = now(),
|
|
769
|
+
-- A cadence that ended on a step that could not run did not
|
|
770
|
+
-- finish; saying so is the difference between "nobody answers the
|
|
771
|
+
-- third message" and "the third message never left".
|
|
772
|
+
exit_reason = CASE WHEN v_error IS NOT NULL
|
|
773
|
+
THEN 'last step failed: ' || v_error END
|
|
774
|
+
WHERE id = e.id;
|
|
775
|
+
v_done := v_done + 1;
|
|
776
|
+
ELSE
|
|
777
|
+
UPDATE public.plg_automations_enrollments
|
|
778
|
+
SET current_position = v_next.position,
|
|
779
|
+
next_run_at = now() + make_interval(mins => v_next.delay_minutes),
|
|
780
|
+
updated_at = now()
|
|
781
|
+
WHERE id = e.id;
|
|
782
|
+
END IF;
|
|
783
|
+
|
|
784
|
+
v_next := NULL; v_step := NULL; v_error := NULL;
|
|
785
|
+
END LOOP;
|
|
786
|
+
|
|
787
|
+
RETURN jsonb_build_object('steps_ran', v_ran, 'completed', v_done, 'failed', v_failed);
|
|
788
|
+
END $$;
|
|
789
|
+
|
|
790
|
+
REVOKE ALL ON FUNCTION public.automations_flow_enroll(uuid, uuid, uuid, text, uuid) FROM PUBLIC;
|
|
791
|
+
GRANT EXECUTE ON FUNCTION public.automations_flow_enroll(uuid, uuid, uuid, text, uuid) TO service_role;
|
|
792
|
+
REVOKE ALL ON FUNCTION public.automations_flow_exit(uuid, uuid, text, uuid[]) FROM PUBLIC;
|
|
793
|
+
GRANT EXECUTE ON FUNCTION public.automations_flow_exit(uuid, uuid, text, uuid[]) TO authenticated;
|
|
794
|
+
GRANT EXECUTE ON FUNCTION public.automations_flow_exit(uuid, uuid, text, uuid[]) TO service_role;
|
|
795
|
+
REVOKE ALL ON FUNCTION public.automations_flows_run_due(integer) FROM PUBLIC;
|
|
796
|
+
GRANT EXECUTE ON FUNCTION public.automations_flows_run_due(integer) TO service_role;
|
|
797
|
+
|
|
798
|
+
-- \u2500\u2500 the two handlers the event log calls \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
799
|
+
|
|
800
|
+
-- Who the event is about.
|
|
801
|
+
--
|
|
802
|
+
-- \`consume_event_log\` calls a handler with \`to_jsonb(v_event)\` \u2014 the whole
|
|
803
|
+
-- \`event_log\` ROW. So \`tenant_id\`, \`event_key\`, \`subject_type\` and \`subject_id\`
|
|
804
|
+
-- are at the top level and everything the emitter chose to send is nested under
|
|
805
|
+
-- \`payload\`. The first cut read \`person_id\` at the top level, where it never is:
|
|
806
|
+
-- every delivery answered "payload carries no tenant, person or event", was
|
|
807
|
+
-- marked DELIVERED, and the cadence enrolled nobody, ever, with nothing
|
|
808
|
+
-- reporting it.
|
|
809
|
+
--
|
|
810
|
+
-- \`subject_id\` first, because that is the convention the shipped handlers use
|
|
811
|
+
-- (\`plg_notifications_on_fulfillment_completed\` reads exactly that), and it is
|
|
812
|
+
-- \`text\` on the row. The payload fallbacks are for emitters that name the person
|
|
813
|
+
-- there instead.
|
|
814
|
+
CREATE OR REPLACE FUNCTION public.automations_event_person(p_event jsonb)
|
|
815
|
+
RETURNS uuid
|
|
816
|
+
LANGUAGE sql IMMUTABLE
|
|
817
|
+
SET search_path TO ''
|
|
818
|
+
AS $$
|
|
819
|
+
SELECT nullif(coalesce(
|
|
820
|
+
CASE WHEN p_event ->> 'subject_type' IN ('lead', 'person', 'contact')
|
|
821
|
+
THEN p_event ->> 'subject_id' END,
|
|
822
|
+
p_event -> 'payload' ->> 'person_id',
|
|
823
|
+
p_event -> 'payload' ->> 'lead_id',
|
|
824
|
+
p_event -> 'payload' ->> 'contact_id',
|
|
825
|
+
p_event ->> 'person_id',
|
|
826
|
+
p_event ->> 'lead_id'
|
|
827
|
+
), '')::uuid
|
|
828
|
+
$$;
|
|
829
|
+
|
|
830
|
+
REVOKE ALL ON FUNCTION public.automations_event_person(jsonb) FROM PUBLIC;
|
|
831
|
+
GRANT EXECUTE ON FUNCTION public.automations_event_person(jsonb) TO service_role;
|
|
832
|
+
|
|
833
|
+
-- Enrol on the flow's entry event. The payload names the person; a fact that
|
|
834
|
+
-- does not carry one cannot start a cadence.
|
|
835
|
+
CREATE OR REPLACE FUNCTION public.plg_automations_on_entry_event(p_event jsonb)
|
|
836
|
+
RETURNS jsonb
|
|
837
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
838
|
+
SET search_path TO ''
|
|
839
|
+
AS $$
|
|
840
|
+
DECLARE
|
|
841
|
+
v_tenant uuid := nullif(p_event ->> 'tenant_id', '')::uuid;
|
|
842
|
+
v_event text := p_event ->> 'event_key';
|
|
843
|
+
v_person uuid := public.automations_event_person(p_event);
|
|
844
|
+
f record; v_n integer := 0;
|
|
845
|
+
BEGIN
|
|
846
|
+
IF v_tenant IS NULL OR v_person IS NULL OR v_event IS NULL THEN
|
|
847
|
+
RETURN jsonb_build_object('enrolled', 0, 'reason', 'event carries no tenant, person or key');
|
|
848
|
+
END IF;
|
|
849
|
+
|
|
850
|
+
FOR f IN SELECT id FROM public.plg_automations_flows
|
|
851
|
+
WHERE tenant_id = v_tenant AND entry_event = v_event AND is_active
|
|
852
|
+
LOOP
|
|
853
|
+
IF public.automations_flow_enroll(
|
|
854
|
+
v_tenant, f.id, v_person,
|
|
855
|
+
p_event ->> 'subject_type', nullif(p_event ->> 'subject_id', '')::uuid) IS NOT NULL THEN
|
|
856
|
+
v_n := v_n + 1;
|
|
857
|
+
END IF;
|
|
858
|
+
END LOOP;
|
|
859
|
+
|
|
860
|
+
RETURN jsonb_build_object('enrolled', v_n);
|
|
861
|
+
END $$;
|
|
862
|
+
|
|
863
|
+
-- Leave the flows whose own \`exit_events\` name this fact.
|
|
864
|
+
CREATE OR REPLACE FUNCTION public.plg_automations_on_exit_event(p_event jsonb)
|
|
865
|
+
RETURNS jsonb
|
|
866
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
867
|
+
SET search_path TO ''
|
|
868
|
+
AS $$
|
|
869
|
+
DECLARE
|
|
870
|
+
v_tenant uuid := nullif(p_event ->> 'tenant_id', '')::uuid;
|
|
871
|
+
v_event text := p_event ->> 'event_key';
|
|
872
|
+
v_person uuid := public.automations_event_person(p_event);
|
|
873
|
+
v_flows uuid[]; v_n integer := 0;
|
|
874
|
+
BEGIN
|
|
875
|
+
IF v_tenant IS NULL OR v_person IS NULL OR v_event IS NULL THEN
|
|
876
|
+
RETURN jsonb_build_object('exited', 0, 'reason', 'event carries no tenant, person or key');
|
|
877
|
+
END IF;
|
|
878
|
+
|
|
879
|
+
SELECT array_agg(id) INTO v_flows
|
|
880
|
+
FROM public.plg_automations_flows
|
|
881
|
+
WHERE tenant_id = v_tenant AND is_active AND v_event = ANY (exit_events);
|
|
882
|
+
|
|
883
|
+
IF v_flows IS NOT NULL THEN
|
|
884
|
+
v_n := public.automations_flow_exit(v_tenant, v_person, v_event, v_flows);
|
|
885
|
+
END IF;
|
|
886
|
+
|
|
887
|
+
RETURN jsonb_build_object('exited', v_n);
|
|
888
|
+
END $$;
|
|
889
|
+
|
|
890
|
+
REVOKE ALL ON FUNCTION public.plg_automations_on_entry_event(jsonb) FROM PUBLIC;
|
|
891
|
+
GRANT EXECUTE ON FUNCTION public.plg_automations_on_entry_event(jsonb) TO service_role;
|
|
892
|
+
REVOKE ALL ON FUNCTION public.plg_automations_on_exit_event(jsonb) FROM PUBLIC;
|
|
893
|
+
GRANT EXECUTE ON FUNCTION public.plg_automations_on_exit_event(jsonb) TO service_role;
|
|
894
|
+
|
|
895
|
+
-- \u2500\u2500 subscription \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
896
|
+
|
|
897
|
+
-- The manifest DECLARES the reaction; this is what makes the event log call it.
|
|
898
|
+
-- Both handlers on every CRM fact, because which fact opens a cadence and which
|
|
899
|
+
-- closes it is the operator's choice: the handler looks the flow up and returns
|
|
900
|
+
-- immediately when no flow in that tenant names the event.
|
|
901
|
+
-- Written out one call per line rather than looped over an array. The loop was
|
|
902
|
+
-- shorter and \`check-event-contract\` could not read it: its registration parser
|
|
903
|
+
-- matches literal arguments, so a FOREACH registering fourteen subscriptions
|
|
904
|
+
-- looked like zero. Same lesson as the manifest side \u2014 the gate reads source,
|
|
905
|
+
-- so the source has to say it.
|
|
906
|
+
SELECT public.register_event_subscription('automations', 'crm.lead_created', 'plg_automations_on_entry_event',
|
|
907
|
+
'Enrol into flows whose entry event is crm.lead_created', 5);
|
|
908
|
+
SELECT public.register_event_subscription('automations', 'crm.lead_created', 'plg_automations_on_exit_event',
|
|
909
|
+
'Leave flows that exit on crm.lead_created', 5);
|
|
910
|
+
SELECT public.register_event_subscription('automations', 'crm.stage_changed', 'plg_automations_on_entry_event',
|
|
911
|
+
'Enrol into flows whose entry event is crm.stage_changed', 5);
|
|
912
|
+
SELECT public.register_event_subscription('automations', 'crm.stage_changed', 'plg_automations_on_exit_event',
|
|
913
|
+
'Leave flows that exit on crm.stage_changed', 5);
|
|
914
|
+
SELECT public.register_event_subscription('automations', 'crm.quote_sent', 'plg_automations_on_entry_event',
|
|
915
|
+
'Enrol into flows whose entry event is crm.quote_sent', 5);
|
|
916
|
+
SELECT public.register_event_subscription('automations', 'crm.quote_sent', 'plg_automations_on_exit_event',
|
|
917
|
+
'Leave flows that exit on crm.quote_sent', 5);
|
|
918
|
+
SELECT public.register_event_subscription('automations', 'crm.quote_approved', 'plg_automations_on_entry_event',
|
|
919
|
+
'Enrol into flows whose entry event is crm.quote_approved', 5);
|
|
920
|
+
SELECT public.register_event_subscription('automations', 'crm.quote_approved', 'plg_automations_on_exit_event',
|
|
921
|
+
'Leave flows that exit on crm.quote_approved', 5);
|
|
922
|
+
SELECT public.register_event_subscription('automations', 'crm.deal_won', 'plg_automations_on_entry_event',
|
|
923
|
+
'Enrol into flows whose entry event is crm.deal_won', 5);
|
|
924
|
+
SELECT public.register_event_subscription('automations', 'crm.deal_won', 'plg_automations_on_exit_event',
|
|
925
|
+
'Leave flows that exit on crm.deal_won', 5);
|
|
926
|
+
SELECT public.register_event_subscription('automations', 'crm.deal_lost', 'plg_automations_on_entry_event',
|
|
927
|
+
'Enrol into flows whose entry event is crm.deal_lost', 5);
|
|
928
|
+
SELECT public.register_event_subscription('automations', 'crm.deal_lost', 'plg_automations_on_exit_event',
|
|
929
|
+
'Leave flows that exit on crm.deal_lost', 5);
|
|
930
|
+
SELECT public.register_event_subscription('automations', 'crm.deal_idle', 'plg_automations_on_entry_event',
|
|
931
|
+
'Enrol into flows whose entry event is crm.deal_idle', 5);
|
|
932
|
+
SELECT public.register_event_subscription('automations', 'crm.deal_idle', 'plg_automations_on_exit_event',
|
|
933
|
+
'Leave flows that exit on crm.deal_idle', 5);
|
|
934
|
+
|
|
935
|
+
-- \u2500\u2500 view \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
936
|
+
|
|
937
|
+
-- Per-step numbers, which is the question a cadence is judged by: where do
|
|
938
|
+
-- people stop. Aggregating per FLOW would hide that the third message is the
|
|
939
|
+
-- one nobody answers.
|
|
940
|
+
CREATE OR REPLACE VIEW public.v_automations_flow_steps WITH (security_invoker='true') AS
|
|
941
|
+
SELECT
|
|
942
|
+
s.tenant_id, s.flow_id, s.id AS step_id, s.position, s.kind,
|
|
943
|
+
s.template_key, s.channel, s.delay_minutes, s.task_title,
|
|
944
|
+
count(q.id) FILTER (WHERE q.id IS NOT NULL) AS queued,
|
|
945
|
+
count(q.id) FILTER (WHERE q.status = 'sent') AS sent,
|
|
946
|
+
count(q.id) FILTER (WHERE q.status = 'blocked') AS blocked,
|
|
947
|
+
count(q.id) FILTER (WHERE q.status = 'failed') AS failed,
|
|
948
|
+
count(q.id) FILTER (WHERE q.status = 'cancelled') AS cancelled
|
|
949
|
+
FROM public.plg_automations_flow_steps s
|
|
950
|
+
LEFT JOIN public.plg_automations_enrollments e ON e.flow_id = s.flow_id
|
|
951
|
+
LEFT JOIN public.plg_notifications_queue q
|
|
952
|
+
ON q.dedupe_key = 'flow:' || e.id::text || ':' || s.position::text
|
|
953
|
+
GROUP BY s.tenant_id, s.flow_id, s.id, s.position, s.kind,
|
|
954
|
+
s.template_key, s.channel, s.delay_minutes, s.task_title;
|
|
955
|
+
|
|
956
|
+
GRANT SELECT ON public.v_automations_flow_steps TO authenticated;
|
|
957
|
+
GRANT SELECT ON public.v_automations_flow_steps TO service_role;
|
|
958
|
+
`;
|
|
959
|
+
var MIGRATION_002_THE_ENGINE_IS_SERVER_ONLY = `-- 002_the_engine_is_server_only.sql
|
|
960
|
+
--
|
|
961
|
+
-- 001 meant the cadence engine to be service_role only, and wrote
|
|
962
|
+
-- \`REVOKE ALL ... FROM PUBLIC\`. That does not undo the pool's default
|
|
963
|
+
-- privileges, which grant EXECUTE to \`authenticated\` on every new function in
|
|
964
|
+
-- public \u2014 so on the live pool any logged-in user of any tenant could enrol a
|
|
965
|
+
-- person into another tenant's flow, exit them, or advance every tenant's
|
|
966
|
+
-- cadences at once, all SECURITY DEFINER with no caller check.
|
|
967
|
+
--
|
|
968
|
+
-- Callers are all server-side: \`event_runner_tick\` (pg_cron, as postgres)
|
|
969
|
+
-- calls \`automations_flows_run_due\`, the event log calls the two handlers, and
|
|
970
|
+
-- the handlers call enrol/exit. No screen, edge function or agent tool does.
|
|
971
|
+
-- \`automations_flow_exit\` was granted to \`authenticated\` for a manual
|
|
972
|
+
-- "take this person out" that was never built; it goes too, and comes back
|
|
973
|
+
-- behind a tenant + permission check when a screen needs it.
|
|
974
|
+
|
|
975
|
+
REVOKE ALL ON FUNCTION public.automations_flow_enroll(uuid, uuid, uuid, text, uuid) FROM PUBLIC, anon, authenticated;
|
|
976
|
+
GRANT EXECUTE ON FUNCTION public.automations_flow_enroll(uuid, uuid, uuid, text, uuid) TO service_role;
|
|
977
|
+
|
|
978
|
+
REVOKE ALL ON FUNCTION public.automations_flow_exit(uuid, uuid, text, uuid[]) FROM PUBLIC, anon, authenticated;
|
|
979
|
+
GRANT EXECUTE ON FUNCTION public.automations_flow_exit(uuid, uuid, text, uuid[]) TO service_role;
|
|
980
|
+
|
|
981
|
+
REVOKE ALL ON FUNCTION public.automations_flows_run_due(integer) FROM PUBLIC, anon, authenticated;
|
|
982
|
+
GRANT EXECUTE ON FUNCTION public.automations_flows_run_due(integer) TO service_role;
|
|
983
|
+
|
|
984
|
+
REVOKE ALL ON FUNCTION public.plg_automations_on_entry_event(jsonb) FROM PUBLIC, anon, authenticated;
|
|
985
|
+
GRANT EXECUTE ON FUNCTION public.plg_automations_on_entry_event(jsonb) TO service_role;
|
|
986
|
+
|
|
987
|
+
REVOKE ALL ON FUNCTION public.plg_automations_on_exit_event(jsonb) FROM PUBLIC, anon, authenticated;
|
|
988
|
+
GRANT EXECUTE ON FUNCTION public.plg_automations_on_exit_event(jsonb) TO service_role;
|
|
989
|
+
`;
|
|
990
|
+
var MIGRATIONS = [
|
|
991
|
+
{ id: "000_baseline", sql: MIGRATION_000_BASELINE },
|
|
992
|
+
{ id: "001_the_flow_has_steps", sql: MIGRATION_001_THE_FLOW_HAS_STEPS },
|
|
993
|
+
{ id: "002_the_engine_is_server_only", sql: MIGRATION_002_THE_ENGINE_IS_SERVER_ONLY }
|
|
994
|
+
];
|
|
995
|
+
|
|
996
|
+
export { MIGRATIONS, MIGRATION_000_BASELINE, MIGRATION_001_THE_FLOW_HAS_STEPS, MIGRATION_002_THE_ENGINE_IS_SERVER_ONLY };
|
|
997
|
+
//# sourceMappingURL=main.js.map
|
|
998
|
+
//# sourceMappingURL=main.js.map
|