@fayz-ai/plugin-crm 0.3.0 → 0.8.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,25 +1,25 @@
1
1
  -- CRM Plugin: Base Tables
2
- -- Leads use saas_core.persons (kind='lead')
3
- -- Deals use saas_core.orders (kind='deal')
4
- -- Quotes use saas_core.orders (kind='quote') + saas_core.order_items
2
+ -- Leads use public.people (kind='lead')
3
+ -- Deals use public.orders (kind='deal')
4
+ -- Quotes use public.orders (kind='quote') + public.order_items
5
5
  -- These are plugin-specific extension tables
6
6
 
7
- CREATE TABLE IF NOT EXISTS public.pipelines (
7
+ CREATE TABLE IF NOT EXISTS public.plg_crm_pipelines (
8
8
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
9
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
9
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
10
10
  name text NOT NULL,
11
11
  is_default boolean DEFAULT false,
12
12
  is_active boolean NOT NULL DEFAULT true,
13
13
  created_at timestamptz NOT NULL DEFAULT now(),
14
14
  updated_at timestamptz NOT NULL DEFAULT now()
15
15
  );
16
- ALTER TABLE public.pipelines ENABLE ROW LEVEL SECURITY;
17
- CREATE INDEX IF NOT EXISTS idx_pipelines_tenant ON public.pipelines(tenant_id);
16
+ ALTER TABLE public.plg_crm_pipelines ENABLE ROW LEVEL SECURITY;
17
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_pipelines_tenant ON public.plg_crm_pipelines(tenant_id);
18
18
 
19
- CREATE TABLE IF NOT EXISTS public.pipeline_stages (
19
+ CREATE TABLE IF NOT EXISTS public.plg_crm_pipeline_stages (
20
20
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
21
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
22
- pipeline_id uuid NOT NULL REFERENCES public.pipelines(id) ON DELETE CASCADE,
21
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
22
+ pipeline_id uuid NOT NULL REFERENCES public.plg_crm_pipelines(id) ON DELETE CASCADE,
23
23
  name text NOT NULL,
24
24
  "order" integer NOT NULL DEFAULT 0,
25
25
  color text DEFAULT '#6366f1',
@@ -28,46 +28,46 @@ CREATE TABLE IF NOT EXISTS public.pipeline_stages (
28
28
  is_lost boolean DEFAULT false,
29
29
  created_at timestamptz NOT NULL DEFAULT now()
30
30
  );
31
- ALTER TABLE public.pipeline_stages ENABLE ROW LEVEL SECURITY;
32
- CREATE INDEX IF NOT EXISTS idx_pipeline_stages_tenant ON public.pipeline_stages(tenant_id);
33
- CREATE INDEX IF NOT EXISTS idx_pipeline_stages_pipeline ON public.pipeline_stages(pipeline_id);
31
+ ALTER TABLE public.plg_crm_pipeline_stages ENABLE ROW LEVEL SECURITY;
32
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_pipeline_stages_tenant ON public.plg_crm_pipeline_stages(tenant_id);
33
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_pipeline_stages_pipeline ON public.plg_crm_pipeline_stages(pipeline_id);
34
34
 
35
- CREATE TABLE IF NOT EXISTS public.lead_sources (
35
+ CREATE TABLE IF NOT EXISTS public.plg_crm_lead_sources (
36
36
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
37
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
37
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
38
38
  name text NOT NULL,
39
39
  is_active boolean NOT NULL DEFAULT true,
40
40
  created_at timestamptz NOT NULL DEFAULT now()
41
41
  );
42
- ALTER TABLE public.lead_sources ENABLE ROW LEVEL SECURITY;
43
- CREATE INDEX IF NOT EXISTS idx_lead_sources_tenant ON public.lead_sources(tenant_id);
42
+ ALTER TABLE public.plg_crm_lead_sources ENABLE ROW LEVEL SECURITY;
43
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_lead_sources_tenant ON public.plg_crm_lead_sources(tenant_id);
44
44
 
45
- CREATE TABLE IF NOT EXISTS public.crm_tags (
45
+ CREATE TABLE IF NOT EXISTS public.plg_crm_tags (
46
46
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
47
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
47
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
48
48
  name text NOT NULL,
49
49
  color text DEFAULT '#6366f1',
50
50
  is_active boolean NOT NULL DEFAULT true,
51
51
  created_at timestamptz NOT NULL DEFAULT now()
52
52
  );
53
- ALTER TABLE public.crm_tags ENABLE ROW LEVEL SECURITY;
54
- CREATE INDEX IF NOT EXISTS idx_crm_tags_tenant ON public.crm_tags(tenant_id);
53
+ ALTER TABLE public.plg_crm_tags ENABLE ROW LEVEL SECURITY;
54
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_tags_tenant ON public.plg_crm_tags(tenant_id);
55
55
 
56
- -- Deal extension: links saas_core.orders (kind='deal') to pipeline stage
57
- CREATE TABLE IF NOT EXISTS public.deal_extensions (
56
+ -- Deal extension: links public.orders (kind='deal') to pipeline stage
57
+ CREATE TABLE IF NOT EXISTS public.plg_crm_deal_extensions (
58
58
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
59
- order_id uuid NOT NULL REFERENCES saas_core.orders(id) ON DELETE CASCADE,
60
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
61
- pipeline_id uuid REFERENCES public.pipelines(id),
62
- stage_id uuid REFERENCES public.pipeline_stages(id),
59
+ order_id uuid NOT NULL REFERENCES public.orders(id) ON DELETE CASCADE,
60
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
61
+ pipeline_id uuid REFERENCES public.plg_crm_pipelines(id),
62
+ stage_id uuid REFERENCES public.plg_crm_pipeline_stages(id),
63
63
  probability numeric(5,2) DEFAULT 0,
64
64
  expected_close_date date,
65
- lead_id uuid REFERENCES saas_core.persons(id),
65
+ lead_id uuid REFERENCES public.people(id),
66
66
  lost_reason text,
67
67
  created_at timestamptz NOT NULL DEFAULT now(),
68
68
  updated_at timestamptz NOT NULL DEFAULT now()
69
69
  );
70
- ALTER TABLE public.deal_extensions ENABLE ROW LEVEL SECURITY;
71
- CREATE INDEX IF NOT EXISTS idx_deal_extensions_tenant ON public.deal_extensions(tenant_id);
72
- CREATE INDEX IF NOT EXISTS idx_deal_extensions_order ON public.deal_extensions(order_id);
73
- CREATE INDEX IF NOT EXISTS idx_deal_extensions_stage ON public.deal_extensions(stage_id);
70
+ ALTER TABLE public.plg_crm_deal_extensions ENABLE ROW LEVEL SECURITY;
71
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_deal_extensions_tenant ON public.plg_crm_deal_extensions(tenant_id);
72
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_deal_extensions_order ON public.plg_crm_deal_extensions(order_id);
73
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_deal_extensions_stage ON public.plg_crm_deal_extensions(stage_id);
@@ -1,21 +1,21 @@
1
1
  -- CRM Plugin: Activities & Tasks
2
2
 
3
- CREATE TABLE IF NOT EXISTS public.crm_activity_types (
3
+ CREATE TABLE IF NOT EXISTS public.plg_crm_activity_types (
4
4
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
5
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
5
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
6
6
  name text NOT NULL,
7
7
  is_active boolean NOT NULL DEFAULT true,
8
8
  created_at timestamptz NOT NULL DEFAULT now()
9
9
  );
10
- ALTER TABLE public.crm_activity_types ENABLE ROW LEVEL SECURITY;
11
- CREATE INDEX IF NOT EXISTS idx_crm_activity_types_tenant ON public.crm_activity_types(tenant_id);
10
+ ALTER TABLE public.plg_crm_activity_types ENABLE ROW LEVEL SECURITY;
11
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_activity_types_tenant ON public.plg_crm_activity_types(tenant_id);
12
12
 
13
- CREATE TABLE IF NOT EXISTS public.crm_activities (
13
+ CREATE TABLE IF NOT EXISTS public.plg_crm_activities (
14
14
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
15
- tenant_id uuid NOT NULL REFERENCES saas_core.tenants(id) ON DELETE CASCADE,
16
- deal_id uuid REFERENCES saas_core.orders(id),
17
- lead_id uuid REFERENCES saas_core.persons(id),
18
- contact_id uuid REFERENCES saas_core.persons(id),
15
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
16
+ deal_id uuid REFERENCES public.orders(id),
17
+ lead_id uuid REFERENCES public.people(id),
18
+ contact_id uuid REFERENCES public.people(id),
19
19
  contact_name text,
20
20
  activity_type text NOT NULL,
21
21
  title text NOT NULL,
@@ -26,8 +26,8 @@ CREATE TABLE IF NOT EXISTS public.crm_activities (
26
26
  assigned_to_name text,
27
27
  created_at timestamptz NOT NULL DEFAULT now()
28
28
  );
29
- ALTER TABLE public.crm_activities ENABLE ROW LEVEL SECURITY;
30
- CREATE INDEX IF NOT EXISTS idx_crm_activities_tenant ON public.crm_activities(tenant_id);
31
- CREATE INDEX IF NOT EXISTS idx_crm_activities_deal ON public.crm_activities(deal_id);
32
- CREATE INDEX IF NOT EXISTS idx_crm_activities_lead ON public.crm_activities(lead_id);
33
- CREATE INDEX IF NOT EXISTS idx_crm_activities_due ON public.crm_activities(tenant_id, due_date);
29
+ ALTER TABLE public.plg_crm_activities ENABLE ROW LEVEL SECURITY;
30
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_activities_tenant ON public.plg_crm_activities(tenant_id);
31
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_activities_deal ON public.plg_crm_activities(deal_id);
32
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_activities_lead ON public.plg_crm_activities(lead_id);
33
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_activities_due ON public.plg_crm_activities(tenant_id, due_date);
@@ -7,8 +7,8 @@ DO $$
7
7
  DECLARE t text;
8
8
  BEGIN
9
9
  FOR t IN SELECT unnest(ARRAY[
10
- 'pipelines','pipeline_stages','lead_sources','crm_tags',
11
- 'deal_extensions','crm_activity_types','crm_activities'
10
+ 'plg_crm_pipelines','plg_crm_pipeline_stages','plg_crm_lead_sources','plg_crm_tags',
11
+ 'plg_crm_deal_extensions','plg_crm_activity_types','plg_crm_activities'
12
12
  ])
13
13
  LOOP
14
14
  EXECUTE format('ALTER TABLE public.%I ENABLE ROW LEVEL SECURITY', t);
@@ -28,7 +28,7 @@ BEGIN
28
28
  END LOOP;
29
29
  END $$;
30
30
 
31
- -- v_leads: saas_core.persons (kind='lead')
31
+ -- v_leads: public.people (kind='lead')
32
32
  CREATE OR REPLACE VIEW public.v_leads WITH (security_invoker=true) AS
33
33
  SELECT
34
34
  p.id, p.tenant_id, p.name, p.email, p.phone, p.notes, p.tags, p.is_active,
@@ -39,11 +39,11 @@ SELECT
39
39
  p.metadata->>'value' AS lead_value,
40
40
  p.metadata->>'assignedToId' AS assigned_to_id,
41
41
  p.created_at, p.updated_at
42
- FROM saas_core.persons p
42
+ FROM public.people p
43
43
  WHERE p.kind = 'lead';
44
44
  GRANT SELECT ON public.v_leads TO authenticated;
45
45
 
46
- -- v_deals: saas_core.orders (kind='deal') JOIN deal_extensions JOIN pipeline_stages
46
+ -- v_deals: public.orders (kind='deal') JOIN plg_crm_deal_extensions JOIN plg_crm_pipeline_stages
47
47
  CREATE OR REPLACE VIEW public.v_deals WITH (security_invoker=true) AS
48
48
  SELECT
49
49
  o.id, o.tenant_id, o.status,
@@ -54,8 +54,8 @@ SELECT
54
54
  de.pipeline_id, de.stage_id, de.probability, de.expected_close_date,
55
55
  de.lead_id, de.lost_reason,
56
56
  ps.name AS stage_name, ps.color AS stage_color, ps."order" AS stage_order
57
- FROM saas_core.orders o
58
- LEFT JOIN public.deal_extensions de ON de.order_id = o.id
59
- LEFT JOIN public.pipeline_stages ps ON ps.id = de.stage_id
57
+ FROM public.orders o
58
+ LEFT JOIN public.plg_crm_deal_extensions de ON de.order_id = o.id
59
+ LEFT JOIN public.plg_crm_pipeline_stages ps ON ps.id = de.stage_id
60
60
  WHERE o.kind = 'deal';
61
61
  GRANT SELECT ON public.v_deals TO authenticated;
@@ -1,5 +1,5 @@
1
1
  -- CRM plugin — seed: default "Sales Pipeline" + canonical stages.
2
- -- The pipeline board (#/sales/pipeline) reads public.pipelines / pipeline_stages
2
+ -- The pipeline board (#/sales/pipeline) reads public.plg_crm_pipelines / plg_crm_pipeline_stages
3
3
  -- directly; a fresh tenant has zero rows and the board renders blank. This
4
4
  -- backfills the default pipeline (matching the plugin's mock/offline provider)
5
5
  -- for every tenant that has none. Idempotent — tenants with a pipeline are skipped.
@@ -11,15 +11,15 @@ DECLARE
11
11
  p_id uuid;
12
12
  BEGIN
13
13
  FOR t_id IN (
14
- SELECT id FROM saas_core.tenants
15
- WHERE id NOT IN (SELECT tenant_id FROM public.pipelines)
14
+ SELECT id FROM public.tenants
15
+ WHERE id NOT IN (SELECT tenant_id FROM public.plg_crm_pipelines)
16
16
  )
17
17
  LOOP
18
- INSERT INTO public.pipelines (tenant_id, name, is_default, is_active)
18
+ INSERT INTO public.plg_crm_pipelines (tenant_id, name, is_default, is_active)
19
19
  VALUES (t_id, 'Sales Pipeline', true, true)
20
20
  RETURNING id INTO p_id;
21
21
 
22
- INSERT INTO public.pipeline_stages (tenant_id, pipeline_id, name, "order", color, probability, is_won, is_lost) VALUES
22
+ INSERT INTO public.plg_crm_pipeline_stages (tenant_id, pipeline_id, name, "order", color, probability, is_won, is_lost) VALUES
23
23
  (t_id, p_id, 'New', 0, '#6366f1', 10, false, false),
24
24
  (t_id, p_id, 'Contacted', 1, '#3b82f6', 25, false, false),
25
25
  (t_id, p_id, 'Qualified', 2, '#f59e0b', 50, false, false),
@@ -0,0 +1,247 @@
1
+ // AUTO-GENERATED from 000_plg_rename.sql, 001_crm_base.sql, 002_activities.sql, 003_crm_views_rls.sql, 004_seed_default_pipeline.sql — regenerate with scripts/embed-migrations.mjs
2
+ // SQL files are the source of truth; this inline copy lets the manifest declare
3
+ // migrations as data. Do not edit by hand — run the embed script instead.
4
+
5
+ export const MIGRATION_000_PLG_RENAME = `-- 000_plg_rename.sql — rename legacy CRM tables to the plg_crm_* convention for
6
+ -- pools provisioned before the industry-pool rename. Guarded: only fires when the
7
+ -- legacy name exists and the target does not, so fresh pools skip every branch and
8
+ -- the create-table migrations below build clean.
9
+ DO $$
10
+ BEGIN
11
+ IF to_regclass('public.crm_activities') IS NOT NULL AND to_regclass('public.plg_crm_activities') IS NULL THEN
12
+ ALTER TABLE public.crm_activities RENAME TO plg_crm_activities;
13
+ END IF;
14
+ IF to_regclass('public.crm_tags') IS NOT NULL AND to_regclass('public.plg_crm_tags') IS NULL THEN
15
+ ALTER TABLE public.crm_tags RENAME TO plg_crm_tags;
16
+ END IF;
17
+ IF to_regclass('public.crm_activity_types') IS NOT NULL AND to_regclass('public.plg_crm_activity_types') IS NULL THEN
18
+ ALTER TABLE public.crm_activity_types RENAME TO plg_crm_activity_types;
19
+ END IF;
20
+ IF to_regclass('public.pipelines') IS NOT NULL AND to_regclass('public.plg_crm_pipelines') IS NULL THEN
21
+ ALTER TABLE public.pipelines RENAME TO plg_crm_pipelines;
22
+ END IF;
23
+ IF to_regclass('public.pipeline_stages') IS NOT NULL AND to_regclass('public.plg_crm_pipeline_stages') IS NULL THEN
24
+ ALTER TABLE public.pipeline_stages RENAME TO plg_crm_pipeline_stages;
25
+ END IF;
26
+ IF to_regclass('public.deal_extensions') IS NOT NULL AND to_regclass('public.plg_crm_deal_extensions') IS NULL THEN
27
+ ALTER TABLE public.deal_extensions RENAME TO plg_crm_deal_extensions;
28
+ END IF;
29
+ IF to_regclass('public.lead_sources') IS NOT NULL AND to_regclass('public.plg_crm_lead_sources') IS NULL THEN
30
+ ALTER TABLE public.lead_sources RENAME TO plg_crm_lead_sources;
31
+ END IF;
32
+ END $$;
33
+ `
34
+
35
+ export const MIGRATION_001_CRM_BASE = `-- CRM Plugin: Base Tables
36
+ -- Leads use public.people (kind='lead')
37
+ -- Deals use public.orders (kind='deal')
38
+ -- Quotes use public.orders (kind='quote') + public.order_items
39
+ -- These are plugin-specific extension tables
40
+
41
+ CREATE TABLE IF NOT EXISTS public.plg_crm_pipelines (
42
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
43
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
44
+ name text NOT NULL,
45
+ is_default boolean DEFAULT false,
46
+ is_active boolean NOT NULL DEFAULT true,
47
+ created_at timestamptz NOT NULL DEFAULT now(),
48
+ updated_at timestamptz NOT NULL DEFAULT now()
49
+ );
50
+ ALTER TABLE public.plg_crm_pipelines ENABLE ROW LEVEL SECURITY;
51
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_pipelines_tenant ON public.plg_crm_pipelines(tenant_id);
52
+
53
+ CREATE TABLE IF NOT EXISTS public.plg_crm_pipeline_stages (
54
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
55
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
56
+ pipeline_id uuid NOT NULL REFERENCES public.plg_crm_pipelines(id) ON DELETE CASCADE,
57
+ name text NOT NULL,
58
+ "order" integer NOT NULL DEFAULT 0,
59
+ color text DEFAULT '#6366f1',
60
+ probability numeric(5,2) DEFAULT 0,
61
+ is_won boolean DEFAULT false,
62
+ is_lost boolean DEFAULT false,
63
+ created_at timestamptz NOT NULL DEFAULT now()
64
+ );
65
+ ALTER TABLE public.plg_crm_pipeline_stages ENABLE ROW LEVEL SECURITY;
66
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_pipeline_stages_tenant ON public.plg_crm_pipeline_stages(tenant_id);
67
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_pipeline_stages_pipeline ON public.plg_crm_pipeline_stages(pipeline_id);
68
+
69
+ CREATE TABLE IF NOT EXISTS public.plg_crm_lead_sources (
70
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
71
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
72
+ name text NOT NULL,
73
+ is_active boolean NOT NULL DEFAULT true,
74
+ created_at timestamptz NOT NULL DEFAULT now()
75
+ );
76
+ ALTER TABLE public.plg_crm_lead_sources ENABLE ROW LEVEL SECURITY;
77
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_lead_sources_tenant ON public.plg_crm_lead_sources(tenant_id);
78
+
79
+ CREATE TABLE IF NOT EXISTS public.plg_crm_tags (
80
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
81
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
82
+ name text NOT NULL,
83
+ color text DEFAULT '#6366f1',
84
+ is_active boolean NOT NULL DEFAULT true,
85
+ created_at timestamptz NOT NULL DEFAULT now()
86
+ );
87
+ ALTER TABLE public.plg_crm_tags ENABLE ROW LEVEL SECURITY;
88
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_tags_tenant ON public.plg_crm_tags(tenant_id);
89
+
90
+ -- Deal extension: links public.orders (kind='deal') to pipeline stage
91
+ CREATE TABLE IF NOT EXISTS public.plg_crm_deal_extensions (
92
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
93
+ order_id uuid NOT NULL REFERENCES public.orders(id) ON DELETE CASCADE,
94
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
95
+ pipeline_id uuid REFERENCES public.plg_crm_pipelines(id),
96
+ stage_id uuid REFERENCES public.plg_crm_pipeline_stages(id),
97
+ probability numeric(5,2) DEFAULT 0,
98
+ expected_close_date date,
99
+ lead_id uuid REFERENCES public.people(id),
100
+ lost_reason text,
101
+ created_at timestamptz NOT NULL DEFAULT now(),
102
+ updated_at timestamptz NOT NULL DEFAULT now()
103
+ );
104
+ ALTER TABLE public.plg_crm_deal_extensions ENABLE ROW LEVEL SECURITY;
105
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_deal_extensions_tenant ON public.plg_crm_deal_extensions(tenant_id);
106
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_deal_extensions_order ON public.plg_crm_deal_extensions(order_id);
107
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_deal_extensions_stage ON public.plg_crm_deal_extensions(stage_id);
108
+ `
109
+
110
+ export const MIGRATION_002_ACTIVITIES = `-- CRM Plugin: Activities & Tasks
111
+
112
+ CREATE TABLE IF NOT EXISTS public.plg_crm_activity_types (
113
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
114
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
115
+ name text NOT NULL,
116
+ is_active boolean NOT NULL DEFAULT true,
117
+ created_at timestamptz NOT NULL DEFAULT now()
118
+ );
119
+ ALTER TABLE public.plg_crm_activity_types ENABLE ROW LEVEL SECURITY;
120
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_activity_types_tenant ON public.plg_crm_activity_types(tenant_id);
121
+
122
+ CREATE TABLE IF NOT EXISTS public.plg_crm_activities (
123
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
124
+ tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
125
+ deal_id uuid REFERENCES public.orders(id),
126
+ lead_id uuid REFERENCES public.people(id),
127
+ contact_id uuid REFERENCES public.people(id),
128
+ contact_name text,
129
+ activity_type text NOT NULL,
130
+ title text NOT NULL,
131
+ description text,
132
+ due_date date,
133
+ completed_at timestamptz,
134
+ assigned_to_id uuid,
135
+ assigned_to_name text,
136
+ created_at timestamptz NOT NULL DEFAULT now()
137
+ );
138
+ ALTER TABLE public.plg_crm_activities ENABLE ROW LEVEL SECURITY;
139
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_activities_tenant ON public.plg_crm_activities(tenant_id);
140
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_activities_deal ON public.plg_crm_activities(deal_id);
141
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_activities_lead ON public.plg_crm_activities(lead_id);
142
+ CREATE INDEX IF NOT EXISTS idx_plg_crm_activities_due ON public.plg_crm_activities(tenant_id, due_date);
143
+ `
144
+
145
+ export const MIGRATION_003_CRM_VIEWS_RLS = `-- CRM plugin — companion SQL (functions/views/RLS Drizzle doesn't diff).
146
+ -- Tables come from the Drizzle schema (src/schema/); this is everything else the
147
+ -- plugin's data provider depends on: RLS policies + grants + the read-views.
148
+ -- Idempotent. Canonical RLS form: tenant_id IN (SELECT public.user_tenant_ids()).
149
+
150
+ DO $$
151
+ DECLARE t text;
152
+ BEGIN
153
+ FOR t IN SELECT unnest(ARRAY[
154
+ 'plg_crm_pipelines','plg_crm_pipeline_stages','plg_crm_lead_sources','plg_crm_tags',
155
+ 'plg_crm_deal_extensions','plg_crm_activity_types','plg_crm_activities'
156
+ ])
157
+ LOOP
158
+ EXECUTE format('ALTER TABLE public.%I ENABLE ROW LEVEL SECURITY', t);
159
+ EXECUTE format('GRANT SELECT, INSERT, UPDATE, DELETE ON public.%I TO authenticated', t);
160
+ IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='public' AND tablename=t AND policyname=t||'_select') THEN
161
+ EXECUTE format('CREATE POLICY %I ON public.%I FOR SELECT TO authenticated USING (tenant_id IN (SELECT public.user_tenant_ids()))', t||'_select', t);
162
+ END IF;
163
+ IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='public' AND tablename=t AND policyname=t||'_insert') THEN
164
+ EXECUTE format('CREATE POLICY %I ON public.%I FOR INSERT TO authenticated WITH CHECK (tenant_id IN (SELECT public.user_tenant_ids()))', t||'_insert', t);
165
+ END IF;
166
+ IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='public' AND tablename=t AND policyname=t||'_update') THEN
167
+ EXECUTE format('CREATE POLICY %I ON public.%I FOR UPDATE TO authenticated USING (tenant_id IN (SELECT public.user_tenant_ids()))', t||'_update', t);
168
+ END IF;
169
+ IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='public' AND tablename=t AND policyname=t||'_delete') THEN
170
+ EXECUTE format('CREATE POLICY %I ON public.%I FOR DELETE TO authenticated USING (tenant_id IN (SELECT public.user_tenant_ids()))', t||'_delete', t);
171
+ END IF;
172
+ END LOOP;
173
+ END $$;
174
+
175
+ -- v_leads: public.people (kind='lead')
176
+ CREATE OR REPLACE VIEW public.v_leads WITH (security_invoker=true) AS
177
+ SELECT
178
+ p.id, p.tenant_id, p.name, p.email, p.phone, p.notes, p.tags, p.is_active,
179
+ p.metadata->>'company' AS company,
180
+ p.metadata->>'sourceId' AS source_id,
181
+ p.metadata->>'sourceName' AS source_name,
182
+ COALESCE(p.metadata->>'status','new') AS lead_status,
183
+ p.metadata->>'value' AS lead_value,
184
+ p.metadata->>'assignedToId' AS assigned_to_id,
185
+ p.created_at, p.updated_at
186
+ FROM public.people p
187
+ WHERE p.kind = 'lead';
188
+ GRANT SELECT ON public.v_leads TO authenticated;
189
+
190
+ -- v_deals: public.orders (kind='deal') JOIN plg_crm_deal_extensions JOIN plg_crm_pipeline_stages
191
+ CREATE OR REPLACE VIEW public.v_deals WITH (security_invoker=true) AS
192
+ SELECT
193
+ o.id, o.tenant_id, o.status,
194
+ o.total AS value, o.notes AS title,
195
+ o.party_id AS contact_id, o.assignee_id AS assigned_to_id, o.tags,
196
+ o.metadata->>'contactName' AS contact_name,
197
+ o.created_at, o.updated_at,
198
+ de.pipeline_id, de.stage_id, de.probability, de.expected_close_date,
199
+ de.lead_id, de.lost_reason,
200
+ ps.name AS stage_name, ps.color AS stage_color, ps."order" AS stage_order
201
+ FROM public.orders o
202
+ LEFT JOIN public.plg_crm_deal_extensions de ON de.order_id = o.id
203
+ LEFT JOIN public.plg_crm_pipeline_stages ps ON ps.id = de.stage_id
204
+ WHERE o.kind = 'deal';
205
+ GRANT SELECT ON public.v_deals TO authenticated;
206
+ `
207
+
208
+ export const MIGRATION_004_SEED_DEFAULT_PIPELINE = `-- CRM plugin — seed: default "Sales Pipeline" + canonical stages.
209
+ -- The pipeline board (#/sales/pipeline) reads public.plg_crm_pipelines / plg_crm_pipeline_stages
210
+ -- directly; a fresh tenant has zero rows and the board renders blank. This
211
+ -- backfills the default pipeline (matching the plugin's mock/offline provider)
212
+ -- for every tenant that has none. Idempotent — tenants with a pipeline are skipped.
213
+ -- Plugin-owned so EVERY app gets it on install (not re-authored per app).
214
+
215
+ DO $$
216
+ DECLARE
217
+ t_id uuid;
218
+ p_id uuid;
219
+ BEGIN
220
+ FOR t_id IN (
221
+ SELECT id FROM public.tenants
222
+ WHERE id NOT IN (SELECT tenant_id FROM public.plg_crm_pipelines)
223
+ )
224
+ LOOP
225
+ INSERT INTO public.plg_crm_pipelines (tenant_id, name, is_default, is_active)
226
+ VALUES (t_id, 'Sales Pipeline', true, true)
227
+ RETURNING id INTO p_id;
228
+
229
+ INSERT INTO public.plg_crm_pipeline_stages (tenant_id, pipeline_id, name, "order", color, probability, is_won, is_lost) VALUES
230
+ (t_id, p_id, 'New', 0, '#6366f1', 10, false, false),
231
+ (t_id, p_id, 'Contacted', 1, '#3b82f6', 25, false, false),
232
+ (t_id, p_id, 'Qualified', 2, '#f59e0b', 50, false, false),
233
+ (t_id, p_id, 'Proposal', 3, '#f97316', 75, false, false),
234
+ (t_id, p_id, 'Negotiation', 4, '#8b5cf6', 90, false, false),
235
+ (t_id, p_id, 'Won', 5, '#22c55e', 100, true, false),
236
+ (t_id, p_id, 'Lost', 6, '#ef4444', 0, false, true);
237
+ END LOOP;
238
+ END $$;
239
+ `
240
+
241
+ export const MIGRATIONS: Array<{ id: string; sql: string }> = [
242
+ { id: "000_plg_rename", sql: MIGRATION_000_PLG_RENAME },
243
+ { id: "001_crm_base", sql: MIGRATION_001_CRM_BASE },
244
+ { id: "002_activities", sql: MIGRATION_002_ACTIVITIES },
245
+ { id: "003_crm_views_rls", sql: MIGRATION_003_CRM_VIEWS_RLS },
246
+ { id: "004_seed_default_pipeline", sql: MIGRATION_004_SEED_DEFAULT_PIPELINE },
247
+ ]
package/src/registries.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { PluginRegistryDef } from '@fayz-ai/core'
2
2
  import type { EntityDef } from '@fayz-ai/core'
3
+ import { T } from './data/tables'
3
4
 
4
5
  const leadSourceEntity: EntityDef = {
5
6
  name: 'Lead Source',
@@ -11,7 +12,7 @@ const leadSourceEntity: EntityDef = {
11
12
  { key: 'name', label: 'Name', type: 'text', required: true, showInTable: true },
12
13
  { key: 'isActive', label: 'Active', type: 'boolean', showInTable: true, defaultValue: true },
13
14
  ],
14
- data: { table: 'lead_sources', tenantScoped: true },
15
+ data: { table: T.leadSources, tenantScoped: true },
15
16
  }
16
17
 
17
18
  const tagEntity: EntityDef = {
@@ -25,7 +26,7 @@ const tagEntity: EntityDef = {
25
26
  { key: 'color', label: 'Color', type: 'color', showInTable: true, defaultValue: '#6366f1' },
26
27
  { key: 'isActive', label: 'Active', type: 'boolean', showInTable: true, defaultValue: true },
27
28
  ],
28
- data: { table: 'crm_tags', tenantScoped: true },
29
+ data: { table: T.tags, tenantScoped: true },
29
30
  }
30
31
 
31
32
  const pipelineStageEntity: EntityDef = {
@@ -40,7 +41,7 @@ const pipelineStageEntity: EntityDef = {
40
41
  key: 'pipelineId',
41
42
  label: 'Pipeline',
42
43
  type: 'relation',
43
- relation: { table: 'pipelines', labelField: 'name' },
44
+ relation: { table: T.pipelines, labelField: 'name' },
44
45
  required: true,
45
46
  showInTable: true,
46
47
  },
@@ -50,7 +51,7 @@ const pipelineStageEntity: EntityDef = {
50
51
  { key: 'isWon', label: 'Won Stage', type: 'boolean', showInTable: true, defaultValue: false },
51
52
  { key: 'isLost', label: 'Lost Stage', type: 'boolean', showInTable: true, defaultValue: false },
52
53
  ],
53
- data: { table: 'pipeline_stages', tenantScoped: true },
54
+ data: { table: T.pipelineStages, tenantScoped: true },
54
55
  }
55
56
 
56
57
  const activityTypeEntity: EntityDef = {
@@ -63,7 +64,7 @@ const activityTypeEntity: EntityDef = {
63
64
  { key: 'name', label: 'Name', type: 'text', required: true, showInTable: true },
64
65
  { key: 'isActive', label: 'Active', type: 'boolean', showInTable: true, defaultValue: true },
65
66
  ],
66
- data: { table: 'crm_activity_types', tenantScoped: true },
67
+ data: { table: T.activityTypes, tenantScoped: true },
67
68
  }
68
69
 
69
70
  export const crmRegistries: PluginRegistryDef[] = [
@@ -2,28 +2,28 @@
2
2
  // pgTable/uuid directly from drizzle-orm/pg-core. See MIGRATION-ARCHITECTURE §6.
3
3
  import {
4
4
  pgTable, uuid, text, boolean, integer, numeric, date, timestamp, index,
5
- tenantId, timestamps, createdAt, persons, orders,
5
+ tenantId, timestamps, createdAt, people, orders,
6
6
  } from '@fayz-ai/db'
7
7
 
8
8
  // ---------------------------------------------------------------------------
9
9
  // CRM plugin — Ring-1 schema (schema-as-code).
10
10
  //
11
- // Leads live on saas_core.persons (kind='lead'); deals/quotes on saas_core.orders.
11
+ // Leads live on public.people (kind='lead'); deals/quotes on public.orders.
12
12
  // These tables extend that spine with pipeline, tagging and activity data.
13
13
  // RLS policies, grants and the v_leads/v_deals read-views are companion SQL
14
14
  // (Drizzle doesn't diff grants/views) — see the app's drizzle/companion/.
15
15
  // ---------------------------------------------------------------------------
16
16
 
17
- export const pipelines = pgTable('pipelines', {
17
+ export const pipelines = pgTable('plg_crm_pipelines', {
18
18
  id: uuid('id').primaryKey().defaultRandom(),
19
19
  tenantId: tenantId(),
20
20
  name: text('name').notNull(),
21
21
  isDefault: boolean('is_default').default(false),
22
22
  isActive: boolean('is_active').notNull().default(true),
23
23
  ...timestamps,
24
- }, (t) => [index('idx_pipelines_tenant').on(t.tenantId)])
24
+ }, (t) => [index('idx_plg_crm_pipelines_tenant').on(t.tenantId)])
25
25
 
26
- export const pipelineStages = pgTable('pipeline_stages', {
26
+ export const pipelineStages = pgTable('plg_crm_pipeline_stages', {
27
27
  id: uuid('id').primaryKey().defaultRandom(),
28
28
  tenantId: tenantId(),
29
29
  pipelineId: uuid('pipeline_id').notNull().references(() => pipelines.id, { onDelete: 'cascade' }),
@@ -35,28 +35,28 @@ export const pipelineStages = pgTable('pipeline_stages', {
35
35
  isLost: boolean('is_lost').default(false),
36
36
  ...createdAt,
37
37
  }, (t) => [
38
- index('idx_pipeline_stages_tenant').on(t.tenantId),
39
- index('idx_pipeline_stages_pipeline').on(t.pipelineId),
38
+ index('idx_plg_crm_pipeline_stages_tenant').on(t.tenantId),
39
+ index('idx_plg_crm_pipeline_stages_pipeline').on(t.pipelineId),
40
40
  ])
41
41
 
42
- export const leadSources = pgTable('lead_sources', {
42
+ export const leadSources = pgTable('plg_crm_lead_sources', {
43
43
  id: uuid('id').primaryKey().defaultRandom(),
44
44
  tenantId: tenantId(),
45
45
  name: text('name').notNull(),
46
46
  isActive: boolean('is_active').notNull().default(true),
47
47
  ...createdAt,
48
- }, (t) => [index('idx_lead_sources_tenant').on(t.tenantId)])
48
+ }, (t) => [index('idx_plg_crm_lead_sources_tenant').on(t.tenantId)])
49
49
 
50
- export const crmTags = pgTable('crm_tags', {
50
+ export const crmTags = pgTable('plg_crm_tags', {
51
51
  id: uuid('id').primaryKey().defaultRandom(),
52
52
  tenantId: tenantId(),
53
53
  name: text('name').notNull(),
54
54
  color: text('color').default('#6366f1'),
55
55
  isActive: boolean('is_active').notNull().default(true),
56
56
  ...createdAt,
57
- }, (t) => [index('idx_crm_tags_tenant').on(t.tenantId)])
57
+ }, (t) => [index('idx_plg_crm_tags_tenant').on(t.tenantId)])
58
58
 
59
- export const dealExtensions = pgTable('deal_extensions', {
59
+ export const dealExtensions = pgTable('plg_crm_deal_extensions', {
60
60
  id: uuid('id').primaryKey().defaultRandom(),
61
61
  orderId: uuid('order_id').notNull().references(() => orders.id, { onDelete: 'cascade' }),
62
62
  tenantId: tenantId(),
@@ -64,29 +64,29 @@ export const dealExtensions = pgTable('deal_extensions', {
64
64
  stageId: uuid('stage_id').references(() => pipelineStages.id),
65
65
  probability: numeric('probability', { precision: 5, scale: 2 }).default('0'),
66
66
  expectedCloseDate: date('expected_close_date'),
67
- leadId: uuid('lead_id').references(() => persons.id),
67
+ leadId: uuid('lead_id').references(() => people.id),
68
68
  lostReason: text('lost_reason'),
69
69
  ...timestamps,
70
70
  }, (t) => [
71
- index('idx_deal_extensions_tenant').on(t.tenantId),
72
- index('idx_deal_extensions_order').on(t.orderId),
73
- index('idx_deal_extensions_stage').on(t.stageId),
71
+ index('idx_plg_crm_deal_extensions_tenant').on(t.tenantId),
72
+ index('idx_plg_crm_deal_extensions_order').on(t.orderId),
73
+ index('idx_plg_crm_deal_extensions_stage').on(t.stageId),
74
74
  ])
75
75
 
76
- export const crmActivityTypes = pgTable('crm_activity_types', {
76
+ export const crmActivityTypes = pgTable('plg_crm_activity_types', {
77
77
  id: uuid('id').primaryKey().defaultRandom(),
78
78
  tenantId: tenantId(),
79
79
  name: text('name').notNull(),
80
80
  isActive: boolean('is_active').notNull().default(true),
81
81
  ...createdAt,
82
- }, (t) => [index('idx_crm_activity_types_tenant').on(t.tenantId)])
82
+ }, (t) => [index('idx_plg_crm_activity_types_tenant').on(t.tenantId)])
83
83
 
84
- export const crmActivities = pgTable('crm_activities', {
84
+ export const crmActivities = pgTable('plg_crm_activities', {
85
85
  id: uuid('id').primaryKey().defaultRandom(),
86
86
  tenantId: tenantId(),
87
87
  dealId: uuid('deal_id').references(() => orders.id),
88
- leadId: uuid('lead_id').references(() => persons.id),
89
- contactId: uuid('contact_id').references(() => persons.id),
88
+ leadId: uuid('lead_id').references(() => people.id),
89
+ contactId: uuid('contact_id').references(() => people.id),
90
90
  contactName: text('contact_name'),
91
91
  activityType: text('activity_type').notNull(),
92
92
  title: text('title').notNull(),
@@ -97,8 +97,8 @@ export const crmActivities = pgTable('crm_activities', {
97
97
  assignedToName: text('assigned_to_name'),
98
98
  ...createdAt,
99
99
  }, (t) => [
100
- index('idx_crm_activities_tenant').on(t.tenantId),
101
- index('idx_crm_activities_deal').on(t.dealId),
102
- index('idx_crm_activities_lead').on(t.leadId),
103
- index('idx_crm_activities_due').on(t.tenantId, t.dueDate),
100
+ index('idx_plg_crm_activities_tenant').on(t.tenantId),
101
+ index('idx_plg_crm_activities_deal').on(t.dealId),
102
+ index('idx_plg_crm_activities_lead').on(t.leadId),
103
+ index('idx_plg_crm_activities_due').on(t.tenantId, t.dueDate),
104
104
  ])