@erxes/core-contract 0.1.0-v2.0 → 0.1.0-v2.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.
Files changed (94) hide show
  1. package/dist/activities.d.ts +21 -14
  2. package/dist/activities.js +6 -10
  3. package/dist/audit.d.ts +13 -17
  4. package/dist/audit.js +9 -9
  5. package/dist/auth.d.ts +33 -35
  6. package/dist/auth.js +8 -7
  7. package/dist/automationDefinition.d.ts +4 -3
  8. package/dist/automationDefinition.js +9 -31
  9. package/dist/automationRuns.d.ts +6 -5
  10. package/dist/automationRuns.js +8 -22
  11. package/dist/automations.d.ts +35 -36
  12. package/dist/automations.js +15 -23
  13. package/dist/common.d.ts +29 -4
  14. package/dist/common.js +30 -10
  15. package/dist/companies.d.ts +34 -34
  16. package/dist/companies.js +6 -5
  17. package/dist/contract/routes.json.js +1794 -0
  18. package/dist/customerAuth.d.ts +222 -0
  19. package/dist/customerAuth.js +180 -0
  20. package/dist/customers.d.ts +51 -54
  21. package/dist/customers.js +7 -12
  22. package/dist/db/src/enums.d.ts +8 -0
  23. package/dist/db/src/enums.js +336 -0
  24. package/dist/db/src/schema/auditStreams.js +6 -14
  25. package/dist/db/src/schema/authTokens.js +4 -6
  26. package/dist/db/src/schema/automations.js +12 -28
  27. package/dist/db/src/schema/common.d.ts +2 -6
  28. package/dist/db/src/schema/common.js +1 -24
  29. package/dist/db/src/schema/companies.js +6 -5
  30. package/dist/db/src/schema/customerAuth.js +101 -0
  31. package/dist/db/src/schema/customers.js +6 -6
  32. package/dist/db/src/schema/events.js +12 -60
  33. package/dist/db/src/schema/fieldValues.js +2 -2
  34. package/dist/db/src/schema/fields.js +10 -28
  35. package/dist/db/src/schema/index.js +21 -20
  36. package/dist/db/src/schema/leadStages.js +5 -10
  37. package/dist/db/src/schema/leads.js +5 -10
  38. package/dist/db/src/schema/oauth.js +3 -3
  39. package/dist/db/src/schema/payments.js +12 -22
  40. package/dist/db/src/schema/platform.js +11 -26
  41. package/dist/db/src/schema/plugins.js +4 -4
  42. package/dist/db/src/schema/products.js +7 -8
  43. package/dist/db/src/schema/rateLimits.js +7 -0
  44. package/dist/db/src/schema/saas.js +3 -9
  45. package/dist/db/src/schema/tags.js +6 -5
  46. package/dist/db/src/schema/transfer.js +12 -32
  47. package/dist/db/src/schema/trash.js +6 -7
  48. package/dist/db/src/schema/users.js +6 -10
  49. package/dist/db/src/schema/views.js +5 -13
  50. package/dist/db/src/schema/webhooks.js +7 -14
  51. package/dist/db/src/table.js +16 -2
  52. package/dist/events.d.ts +19 -23
  53. package/dist/events.js +16 -18
  54. package/dist/fields.d.ts +22 -50
  55. package/dist/fields.js +6 -28
  56. package/dist/files.d.ts +10 -5
  57. package/dist/files.js +4 -3
  58. package/dist/index.d.ts +557 -321
  59. package/dist/index.js +17 -16
  60. package/dist/leads.d.ts +5 -3
  61. package/dist/leads.js +8 -7
  62. package/dist/notifications.d.ts +4 -48
  63. package/dist/notifications.js +7 -73
  64. package/dist/organizations.d.ts +3 -3
  65. package/dist/payments.d.ts +28 -58
  66. package/dist/payments.js +10 -34
  67. package/dist/platform.d.ts +1 -0
  68. package/dist/platform.js +2 -5
  69. package/dist/plugins.d.ts +10 -4
  70. package/dist/plugins.js +19 -9
  71. package/dist/products.d.ts +31 -36
  72. package/dist/products.js +8 -7
  73. package/dist/roles.d.ts +0 -3
  74. package/dist/roles.js +5 -5
  75. package/dist/routes.d.ts +8 -0
  76. package/dist/routes.js +13 -0
  77. package/dist/saas.d.ts +0 -6
  78. package/dist/saas.js +5 -27
  79. package/dist/tags.d.ts +4 -4
  80. package/dist/tags.js +3 -3
  81. package/dist/transfer.d.ts +62 -24
  82. package/dist/transfer.js +15 -36
  83. package/dist/trash.d.ts +23 -4
  84. package/dist/trash.js +5 -5
  85. package/dist/users.d.ts +27 -41
  86. package/dist/users.js +13 -26
  87. package/dist/values.d.ts +159 -0
  88. package/dist/values.js +144 -0
  89. package/dist/views.d.ts +8 -8
  90. package/dist/views.js +8 -21
  91. package/dist/webhooks.d.ts +26 -20
  92. package/dist/webhooks.js +18 -11
  93. package/package.json +4 -1
  94. package/dist/db/src/schema/jobs.js +0 -44
