@fayz-ai/plugin-crm 0.9.0 → 0.9.1
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/README.md +3 -3
- package/dist/components/AutomationBolt.d.ts +22 -0
- package/dist/components/AutomationBolt.d.ts.map +1 -0
- package/dist/{CrmContext.d.ts → context.d.ts} +4 -2
- package/dist/context.d.ts.map +1 -0
- package/dist/data/registries.d.ts.map +1 -0
- package/dist/index.d.ts +16 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +573 -488
- package/dist/index.js.map +1 -1
- package/dist/{cascade.d.ts → lib/cascade.d.ts} +1 -1
- package/dist/lib/cascade.d.ts.map +1 -0
- package/dist/lib/message-events.d.ts +13 -0
- package/dist/lib/message-events.d.ts.map +1 -0
- package/dist/locales/en.d.ts.map +1 -1
- package/dist/locales/pt-BR.d.ts.map +1 -1
- package/dist/public/context.d.ts +1 -1
- package/dist/public/context.d.ts.map +1 -1
- package/dist/public/createPublicFormsPlugin.d.ts +1 -1
- package/dist/public/createPublicFormsPlugin.d.ts.map +1 -1
- package/dist/public/{data.supabase.d.ts → data/supabase.d.ts} +2 -2
- package/dist/public/data/supabase.d.ts.map +1 -0
- package/dist/public/index.d.ts +6 -6
- package/dist/public/index.d.ts.map +1 -1
- package/dist/public/index.js +2 -2
- package/dist/public/index.js.map +1 -1
- package/dist/public/{inbox.d.ts → lib/inbox.d.ts} +1 -1
- package/dist/public/lib/inbox.d.ts.map +1 -0
- package/dist/public/{uploads.d.ts → lib/uploads.d.ts} +1 -1
- package/dist/public/lib/uploads.d.ts.map +1 -0
- package/dist/store.d.ts +9 -0
- package/dist/store.d.ts.map +1 -1
- package/dist/views/ActivityListView.d.ts.map +1 -1
- package/dist/{CrmPage.d.ts → views/CrmPage.d.ts} +3 -3
- package/dist/views/CrmPage.d.ts.map +1 -0
- package/dist/views/DealSidebar.d.ts.map +1 -1
- package/dist/views/LeadListView.d.ts.map +1 -1
- package/dist/views/PipelineView.d.ts +3 -1
- package/dist/views/PipelineView.d.ts.map +1 -1
- package/dist/views/QuoteDetailView.d.ts.map +1 -1
- package/dist/views/QuoteFormView.d.ts.map +1 -1
- package/dist/views/QuoteListView.d.ts.map +1 -1
- package/dist/views/dashboardWidgets.d.ts +1 -1
- package/dist/views/dashboardWidgets.d.ts.map +1 -1
- package/package.json +6 -10
- package/dist/CrmContext.d.ts.map +0 -1
- package/dist/CrmPage.d.ts.map +0 -1
- package/dist/cascade.d.ts.map +0 -1
- package/dist/public/data.supabase.d.ts.map +0 -1
- package/dist/public/inbox.d.ts.map +0 -1
- package/dist/public/uploads.d.ts.map +0 -1
- package/dist/registries.d.ts.map +0 -1
- package/src/CrmContext.tsx +0 -30
- package/src/CrmPage.tsx +0 -150
- package/src/README.md +0 -97
- package/src/cascade.ts +0 -26
- package/src/components/BoardFilters.tsx +0 -190
- package/src/components/CrmGeneralSettings.tsx +0 -56
- package/src/components/CrmOnboarding.tsx +0 -52
- package/src/components/LeadAttachments.tsx +0 -140
- package/src/components/PipelineSettings.tsx +0 -45
- package/src/components/QuoteStatusDropdown.tsx +0 -226
- package/src/components/TagChips.tsx +0 -80
- package/src/data/index.ts +0 -2
- package/src/data/mock.ts +0 -559
- package/src/data/supabase.ts +0 -928
- package/src/data/tables.ts +0 -11
- package/src/data/types.ts +0 -61
- package/src/index.ts +0 -429
- package/src/lib/access.ts +0 -11
- package/src/lib/form-answers.tsx +0 -80
- package/src/lib/relative-time.ts +0 -84
- package/src/lib/tenant.ts +0 -4
- package/src/locales/en.ts +0 -307
- package/src/locales/index.ts +0 -7
- package/src/locales/pt-BR.ts +0 -307
- package/src/migrations/000_plg_rename.sql +0 -28
- package/src/migrations/001_crm_base.sql +0 -73
- package/src/migrations/002_activities.sql +0 -33
- package/src/migrations/003_crm_views_rls.sql +0 -69
- package/src/migrations/004_seed_default_pipeline.sql +0 -31
- package/src/migrations/005_public_lead.sql +0 -189
- package/src/migrations/006_lead_enters_pipeline.sql +0 -265
- package/src/migrations/007_activity_log.sql +0 -55
- package/src/migrations/008_view_field_aliases.sql +0 -43
- package/src/migrations/009_deal_archived_status.sql +0 -30
- package/src/migrations/010_quote_archived_status.sql +0 -40
- package/src/migrations/011_agent_rpc_update_lead.sql +0 -153
- package/src/migrations/012_agent_rpc_create_lead.sql +0 -128
- package/src/migrations/013_tag_slugs.sql +0 -177
- package/src/migrations/014_public_lead_tags.sql +0 -172
- package/src/migrations/015_lead_attachments.sql +0 -357
- package/src/migrations/016_public_lead_activity.sql +0 -123
- package/src/migrations/index.ts +0 -2023
- package/src/public/context.tsx +0 -150
- package/src/public/createPublicFormsPlugin.ts +0 -282
- package/src/public/data.supabase.ts +0 -92
- package/src/public/inbox.ts +0 -143
- package/src/public/index.ts +0 -51
- package/src/public/types.ts +0 -173
- package/src/public/uploads.ts +0 -205
- package/src/registries.ts +0 -162
- package/src/schema/index.ts +0 -104
- package/src/store.ts +0 -472
- package/src/types.ts +0 -337
- package/src/views/ActivityListView.tsx +0 -264
- package/src/views/DashboardView.tsx +0 -12
- package/src/views/DealSidebar.tsx +0 -482
- package/src/views/LeadDetailView.tsx +0 -378
- package/src/views/LeadFormView.tsx +0 -66
- package/src/views/LeadListView.tsx +0 -100
- package/src/views/PipelineView.tsx +0 -446
- package/src/views/QuoteDetailView.tsx +0 -412
- package/src/views/QuoteFormView.tsx +0 -494
- package/src/views/QuoteListView.tsx +0 -115
- package/src/views/dashboardWidgets.tsx +0 -248
- /package/dist/{registries.d.ts → data/registries.d.ts} +0 -0
|
@@ -1,28 +0,0 @@
|
|
|
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,73 +0,0 @@
|
|
|
1
|
-
-- CRM Plugin: Base Tables
|
|
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
|
-
-- These are plugin-specific extension tables
|
|
6
|
-
|
|
7
|
-
CREATE TABLE IF NOT EXISTS public.plg_crm_pipelines (
|
|
8
|
-
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
9
|
-
tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
|
|
10
|
-
name text NOT NULL,
|
|
11
|
-
is_default boolean DEFAULT false,
|
|
12
|
-
is_active boolean NOT NULL DEFAULT true,
|
|
13
|
-
created_at timestamptz NOT NULL DEFAULT now(),
|
|
14
|
-
updated_at timestamptz NOT NULL DEFAULT now()
|
|
15
|
-
);
|
|
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
|
-
|
|
19
|
-
CREATE TABLE IF NOT EXISTS public.plg_crm_pipeline_stages (
|
|
20
|
-
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
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
|
-
name text NOT NULL,
|
|
24
|
-
"order" integer NOT NULL DEFAULT 0,
|
|
25
|
-
color text DEFAULT '#6366f1',
|
|
26
|
-
probability numeric(5,2) DEFAULT 0,
|
|
27
|
-
is_won boolean DEFAULT false,
|
|
28
|
-
is_lost boolean DEFAULT false,
|
|
29
|
-
created_at timestamptz NOT NULL DEFAULT now()
|
|
30
|
-
);
|
|
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
|
-
|
|
35
|
-
CREATE TABLE IF NOT EXISTS public.plg_crm_lead_sources (
|
|
36
|
-
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
37
|
-
tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
|
|
38
|
-
name text NOT NULL,
|
|
39
|
-
is_active boolean NOT NULL DEFAULT true,
|
|
40
|
-
created_at timestamptz NOT NULL DEFAULT now()
|
|
41
|
-
);
|
|
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
|
-
|
|
45
|
-
CREATE TABLE IF NOT EXISTS public.plg_crm_tags (
|
|
46
|
-
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
47
|
-
tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
|
|
48
|
-
name text NOT NULL,
|
|
49
|
-
color text DEFAULT '#6366f1',
|
|
50
|
-
is_active boolean NOT NULL DEFAULT true,
|
|
51
|
-
created_at timestamptz NOT NULL DEFAULT now()
|
|
52
|
-
);
|
|
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
|
-
|
|
56
|
-
-- Deal extension: links public.orders (kind='deal') to pipeline stage
|
|
57
|
-
CREATE TABLE IF NOT EXISTS public.plg_crm_deal_extensions (
|
|
58
|
-
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
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
|
-
probability numeric(5,2) DEFAULT 0,
|
|
64
|
-
expected_close_date date,
|
|
65
|
-
lead_id uuid REFERENCES public.people(id),
|
|
66
|
-
lost_reason text,
|
|
67
|
-
created_at timestamptz NOT NULL DEFAULT now(),
|
|
68
|
-
updated_at timestamptz NOT NULL DEFAULT now()
|
|
69
|
-
);
|
|
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,33 +0,0 @@
|
|
|
1
|
-
-- CRM Plugin: Activities & Tasks
|
|
2
|
-
|
|
3
|
-
CREATE TABLE IF NOT EXISTS public.plg_crm_activity_types (
|
|
4
|
-
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
5
|
-
tenant_id uuid NOT NULL REFERENCES public.tenants(id) ON DELETE CASCADE,
|
|
6
|
-
name text NOT NULL,
|
|
7
|
-
is_active boolean NOT NULL DEFAULT true,
|
|
8
|
-
created_at timestamptz NOT NULL DEFAULT now()
|
|
9
|
-
);
|
|
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
|
-
|
|
13
|
-
CREATE TABLE IF NOT EXISTS public.plg_crm_activities (
|
|
14
|
-
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
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
|
-
contact_name text,
|
|
20
|
-
activity_type text NOT NULL,
|
|
21
|
-
title text NOT NULL,
|
|
22
|
-
description text,
|
|
23
|
-
due_date date,
|
|
24
|
-
completed_at timestamptz,
|
|
25
|
-
assigned_to_id uuid,
|
|
26
|
-
assigned_to_name text,
|
|
27
|
-
created_at timestamptz NOT NULL DEFAULT now()
|
|
28
|
-
);
|
|
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);
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
-- CRM plugin — companion SQL (functions/views/RLS Drizzle doesn't diff).
|
|
2
|
-
-- Tables come from the Drizzle schema (src/schema/); this is everything else the
|
|
3
|
-
-- plugin's data provider depends on: RLS policies + grants + the read-views.
|
|
4
|
-
-- Idempotent. Canonical RLS form: tenant_id IN (SELECT public.user_tenant_ids()).
|
|
5
|
-
|
|
6
|
-
DO $$
|
|
7
|
-
DECLARE t text;
|
|
8
|
-
BEGIN
|
|
9
|
-
FOR t IN SELECT unnest(ARRAY[
|
|
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
|
-
])
|
|
13
|
-
LOOP
|
|
14
|
-
EXECUTE format('ALTER TABLE public.%I ENABLE ROW LEVEL SECURITY', t);
|
|
15
|
-
EXECUTE format('GRANT SELECT, INSERT, UPDATE, DELETE ON public.%I TO authenticated', t);
|
|
16
|
-
IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='public' AND tablename=t AND policyname=t||'_select') THEN
|
|
17
|
-
EXECUTE format('CREATE POLICY %I ON public.%I FOR SELECT TO authenticated USING (tenant_id IN (SELECT public.user_tenant_ids()))', t||'_select', t);
|
|
18
|
-
END IF;
|
|
19
|
-
IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='public' AND tablename=t AND policyname=t||'_insert') THEN
|
|
20
|
-
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);
|
|
21
|
-
END IF;
|
|
22
|
-
IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='public' AND tablename=t AND policyname=t||'_update') THEN
|
|
23
|
-
EXECUTE format('CREATE POLICY %I ON public.%I FOR UPDATE TO authenticated USING (tenant_id IN (SELECT public.user_tenant_ids()))', t||'_update', t);
|
|
24
|
-
END IF;
|
|
25
|
-
IF NOT EXISTS (SELECT 1 FROM pg_policies WHERE schemaname='public' AND tablename=t AND policyname=t||'_delete') THEN
|
|
26
|
-
EXECUTE format('CREATE POLICY %I ON public.%I FOR DELETE TO authenticated USING (tenant_id IN (SELECT public.user_tenant_ids()))', t||'_delete', t);
|
|
27
|
-
END IF;
|
|
28
|
-
END LOOP;
|
|
29
|
-
END $$;
|
|
30
|
-
|
|
31
|
-
-- v_leads: public.people (kind='lead')
|
|
32
|
-
-- DROP first: 005 widens this view (form_id/custom_fields/utm), and CREATE OR
|
|
33
|
-
-- REPLACE cannot narrow a live view — replaying 003 on a pool that already ran
|
|
34
|
-
-- 005 would fail with "cannot drop columns from view". 005 replays right after
|
|
35
|
-
-- and widens it again, so the net result is unchanged.
|
|
36
|
-
DROP VIEW IF EXISTS public.v_leads;
|
|
37
|
-
CREATE VIEW public.v_leads WITH (security_invoker=true) AS
|
|
38
|
-
SELECT
|
|
39
|
-
p.id, p.tenant_id, p.name, p.email, p.phone, p.notes, p.tags, p.is_active,
|
|
40
|
-
p.metadata->>'company' AS company,
|
|
41
|
-
p.metadata->>'sourceId' AS source_id,
|
|
42
|
-
p.metadata->>'sourceName' AS source_name,
|
|
43
|
-
COALESCE(p.metadata->>'status','new') AS lead_status,
|
|
44
|
-
p.metadata->>'value' AS lead_value,
|
|
45
|
-
p.metadata->>'assignedToId' AS assigned_to_id,
|
|
46
|
-
p.created_at, p.updated_at
|
|
47
|
-
FROM public.people p
|
|
48
|
-
WHERE p.kind = 'lead';
|
|
49
|
-
GRANT SELECT ON public.v_leads TO authenticated;
|
|
50
|
-
|
|
51
|
-
-- v_deals: public.orders (kind='deal') JOIN plg_crm_deal_extensions JOIN plg_crm_pipeline_stages
|
|
52
|
-
-- DROP first for the same reason as v_leads above: 008 appends the `stage`
|
|
53
|
-
-- alias, and CREATE OR REPLACE cannot narrow a live view on replay.
|
|
54
|
-
DROP VIEW IF EXISTS public.v_deals;
|
|
55
|
-
CREATE VIEW public.v_deals WITH (security_invoker=true) AS
|
|
56
|
-
SELECT
|
|
57
|
-
o.id, o.tenant_id, o.status,
|
|
58
|
-
o.total AS value, o.notes AS title,
|
|
59
|
-
o.party_id AS contact_id, o.assignee_id AS assigned_to_id, o.tags,
|
|
60
|
-
o.metadata->>'contactName' AS contact_name,
|
|
61
|
-
o.created_at, o.updated_at,
|
|
62
|
-
de.pipeline_id, de.stage_id, de.probability, de.expected_close_date,
|
|
63
|
-
de.lead_id, de.lost_reason,
|
|
64
|
-
ps.name AS stage_name, ps.color AS stage_color, ps."order" AS stage_order
|
|
65
|
-
FROM public.orders o
|
|
66
|
-
LEFT JOIN public.plg_crm_deal_extensions de ON de.order_id = o.id
|
|
67
|
-
LEFT JOIN public.plg_crm_pipeline_stages ps ON ps.id = de.stage_id
|
|
68
|
-
WHERE o.kind = 'deal';
|
|
69
|
-
GRANT SELECT ON public.v_deals TO authenticated;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
-- CRM plugin — seed: default "Sales Pipeline" + canonical stages.
|
|
2
|
-
-- The pipeline board (#/sales/pipeline) reads public.plg_crm_pipelines / plg_crm_pipeline_stages
|
|
3
|
-
-- directly; a fresh tenant has zero rows and the board renders blank. This
|
|
4
|
-
-- backfills the default pipeline (matching the plugin's mock/offline provider)
|
|
5
|
-
-- for every tenant that has none. Idempotent — tenants with a pipeline are skipped.
|
|
6
|
-
-- Plugin-owned so EVERY app gets it on install (not re-authored per app).
|
|
7
|
-
|
|
8
|
-
DO $$
|
|
9
|
-
DECLARE
|
|
10
|
-
t_id uuid;
|
|
11
|
-
p_id uuid;
|
|
12
|
-
BEGIN
|
|
13
|
-
FOR t_id IN (
|
|
14
|
-
SELECT id FROM public.tenants
|
|
15
|
-
WHERE id NOT IN (SELECT tenant_id FROM public.plg_crm_pipelines)
|
|
16
|
-
)
|
|
17
|
-
LOOP
|
|
18
|
-
INSERT INTO public.plg_crm_pipelines (tenant_id, name, is_default, is_active)
|
|
19
|
-
VALUES (t_id, 'Sales Pipeline', true, true)
|
|
20
|
-
RETURNING id INTO p_id;
|
|
21
|
-
|
|
22
|
-
INSERT INTO public.plg_crm_pipeline_stages (tenant_id, pipeline_id, name, "order", color, probability, is_won, is_lost) VALUES
|
|
23
|
-
(t_id, p_id, 'New', 0, '#6366f1', 10, false, false),
|
|
24
|
-
(t_id, p_id, 'Contacted', 1, '#3b82f6', 25, false, false),
|
|
25
|
-
(t_id, p_id, 'Qualified', 2, '#f59e0b', 50, false, false),
|
|
26
|
-
(t_id, p_id, 'Proposal', 3, '#f97316', 75, false, false),
|
|
27
|
-
(t_id, p_id, 'Negotiation', 4, '#8b5cf6', 90, false, false),
|
|
28
|
-
(t_id, p_id, 'Won', 5, '#22c55e', 100, true, false),
|
|
29
|
-
(t_id, p_id, 'Lost', 6, '#ef4444', 0, false, true);
|
|
30
|
-
END LOOP;
|
|
31
|
-
END $$;
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
-- plugin-crm 005: anon-safe lead capture from public sites.
|
|
2
|
-
--
|
|
3
|
-
-- The counterpart of agenda's create_public_booking: a marketing site posts a
|
|
4
|
-
-- form and a lead lands in the CRM of the right tenant. Same shape of trust —
|
|
5
|
-
-- the caller is an anonymous browser holding only a publishable key, so every
|
|
6
|
-
-- decision that matters (which tenant, which status, whether it is spam) is
|
|
7
|
-
-- made here and cannot be set by the caller.
|
|
8
|
-
--
|
|
9
|
-
-- Why p_fields jsonb: every landing page asks something different (hair
|
|
10
|
-
-- coverage on one, discount coupon on another, subject/message on a third).
|
|
11
|
-
-- Modelling those as columns means a migration per campaign — the site this was
|
|
12
|
-
-- built for already had 22 columns on its lead table. They go to people.metadata
|
|
13
|
-
-- under 'fields', which v_leads already reads from, so a new form ships with
|
|
14
|
-
-- zero schema change.
|
|
15
|
-
|
|
16
|
-
CREATE OR REPLACE FUNCTION public.create_public_lead(
|
|
17
|
-
p_tenant_id uuid,
|
|
18
|
-
p_name text,
|
|
19
|
-
p_phone text DEFAULT NULL,
|
|
20
|
-
p_email text DEFAULT NULL,
|
|
21
|
-
p_form_id text DEFAULT NULL,
|
|
22
|
-
p_form_name text DEFAULT NULL,
|
|
23
|
-
p_fields jsonb DEFAULT '{}'::jsonb,
|
|
24
|
-
p_notes text DEFAULT NULL,
|
|
25
|
-
p_utm jsonb DEFAULT '{}'::jsonb
|
|
26
|
-
)
|
|
27
|
-
RETURNS TABLE(lead_id uuid, created_at timestamptz)
|
|
28
|
-
LANGUAGE plpgsql
|
|
29
|
-
SECURITY DEFINER
|
|
30
|
-
SET search_path TO 'public'
|
|
31
|
-
AS $function$
|
|
32
|
-
DECLARE
|
|
33
|
-
v_person_id uuid;
|
|
34
|
-
v_phone text;
|
|
35
|
-
v_email text;
|
|
36
|
-
v_fields jsonb;
|
|
37
|
-
v_utm jsonb;
|
|
38
|
-
v_recent int;
|
|
39
|
-
BEGIN
|
|
40
|
-
-- ---------------------------------------------------------------------
|
|
41
|
-
-- validation: anon-callable, so everything is checked here
|
|
42
|
-
-- ---------------------------------------------------------------------
|
|
43
|
-
IF p_name IS NULL OR length(trim(p_name)) NOT BETWEEN 2 AND 120 THEN
|
|
44
|
-
RAISE EXCEPTION 'invalid name';
|
|
45
|
-
END IF;
|
|
46
|
-
|
|
47
|
-
v_phone := NULLIF(regexp_replace(COALESCE(p_phone, ''), '\D', '', 'g'), '');
|
|
48
|
-
IF v_phone IS NOT NULL AND length(v_phone) NOT BETWEEN 8 AND 15 THEN
|
|
49
|
-
RAISE EXCEPTION 'invalid phone';
|
|
50
|
-
END IF;
|
|
51
|
-
|
|
52
|
-
v_email := NULLIF(trim(COALESCE(p_email, '')), '');
|
|
53
|
-
IF v_email IS NOT NULL AND (length(v_email) > 254 OR v_email !~ '^[^@\s]+@[^@\s]+\.[^@\s]+$') THEN
|
|
54
|
-
RAISE EXCEPTION 'invalid email';
|
|
55
|
-
END IF;
|
|
56
|
-
|
|
57
|
-
-- a lead with no way to reach them back is not a lead
|
|
58
|
-
IF v_phone IS NULL AND v_email IS NULL THEN
|
|
59
|
-
RAISE EXCEPTION 'phone or email required';
|
|
60
|
-
END IF;
|
|
61
|
-
|
|
62
|
-
PERFORM 1 FROM tenants t WHERE t.id = p_tenant_id;
|
|
63
|
-
IF NOT FOUND THEN RAISE EXCEPTION 'unknown tenant'; END IF;
|
|
64
|
-
|
|
65
|
-
-- ---------------------------------------------------------------------
|
|
66
|
-
-- custom fields: object-only, bounded, and scalars only.
|
|
67
|
-
-- Bounding the payload keeps a hostile caller from parking documents in
|
|
68
|
-
-- the CRM; flattening nested values keeps the detail panel renderable.
|
|
69
|
-
-- ---------------------------------------------------------------------
|
|
70
|
-
v_fields := COALESCE(p_fields, '{}'::jsonb);
|
|
71
|
-
IF jsonb_typeof(v_fields) <> 'object' THEN
|
|
72
|
-
RAISE EXCEPTION 'fields must be an object';
|
|
73
|
-
END IF;
|
|
74
|
-
IF length(v_fields::text) > 8000 THEN
|
|
75
|
-
RAISE EXCEPTION 'fields too large';
|
|
76
|
-
END IF;
|
|
77
|
-
IF (SELECT count(*) FROM jsonb_object_keys(v_fields)) > 40 THEN
|
|
78
|
-
RAISE EXCEPTION 'too many fields';
|
|
79
|
-
END IF;
|
|
80
|
-
|
|
81
|
-
v_utm := COALESCE(p_utm, '{}'::jsonb);
|
|
82
|
-
IF jsonb_typeof(v_utm) <> 'object' OR length(v_utm::text) > 2000 THEN
|
|
83
|
-
RAISE EXCEPTION 'invalid utm';
|
|
84
|
-
END IF;
|
|
85
|
-
|
|
86
|
-
-- ---------------------------------------------------------------------
|
|
87
|
-
-- anti-spam: same tenant + same contact + same form, twice in 5 minutes is
|
|
88
|
-
-- a double-click or a bot, not two inquiries. Repeat interest days later IS
|
|
89
|
-
-- a new lead, so this window stays deliberately short.
|
|
90
|
-
-- ---------------------------------------------------------------------
|
|
91
|
-
SELECT count(*) INTO v_recent
|
|
92
|
-
FROM people p
|
|
93
|
-
WHERE p.tenant_id = p_tenant_id
|
|
94
|
-
AND p.kind = 'lead'
|
|
95
|
-
AND p.created_at > now() - interval '5 minutes'
|
|
96
|
-
AND COALESCE(p.metadata->>'formId', '') = COALESCE(p_form_id, '')
|
|
97
|
-
AND (
|
|
98
|
-
(v_phone IS NOT NULL AND p.phone = v_phone)
|
|
99
|
-
OR (v_email IS NOT NULL AND p.email = v_email)
|
|
100
|
-
);
|
|
101
|
-
IF v_recent > 0 THEN
|
|
102
|
-
RAISE EXCEPTION 'duplicate submission';
|
|
103
|
-
END IF;
|
|
104
|
-
|
|
105
|
-
-- burst cap per tenant: a flood is never legitimate marketing traffic
|
|
106
|
-
IF (
|
|
107
|
-
SELECT count(*) FROM people p
|
|
108
|
-
WHERE p.tenant_id = p_tenant_id
|
|
109
|
-
AND p.kind = 'lead'
|
|
110
|
-
AND p.created_at > now() - interval '1 minute'
|
|
111
|
-
) >= 20 THEN
|
|
112
|
-
RAISE EXCEPTION 'too many submissions';
|
|
113
|
-
END IF;
|
|
114
|
-
|
|
115
|
-
-- ---------------------------------------------------------------------
|
|
116
|
-
-- the lead. status/kind are hardcoded — the caller cannot promote itself
|
|
117
|
-
-- into a customer or land pre-qualified.
|
|
118
|
-
-- ---------------------------------------------------------------------
|
|
119
|
-
INSERT INTO people (tenant_id, kind, name, phone, email, notes, tags, is_active, metadata)
|
|
120
|
-
VALUES (
|
|
121
|
-
p_tenant_id,
|
|
122
|
-
'lead',
|
|
123
|
-
trim(p_name),
|
|
124
|
-
v_phone,
|
|
125
|
-
v_email,
|
|
126
|
-
left(p_notes, 2000),
|
|
127
|
-
ARRAY[]::text[],
|
|
128
|
-
true,
|
|
129
|
-
jsonb_strip_nulls(
|
|
130
|
-
jsonb_build_object(
|
|
131
|
-
'status', 'new',
|
|
132
|
-
'source', 'public_form',
|
|
133
|
-
'sourceName', COALESCE(NULLIF(trim(COALESCE(p_form_name, '')), ''), 'Site'),
|
|
134
|
-
'formId', NULLIF(trim(COALESCE(p_form_id, '')), ''),
|
|
135
|
-
'fields', v_fields,
|
|
136
|
-
'utm', CASE WHEN v_utm = '{}'::jsonb THEN NULL ELSE v_utm END
|
|
137
|
-
)
|
|
138
|
-
)
|
|
139
|
-
)
|
|
140
|
-
RETURNING id, people.created_at INTO v_person_id, created_at;
|
|
141
|
-
|
|
142
|
-
lead_id := v_person_id;
|
|
143
|
-
RETURN NEXT;
|
|
144
|
-
END;
|
|
145
|
-
$function$;
|
|
146
|
-
|
|
147
|
-
-- anon: the whole point. authenticated too, so a logged-in visitor on the same
|
|
148
|
-
-- site takes the identical path.
|
|
149
|
-
GRANT EXECUTE ON FUNCTION public.create_public_lead(uuid, text, text, text, text, text, jsonb, text, jsonb)
|
|
150
|
-
TO anon, authenticated;
|
|
151
|
-
|
|
152
|
-
-- Leads are read through v_leads, which is tenant-scoped by RLS on people.
|
|
153
|
-
-- No anon SELECT is granted here: a public form writes, it never reads back.
|
|
154
|
-
CREATE INDEX IF NOT EXISTS idx_people_lead_tenant_created
|
|
155
|
-
ON public.people (tenant_id, created_at DESC)
|
|
156
|
-
WHERE kind = 'lead';
|
|
157
|
-
|
|
158
|
-
-- ---------------------------------------------------------------------------
|
|
159
|
-
-- v_leads gains the custom-field payload.
|
|
160
|
-
--
|
|
161
|
-
-- Without this the whole abstraction is write-only: a form's answers reach
|
|
162
|
-
-- people.metadata and no CRM screen can read them back. Columns are APPENDED
|
|
163
|
-
-- (CREATE OR REPLACE VIEW cannot reorder or drop), and the view keeps
|
|
164
|
-
-- security_invoker=true so people's RLS still scopes rows per tenant.
|
|
165
|
-
-- ---------------------------------------------------------------------------
|
|
166
|
-
CREATE OR REPLACE VIEW public.v_leads
|
|
167
|
-
WITH (security_invoker = true) AS
|
|
168
|
-
SELECT id,
|
|
169
|
-
tenant_id,
|
|
170
|
-
name,
|
|
171
|
-
email,
|
|
172
|
-
phone,
|
|
173
|
-
notes,
|
|
174
|
-
tags,
|
|
175
|
-
is_active,
|
|
176
|
-
(metadata ->> 'company'::text) AS company,
|
|
177
|
-
(metadata ->> 'sourceId'::text) AS source_id,
|
|
178
|
-
(metadata ->> 'sourceName'::text) AS source_name,
|
|
179
|
-
COALESCE((metadata ->> 'status'::text), 'new'::text) AS lead_status,
|
|
180
|
-
(metadata ->> 'value'::text) AS lead_value,
|
|
181
|
-
(metadata ->> 'assignedToId'::text) AS assigned_to_id,
|
|
182
|
-
created_at,
|
|
183
|
-
updated_at,
|
|
184
|
-
-- appended:
|
|
185
|
-
(metadata ->> 'formId'::text) AS form_id,
|
|
186
|
-
COALESCE(metadata -> 'fields'::text, '{}'::jsonb) AS custom_fields,
|
|
187
|
-
COALESCE(metadata -> 'utm'::text, '{}'::jsonb) AS utm
|
|
188
|
-
FROM people p
|
|
189
|
-
WHERE (kind = 'lead'::text);
|