@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.
- package/README.md +2 -0
- package/dist/data/supabase.d.ts.map +1 -1
- package/dist/data/tables.d.ts +10 -0
- package/dist/data/tables.d.ts.map +1 -0
- package/dist/index.cjs +44 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +44 -30
- package/dist/index.js.map +1 -1
- package/dist/migrations/index.d.ts +10 -0
- package/dist/migrations/index.d.ts.map +1 -0
- package/dist/registries.d.ts.map +1 -1
- package/dist/schema/index.d.ts +65 -65
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema.cjs +23 -23
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.js +24 -24
- package/dist/schema.js.map +1 -1
- package/package.json +11 -5
- package/src/data/supabase.ts +30 -27
- package/src/data/tables.ts +11 -0
- package/src/index.ts +6 -3
- package/src/migrations/000_plg_rename.sql +28 -0
- package/src/migrations/001_crm_base.sql +32 -32
- package/src/migrations/002_activities.sql +14 -14
- package/src/migrations/003_crm_views_rls.sql +8 -8
- package/src/migrations/004_seed_default_pipeline.sql +5 -5
- package/src/migrations/index.ts +247 -0
- package/src/registries.ts +6 -5
- package/src/schema/index.ts +25 -25
package/dist/schema.cjs
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
var db = require('@fayz-ai/db');
|
|
4
4
|
|
|
5
5
|
// src/schema/index.ts
|
|
6
|
-
var pipelines = db.pgTable("
|
|
6
|
+
var pipelines = db.pgTable("plg_crm_pipelines", {
|
|
7
7
|
id: db.uuid("id").primaryKey().defaultRandom(),
|
|
8
8
|
tenantId: db.tenantId(),
|
|
9
9
|
name: db.text("name").notNull(),
|
|
10
10
|
isDefault: db.boolean("is_default").default(false),
|
|
11
11
|
isActive: db.boolean("is_active").notNull().default(true),
|
|
12
12
|
...db.timestamps
|
|
13
|
-
}, (t) => [db.index("
|
|
14
|
-
var pipelineStages = db.pgTable("
|
|
13
|
+
}, (t) => [db.index("idx_plg_crm_pipelines_tenant").on(t.tenantId)]);
|
|
14
|
+
var pipelineStages = db.pgTable("plg_crm_pipeline_stages", {
|
|
15
15
|
id: db.uuid("id").primaryKey().defaultRandom(),
|
|
16
16
|
tenantId: db.tenantId(),
|
|
17
17
|
pipelineId: db.uuid("pipeline_id").notNull().references(() => pipelines.id, { onDelete: "cascade" }),
|
|
@@ -23,25 +23,25 @@ var pipelineStages = db.pgTable("pipeline_stages", {
|
|
|
23
23
|
isLost: db.boolean("is_lost").default(false),
|
|
24
24
|
...db.createdAt
|
|
25
25
|
}, (t) => [
|
|
26
|
-
db.index("
|
|
27
|
-
db.index("
|
|
26
|
+
db.index("idx_plg_crm_pipeline_stages_tenant").on(t.tenantId),
|
|
27
|
+
db.index("idx_plg_crm_pipeline_stages_pipeline").on(t.pipelineId)
|
|
28
28
|
]);
|
|
29
|
-
var leadSources = db.pgTable("
|
|
29
|
+
var leadSources = db.pgTable("plg_crm_lead_sources", {
|
|
30
30
|
id: db.uuid("id").primaryKey().defaultRandom(),
|
|
31
31
|
tenantId: db.tenantId(),
|
|
32
32
|
name: db.text("name").notNull(),
|
|
33
33
|
isActive: db.boolean("is_active").notNull().default(true),
|
|
34
34
|
...db.createdAt
|
|
35
|
-
}, (t) => [db.index("
|
|
36
|
-
var crmTags = db.pgTable("
|
|
35
|
+
}, (t) => [db.index("idx_plg_crm_lead_sources_tenant").on(t.tenantId)]);
|
|
36
|
+
var crmTags = db.pgTable("plg_crm_tags", {
|
|
37
37
|
id: db.uuid("id").primaryKey().defaultRandom(),
|
|
38
38
|
tenantId: db.tenantId(),
|
|
39
39
|
name: db.text("name").notNull(),
|
|
40
40
|
color: db.text("color").default("#6366f1"),
|
|
41
41
|
isActive: db.boolean("is_active").notNull().default(true),
|
|
42
42
|
...db.createdAt
|
|
43
|
-
}, (t) => [db.index("
|
|
44
|
-
var dealExtensions = db.pgTable("
|
|
43
|
+
}, (t) => [db.index("idx_plg_crm_tags_tenant").on(t.tenantId)]);
|
|
44
|
+
var dealExtensions = db.pgTable("plg_crm_deal_extensions", {
|
|
45
45
|
id: db.uuid("id").primaryKey().defaultRandom(),
|
|
46
46
|
orderId: db.uuid("order_id").notNull().references(() => db.orders.id, { onDelete: "cascade" }),
|
|
47
47
|
tenantId: db.tenantId(),
|
|
@@ -49,27 +49,27 @@ var dealExtensions = db.pgTable("deal_extensions", {
|
|
|
49
49
|
stageId: db.uuid("stage_id").references(() => pipelineStages.id),
|
|
50
50
|
probability: db.numeric("probability", { precision: 5, scale: 2 }).default("0"),
|
|
51
51
|
expectedCloseDate: db.date("expected_close_date"),
|
|
52
|
-
leadId: db.uuid("lead_id").references(() => db.
|
|
52
|
+
leadId: db.uuid("lead_id").references(() => db.people.id),
|
|
53
53
|
lostReason: db.text("lost_reason"),
|
|
54
54
|
...db.timestamps
|
|
55
55
|
}, (t) => [
|
|
56
|
-
db.index("
|
|
57
|
-
db.index("
|
|
58
|
-
db.index("
|
|
56
|
+
db.index("idx_plg_crm_deal_extensions_tenant").on(t.tenantId),
|
|
57
|
+
db.index("idx_plg_crm_deal_extensions_order").on(t.orderId),
|
|
58
|
+
db.index("idx_plg_crm_deal_extensions_stage").on(t.stageId)
|
|
59
59
|
]);
|
|
60
|
-
var crmActivityTypes = db.pgTable("
|
|
60
|
+
var crmActivityTypes = db.pgTable("plg_crm_activity_types", {
|
|
61
61
|
id: db.uuid("id").primaryKey().defaultRandom(),
|
|
62
62
|
tenantId: db.tenantId(),
|
|
63
63
|
name: db.text("name").notNull(),
|
|
64
64
|
isActive: db.boolean("is_active").notNull().default(true),
|
|
65
65
|
...db.createdAt
|
|
66
|
-
}, (t) => [db.index("
|
|
67
|
-
var crmActivities = db.pgTable("
|
|
66
|
+
}, (t) => [db.index("idx_plg_crm_activity_types_tenant").on(t.tenantId)]);
|
|
67
|
+
var crmActivities = db.pgTable("plg_crm_activities", {
|
|
68
68
|
id: db.uuid("id").primaryKey().defaultRandom(),
|
|
69
69
|
tenantId: db.tenantId(),
|
|
70
70
|
dealId: db.uuid("deal_id").references(() => db.orders.id),
|
|
71
|
-
leadId: db.uuid("lead_id").references(() => db.
|
|
72
|
-
contactId: db.uuid("contact_id").references(() => db.
|
|
71
|
+
leadId: db.uuid("lead_id").references(() => db.people.id),
|
|
72
|
+
contactId: db.uuid("contact_id").references(() => db.people.id),
|
|
73
73
|
contactName: db.text("contact_name"),
|
|
74
74
|
activityType: db.text("activity_type").notNull(),
|
|
75
75
|
title: db.text("title").notNull(),
|
|
@@ -80,10 +80,10 @@ var crmActivities = db.pgTable("crm_activities", {
|
|
|
80
80
|
assignedToName: db.text("assigned_to_name"),
|
|
81
81
|
...db.createdAt
|
|
82
82
|
}, (t) => [
|
|
83
|
-
db.index("
|
|
84
|
-
db.index("
|
|
85
|
-
db.index("
|
|
86
|
-
db.index("
|
|
83
|
+
db.index("idx_plg_crm_activities_tenant").on(t.tenantId),
|
|
84
|
+
db.index("idx_plg_crm_activities_deal").on(t.dealId),
|
|
85
|
+
db.index("idx_plg_crm_activities_lead").on(t.leadId),
|
|
86
|
+
db.index("idx_plg_crm_activities_due").on(t.tenantId, t.dueDate)
|
|
87
87
|
]);
|
|
88
88
|
|
|
89
89
|
exports.crmActivities = crmActivities;
|
package/dist/schema.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/schema/index.ts"],"names":["pgTable","uuid","tenantId","text","boolean","timestamps","index","integer","numeric","createdAt","orders","date","
|
|
1
|
+
{"version":3,"sources":["../src/schema/index.ts"],"names":["pgTable","uuid","tenantId","text","boolean","timestamps","index","integer","numeric","createdAt","orders","date","people","timestamp"],"mappings":";;;;;AAgBO,IAAM,SAAA,GAAYA,WAAQ,mBAAA,EAAqB;AAAA,EACpD,IAAIC,OAAA,CAAK,IAAI,CAAA,CAAE,UAAA,GAAa,aAAA,EAAc;AA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AAAA,EAChDA,QAAA,CAAM,6BAA6B,CAAA,CAAE,EAAA,CAAG,EAAE,MAAM,CAAA;AAAA,EAChDA,SAAM,4BAA4B,CAAA,CAAE,GAAG,CAAA,CAAE,QAAA,EAAU,EAAE,OAAO;AAC9D,CAAC","file":"schema.cjs","sourcesContent":["// Builders come from @fayz-ai/db (single drizzle-orm instance) — never import\n// pgTable/uuid directly from drizzle-orm/pg-core. See MIGRATION-ARCHITECTURE §6.\nimport {\n pgTable, uuid, text, boolean, integer, numeric, date, timestamp, index,\n tenantId, timestamps, createdAt, people, orders,\n} from '@fayz-ai/db'\n\n// ---------------------------------------------------------------------------\n// CRM plugin — Ring-1 schema (schema-as-code).\n//\n// Leads live on public.people (kind='lead'); deals/quotes on public.orders.\n// These tables extend that spine with pipeline, tagging and activity data.\n// RLS policies, grants and the v_leads/v_deals read-views are companion SQL\n// (Drizzle doesn't diff grants/views) — see the app's drizzle/companion/.\n// ---------------------------------------------------------------------------\n\nexport const pipelines = pgTable('plg_crm_pipelines', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n name: text('name').notNull(),\n isDefault: boolean('is_default').default(false),\n isActive: boolean('is_active').notNull().default(true),\n ...timestamps,\n}, (t) => [index('idx_plg_crm_pipelines_tenant').on(t.tenantId)])\n\nexport const pipelineStages = pgTable('plg_crm_pipeline_stages', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n pipelineId: uuid('pipeline_id').notNull().references(() => pipelines.id, { onDelete: 'cascade' }),\n name: text('name').notNull(),\n order: integer('order').notNull().default(0),\n color: text('color').default('#6366f1'),\n probability: numeric('probability', { precision: 5, scale: 2 }).default('0'),\n isWon: boolean('is_won').default(false),\n isLost: boolean('is_lost').default(false),\n ...createdAt,\n}, (t) => [\n index('idx_plg_crm_pipeline_stages_tenant').on(t.tenantId),\n index('idx_plg_crm_pipeline_stages_pipeline').on(t.pipelineId),\n])\n\nexport const leadSources = pgTable('plg_crm_lead_sources', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n name: text('name').notNull(),\n isActive: boolean('is_active').notNull().default(true),\n ...createdAt,\n}, (t) => [index('idx_plg_crm_lead_sources_tenant').on(t.tenantId)])\n\nexport const crmTags = pgTable('plg_crm_tags', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n name: text('name').notNull(),\n color: text('color').default('#6366f1'),\n isActive: boolean('is_active').notNull().default(true),\n ...createdAt,\n}, (t) => [index('idx_plg_crm_tags_tenant').on(t.tenantId)])\n\nexport const dealExtensions = pgTable('plg_crm_deal_extensions', {\n id: uuid('id').primaryKey().defaultRandom(),\n orderId: uuid('order_id').notNull().references(() => orders.id, { onDelete: 'cascade' }),\n tenantId: tenantId(),\n pipelineId: uuid('pipeline_id').references(() => pipelines.id),\n stageId: uuid('stage_id').references(() => pipelineStages.id),\n probability: numeric('probability', { precision: 5, scale: 2 }).default('0'),\n expectedCloseDate: date('expected_close_date'),\n leadId: uuid('lead_id').references(() => people.id),\n lostReason: text('lost_reason'),\n ...timestamps,\n}, (t) => [\n index('idx_plg_crm_deal_extensions_tenant').on(t.tenantId),\n index('idx_plg_crm_deal_extensions_order').on(t.orderId),\n index('idx_plg_crm_deal_extensions_stage').on(t.stageId),\n])\n\nexport const crmActivityTypes = pgTable('plg_crm_activity_types', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n name: text('name').notNull(),\n isActive: boolean('is_active').notNull().default(true),\n ...createdAt,\n}, (t) => [index('idx_plg_crm_activity_types_tenant').on(t.tenantId)])\n\nexport const crmActivities = pgTable('plg_crm_activities', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n dealId: uuid('deal_id').references(() => orders.id),\n leadId: uuid('lead_id').references(() => people.id),\n contactId: uuid('contact_id').references(() => people.id),\n contactName: text('contact_name'),\n activityType: text('activity_type').notNull(),\n title: text('title').notNull(),\n description: text('description'),\n dueDate: date('due_date'),\n completedAt: timestamp('completed_at', { withTimezone: true }),\n assignedToId: uuid('assigned_to_id'),\n assignedToName: text('assigned_to_name'),\n ...createdAt,\n}, (t) => [\n index('idx_plg_crm_activities_tenant').on(t.tenantId),\n index('idx_plg_crm_activities_deal').on(t.dealId),\n index('idx_plg_crm_activities_lead').on(t.leadId),\n index('idx_plg_crm_activities_due').on(t.tenantId, t.dueDate),\n])\n"]}
|
package/dist/schema.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { pgTable, timestamps, boolean, text, tenantId, uuid, index, createdAt, numeric, integer,
|
|
1
|
+
import { pgTable, timestamps, boolean, text, tenantId, uuid, index, createdAt, numeric, integer, people, date, orders, timestamp } from '@fayz-ai/db';
|
|
2
2
|
|
|
3
3
|
// src/schema/index.ts
|
|
4
|
-
var pipelines = pgTable("
|
|
4
|
+
var pipelines = pgTable("plg_crm_pipelines", {
|
|
5
5
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
6
6
|
tenantId: tenantId(),
|
|
7
7
|
name: text("name").notNull(),
|
|
8
8
|
isDefault: boolean("is_default").default(false),
|
|
9
9
|
isActive: boolean("is_active").notNull().default(true),
|
|
10
10
|
...timestamps
|
|
11
|
-
}, (t) => [index("
|
|
12
|
-
var pipelineStages = pgTable("
|
|
11
|
+
}, (t) => [index("idx_plg_crm_pipelines_tenant").on(t.tenantId)]);
|
|
12
|
+
var pipelineStages = pgTable("plg_crm_pipeline_stages", {
|
|
13
13
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
14
14
|
tenantId: tenantId(),
|
|
15
15
|
pipelineId: uuid("pipeline_id").notNull().references(() => pipelines.id, { onDelete: "cascade" }),
|
|
@@ -21,25 +21,25 @@ var pipelineStages = pgTable("pipeline_stages", {
|
|
|
21
21
|
isLost: boolean("is_lost").default(false),
|
|
22
22
|
...createdAt
|
|
23
23
|
}, (t) => [
|
|
24
|
-
index("
|
|
25
|
-
index("
|
|
24
|
+
index("idx_plg_crm_pipeline_stages_tenant").on(t.tenantId),
|
|
25
|
+
index("idx_plg_crm_pipeline_stages_pipeline").on(t.pipelineId)
|
|
26
26
|
]);
|
|
27
|
-
var leadSources = pgTable("
|
|
27
|
+
var leadSources = pgTable("plg_crm_lead_sources", {
|
|
28
28
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
29
29
|
tenantId: tenantId(),
|
|
30
30
|
name: text("name").notNull(),
|
|
31
31
|
isActive: boolean("is_active").notNull().default(true),
|
|
32
32
|
...createdAt
|
|
33
|
-
}, (t) => [index("
|
|
34
|
-
var crmTags = pgTable("
|
|
33
|
+
}, (t) => [index("idx_plg_crm_lead_sources_tenant").on(t.tenantId)]);
|
|
34
|
+
var crmTags = pgTable("plg_crm_tags", {
|
|
35
35
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
36
36
|
tenantId: tenantId(),
|
|
37
37
|
name: text("name").notNull(),
|
|
38
38
|
color: text("color").default("#6366f1"),
|
|
39
39
|
isActive: boolean("is_active").notNull().default(true),
|
|
40
40
|
...createdAt
|
|
41
|
-
}, (t) => [index("
|
|
42
|
-
var dealExtensions = pgTable("
|
|
41
|
+
}, (t) => [index("idx_plg_crm_tags_tenant").on(t.tenantId)]);
|
|
42
|
+
var dealExtensions = pgTable("plg_crm_deal_extensions", {
|
|
43
43
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
44
44
|
orderId: uuid("order_id").notNull().references(() => orders.id, { onDelete: "cascade" }),
|
|
45
45
|
tenantId: tenantId(),
|
|
@@ -47,27 +47,27 @@ var dealExtensions = pgTable("deal_extensions", {
|
|
|
47
47
|
stageId: uuid("stage_id").references(() => pipelineStages.id),
|
|
48
48
|
probability: numeric("probability", { precision: 5, scale: 2 }).default("0"),
|
|
49
49
|
expectedCloseDate: date("expected_close_date"),
|
|
50
|
-
leadId: uuid("lead_id").references(() =>
|
|
50
|
+
leadId: uuid("lead_id").references(() => people.id),
|
|
51
51
|
lostReason: text("lost_reason"),
|
|
52
52
|
...timestamps
|
|
53
53
|
}, (t) => [
|
|
54
|
-
index("
|
|
55
|
-
index("
|
|
56
|
-
index("
|
|
54
|
+
index("idx_plg_crm_deal_extensions_tenant").on(t.tenantId),
|
|
55
|
+
index("idx_plg_crm_deal_extensions_order").on(t.orderId),
|
|
56
|
+
index("idx_plg_crm_deal_extensions_stage").on(t.stageId)
|
|
57
57
|
]);
|
|
58
|
-
var crmActivityTypes = pgTable("
|
|
58
|
+
var crmActivityTypes = pgTable("plg_crm_activity_types", {
|
|
59
59
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
60
60
|
tenantId: tenantId(),
|
|
61
61
|
name: text("name").notNull(),
|
|
62
62
|
isActive: boolean("is_active").notNull().default(true),
|
|
63
63
|
...createdAt
|
|
64
|
-
}, (t) => [index("
|
|
65
|
-
var crmActivities = pgTable("
|
|
64
|
+
}, (t) => [index("idx_plg_crm_activity_types_tenant").on(t.tenantId)]);
|
|
65
|
+
var crmActivities = pgTable("plg_crm_activities", {
|
|
66
66
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
67
67
|
tenantId: tenantId(),
|
|
68
68
|
dealId: uuid("deal_id").references(() => orders.id),
|
|
69
|
-
leadId: uuid("lead_id").references(() =>
|
|
70
|
-
contactId: uuid("contact_id").references(() =>
|
|
69
|
+
leadId: uuid("lead_id").references(() => people.id),
|
|
70
|
+
contactId: uuid("contact_id").references(() => people.id),
|
|
71
71
|
contactName: text("contact_name"),
|
|
72
72
|
activityType: text("activity_type").notNull(),
|
|
73
73
|
title: text("title").notNull(),
|
|
@@ -78,10 +78,10 @@ var crmActivities = pgTable("crm_activities", {
|
|
|
78
78
|
assignedToName: text("assigned_to_name"),
|
|
79
79
|
...createdAt
|
|
80
80
|
}, (t) => [
|
|
81
|
-
index("
|
|
82
|
-
index("
|
|
83
|
-
index("
|
|
84
|
-
index("
|
|
81
|
+
index("idx_plg_crm_activities_tenant").on(t.tenantId),
|
|
82
|
+
index("idx_plg_crm_activities_deal").on(t.dealId),
|
|
83
|
+
index("idx_plg_crm_activities_lead").on(t.leadId),
|
|
84
|
+
index("idx_plg_crm_activities_due").on(t.tenantId, t.dueDate)
|
|
85
85
|
]);
|
|
86
86
|
|
|
87
87
|
export { crmActivities, crmActivityTypes, crmTags, dealExtensions, leadSources, pipelineStages, pipelines };
|
package/dist/schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/schema/index.ts"],"names":[],"mappings":";;;AAgBO,IAAM,SAAA,GAAY,QAAQ,
|
|
1
|
+
{"version":3,"sources":["../src/schema/index.ts"],"names":[],"mappings":";;;AAgBO,IAAM,SAAA,GAAY,QAAQ,mBAAA,EAAqB;AAAA,EACpD,IAAI,IAAA,CAAK,IAAI,CAAA,CAAE,UAAA,GAAa,aAAA,EAAc;AAAA,EAC1C,UAAU,QAAA,EAAS;AAAA,EACnB,IAAA,EAAM,IAAA,CAAK,MAAM,CAAA,CAAE,OAAA,EAAQ;AAAA,EAC3B,SAAA,EAAW,OAAA,CAAQ,YAAY,CAAA,CAAE,QAAQ,KAAK,CAAA;AAAA,EAC9C,UAAU,OAAA,CAAQ,WAAW,EAAE,OAAA,EAAQ,CAAE,QAAQ,IAAI,CAAA;AAAA,EACrD,GAAG;AACL,CAAA,EAAG,CAAC,CAAA,KAAM,CAAC,KAAA,CAAM,8BAA8B,EAAE,EAAA,CAAG,CAAA,CAAE,QAAQ,CAAC,CAAC;AAEzD,IAAM,cAAA,GAAiB,QAAQ,yBAAA,EAA2B;AAAA,EAC/D,IAAI,IAAA,CAAK,IAAI,CAAA,CAAE,UAAA,GAAa,aAAA,EAAc;AAAA,EAC1C,UAAU,QAAA,EAAS;AAAA,EACnB,UAAA,EAAY,IAAA,CAAK,aAAa,CAAA,CAAE,OAAA,EAAQ,CAAE,UAAA,CAAW,MAAM,SAAA,CAAU,EAAA,EAAI,EAAE,QAAA,EAAU,WAAW,CAAA;AAAA,EAChG,IAAA,EAAM,IAAA,CAAK,MAAM,CAAA,CAAE,OAAA,EAAQ;AAAA,EAC3B,OAAO,O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UAAU,CAAA;AAAA,EACxB,aAAa,SAAA,CAAU,cAAA,EAAgB,EAAE,YAAA,EAAc,MAAM,CAAA;AAAA,EAC7D,YAAA,EAAc,KAAK,gBAAgB,CAAA;AAAA,EACnC,cAAA,EAAgB,KAAK,kBAAkB,CAAA;AAAA,EACvC,GAAG;AACL,CAAA,EAAG,CAAC,CAAA,KAAM;AAAA,EACR,KAAA,CAAM,+BAA+B,CAAA,CAAE,EAAA,CAAG,EAAE,QAAQ,CAAA;AAAA,EACpD,KAAA,CAAM,6BAA6B,CAAA,CAAE,EAAA,CAAG,EAAE,MAAM,CAAA;AAAA,EAChD,KAAA,CAAM,6BAA6B,CAAA,CAAE,EAAA,CAAG,EAAE,MAAM,CAAA;AAAA,EAChD,MAAM,4BAA4B,CAAA,CAAE,GAAG,CAAA,CAAE,QAAA,EAAU,EAAE,OAAO;AAC9D,CAAC","file":"schema.js","sourcesContent":["// Builders come from @fayz-ai/db (single drizzle-orm instance) — never import\n// pgTable/uuid directly from drizzle-orm/pg-core. See MIGRATION-ARCHITECTURE §6.\nimport {\n pgTable, uuid, text, boolean, integer, numeric, date, timestamp, index,\n tenantId, timestamps, createdAt, people, orders,\n} from '@fayz-ai/db'\n\n// ---------------------------------------------------------------------------\n// CRM plugin — Ring-1 schema (schema-as-code).\n//\n// Leads live on public.people (kind='lead'); deals/quotes on public.orders.\n// These tables extend that spine with pipeline, tagging and activity data.\n// RLS policies, grants and the v_leads/v_deals read-views are companion SQL\n// (Drizzle doesn't diff grants/views) — see the app's drizzle/companion/.\n// ---------------------------------------------------------------------------\n\nexport const pipelines = pgTable('plg_crm_pipelines', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n name: text('name').notNull(),\n isDefault: boolean('is_default').default(false),\n isActive: boolean('is_active').notNull().default(true),\n ...timestamps,\n}, (t) => [index('idx_plg_crm_pipelines_tenant').on(t.tenantId)])\n\nexport const pipelineStages = pgTable('plg_crm_pipeline_stages', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n pipelineId: uuid('pipeline_id').notNull().references(() => pipelines.id, { onDelete: 'cascade' }),\n name: text('name').notNull(),\n order: integer('order').notNull().default(0),\n color: text('color').default('#6366f1'),\n probability: numeric('probability', { precision: 5, scale: 2 }).default('0'),\n isWon: boolean('is_won').default(false),\n isLost: boolean('is_lost').default(false),\n ...createdAt,\n}, (t) => [\n index('idx_plg_crm_pipeline_stages_tenant').on(t.tenantId),\n index('idx_plg_crm_pipeline_stages_pipeline').on(t.pipelineId),\n])\n\nexport const leadSources = pgTable('plg_crm_lead_sources', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n name: text('name').notNull(),\n isActive: boolean('is_active').notNull().default(true),\n ...createdAt,\n}, (t) => [index('idx_plg_crm_lead_sources_tenant').on(t.tenantId)])\n\nexport const crmTags = pgTable('plg_crm_tags', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n name: text('name').notNull(),\n color: text('color').default('#6366f1'),\n isActive: boolean('is_active').notNull().default(true),\n ...createdAt,\n}, (t) => [index('idx_plg_crm_tags_tenant').on(t.tenantId)])\n\nexport const dealExtensions = pgTable('plg_crm_deal_extensions', {\n id: uuid('id').primaryKey().defaultRandom(),\n orderId: uuid('order_id').notNull().references(() => orders.id, { onDelete: 'cascade' }),\n tenantId: tenantId(),\n pipelineId: uuid('pipeline_id').references(() => pipelines.id),\n stageId: uuid('stage_id').references(() => pipelineStages.id),\n probability: numeric('probability', { precision: 5, scale: 2 }).default('0'),\n expectedCloseDate: date('expected_close_date'),\n leadId: uuid('lead_id').references(() => people.id),\n lostReason: text('lost_reason'),\n ...timestamps,\n}, (t) => [\n index('idx_plg_crm_deal_extensions_tenant').on(t.tenantId),\n index('idx_plg_crm_deal_extensions_order').on(t.orderId),\n index('idx_plg_crm_deal_extensions_stage').on(t.stageId),\n])\n\nexport const crmActivityTypes = pgTable('plg_crm_activity_types', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n name: text('name').notNull(),\n isActive: boolean('is_active').notNull().default(true),\n ...createdAt,\n}, (t) => [index('idx_plg_crm_activity_types_tenant').on(t.tenantId)])\n\nexport const crmActivities = pgTable('plg_crm_activities', {\n id: uuid('id').primaryKey().defaultRandom(),\n tenantId: tenantId(),\n dealId: uuid('deal_id').references(() => orders.id),\n leadId: uuid('lead_id').references(() => people.id),\n contactId: uuid('contact_id').references(() => people.id),\n contactName: text('contact_name'),\n activityType: text('activity_type').notNull(),\n title: text('title').notNull(),\n description: text('description'),\n dueDate: date('due_date'),\n completedAt: timestamp('completed_at', { withTimezone: true }),\n assignedToId: uuid('assigned_to_id'),\n assignedToName: text('assigned_to_name'),\n ...createdAt,\n}, (t) => [\n index('idx_plg_crm_activities_tenant').on(t.tenantId),\n index('idx_plg_crm_activities_deal').on(t.dealId),\n index('idx_plg_crm_activities_lead').on(t.leadId),\n index('idx_plg_crm_activities_due').on(t.tenantId, t.dueDate),\n])\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fayz-ai/plugin-crm",
|
|
3
|
-
"
|
|
3
|
+
"fayz": {
|
|
4
|
+
"status": "beta"
|
|
5
|
+
},
|
|
6
|
+
"version": "0.8.0",
|
|
4
7
|
"description": "Fayz SDK — plugin-crm plugin",
|
|
5
8
|
"type": "module",
|
|
6
9
|
"main": "./dist/index.cjs",
|
|
@@ -32,10 +35,10 @@
|
|
|
32
35
|
"lucide-react": "^0.400.0",
|
|
33
36
|
"zustand": "^4.5.0",
|
|
34
37
|
"@tanstack/react-table": "^8.20.0",
|
|
35
|
-
"@fayz-ai/core": "^0.
|
|
36
|
-
"@fayz-ai/
|
|
37
|
-
"@fayz-ai/
|
|
38
|
-
"@fayz-ai/db": "^0.
|
|
38
|
+
"@fayz-ai/core": "^0.8.0",
|
|
39
|
+
"@fayz-ai/ui": "^0.8.0",
|
|
40
|
+
"@fayz-ai/saas": "^0.8.0",
|
|
41
|
+
"@fayz-ai/db": "^0.8.0"
|
|
39
42
|
},
|
|
40
43
|
"devDependencies": {
|
|
41
44
|
"@types/react": "^18.3.0",
|
|
@@ -51,6 +54,9 @@
|
|
|
51
54
|
"fayz-plugin",
|
|
52
55
|
"fayz-sdk"
|
|
53
56
|
],
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
},
|
|
54
60
|
"scripts": {
|
|
55
61
|
"build": "tsup && tsc --emitDeclarationOnly --declaration --declarationMap --noEmit false",
|
|
56
62
|
"dev": "tsup --watch",
|
package/src/data/supabase.ts
CHANGED
|
@@ -8,11 +8,12 @@ import type {
|
|
|
8
8
|
import { getSupabaseClientOptional, getActiveTenantId } from '@fayz-ai/core'
|
|
9
9
|
import { getCrmTenantId } from '../lib/tenant'
|
|
10
10
|
import { deriveLeadStatus, deriveQuoteStatus } from '../cascade'
|
|
11
|
+
import { T } from './tables'
|
|
11
12
|
|
|
12
13
|
function getTenantId(): string | undefined {
|
|
13
14
|
// Plugin-local override wins; otherwise use the app's active tenant (set on
|
|
14
15
|
// org switch). Without this fallback, writes stamp tenant_id=undefined and
|
|
15
|
-
// fail the
|
|
16
|
+
// fail the people/orders RLS WITH CHECK (tenant_id IN user_tenant_ids()).
|
|
16
17
|
return getCrmTenantId() ?? getActiveTenantId()
|
|
17
18
|
}
|
|
18
19
|
|
|
@@ -27,23 +28,25 @@ function snakeToCamel(obj: Record<string, unknown>): Record<string, unknown> {
|
|
|
27
28
|
function getClients() {
|
|
28
29
|
const supabase = getSupabaseClientOptional() as any
|
|
29
30
|
if (!supabase) throw new Error('Supabase not initialized')
|
|
30
|
-
|
|
31
|
+
// Core tables now live in public alongside the plugin tables — a single public
|
|
32
|
+
// client serves both core (people/orders) and plugin (plg_crm_*) tables.
|
|
33
|
+
return { core: supabase, pub: supabase }
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
export function createSupabaseCrmProvider(options?: {
|
|
34
37
|
clientConversion?: { archetypeKind: string; extensionTable: string; fkColumn: string }
|
|
35
38
|
}): CrmDataProvider {
|
|
36
39
|
const provider: CrmDataProvider = {
|
|
37
|
-
// --- Pipelines (public.
|
|
40
|
+
// --- Pipelines (public.plg_crm_pipelines + plg_crm_pipeline_stages) ---
|
|
38
41
|
async getPipelines(): Promise<Pipeline[]> {
|
|
39
42
|
const { pub } = getClients()
|
|
40
|
-
const { data } = await pub.from(
|
|
43
|
+
const { data } = await pub.from(T.pipelines).select('*').eq('is_active', true).order('name')
|
|
41
44
|
return (data ?? []).map((r: any) => snakeToCamel(r) as unknown as Pipeline)
|
|
42
45
|
},
|
|
43
46
|
|
|
44
47
|
async getPipelineStages(pipelineId: string): Promise<PipelineStage[]> {
|
|
45
48
|
const { pub } = getClients()
|
|
46
|
-
const { data } = await pub.from(
|
|
49
|
+
const { data } = await pub.from(T.pipelineStages).select('*').eq('pipeline_id', pipelineId).order('order')
|
|
47
50
|
return (data ?? []).map((r: any) => snakeToCamel(r) as unknown as PipelineStage)
|
|
48
51
|
},
|
|
49
52
|
|
|
@@ -90,7 +93,7 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
90
93
|
async createLead(input: CreateLeadInput): Promise<Lead> {
|
|
91
94
|
const { core } = getClients()
|
|
92
95
|
const tenantId = getTenantId()
|
|
93
|
-
const { data, error } = await core.from('
|
|
96
|
+
const { data, error } = await core.from('people').insert({
|
|
94
97
|
tenant_id: tenantId, kind: 'lead', name: input.name,
|
|
95
98
|
email: input.email, phone: input.phone,
|
|
96
99
|
notes: input.notes, tags: input.tags ?? [], is_active: true,
|
|
@@ -109,7 +112,7 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
109
112
|
if (partial.phone !== undefined) row.phone = partial.phone
|
|
110
113
|
if (partial.notes !== undefined) row.notes = partial.notes
|
|
111
114
|
if (partial.status) row.metadata = { status: partial.status }
|
|
112
|
-
const { data, error } = await core.from('
|
|
115
|
+
const { data, error } = await core.from('people').update(row).eq('id', id).select().single()
|
|
113
116
|
if (error) throw new Error(error.message)
|
|
114
117
|
return (await provider.getLeadById(id))!
|
|
115
118
|
},
|
|
@@ -117,7 +120,7 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
117
120
|
async convertLeadToDeal(leadId: string, dealInput: CreateDealInput): Promise<Deal> {
|
|
118
121
|
const { core } = getClients()
|
|
119
122
|
// Update lead status to converted
|
|
120
|
-
await core.from('
|
|
123
|
+
await core.from('people').update({ metadata: { status: 'converted' } }).eq('id', leadId)
|
|
121
124
|
return provider.createDeal({ ...dealInput, leadId } as any)
|
|
122
125
|
},
|
|
123
126
|
|
|
@@ -203,7 +206,7 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
203
206
|
const { core, pub } = getClients()
|
|
204
207
|
const tenantId = getTenantId()
|
|
205
208
|
|
|
206
|
-
// Create order in
|
|
209
|
+
// Create order in public.orders
|
|
207
210
|
const { data: order, error } = await core.from('orders').insert({
|
|
208
211
|
tenant_id: tenantId, kind: 'deal', status: 'open',
|
|
209
212
|
total: input.value, party_id: input.contactId,
|
|
@@ -214,7 +217,7 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
214
217
|
if (error) throw new Error(error.message)
|
|
215
218
|
|
|
216
219
|
// Create deal extension with pipeline/stage info
|
|
217
|
-
await pub.from(
|
|
220
|
+
await pub.from(T.dealExtensions).insert({
|
|
218
221
|
order_id: order.id, tenant_id: tenantId,
|
|
219
222
|
pipeline_id: input.pipelineId, stage_id: input.stageId,
|
|
220
223
|
probability: 0, expected_close_date: input.expectedCloseDate,
|
|
@@ -244,15 +247,15 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
244
247
|
if (partial.stageId) row.stage_id = partial.stageId
|
|
245
248
|
if (partial.probability != null) row.probability = partial.probability
|
|
246
249
|
if (partial.expectedCloseDate) row.expected_close_date = partial.expectedCloseDate
|
|
247
|
-
await pub.from(
|
|
250
|
+
await pub.from(T.dealExtensions).update(row).eq('order_id', id)
|
|
248
251
|
}
|
|
249
252
|
return (await provider.getDealById(id))!
|
|
250
253
|
},
|
|
251
254
|
|
|
252
255
|
async moveDealToStage(dealId: string, stageId: string): Promise<Deal> {
|
|
253
256
|
const { core, pub } = getClients()
|
|
254
|
-
const { data: stageRow } = await pub.from(
|
|
255
|
-
await pub.from(
|
|
257
|
+
const { data: stageRow } = await pub.from(T.pipelineStages).select('*').eq('id', stageId).single()
|
|
258
|
+
await pub.from(T.dealExtensions).update({
|
|
256
259
|
stage_id: stageId, probability: stageRow?.probability ?? 0,
|
|
257
260
|
}).eq('order_id', dealId)
|
|
258
261
|
if (stageRow?.is_won) await core.from('orders').update({ status: 'won' }).eq('id', dealId)
|
|
@@ -267,10 +270,10 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
267
270
|
tenantId: stageRow.tenant_id, createdAt: stageRow.created_at,
|
|
268
271
|
}
|
|
269
272
|
// Update lead status
|
|
270
|
-
const { data: ext } = await pub.from(
|
|
273
|
+
const { data: ext } = await pub.from(T.dealExtensions).select('lead_id').eq('order_id', dealId).single()
|
|
271
274
|
if (ext?.lead_id) {
|
|
272
275
|
const newLeadStatus = deriveLeadStatus(typedStage)
|
|
273
|
-
await core.from('
|
|
276
|
+
await core.from('people').update({ metadata: { status: newLeadStatus } }).eq('id', ext.lead_id)
|
|
274
277
|
}
|
|
275
278
|
// Update quote statuses
|
|
276
279
|
const { data: quotes } = await core.from('orders').select('id, status').eq('kind', 'quote')
|
|
@@ -288,10 +291,10 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
288
291
|
return (await provider.getDealById(dealId))!
|
|
289
292
|
},
|
|
290
293
|
|
|
291
|
-
// --- Activities (public.
|
|
294
|
+
// --- Activities (public.plg_crm_activities) ---
|
|
292
295
|
async getActivities(query: ActivityQuery): Promise<PaginatedResult<Activity>> {
|
|
293
296
|
const { pub } = getClients()
|
|
294
|
-
let qb = pub.from(
|
|
297
|
+
let qb = pub.from(T.activities).select('*', { count: 'exact' })
|
|
295
298
|
if (query.dealId) qb = qb.eq('deal_id', query.dealId)
|
|
296
299
|
if (query.leadId) qb = qb.eq('lead_id', query.leadId)
|
|
297
300
|
if (query.activityType) qb = qb.eq('activity_type', query.activityType)
|
|
@@ -307,7 +310,7 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
307
310
|
async createActivity(input: CreateActivityInput): Promise<Activity> {
|
|
308
311
|
const { pub } = getClients()
|
|
309
312
|
const tenantId = getTenantId()
|
|
310
|
-
const { data, error } = await pub.from(
|
|
313
|
+
const { data, error } = await pub.from(T.activities).insert({
|
|
311
314
|
tenant_id: tenantId, deal_id: input.dealId, lead_id: input.leadId,
|
|
312
315
|
contact_id: input.contactId, activity_type: input.activityType,
|
|
313
316
|
title: input.title, description: input.description,
|
|
@@ -319,12 +322,12 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
319
322
|
|
|
320
323
|
async completeActivity(id: string): Promise<Activity> {
|
|
321
324
|
const { pub } = getClients()
|
|
322
|
-
const { data, error } = await pub.from(
|
|
325
|
+
const { data, error } = await pub.from(T.activities).update({ completed_at: new Date().toISOString() }).eq('id', id).select().single()
|
|
323
326
|
if (error) throw new Error(error.message)
|
|
324
327
|
return snakeToCamel(data) as unknown as Activity
|
|
325
328
|
},
|
|
326
329
|
|
|
327
|
-
// --- Quotes (
|
|
330
|
+
// --- Quotes (public.orders kind='quote' + order_items) ---
|
|
328
331
|
async getQuotes(query: QuoteQuery): Promise<PaginatedResult<Quote>> {
|
|
329
332
|
const { core } = getClients()
|
|
330
333
|
let qb = core.from('orders').select('*', { count: 'exact' }).eq('kind', 'quote')
|
|
@@ -504,7 +507,7 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
504
507
|
const personToConvert = quote.leadId ?? quote.contactId
|
|
505
508
|
if (personToConvert) {
|
|
506
509
|
const clientKind = options?.clientConversion?.archetypeKind ?? 'client'
|
|
507
|
-
const { data: person } = await core.from('
|
|
510
|
+
const { data: person } = await core.from('people')
|
|
508
511
|
.select('kind, name, email, phone, tenant_id, metadata')
|
|
509
512
|
.eq('id', personToConvert).single()
|
|
510
513
|
|
|
@@ -512,7 +515,7 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
512
515
|
const personKind = (person as any)?.kind as string | undefined
|
|
513
516
|
if (person && (personKind === 'lead' || personKind === 'contact')) {
|
|
514
517
|
const personMeta = (person as any)?.metadata ?? {}
|
|
515
|
-
await core.from('
|
|
518
|
+
await core.from('people').update({
|
|
516
519
|
kind: clientKind,
|
|
517
520
|
metadata: { ...personMeta, convertedFromLead: true },
|
|
518
521
|
}).eq('id', personToConvert)
|
|
@@ -577,7 +580,7 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
577
580
|
|
|
578
581
|
async getDealByLeadId(leadId: string): Promise<Deal | null> {
|
|
579
582
|
const { pub } = getClients()
|
|
580
|
-
const { data } = await pub.from(
|
|
583
|
+
const { data } = await pub.from(T.dealExtensions).select('order_id').eq('lead_id', leadId).limit(1)
|
|
581
584
|
if (!data || data.length === 0) return null
|
|
582
585
|
return provider.getDealById(data[0].order_id)
|
|
583
586
|
},
|
|
@@ -588,8 +591,8 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
588
591
|
const monthStart = new Date().toISOString().slice(0, 7) + '-01'
|
|
589
592
|
const today = new Date().toISOString().slice(0, 10)
|
|
590
593
|
|
|
591
|
-
const { count: totalLeads } = await core.from('
|
|
592
|
-
const { count: newLeads } = await core.from('
|
|
594
|
+
const { count: totalLeads } = await core.from('people').select('*', { count: 'exact', head: true }).eq('kind', 'lead')
|
|
595
|
+
const { count: newLeads } = await core.from('people').select('*', { count: 'exact', head: true }).eq('kind', 'lead').gte('created_at', monthStart)
|
|
593
596
|
const { data: deals } = await core.from('orders').select('status, total, updated_at').eq('kind', 'deal')
|
|
594
597
|
const allDeals = deals ?? []
|
|
595
598
|
const openDeals = allDeals.filter((d: any) => d.status === 'open')
|
|
@@ -597,8 +600,8 @@ export function createSupabaseCrmProvider(options?: {
|
|
|
597
600
|
const wonThisMonth = wonDeals.filter((d: any) => d.updated_at >= monthStart)
|
|
598
601
|
const convertedLeads = allDeals.length // each deal came from a lead conversion
|
|
599
602
|
|
|
600
|
-
const { count: pendingActs } = await pub.from(
|
|
601
|
-
const { count: overdueActs } = await pub.from(
|
|
603
|
+
const { count: pendingActs } = await pub.from(T.activities).select('*', { count: 'exact', head: true }).is('completed_at', null)
|
|
604
|
+
const { count: overdueActs } = await pub.from(T.activities).select('*', { count: 'exact', head: true }).is('completed_at', null).lt('due_date', today)
|
|
602
605
|
|
|
603
606
|
return {
|
|
604
607
|
totalLeads: totalLeads ?? 0,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Central physical-table-name registry for plugin-crm. Providers import T and
|
|
2
|
+
// never hardcode the plg_crm_* strings, so a future rename touches one file.
|
|
3
|
+
export const T = {
|
|
4
|
+
activities: 'plg_crm_activities',
|
|
5
|
+
tags: 'plg_crm_tags',
|
|
6
|
+
activityTypes: 'plg_crm_activity_types',
|
|
7
|
+
pipelines: 'plg_crm_pipelines',
|
|
8
|
+
pipelineStages: 'plg_crm_pipeline_stages',
|
|
9
|
+
dealExtensions: 'plg_crm_deal_extensions',
|
|
10
|
+
leadSources: 'plg_crm_lead_sources',
|
|
11
|
+
} as const
|
package/src/index.ts
CHANGED
|
@@ -54,14 +54,14 @@ export interface CrmPluginOptions {
|
|
|
54
54
|
/** Contact/person lookup for client search in quotes and leads */
|
|
55
55
|
contactLookup?: import('@fayz-ai/saas').EntityLookup
|
|
56
56
|
/** Client conversion config — when a lead is approved, CRM converts the person
|
|
57
|
-
* to a client by updating `
|
|
57
|
+
* to a client by updating `people.kind` and creating the extension table record.
|
|
58
58
|
* @example { archetypeKind: 'customer', extensionTable: 'clients', fkColumn: 'person_id' } */
|
|
59
59
|
clientConversion?: {
|
|
60
|
-
/** The archetype kind to set on
|
|
60
|
+
/** The archetype kind to set on people.kind (e.g., 'customer') */
|
|
61
61
|
archetypeKind: string
|
|
62
62
|
/** Extension table name (e.g., 'clients') in public schema */
|
|
63
63
|
extensionTable: string
|
|
64
|
-
/** FK column pointing to
|
|
64
|
+
/** FK column pointing to people.id (e.g., 'person_id') */
|
|
65
65
|
fkColumn: string
|
|
66
66
|
}
|
|
67
67
|
}
|
|
@@ -137,6 +137,9 @@ export function createCrmPlugin(options?: CrmPluginOptions): PluginManifest {
|
|
|
137
137
|
version: '1.0.0',
|
|
138
138
|
scope: options?.scope ?? 'universal',
|
|
139
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'],
|
|
140
143
|
defaultEnabled: true,
|
|
141
144
|
dependencies: [],
|
|
142
145
|
declaredFeatures: [
|
|
@@ -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 $$;
|