@erxes/core-contract 0.1.0-v2.0 → 0.1.0-v2.2

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 (102) hide show
  1. package/dist/activities.d.ts +1394 -139
  2. package/dist/activities.js +96 -40
  3. package/dist/audit.d.ts +1092 -48
  4. package/dist/audit.js +37 -23
  5. package/dist/auth.d.ts +2867 -61
  6. package/dist/auth.js +55 -25
  7. package/dist/automationRuns.d.ts +12 -13
  8. package/dist/automationRuns.js +15 -30
  9. package/dist/automations.d.ts +2474 -93
  10. package/dist/automations.js +56 -44
  11. package/dist/client.d.ts +30704 -0
  12. package/dist/client.js +62 -0
  13. package/dist/common.d.ts +28 -16
  14. package/dist/common.js +30 -22
  15. package/dist/companies.d.ts +1154 -44
  16. package/dist/companies.js +33 -17
  17. package/dist/contract/routes.json.js +1810 -0
  18. package/dist/customerAuth.d.ts +1661 -0
  19. package/dist/customerAuth.js +197 -0
  20. package/dist/customers.d.ts +1283 -65
  21. package/dist/customers.js +35 -25
  22. package/dist/{automationDefinition.d.ts → db/src/automationDefinition.d.ts} +4 -3
  23. package/dist/{automationDefinition.js → db/src/automationDefinition.js} +10 -32
  24. package/dist/db/src/enums.d.ts +8 -0
  25. package/dist/db/src/enums.js +334 -0
  26. package/dist/db/src/schema/auditStreams.js +6 -14
  27. package/dist/db/src/schema/authTokens.js +4 -6
  28. package/dist/db/src/schema/automations.d.ts +1 -0
  29. package/dist/db/src/schema/automations.js +15 -31
  30. package/dist/db/src/schema/common.d.ts +2 -6
  31. package/dist/db/src/schema/common.js +1 -24
  32. package/dist/db/src/schema/companies.js +6 -5
  33. package/dist/db/src/schema/customerAuth.js +101 -0
  34. package/dist/db/src/schema/customers.js +6 -6
  35. package/dist/db/src/schema/events.js +12 -60
  36. package/dist/db/src/schema/fieldValues.js +2 -2
  37. package/dist/db/src/schema/fields.js +10 -28
  38. package/dist/db/src/schema/index.d.ts +3 -0
  39. package/dist/db/src/schema/index.js +22 -20
  40. package/dist/db/src/schema/leadStages.js +5 -10
  41. package/dist/db/src/schema/leads.js +6 -11
  42. package/dist/db/src/schema/notes.js +92 -0
  43. package/dist/db/src/schema/oauth.js +3 -3
  44. package/dist/db/src/schema/payments.js +13 -23
  45. package/dist/db/src/schema/platform.js +11 -31
  46. package/dist/db/src/schema/plugins.js +10 -5
  47. package/dist/db/src/schema/products.js +7 -8
  48. package/dist/db/src/schema/rateLimits.js +7 -0
  49. package/dist/db/src/schema/saas.js +3 -9
  50. package/dist/db/src/schema/tags.js +6 -5
  51. package/dist/db/src/schema/transfer.js +13 -33
  52. package/dist/db/src/schema/trash.js +6 -7
  53. package/dist/db/src/schema/users.js +6 -10
  54. package/dist/db/src/schema/views.js +5 -13
  55. package/dist/db/src/schema/webhooks.js +7 -14
  56. package/dist/db/src/table.js +16 -2
  57. package/dist/errors.d.ts +516 -0
  58. package/dist/errors.js +218 -0
  59. package/dist/events.d.ts +585 -140
  60. package/dist/events.js +57 -42
  61. package/dist/fields.d.ts +1478 -66
  62. package/dist/fields.js +33 -47
  63. package/dist/files.d.ts +766 -62
  64. package/dist/files.js +34 -22
  65. package/dist/index.d.ts +25707 -902
  66. package/dist/index.js +22 -20
  67. package/dist/leads.d.ts +1108 -22
  68. package/dist/leads.js +39 -20
  69. package/dist/notifications.d.ts +691 -94
  70. package/dist/notifications.js +45 -91
  71. package/dist/organizations.d.ts +263 -5
  72. package/dist/organizations.js +21 -2
  73. package/dist/payments.d.ts +1794 -120
  74. package/dist/payments.js +58 -60
  75. package/dist/platform.d.ts +12 -20
  76. package/dist/platform.js +8 -21
  77. package/dist/plugins.d.ts +808 -71
  78. package/dist/plugins.js +66 -35
  79. package/dist/products.d.ts +1411 -53
  80. package/dist/products.js +40 -23
  81. package/dist/roles.d.ts +560 -8
  82. package/dist/roles.js +25 -11
  83. package/dist/routes.d.ts +8 -0
  84. package/dist/routes.js +13 -0
  85. package/dist/saas.d.ts +164 -8
  86. package/dist/saas.js +14 -29
  87. package/dist/tags.d.ts +344 -8
  88. package/dist/tags.js +16 -8
  89. package/dist/transfer.d.ts +1361 -36
  90. package/dist/transfer.js +47 -49
  91. package/dist/trash.d.ts +299 -23
  92. package/dist/trash.js +27 -20
  93. package/dist/users.d.ts +1233 -59
  94. package/dist/users.js +48 -39
  95. package/dist/values.d.ts +150 -0
  96. package/dist/values.js +139 -0
  97. package/dist/views.d.ts +404 -13
  98. package/dist/views.js +18 -28
  99. package/dist/webhooks.d.ts +791 -29
  100. package/dist/webhooks.js +36 -21
  101. package/package.json +9 -2
  102. package/dist/db/src/schema/jobs.js +0 -44
