@fayz-ai/plugin-crm 0.2.3 → 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.
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import React from 'react'
2
2
  import type { PluginManifest, PluginScope, VerticalId } from '@fayz-ai/core'
3
- import type { EntityLookupMap } from '@fayz-ai/saas'
3
+ import { PluginSettingsPanel, type EntityLookupMap } from '@fayz-ai/saas'
4
4
  import { CrmPage } from './CrmPage'
5
5
  import { createCrmDashboardWidgets } from './views/dashboardWidgets'
6
6
  import { CrmContextProvider, type ResolvedCrmConfig } from './CrmContext'
@@ -12,7 +12,6 @@ import { createCrmStore } from './store'
12
12
  import { crmRegistries } from './registries'
13
13
  import { crmLocales } from './locales'
14
14
  import { CrmGeneralSettings } from './components/CrmGeneralSettings'
15
- import { PipelineSettings } from './components/PipelineSettings'
16
15
 
17
16
  // ---------------------------------------------------------------------------
18
17
  // Public types
@@ -55,14 +54,14 @@ export interface CrmPluginOptions {
55
54
  /** Contact/person lookup for client search in quotes and leads */
56
55
  contactLookup?: import('@fayz-ai/saas').EntityLookup
57
56
  /** Client conversion config — when a lead is approved, CRM converts the person
58
- * to a client by updating `persons.kind` and creating the extension table record.
57
+ * to a client by updating `people.kind` and creating the extension table record.
59
58
  * @example { archetypeKind: 'customer', extensionTable: 'clients', fkColumn: 'person_id' } */
60
59
  clientConversion?: {
61
- /** The archetype kind to set on persons.kind (e.g., 'customer') */
60
+ /** The archetype kind to set on people.kind (e.g., 'customer') */
62
61
  archetypeKind: string
63
62
  /** Extension table name (e.g., 'clients') in public schema */
64
63
  extensionTable: string
65
- /** FK column pointing to persons.id (e.g., 'person_id') */
64
+ /** FK column pointing to people.id (e.g., 'person_id') */
66
65
  fkColumn: string
67
66
  }
68
67
  }