@@ -1,29 +1,15 @@
1
- import { pgTable } from "../table.js";
1
+ import { ACTIVITY_TARGETS, ACTOR_TYPES, FILE_STATUSES, NOTIFICATION_PRIORITIES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
2
3
  import { createdAt, updatedAt } from "./common.js";
3
4
  import { organizations } from "./organizations.js";
4
5
  import { users } from "./users.js";
5
6
  import { bigint, check, foreignKey, index, jsonb, primaryKey, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
6
7
  import { sql } from "drizzle-orm";
7
8
  // ../db/src/schema/platform.ts
8
- /**
9
- * Who did something: a team member, a plugin (by name, e.g. `hr`) or core
10
- * itself (`system`: cleanups, imports). Ids only — names are resolved when shown.
11
- */
12
- const ACTOR_TYPES = [
13
- "user",
14
- "plugin",
15
- "system"
16
- ];
17
9
  const timestamptz = () => timestamp({
18
10
  withTimezone: true,
19
11
  mode: "date"
20
12
  });
21
- /** `deleting`: removed, waiting for storage to confirm the bytes are gone (retried until it does). */
22
- const FILE_STATUSES = [
23
- "pending",
24
- "ready",
25
- "deleting"
26
- ];
27
13
  /**
28
14
  * Uploaded files: metadata only, the bytes live in the storage driver
29
15
  * (local disk, S3, R2, MinIO) under `key`. A row starts `pending` when an
@@ -32,7 +18,7 @@ const FILE_STATUSES = [
32
18
  */
33
19
  const files = pgTable("files", {
34
20
  id: text().primaryKey(),
35
- orgId: text().notNull(),
21
+ orgId: orgIdColumn(),
36
22
  /** Object key in the storage driver: `<orgId>/<yyyy>/<mm>/<id>`. */
37
23
  key: text().notNull(),
38
24
  name: text().notNull(),
@@ -54,18 +40,17 @@ const files = pgTable("files", {
54
40
  foreignColumns: [organizations.id],
55
41
  name: "core_files_org_id_fkey"
56
42
  }).onDelete("cascade"),
57
- check("core_files_status_check", sql`${table.status} IN ('pending', 'ready', 'deleting')`),
43
+ check("core_files_status_check", inList(table.status, FILE_STATUSES)),
58
44
  index("core_files_deleting_idx").on(table.updatedAt).where(sql`${table.status} = 'deleting'`),
59
45
  check("core_files_size_check", sql`${table.size} >= 0`)
60
46
  ]);
61
- const NOTIFICATION_PRIORITIES = ["normal", "high"];
62
47
  /**
63
48
  * A team member's inbox. Core and plugins create them; one row per recipient.
64
49
  * `dedupeKey` makes creating idempotent: a retried request does not notify twice.
65
50
  */
66
51
  const notifications = pgTable("notifications", {
67
52
  id: text().primaryKey(),
68
- orgId: text().notNull(),
53
+ orgId: orgIdColumn(),
69
54
  userId: text().notNull(),
70
55
  /** `core` or the plugin that sent it. */
71
56
  source: text().notNull(),
@@ -100,7 +85,7 @@ const notifications = pgTable("notifications", {
100
85
  foreignColumns: [users.id],
101
86
  name: "core_notifications_user_id_fkey"
102
87
  }).onDelete("cascade"),
103
- check("core_notifications_priority_check", sql`${table.priority} IN ('normal', 'high')`)
88
+ check("core_notifications_priority_check", inList(table.priority, NOTIFICATION_PRIORITIES))
104
89
  ]);
105
90
  /**
106
91
  * Timelines: what happened with a customer or company — or a plugin's own
@@ -110,7 +95,7 @@ const notifications = pgTable("notifications", {
110
95
  */
111
96
  const activities = pgTable("activities", {
112
97
  id: text().primaryKey(),
113
- orgId: text().notNull(),
98
+ orgId: orgIdColumn(),
114
99
  /** `customer`, `company`, or a plugin's record type (`hr.employee`). */
115
100
  targetType: text().notNull(),
116
101
  targetId: text().notNull(),
@@ -142,7 +127,7 @@ const activities = pgTable("activities", {
142
127
  foreignColumns: [organizations.id],
143
128
  name: "core_activities_org_id_fkey"
144
129
  }).onDelete("cascade"),
145
- check("core_activities_target_type_check", sql`${table.targetType} IN ('customer', 'company') OR ${table.targetType} ~ '^[a-z][a-z0-9_]*\.[a-z][a-z0-9_]*$'`),
130
+ check("core_activities_target_type_check", sql`${inList(table.targetType, ACTIVITY_TARGETS)} OR ${table.targetType} ~ '^[a-z][a-z0-9_]*\.[a-z][a-z0-9_]*$'`),
146
131
  index("core_activities_actor_idx").on(table.orgId, table.targetType, table.targetId, table.actorId)
147
132
  ]);
148
133
  /**
@@ -152,7 +137,7 @@ const activities = pgTable("activities", {
152
137
  */
153
138
  const auditLogs = pgTable("audit_logs", {
154
139
  id: text().primaryKey(),
155
- orgId: text().notNull(),
140
+ orgId: orgIdColumn(),
156
141
  actorType: text({ enum: ACTOR_TYPES }).notNull(),
157
142
  actorId: text(),
158
143
  /** `<record>.<verb>`: `customer.deleted`, `user.role_changed`, `auth.signed_in`. */
@@ -190,7 +175,7 @@ const auditLogs = pgTable("audit_logs", {
190
175
  }).onDelete("cascade")
191
176
  ]);
192
177
  pgTable("audit_checkpoints", {
193
- orgId: text().notNull(),
178
+ orgId: orgIdColumn(),
194
179
  chainSeq: bigint({ mode: "number" }).notNull(),
195
180
  hash: text().notNull(),
196
181
  /** Signing key id (`kid` in the published JWKS). */
@@ -206,4 +191,4 @@ pgTable("audit_checkpoints", {
206
191
  foreignColumns: [organizations.id],
207
192
  name: "core_audit_checkpoints_org_id_fkey"
208
193
  }).onDelete("cascade")]);
209
- export { ACTOR_TYPES, FILE_STATUSES, NOTIFICATION_PRIORITIES, activities, auditLogs, files, notifications };
194
+ export { activities, auditLogs, files, notifications };
@@ -1,4 +1,5 @@
1
- import { pgTable } from "../table.js";
1
+ import { PLUGIN_STATUSES } from "../enums.js";
2
+ import { orgIdColumn, pgTable } from "../table.js";
2
3
  import { createdAt, updatedAt } from "./common.js";
3
4
  import { organizations } from "./organizations.js";
4
5
  import { foreignKey, index, jsonb, primaryKey, text, timestamp } from "drizzle-orm/pg-core";
@@ -8,7 +9,6 @@ const timestamptz = () => timestamp({
8
9
  withTimezone: true,
9
10
  mode: "date"
10
11
  });
11
- const PLUGIN_STATUSES = ["active", "disabled"];
12
12
  /**
13
13
  * Plugins this deployment knows (the registry). A plugin authenticates with
14
14
  * its client secret (only the SHA-256 is stored) and exchanges it for
@@ -29,7 +29,7 @@ const plugins = pgTable("plugins", {
29
29
  updatedAt: updatedAt()
30
30
  });
31
31
  pgTable("plugin_installations", {
32
- orgId: text().notNull(),
32
+ orgId: orgIdColumn(),
33
33
  plugin: text().notNull(),
34
34
  status: text({ enum: PLUGIN_STATUSES }).default("active").notNull(),
35
35
  /** Why it is off (`Suspended: 600 deletions in 5 minutes`), for the owner. */
@@ -56,4 +56,4 @@ pgTable("plugin_installations", {
56
56
  name: "core_plugin_installations_plugin_fkey"
57
57
  }).onDelete("cascade")
58
58
  ]);