@@ -1,16 +1,11 @@
1
- import { pgTable } from "../table.js";
1
+ import { ACTOR_TYPES, LEAD_EVENTS } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
2
3
  import { companies } from "./companies.js";
3
4
  import { customers } from "./customers.js";
4
5
  import { organizations } from "./organizations.js";
5
6
  import { check, foreignKey, index, text, timestamp } from "drizzle-orm/pg-core";
6
7
  import { sql } from "drizzle-orm";
7
8
  // ../db/src/schema/leads.ts
8
- const LEAD_EVENTS = [
9
- "created",
10
- "stage",
11
- "converted",
12
- "reopened"
13
- ];
14
9
  /**
15
10
  * A lead's journey: where it entered, every stage move, its conversion to a
16
11
  * customer (and back). Funnel reports, time in each stage and conversion
@@ -19,7 +14,7 @@ const LEAD_EVENTS = [
19
14
  */
20
15
  const leadEvents = pgTable("lead_events", {
21
16
  id: text().primaryKey(),
22
- orgId: text().notNull(),
17
+ orgId: orgIdColumn(),
23
18
  /** The person, or (`companyId`) the company: exactly one. */
24
19
  customerId: text(),
25
20
  companyId: text(),
@@ -28,7 +23,7 @@ const leadEvents = pgTable("lead_events", {
28
23
  toStageId: text(),
29
24
  /** Why it was disqualified, or a note on the move. */
30
25
  reason: text(),
31
- actorType: text().notNull(),
26
+ actorType: text({ enum: ACTOR_TYPES }).notNull(),
32
27
  actorId: text(),
33
28
  createdAt: timestamp({
34
29
  withTimezone: true,
@@ -54,6 +49,6 @@ const leadEvents = pgTable("lead_events", {
54
49
  foreignColumns: [organizations.id],
55
50
  name: "core_lead_events_org_id_fkey"
56
51
  }).onDelete("cascade"),
57
- check("core_lead_events_event_check", sql`${table.event} IN ('created', 'stage', 'converted', 'reopened')`)
52
+ check("core_lead_events_event_check", inList(table.event, LEAD_EVENTS))
58
53
  ]);
59
- export { LEAD_EVENTS, leadEvents };
54
+ export { leadEvents };
@@ -0,0 +1,92 @@
1
+ import { orgIdColumn, pgTable } from "../table.js";
2
+ import { createdAt } from "./common.js";
3
+ import { organizations } from "./organizations.js";
4
+ import { activities } from "./platform.js";
5
+ import { users } from "./users.js";
6
+ import { foreignKey, index, primaryKey, text, timestamp } from "drizzle-orm/pg-core";
7
+ import { sql } from "drizzle-orm";
8
+ // ../db/src/schema/notes.ts
9
+ const timestamptz = () => timestamp({
10
+ withTimezone: true,
11
+ mode: "date"
12
+ });
13
+ /**
14
+ * A member's note on a timeline. The note is its timeline entry's own row
15
+ * (same id, `<target>.note`): the entry says where it is and who wrote it
16
+ * (`target_*`, `actor_id`), this row holds what only a note has. Deleting the
17
+ * entry deletes the note.
18
+ */
19
+ const notes = pgTable("notes", {
20
+ id: text().primaryKey(),
21
+ orgId: orgIdColumn(),
22
+ body: text().notNull(),
23
+ /** Members mentioned with @: they are notified. */
24
+ mentions: text().array().notNull().default(sql`'{}'::text[]`),
25
+ /** Attached files (uploaded through `/files`). */
26
+ fileIds: text().array().notNull().default(sql`'{}'::text[]`),
27
+ /** Pinned to the top of the timeline. */
28
+ pinnedAt: timestamptz(),
29
+ /** Last reworded. */
30
+ editedAt: timestamptz()
31
+ }, (table) => [foreignKey({
32
+ columns: [table.id],
33
+ foreignColumns: [activities.id],
34
+ name: "core_notes_id_fkey"
35
+ }).onDelete("cascade"), foreignKey({
36
+ columns: [table.orgId],
37
+ foreignColumns: [organizations.id],
38
+ name: "core_notes_org_id_fkey"
39
+ }).onDelete("cascade")]);
40
+ /** Earlier wordings of a note: each edit keeps the text it replaced. */
41
+ const noteVersions = pgTable("note_versions", {
42
+ id: text().primaryKey(),
43
+ orgId: orgIdColumn(),
44
+ noteId: text().notNull(),
45
+ body: text().notNull(),
46
+ /** When this wording was written. */
47
+ editedAt: timestamptz().notNull()
48
+ }, (table) => [
49
+ index("core_note_versions_note_idx").on(table.orgId, table.noteId, table.editedAt),
50
+ foreignKey({
51
+ columns: [table.noteId],
52
+ foreignColumns: [notes.id],
53
+ name: "core_note_versions_note_id_fkey"
54
+ }).onDelete("cascade"),
55
+ foreignKey({
56
+ columns: [table.orgId],
57
+ foreignColumns: [organizations.id],
58
+ name: "core_note_versions_org_id_fkey"
59
+ }).onDelete("cascade")
60
+ ]);
61
+ pgTable("reactions", {
62
+ orgId: orgIdColumn(),
63
+ activityId: text().notNull(),
64
+ userId: text().notNull(),
65
+ emoji: text().notNull(),
66
+ createdAt: createdAt()
67
+ }, (table) => [
68
+ primaryKey({
69
+ columns: [
70
+ table.activityId,
71
+ table.emoji,
72
+ table.userId
73
+ ],
74
+ name: "core_reactions_pkey"
75
+ }),
76
+ foreignKey({
77
+ columns: [table.activityId],
78
+ foreignColumns: [activities.id],
79
+ name: "core_reactions_activity_id_fkey"
80
+ }).onDelete("cascade"),
81
+ foreignKey({
82
+ columns: [table.userId],
83
+ foreignColumns: [users.id],
84
+ name: "core_reactions_user_id_fkey"
85
+ }).onDelete("cascade"),
86
+ foreignKey({
87
+ columns: [table.orgId],
88
+ foreignColumns: [organizations.id],
89
+ name: "core_reactions_org_id_fkey"
90
+ }).onDelete("cascade")
91
+ ]);
92
+ export { noteVersions, notes };
@@ -1,4 +1,4 @@
1
- import { pgTable } from "../table.js";
1
+ import { orgIdColumn, pgTable } from "../table.js";
2
2
  import { createdAt } from "./common.js";
3
3
  import { organizations } from "./organizations.js";
4
4
  import { foreignKey, index, integer, text, timestamp } from "drizzle-orm/pg-core";
@@ -9,7 +9,7 @@ const timestamptz = () => timestamp({
9
9
  });
10
10
  pgTable("oauth_codes", {
11
11
  id: text().primaryKey(),
12
- orgId: text().notNull(),
12
+ orgId: orgIdColumn(),
13
13
  clientId: text().notNull(),
14
14
  userId: text().notNull(),
15
15
  codeHash: text().notNull(),
@@ -27,7 +27,7 @@ pgTable("oauth_codes", {
27
27
  }).onDelete("cascade")]);
28
28
  pgTable("oauth_refresh_tokens", {
29
29
  id: text().primaryKey(),
30
- orgId: text().notNull(),
30
+ orgId: orgIdColumn(),
31
31
  clientId: text().notNull(),
32
32
  userId: text().notNull(),
33
33
  familyId: text().notNull(),
@@ -1,4 +1,5 @@
1
- import { pgTable } from "../table.js";
1
+ import { ACTOR_TYPES, INVOICE_STATUSES, PAYMENT_GATEWAY_KINDS, PAYMENT_GATEWAY_STATUSES, REFUND_METHODS, REFUND_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, jsonb, primaryKey, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
@@ -8,35 +9,24 @@ const timestamptz = () => timestamp({
8
9
  withTimezone: true,
9
10
  mode: "date"
10
11
  });
11
- const PAYMENT_GATEWAY_KINDS = ["qpay", "test"];
12
- const INVOICE_STATUSES = [
13
- "pending",
14
- "paid",
15
- "refunded",
16
- "cancelled",
17
- "expired",
18
- "failed"
19
- ];
20
- const REFUND_STATUSES = ["succeeded", "failed"];
21
- const REFUND_METHODS = ["gateway", "manual"];
22
12
  /**
23
13
  * An organisation's payment gateway connection (QPay, …). Credentials are
24
14
  * sealed (`src/lib/secrets.ts`); the API never returns them.
25
15
  */
26
16
  const paymentGateways = pgTable("payment_gateways", {
27
17
  id: text().primaryKey(),
28
- orgId: text().notNull(),
18
+ orgId: orgIdColumn(),
29
19
  kind: text({ enum: PAYMENT_GATEWAY_KINDS }).notNull(),
30
20
  name: text().notNull(),
31
21
  /** Sealed JSON of the gateway's settings (client id, secret, invoice code, …). */
32
22
  configSealed: text().notNull(),
33
- status: text({ enum: ["active", "disabled"] }).default("active").notNull(),
23
+ status: text({ enum: PAYMENT_GATEWAY_STATUSES }).default("active").notNull(),
34
24
  createdBy: text(),
35
25
  createdAt: createdAt(),
36
26
  updatedAt: updatedAt()
37
27
  }, (table) => [
38
28
  uniqueIndex("core_payment_gateways_org_id_id_key").on(table.orgId, table.id),
39
- check("core_payment_gateways_kind_check", sql`${table.kind} IN ('qpay', 'test')`),
29
+ check("core_payment_gateways_kind_check", inList(table.kind, PAYMENT_GATEWAY_KINDS)),
40
30
  foreignKey({
41
31
  columns: [table.orgId],
42
32
  foreignColumns: [organizations.id],
@@ -51,7 +41,7 @@ const paymentGateways = pgTable("payment_gateways", {
51
41
  */
52
42
  const paymentInvoices = pgTable("payment_invoices", {
53
43
  id: text().primaryKey(),
54
- orgId: text().notNull(),
44
+ orgId: orgIdColumn(),
55
45
  gatewayId: text().notNull(),
56
46
  /** `core` or the plugin that asked for it. */
57
47
  source: text().notNull(),
@@ -86,7 +76,7 @@ const paymentInvoices = pgTable("payment_invoices", {
86
76
  index("core_payment_invoices_reference_idx").on(table.orgId, table.source, table.referenceType, table.referenceId),
87
77
  index("core_payment_invoices_pending_idx").on(table.expiresAt).where(sql`${table.status} = 'pending'`),
88
78
  uniqueIndex("core_payment_invoices_dedupe_key").on(table.orgId, table.source, table.dedupeKey).where(sql`${table.dedupeKey} IS NOT NULL`),
89
- check("core_payment_invoices_status_check", sql`${table.status} IN ('pending', 'paid', 'refunded', 'cancelled', 'expired', 'failed')`),
79
+ check("core_payment_invoices_status_check", inList(table.status, INVOICE_STATUSES)),
90
80
  check("core_payment_invoices_amount_check", sql`${table.amount} > 0`),
91
81
  check("core_payment_invoices_refunded_check", sql`${table.refundedAmount} >= 0 AND ${table.refundedAmount} <= coalesce(${table.paidAmount}, ${table.amount})`),
92
82
  foreignKey({
@@ -102,7 +92,7 @@ const paymentInvoices = pgTable("payment_invoices", {
102
92
  */
103
93
  const paymentRefunds = pgTable("payment_refunds", {
104
94
  id: text().primaryKey(),
105
- orgId: text().notNull(),
95
+ orgId: orgIdColumn(),
106
96
  invoiceId: text().notNull(),
107
97
  amount: bigint({ mode: "number" }).notNull(),
108
98
  reason: text().notNull(),
@@ -111,7 +101,7 @@ const paymentRefunds = pgTable("payment_refunds", {
111
101
  gatewayRefundId: text(),
112
102
  error: text(),
113
103
  /** `user` or `plugin`, and who. */
114
- requestedByType: text().notNull(),
104
+ requestedByType: text({ enum: ACTOR_TYPES }).notNull(),
115
105
  requestedBy: text(),
116
106
  /** Retry-safe: one refund per invoice and key. */
117
107
  dedupeKey: text(),
@@ -120,8 +110,8 @@ const paymentRefunds = pgTable("payment_refunds", {
120
110
  index("core_payment_refunds_invoice_idx").on(table.orgId, table.invoiceId, table.createdAt),
121
111
  uniqueIndex("core_payment_refunds_dedupe_key").on(table.orgId, table.invoiceId, table.dedupeKey).where(sql`${table.dedupeKey} IS NOT NULL`),
122
112
  check("core_payment_refunds_amount_check", sql`${table.amount} > 0`),
123
- check("core_payment_refunds_method_check", sql`${table.method} IN ('gateway', 'manual')`),
124
- check("core_payment_refunds_status_check", sql`${table.status} IN ('succeeded', 'failed')`),
113
+ check("core_payment_refunds_method_check", inList(table.method, REFUND_METHODS)),
114
+ check("core_payment_refunds_status_check", inList(table.status, REFUND_STATUSES)),
125
115
  foreignKey({
126
116
  columns: [table.orgId, table.invoiceId],
127
117
  foreignColumns: [paymentInvoices.orgId, paymentInvoices.id],
@@ -134,7 +124,7 @@ const paymentRefunds = pgTable("payment_refunds", {
134
124
  * (none listed: any active one).
135
125
  */
136
126
  const paymentPluginSettings = pgTable("payment_plugin_settings", {
137
- orgId: text().notNull(),
127
+ orgId: orgIdColumn(),
138
128
  plugin: text().notNull(),
139
129
  defaultGatewayId: text(),
140
130
  allowedGatewayIds: text().array().default(sql`'{}'::text[]`).notNull(),
@@ -148,4 +138,4 @@ const paymentPluginSettings = pgTable("payment_plugin_settings", {
148
138
  foreignColumns: [organizations.id],
149
139
  name: "core_payment_plugin_settings_org_id_fkey"
150
140
  }).onDelete("cascade")]);
151
- export { INVOICE_STATUSES, PAYMENT_GATEWAY_KINDS, REFUND_METHODS, REFUND_STATUSES, paymentGateways, paymentInvoices, paymentPluginSettings, paymentRefunds };
141
+ export { paymentGateways, paymentInvoices, paymentPluginSettings, paymentRefunds };
@@ -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(),
@@ -127,14 +112,9 @@ const activities = pgTable("activities", {
127
112
  actorType: text({ enum: ACTOR_TYPES }).notNull(),
128
113
  actorId: text(),
129
114
  dedupeKey: text(),
130
- /** Notes: pinned to the top of the timeline. */
131
- pinnedAt: timestamptz(),
132
- /** Notes: last edited. */
133
- editedAt: timestamptz(),
134
115
  occurredAt: timestamptz().defaultNow().notNull(),
135
116
  createdAt: createdAt()
136
117
  }, (table) => [
137
- index("core_activities_pinned_idx").on(table.orgId, table.targetType, table.targetId).where(sql`${table.pinnedAt} IS NOT NULL`),
138
118
  index("core_activities_target_idx").on(table.orgId, table.targetType, table.targetId, table.occurredAt.desc(), table.id.desc()),
139
119
  uniqueIndex("core_activities_dedupe_key").on(table.orgId, table.source, table.dedupeKey).where(sql`${table.dedupeKey} IS NOT NULL`),
140
120
  foreignKey({
@@ -142,7 +122,7 @@ const activities = pgTable("activities", {
142
122
  foreignColumns: [organizations.id],
143
123
  name: "core_activities_org_id_fkey"
144
124
  }).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_]*$'`),
125
+ 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
126
  index("core_activities_actor_idx").on(table.orgId, table.targetType, table.targetId, table.actorId)
147
127
  ]);
148
128
  /**
@@ -152,7 +132,7 @@ const activities = pgTable("activities", {
152
132
  */
153
133
  const auditLogs = pgTable("audit_logs", {
154
134
  id: text().primaryKey(),
155
- orgId: text().notNull(),
135
+ orgId: orgIdColumn(),
156
136
  actorType: text({ enum: ACTOR_TYPES }).notNull(),
157
137
  actorId: text(),
158
138
  /** `<record>.<verb>`: `customer.deleted`, `user.role_changed`, `auth.signed_in`. */
@@ -190,7 +170,7 @@ const auditLogs = pgTable("audit_logs", {
190
170
  }).onDelete("cascade")
191
171
  ]);
192
172
  pgTable("audit_checkpoints", {
193
- orgId: text().notNull(),
173
+ orgId: orgIdColumn(),
194
174
  chainSeq: bigint({ mode: "number" }).notNull(),
195
175
  hash: text().notNull(),
196
176
  /** Signing key id (`kid` in the published JWKS). */
@@ -206,4 +186,4 @@ pgTable("audit_checkpoints", {
206
186
  foreignColumns: [organizations.id],
207
187
  name: "core_audit_checkpoints_org_id_fkey"
208
188
  }).onDelete("cascade")]);
209
- export { ACTOR_TYPES, FILE_STATUSES, NOTIFICATION_PRIORITIES, activities, auditLogs, files, notifications };
189
+ 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
@@ -28,8 +28,13 @@ const plugins = pgTable("plugins", {
28
28
  createdAt: createdAt(),
29
29
  updatedAt: updatedAt()
30
30
  });
31
- pgTable("plugin_installations", {
32
- orgId: text().notNull(),
31
+ /**
32
+ * A plugin installed in an organisation, with the core scopes its owner
33
+ * granted. Tokens are issued only for installed, active plugins, and every
34
+ * request re-checks, so uninstalling cuts a plugin off at once.
35
+ */
36
+ const pluginInstallations = pgTable("plugin_installations", {
37
+ orgId: orgIdColumn(),
33
38
  plugin: text().notNull(),
34
39
  status: text({ enum: PLUGIN_STATUSES }).default("active").notNull(),
35
40
  /** Why it is off (`Suspended: 600 deletions in 5 minutes`), for the owner. */
@@ -56,4 +61,4 @@ pgTable("plugin_installations", {
56
61
  name: "core_plugin_installations_plugin_fkey"
57
62
  }).onDelete("cascade")
58
63
  ]);
59
- export { PLUGIN_STATUSES, plugins };
64
+ export { pluginInstallations, 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_ROW_ACTIONS, 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,13 +51,13 @@ 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(),
78
58
  cells: jsonb().$type().notNull(),
79
59
  /** From the check, then the run: `create`, `update`, `skip` or `error`. */
80
- action: text(),
60
+ action: text({ enum: IMPORT_ROW_ACTIONS }),
81
61
  error: text(),
82
62
  recordId: text()
83
63
  }, (table) => [
@@ -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 };