@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,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 };