59
- export { PLUGIN_STATUSES, plugins };
59
+ export { plugins };
@@ -1,15 +1,14 @@
1
- import { pgTable } from "../table.js";
1
+ import { PRODUCT_KINDS, PRODUCT_STATUSES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
2
3
  import { createdAt, updatedAt } from "./common.js";
3
4
  import { organizations } from "./organizations.js";
4
5
  import { bigint, check, foreignKey, index, integer, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
5
6
  import { sql } from "drizzle-orm";
6
7
  // ../db/src/schema/products.ts
7
- const PRODUCT_KINDS = ["goods", "service"];
8
- const PRODUCT_STATUSES = ["active", "archived"];
9
8
  /** Groups of products, nested by `parentId`. */
10
9
  const productCategories = pgTable("product_categories", {
11
10
  id: text().primaryKey(),
12
- orgId: text().notNull(),
11
+ orgId: orgIdColumn(),
13
12
  name: text().notNull(),
14
13
  parentId: text(),
15
14
  createdBy: text(),
@@ -40,7 +39,7 @@ const productCategories = pgTable("product_categories", {
40
39
  */
41
40
  const products = pgTable("products", {
42
41
  id: text().primaryKey(),
43
- orgId: text().notNull(),
42
+ orgId: orgIdColumn(),
44
43
  kind: text({ enum: PRODUCT_KINDS }).notNull(),
45
44
  status: text({ enum: PRODUCT_STATUSES }).default("active").notNull(),
46
45
  name: text().notNull(),
@@ -86,9 +85,9 @@ const products = pgTable("products", {
86
85
  foreignColumns: [productCategories.orgId, productCategories.id],
87
86
  name: "core_products_category_fkey"
88
87
  }),
89
- check("core_products_kind_check", sql`${table.kind} IN ('goods', 'service')`),
90
- check("core_products_status_check", sql`${table.status} IN ('active', 'archived')`),
88
+ check("core_products_kind_check", inList(table.kind, PRODUCT_KINDS)),
89
+ check("core_products_status_check", inList(table.status, PRODUCT_STATUSES)),
91
90
  check("core_products_price_check", sql`${table.price} >= 0`),
92
91
  check("core_products_size_check", sql`coalesce(${table.weightGrams}, 0) >= 0 AND coalesce(${table.lengthMm}, 0) >= 0 AND coalesce(${table.widthMm}, 0) >= 0 AND coalesce(${table.heightMm}, 0) >= 0`)
93
92
  ]);
94
- export { PRODUCT_KINDS, PRODUCT_STATUSES, productCategories, products };
93
+ export { productCategories, products };
@@ -0,0 +1,7 @@
1
+ import { pgTable } from "../table.js";
2
+ import { bigint, integer, text } from "drizzle-orm/pg-core";
3
+ pgTable("rate_limits", {
4
+ key: text().primaryKey(),
5
+ points: integer().notNull().default(0),
6
+ expire: bigint({ mode: "number" })
7
+ });
@@ -1,17 +1,12 @@
1
- import { pgTable } from "../table.js";
1
+ import { ORGANIZATION_STATUSES } from "../enums.js";
2
+ import { inList, pgTable } from "../table.js";
2
3
  import { organizations } from "./organizations.js";
3
4
  import { check, foreignKey, jsonb, text, timestamp } from "drizzle-orm/pg-core";
4
- import { sql } from "drizzle-orm";
5
5
  // ../db/src/schema/saas.ts
6
6
  const timestamptz = () => timestamp({
7
7
  withTimezone: true,
8
8
  mode: "date"
9
9
  });
10
- const ORGANIZATION_STATUSES = [
11
- "active",
12
- "suspended",
13
- "inactive"
14
- ];
15
10
  pgTable("entitlements", {
16
11
  orgId: text().primaryKey(),
17
12
  /** erxes.io's organisation status: `inactive` / `suspended` keep everyone out. */
@@ -22,7 +17,7 @@ pgTable("entitlements", {
22
17
  fetchedAt: timestamptz().notNull(),
23
18
  /** The last fetch that failed (the stored answer stays in force). */
24
19
  lastError: text()
25
- }, (table) => [check("core_entitlements_org_status_check", sql`${table.orgStatus} IN ('active', 'suspended', 'inactive')`), foreignKey({
20
+ }, (table) => [check("core_entitlements_org_status_check", inList(table.orgStatus, ORGANIZATION_STATUSES)), foreignKey({
26
21
  columns: [table.orgId],
27
22
  foreignColumns: [organizations.id],
28
23
  name: "core_entitlements_org_id_fkey"
@@ -32,4 +27,3 @@ pgTable("used_tokens", {
32
27
  usedAt: timestamptz().defaultNow().notNull(),
33
28
  expiresAt: timestamptz().notNull()
34
29
  });
35
- export { ORGANIZATION_STATUSES };
@@ -1,5 +1,6 @@
1
- import { pgTable } from "../table.js";
2
- import { CONTENT_TYPES, createdAt, updatedAt } from "./common.js";
1
+ import { CONTENT_TYPES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
3
+ import { createdAt, updatedAt } from "./common.js";
3
4
  import { companies } from "./companies.js";
4
5
  import { customers } from "./customers.js";
5
6
  import { organizations } from "./organizations.js";
@@ -9,7 +10,7 @@ import { sql } from "drizzle-orm";
9
10
  /** Labels for customers and companies (plugins keep their own tags). */
10
11
  const tags = pgTable("tags", {
11
12
  id: text().primaryKey(),
12
- orgId: text().notNull(),
13
+ orgId: orgIdColumn(),
13
14
  type: text({ enum: CONTENT_TYPES }).notNull(),
14
15
  name: text().notNull(),
15
16
  colorCode: text(),
@@ -23,7 +24,7 @@ const tags = pgTable("tags", {
23
24
  foreignColumns: [organizations.id],
24
25
  name: "core_tags_org_id_fkey"
25
26
  }).onDelete("cascade"),
26
- check("core_tags_type_check", sql`${table.type} IN ('customer', 'company')`)
27
+ check("core_tags_type_check", inList(table.type, CONTENT_TYPES))
27
28
  ]);
28
29
  /**
29
30
  * Which tags a record has. One table per record type so both sides have real
@@ -33,7 +34,7 @@ const tags = pgTable("tags", {
33
34
  const tagLinks = (name, recordColumn) => {
34
35
  const records = recordColumn === "customer_id" ? customers : companies;
35
36
  return pgTable(name, {
36
- orgId: text().notNull(),
37
+ orgId: orgIdColumn(),
37
38
  recordId: text(recordColumn).notNull(),
38
39
  tagId: text().notNull(),
39
40
  createdAt: createdAt()
@@ -1,33 +1,13 @@
1
- import { pgTable } from "../table.js";
1
+ import { EXPORT_FORMATS, EXPORT_STATUSES, IMPORT_STATUSES, TRANSFER_RECORD_TYPES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
2
3
  import { createdAt, updatedAt } from "./common.js";
3
4
  import { organizations } from "./organizations.js";
4
5
  import { check, foreignKey, index, integer, jsonb, primaryKey, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
5
- import { sql } from "drizzle-orm";
6
6
  // ../db/src/schema/transfer.ts
7
7
  const timestamptz = () => timestamp({
8
8
  withTimezone: true,
9
9
  mode: "date"
10
10
  });
11
- const TRANSFER_RECORD_TYPES = [
12
- "customer",
13
- "lead",
14
- "company"
15
- ];
16
- const IMPORT_STATUSES = [
17
- "parsing",
18
- "mapping",
19
- "checking",
20
- "checked",
21
- "running",
22
- "done",
23
- "failed",
24
- "undone"
25
- ];
26
- const EXPORT_STATUSES = [
27
- "running",
28
- "done",
29
- "failed"
30
- ];
31
11
  /**
32
12
  * An import of customers, leads or companies from a CSV or Excel file: parsed
33
13
  * into rows, mapped to fields, checked (a dry run), then run in batches. The
@@ -35,7 +15,7 @@ const EXPORT_STATUSES = [
35
15
  */
36
16
  const imports = pgTable("imports", {
37
17
  id: text().primaryKey(),
38
- orgId: text().notNull(),
18
+ orgId: orgIdColumn(),
39
19
  recordType: text({ enum: TRANSFER_RECORD_TYPES }).notNull(),
40
20
  fileId: text().notNull(),
41
21
  fileName: text().notNull(),
@@ -61,8 +41,8 @@ const imports = pgTable("imports", {
61
41
  }, (table) => [
62
42
  index("core_imports_org_created_idx").on(table.orgId, table.createdAt.desc()),
63
43
  uniqueIndex("core_imports_org_id_id_key").on(table.orgId, table.id),
64
- check("core_imports_record_type_check", sql`${table.recordType} IN ('customer', 'lead', 'company')`),
65
- check("core_imports_status_check", sql`${table.status} IN ('parsing', 'mapping', 'checking', 'checked', 'running', 'done', 'failed', 'undone')`),
44
+ check("core_imports_record_type_check", inList(table.recordType, TRANSFER_RECORD_TYPES)),
45
+ check("core_imports_status_check", inList(table.status, IMPORT_STATUSES)),
66
46
  foreignKey({
67
47
  columns: [table.orgId],
68
48
  foreignColumns: [organizations.id],
@@ -71,7 +51,7 @@ const imports = pgTable("imports", {
71
51
  ]);
72
52
  /** One row of an import's file: its cells, what the check said, what running it did. */
73
53
  const importRows = pgTable("import_rows", {
74
- orgId: text().notNull(),
54
+ orgId: orgIdColumn(),
75
55
  importId: text().notNull(),
76
56
  /** 1-based, after the header (as people count rows in a spreadsheet: row 2 is the first). */
77
57
  rowNumber: integer().notNull(),
@@ -95,9 +75,9 @@ const importRows = pgTable("import_rows", {
95
75
  /** An export of a list (its filter, search, view) to CSV or Excel, made in the background. */
96
76
  const exports = pgTable("exports", {
97
77
  id: text().primaryKey(),
98
- orgId: text().notNull(),
78
+ orgId: orgIdColumn(),
99
79
  recordType: text({ enum: TRANSFER_RECORD_TYPES }).notNull(),
100
- format: text({ enum: ["csv", "xlsx"] }).notNull(),
80
+ format: text({ enum: EXPORT_FORMATS }).notNull(),
101
81
  /** The list query: `filter` (condition group), `search`, `tagIds`, `ownerId`. */
102
82
  query: jsonb().$type().default({}).notNull(),
103
83
  columns: jsonb().$type().notNull(),
@@ -110,13 +90,13 @@ const exports = pgTable("exports", {
110
90
  finishedAt: timestamptz()
111
91
  }, (table) => [
112
92
  index("core_exports_org_created_idx").on(table.orgId, table.createdAt.desc()),
113
- check("core_exports_record_type_check", sql`${table.recordType} IN ('customer', 'lead', 'company')`),
114
- check("core_exports_format_check", sql`${table.format} IN ('csv', 'xlsx')`),
115
- check("core_exports_status_check", sql`${table.status} IN ('running', 'done', 'failed')`),
93
+ check("core_exports_record_type_check", inList(table.recordType, TRANSFER_RECORD_TYPES)),
94
+ check("core_exports_format_check", inList(table.format, EXPORT_FORMATS)),
95
+ check("core_exports_status_check", inList(table.status, EXPORT_STATUSES)),
116
96
  foreignKey({
117
97
  columns: [table.orgId],
118
98
  foreignColumns: [organizations.id],
119
99
  name: "core_exports_org_id_fkey"
120
100
  }).onDelete("cascade")
121
101
  ]);
122
- export { EXPORT_STATUSES, IMPORT_STATUSES, TRANSFER_RECORD_TYPES, exports, importRows, imports };
102
+ export { exports, importRows, imports };
@@ -1,8 +1,7 @@
1
- import { ACTOR_TYPES } from "./platform.js";
2
- import { pgTable } from "../table.js";
1
+ import { ACTOR_TYPES, CONTENT_TYPES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
3
3
  import { organizations } from "./organizations.js";
4
4
  import { check, foreignKey, index, integer, jsonb, primaryKey, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
5
- import { sql } from "drizzle-orm";
6
5
  // ../db/src/schema/trash.ts
7
6
  const timestamptz = () => timestamp({
8
7
  withTimezone: true,
@@ -15,8 +14,8 @@ const timestamptz = () => timestamp({
15
14
  */
16
15
  const trash = pgTable("trash", {
17
16
  id: text().primaryKey(),
18
- orgId: text().notNull(),
19
- recordType: text({ enum: ["customer", "company"] }).notNull(),
17
+ orgId: orgIdColumn(),
18
+ recordType: text({ enum: CONTENT_TYPES }).notNull(),
20
19
  recordId: text().notNull(),
21
20
  /** Its name at deletion, for the bin's list. */
22
21
  label: text(),
@@ -33,7 +32,7 @@ const trash = pgTable("trash", {
33
32
  uniqueIndex("core_trash_record_key").on(table.orgId, table.recordType, table.recordId),
34
33
  index("core_trash_org_idx").on(table.orgId, table.deletedAt.desc()),
35
34
  index("core_trash_purge_idx").on(table.purgeAt),
36
- check("core_trash_record_type_check", sql`${table.recordType} IN ('customer', 'company')`),
35
+ check("core_trash_record_type_check", inList(table.recordType, CONTENT_TYPES)),
37
36
  foreignKey({
38
37
  columns: [table.orgId],
39
38
  foreignColumns: [organizations.id],
@@ -41,7 +40,7 @@ const trash = pgTable("trash", {
41
40
  }).onDelete("cascade")
42
41
  ]);
43
42
  pgTable("deletion_counters", {
44
- orgId: text().notNull(),
43
+ orgId: orgIdColumn(),
45
44
  /** `user:<id>` or `plugin:<name>`. */
46
45
  actor: text().notNull(),
47
46
  bucket: timestamp({
@@ -1,13 +1,9 @@
1
- import { pgTable } from "../table.js";
1
+ import { USER_STATUSES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
2
3
  import { organizations } from "./organizations.js";
3
4
  import { boolean, check, date, foreignKey, index, integer, jsonb, primaryKey, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
4
5
  import { sql } from "drizzle-orm";
5
6
  // ../db/src/schema/users.ts
6
- const USER_STATUSES = [
7
- "invited",
8
- "active",
9
- "disabled"
10
- ];
11
7
  /**
12
8
  * Roles of an organisation: named sets of permissions (`contacts.delete`,
13
9
  * `hr.leave.approve`). Built in: `owner` (everything, including owner-only
@@ -15,7 +11,7 @@ const USER_STATUSES = [
15
11
  * Custom roles have keys `r_<id>`.
16
12
  */
17
13
  const roles = pgTable("roles", {
18
- orgId: text().notNull(),
14
+ orgId: orgIdColumn(),
19
15
  key: text().notNull(),
20
16
  name: text().notNull(),
21
17
  description: text(),
@@ -41,7 +37,7 @@ const roles = pgTable("roles", {
41
37
  /** Team members of an organisation. */
42
38
  const users = pgTable("users", {
43
39
  id: text().primaryKey(),
44
- orgId: text().notNull(),
40
+ orgId: orgIdColumn(),
45
41
  email: text().notNull(),
46
42
  username: text(),
47
43
  /** Display name; kept in step with first/middle/last name. */
@@ -115,6 +111,6 @@ const users = pgTable("users", {
115
111
  foreignColumns: [roles.orgId, roles.key],
116
112
  name: "core_users_role_fkey"
117
113
  }),
118
- check("core_users_status_check", sql`${table.status} IN ('invited', 'active', 'disabled')`)
114
+ check("core_users_status_check", inList(table.status, USER_STATUSES))
119
115
  ]);
120
- export { USER_STATUSES, roles, users };
116
+ export { roles, users };
@@ -1,17 +1,9 @@
1
- import { pgTable } from "../table.js";
1
+ import { VIEW_RECORD_TYPES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
2
3
  import { createdAt, updatedAt } from "./common.js";
3
4
  import { organizations } from "./organizations.js";
4
5
  import { boolean, check, foreignKey, index, jsonb, text } from "drizzle-orm/pg-core";
5
- import { sql } from "drizzle-orm";
6
6
  // ../db/src/schema/views.ts
7
- /** Lists views belong to: customers, leads (people), companies, company leads, products. */
8
- const VIEW_RECORD_TYPES = [
9
- "customer",
10
- "lead",
11
- "company",
12
- "company_lead",
13
- "product"
14
- ];
15
7
  /**
16
8
  * A saved list: a name, a filter (the same condition language automations
17
9
  * use), the search, columns and sort. Private to its creator or shared with
@@ -19,7 +11,7 @@ const VIEW_RECORD_TYPES = [
19
11
  */
20
12
  const views = pgTable("views", {
21
13
  id: text().primaryKey(),
22
- orgId: text().notNull(),
14
+ orgId: orgIdColumn(),
23
15
  recordType: text({ enum: VIEW_RECORD_TYPES }).notNull(),
24
16
  name: text().notNull(),
25
17
  filter: jsonb().$type(),
@@ -33,11 +25,11 @@ const views = pgTable("views", {
33
25
  updatedAt: updatedAt()
34
26
  }, (table) => [
35
27
  index("core_views_org_type_idx").on(table.orgId, table.recordType),
36
- check("core_views_record_type_check", sql`${table.recordType} IN ('customer', 'lead', 'company', 'company_lead', 'product')`),
28
+ check("core_views_record_type_check", inList(table.recordType, VIEW_RECORD_TYPES)),
37
29
  foreignKey({
38
30
  columns: [table.orgId],
39
31
  foreignColumns: [organizations.id],
40
32
  name: "core_views_org_id_fkey"
41
33
  }).onDelete("cascade")
42
34
  ]);
43
- export { VIEW_RECORD_TYPES, views };
35
+ export { views };
@@ -1,19 +1,13 @@
1
- import { pgTable } from "../table.js";
1
+ import { WEBHOOK_DELIVERY_STATUSES, WEBHOOK_STATUSES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
2
3
  import { createdAt, updatedAt } from "./common.js";
3
4
  import { organizations } from "./organizations.js";
4
5
  import { check, foreignKey, index, integer, text, timestamp } from "drizzle-orm/pg-core";
5
- import { sql } from "drizzle-orm";
6
6
  // ../db/src/schema/webhooks.ts
7
7
  const timestamptz = () => timestamp({
8
8
  withTimezone: true,
9
9
  mode: "date"
10
10
  });
11
- const WEBHOOK_STATUSES = ["active", "disabled"];
12
- const WEBHOOK_DELIVERY_STATUSES = [
13
- "pending",
14
- "succeeded",
15
- "failed"
16
- ];
17
11
  /**
18
12
  * Outgoing webhooks: an organisation's endpoint that receives its events
19
13
  * (`core.customer.created`, `hr.leave.approved`, prefixes like `core.*`),
@@ -21,7 +15,7 @@ const WEBHOOK_DELIVERY_STATUSES = [
21
15
  */
22
16
  const webhooks = pgTable("webhooks", {
23
17
  id: text().primaryKey(),
24
- orgId: text().notNull(),
18
+ orgId: orgIdColumn(),
25
19
  url: text().notNull(),
26
20
  description: text(),
27
21
  /** Event types, `<prefix>.*`, or `*` for everything. */
@@ -38,7 +32,7 @@ const webhooks = pgTable("webhooks", {
38
32
  updatedAt: updatedAt()
39
33
  }, (table) => [
40
34
  index("core_webhooks_org_idx").on(table.orgId, table.status),
41
- check("core_webhooks_status_check", sql`${table.status} IN ('active', 'disabled')`),
35
+ check("core_webhooks_status_check", inList(table.status, WEBHOOK_STATUSES)),
42
36
  foreignKey({
43
37
  columns: [table.orgId],
44
38
  foreignColumns: [organizations.id],
@@ -48,7 +42,7 @@ const webhooks = pgTable("webhooks", {
48
42
  /** Each event sent (or being sent) to an endpoint, with the last attempt's outcome. */
49
43
  const webhookDeliveries = pgTable("webhook_deliveries", {
50
44
  id: text().primaryKey(),
51
- orgId: text().notNull(),
45
+ orgId: orgIdColumn(),
52
46
  webhookId: text().notNull(),
53
47
  eventId: text().notNull(),
54
48
  eventType: text().notNull(),
@@ -59,14 +53,13 @@ const webhookDeliveries = pgTable("webhook_deliveries", {
59
53
  responseBody: text(),
60
54
  error: text(),
61
55
  durationMs: integer(),
62
- nextAttemptAt: timestamptz(),
63
56
  deliveredAt: timestamptz(),
64
57
  createdAt: createdAt(),
65
58
  updatedAt: updatedAt()
66
59
  }, (table) => [
67
60
  index("core_webhook_deliveries_webhook_idx").on(table.orgId, table.webhookId, table.createdAt.desc()),
68
61
  index("core_webhook_deliveries_created_idx").on(table.createdAt),
69
- check("core_webhook_deliveries_status_check", sql`${table.status} IN ('pending', 'succeeded', 'failed')`),
62
+ check("core_webhook_deliveries_status_check", inList(table.status, WEBHOOK_DELIVERY_STATUSES)),
70
63
  foreignKey({
71
64
  columns: [table.orgId],
72
65
  foreignColumns: [organizations.id],
@@ -78,4 +71,4 @@ const webhookDeliveries = pgTable("webhook_deliveries", {
78
71
  name: "core_webhook_deliveries_webhook_id_fkey"
79
72
  }).onDelete("cascade")
80
73
  ]);
81
- export { WEBHOOK_DELIVERY_STATUSES, WEBHOOK_STATUSES, webhookDeliveries, webhooks };
74
+ export { webhookDeliveries, webhooks };
@@ -1,4 +1,5 @@
1
- import { pgTableCreator } from "drizzle-orm/pg-core";
1
+ import { pgTableCreator, text } from "drizzle-orm/pg-core";
2
+ import { sql } from "drizzle-orm";
2
3
  // ../db/src/table.ts
3
4
  /**
4
5
  * Every core table is `core_<name>`: the database is shared with plugins
@@ -6,4 +7,17 @@ import { pgTableCreator } from "drizzle-orm/pg-core";
6
7
  * so they repeat the full prefixed table name.
7
8
  */
8
9
  const pgTable = pgTableCreator((name) => `core_${name}`);
9
- export { pgTable };
10
+ /**
11
+ * The organisation a row belongs to. An insert takes it from the scope
12
+ * (`withOrg` sets `app.org_id`), so services never pass it, and row level
13
+ * security checks it. A `withSystem` scope has no organisation: its inserts
14
+ * name one, or fail on NOT NULL.
15
+ */
16
+ const orgIdColumn = () => text().notNull().default(sql`nullif(current_setting('app.org_id', true), '')`);
17
+ const literal = (value) => typeof value === "number" ? String(value) : `'${value.replaceAll("'", "''")}'`;
18
+ /**
19
+ * `column IN ('a', 'b')` for a CHECK, from the list in `./enums` the column's
20
+ * `enum` uses. A constraint can't take parameters, so the values are inlined.
21
+ */
22
+ const inList = (column, values) => sql`${column} IN (${sql.raw(values.map(literal).join(", "))})`;
23
+ export { inList, orgIdColumn, pgTable };