@@ -138,6 +137,9 @@ export function createCrmPlugin(options?: CrmPluginOptions): PluginManifest {
138
137
  version: '1.0.0',
139
138
  scope: options?.scope ?? 'universal',
140
139
  verticalId: options?.verticalId,
140
+ // Sales-pipeline/lead-management is a business-ops tool — targets the
141
+ // saas/admin Panel world, not the ecommerce storefront.
142
+ scaffolds: ['saas'],
141
143
  defaultEnabled: true,
142
144
  dependencies: [],
143
145
  declaredFeatures: [
@@ -218,12 +220,14 @@ export function createCrmPlugin(options?: CrmPluginOptions): PluginManifest {
218
220
  component: (() => {
219
221
  const Tab: React.FC = () =>
220
222
  React.createElement(CrmContextProvider, { config, provider, store },
221
- React.createElement(
222
- 'div',
223
- { style: { display: 'flex', flexDirection: 'column', gap: '20px' } },
224
- React.createElement(CrmGeneralSettings),
225
- React.createElement(PipelineSettings),
226
- ),
223
+ React.createElement(PluginSettingsPanel, {
224
+ title: 'Sales & CRM',
225
+ subtitle: 'Pipeline, lead management, and CRM properties',
226
+ generalSettings: React.createElement(CrmGeneralSettings),
227
+ registries: crmRegistries,
228
+ hostPluginId: 'crm',
229
+ routeBase: '/settings/crm',
230
+ }),
227
231
  )
228
232
  Tab.displayName = 'CrmSettingsTab'
229
233
  return Tab
@@ -0,0 +1,28 @@
1
+ -- 000_plg_rename.sql — rename legacy CRM tables to the plg_crm_* convention for
2
+ -- pools provisioned before the industry-pool rename. Guarded: only fires when the
3
+ -- legacy name exists and the target does not, so fresh pools skip every branch and
4
+ -- the create-table migrations below build clean.
5
+ DO $$
6
+ BEGIN
7
+ IF to_regclass('public.crm_activities') IS NOT NULL AND to_regclass('public.plg_crm_activities') IS NULL THEN
8
+ ALTER TABLE public.crm_activities RENAME TO plg_crm_activities;
9
+ END IF;
10
+ IF to_regclass('public.crm_tags') IS NOT NULL AND to_regclass('public.plg_crm_tags') IS NULL THEN
11
+ ALTER TABLE public.crm_tags RENAME TO plg_crm_tags;
12
+ END IF;
13
+ IF to_regclass('public.crm_activity_types') IS NOT NULL AND to_regclass('public.plg_crm_activity_types') IS NULL THEN
14
+ ALTER TABLE public.crm_activity_types RENAME TO plg_crm_activity_types;
15
+ END IF;
16
+ IF to_regclass('public.pipelines') IS NOT NULL AND to_regclass('public.plg_crm_pipelines') IS NULL THEN
17
+ ALTER TABLE public.pipelines RENAME TO plg_crm_pipelines;
18
+ END IF;
19
+ IF to_regclass('public.pipeline_stages') IS NOT NULL AND to_regclass('public.plg_crm_pipeline_stages') IS NULL THEN
20
+ ALTER TABLE public.pipeline_stages RENAME TO plg_crm_pipeline_stages;
21
+ END IF;
22
+ IF to_regclass('public.deal_extensions') IS NOT NULL AND to_regclass('public.plg_crm_deal_extensions') IS NULL THEN
23
+ ALTER TABLE public.deal_extensions RENAME TO plg_crm_deal_extensions;
24
+ END IF;
25
+ IF to_regclass('public.lead_sources') IS NOT NULL AND to_regclass('public.plg_crm_lead_sources') IS NULL THEN
26
+ ALTER TABLE public.lead_sources RENAME TO plg_crm_lead_sources;
27
+ END IF;
28
+ END $$;
@@ -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 = {
@@ -22,10 +23,35 @@ const tagEntity: EntityDef = {
22
23
  defaultSort: 'name',
23
24
  fields: [
24
25
  { key: 'name', label: 'Name', type: 'text', required: true, showInTable: true },
25
- { key: 'color', label: 'Color', type: 'text', showInTable: true, defaultValue: '#6366f1' },
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 },
30
+ }
31
+
32
+ const pipelineStageEntity: EntityDef = {
33
+ name: 'Stage',
34
+ namePlural: 'Stages',
35
+ icon: 'Workflow',
36
+ displayField: 'name',
37
+ defaultSort: 'order',
38
+ fields: [
39
+ { key: 'name', label: 'Name', type: 'text', required: true, showInTable: true, searchable: true },
40
+ {
41
+ key: 'pipelineId',
42
+ label: 'Pipeline',
43
+ type: 'relation',
44
+ relation: { table: T.pipelines, labelField: 'name' },
45
+ required: true,
46
+ showInTable: true,
47
+ },
48
+ { key: 'order', label: 'Order', type: 'number', required: true, showInTable: true, defaultValue: 0 },
49
+ { key: 'color', label: 'Color', type: 'color', showInTable: true, defaultValue: '#6366f1' },
50
+ { key: 'probability', label: 'Probability %', type: 'number', min: 0, max: 100, showInTable: true, defaultValue: 0 },
51
+ { key: 'isWon', label: 'Won Stage', type: 'boolean', showInTable: true, defaultValue: false },
52
+ { key: 'isLost', label: 'Lost Stage', type: 'boolean', showInTable: true, defaultValue: false },
53
+ ],
54
+ data: { table: T.pipelineStages, tenantScoped: true },
29
55
  }
30
56
 
31
57
  const activityTypeEntity: EntityDef = {
@@ -38,10 +64,25 @@ const activityTypeEntity: EntityDef = {
38
64
  { key: 'name', label: 'Name', type: 'text', required: true, showInTable: true },
39
65
  { key: 'isActive', label: 'Active', type: 'boolean', showInTable: true, defaultValue: true },
40
66
  ],
41
- data: { table: 'crm_activity_types', tenantScoped: true },
67
+ data: { table: T.activityTypes, tenantScoped: true },
42
68
  }
43
69
 
44
70
  export const crmRegistries: PluginRegistryDef[] = [
71
+ {
72
+ id: 'stages',
73
+ entity: pipelineStageEntity,
74
+ icon: 'Workflow',
75
+ description: 'Pipeline stages used by CRM deals',
76
+ display: 'table',
77
+ seedData: [
78
+ { id: 'stage-new', pipelineId: 'pipe-default', name: 'New Lead', order: 1, color: '#64748b', probability: 10, isWon: false, isLost: false },
79
+ { id: 'stage-qualified', pipelineId: 'pipe-default', name: 'Qualified', order: 2, color: '#3b82f6', probability: 30, isWon: false, isLost: false },
80
+ { id: 'stage-proposal', pipelineId: 'pipe-default', name: 'Proposal', order: 3, color: '#8b5cf6', probability: 60, isWon: false, isLost: false },
81
+ { id: 'stage-negotiation', pipelineId: 'pipe-default', name: 'Negotiation', order: 4, color: '#f59e0b', probability: 80, isWon: false, isLost: false },
82
+ { id: 'stage-won', pipelineId: 'pipe-default', name: 'Won', order: 5, color: '#10b981', probability: 100, isWon: true, isLost: false },
83
+ { id: 'stage-lost', pipelineId: 'pipe-default', name: 'Lost', order: 6, color: '#ef4444', probability: 0, isWon: false, isLost: true },
84
+ ],
85
+ },
45
86
  {
46
87
  id: 'lead-sources',
47
88
  entity: leadSourceEntity,