@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,6 +1,7 @@
1
- import { pgTable } from "../table.js";
1
+ import { CUSTOMER_STATES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
2
3
  import { createdAt, updatedAt } from "./common.js";
3
- import { CUSTOMER_STATES, customers } from "./customers.js";
4
+ import { customers } from "./customers.js";
4
5
  import { leadStages } from "./leadStages.js";
5
6
  import { organizations } from "./organizations.js";
6
7
  import { users } from "./users.js";
@@ -10,7 +11,7 @@ import { sql } from "drizzle-orm";
10
11
  /** Organisations the organisation works with, shared by every plugin. */
11
12
  const companies = pgTable("companies", {
12
13
  id: text().primaryKey(),
13
- orgId: text().notNull(),
14
+ orgId: orgIdColumn(),
14
15
  primaryName: text().notNull(),
15
16
  /** `lead`: not a customer yet; companies go through the same lead stages as people. */
16
17
  state: text({ enum: CUSTOMER_STATES }).default("customer").notNull(),
@@ -66,7 +67,7 @@ const companies = pgTable("companies", {
66
67
  foreignColumns: [leadStages.id],
67
68
  name: "core_companies_lead_stage_id_fkey"
68
69
  }).onDelete("set null"),
69
- check("core_companies_state_check", sql`${table.state} IN ('lead', 'customer')`),
70
+ check("core_companies_state_check", inList(table.state, CUSTOMER_STATES)),
70
71
  index("core_companies_org_owner_idx").on(table.orgId, table.ownerId),
71
72
  index("core_companies_org_industry_idx").on(table.orgId, sql`lower(${table.industry})`),
72
73
  index("core_companies_search_trgm_idx").using("gin", table.searchText.op("gin_trgm_ops")),
@@ -88,7 +89,7 @@ const companies = pgTable("companies", {
88
89
  * and since when they are not there any more (the link stays, as history).
89
90
  */
90
91
  const customerCompanies = pgTable("customer_companies", {
91
- orgId: text().notNull(),
92
+ orgId: orgIdColumn(),
92
93
  customerId: text().notNull(),
93
94
  companyId: text().notNull(),
94
95
  /** "CEO", "Buyer", "Accountant" … */
@@ -0,0 +1,101 @@
1
+ import { CUSTOMER_CODE_PURPOSES, CUSTOMER_LOGIN_KINDS } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
3
+ import { createdAt } from "./common.js";
4
+ import { customers } from "./customers.js";
5
+ import { organizations } from "./organizations.js";
6
+ import { check, foreignKey, index, integer, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
7
+ // ../db/src/schema/customerAuth.ts
8
+ const timestamptz = () => timestamp({
9
+ withTimezone: true,
10
+ mode: "date"
11
+ });
12
+ /**
13
+ * An email address or phone a customer proved with a code: they sign in with
14
+ * it. One per address in an organisation (decision 42).
15
+ */
16
+ const customerLogins = pgTable("customer_logins", {
17
+ id: text().primaryKey(),
18
+ orgId: orgIdColumn(),
19
+ customerId: text().notNull(),
20
+ kind: text({ enum: CUSTOMER_LOGIN_KINDS }).notNull(),
21
+ /** Normalised: lower-case email, phone without formatting. */
22
+ value: text().notNull(),
23
+ createdAt: createdAt(),
24
+ lastUsedAt: timestamptz()
25
+ }, (table) => [
26
+ uniqueIndex("core_customer_logins_org_kind_value_key").on(table.orgId, table.kind, table.value),
27
+ index("core_customer_logins_org_customer_idx").on(table.orgId, table.customerId),
28
+ foreignKey({
29
+ columns: [table.orgId],
30
+ foreignColumns: [organizations.id],
31
+ name: "core_customer_logins_org_id_fkey"
32
+ }).onDelete("cascade"),
33
+ foreignKey({
34
+ columns: [table.customerId],
35
+ foreignColumns: [customers.id],
36
+ name: "core_customer_logins_customer_id_fkey"
37
+ }).onDelete("cascade"),
38
+ check("core_customer_logins_kind_check", inList(table.kind, CUSTOMER_LOGIN_KINDS))
39
+ ]);
40
+ pgTable("customer_codes", {
41
+ id: text().primaryKey(),
42
+ orgId: orgIdColumn(),
43
+ kind: text({ enum: CUSTOMER_LOGIN_KINDS }).notNull(),
44
+ value: text().notNull(),
45
+ purpose: text({ enum: CUSTOMER_CODE_PURPOSES }).notNull(),
46
+ /** `add_login`: the signed-in customer the address will belong to. */
47
+ customerId: text(),
48
+ codeHash: text().notNull(),
49
+ attempts: integer().default(0).notNull(),
50
+ ip: text(),
51
+ expiresAt: timestamptz().notNull(),
52
+ usedAt: timestamptz(),
53
+ createdAt: createdAt()
54
+ }, (table) => [
55
+ index("core_customer_codes_org_address_idx").on(table.orgId, table.kind, table.value, table.createdAt.desc()),
56
+ foreignKey({
57
+ columns: [table.orgId],
58
+ foreignColumns: [organizations.id],
59
+ name: "core_customer_codes_org_id_fkey"
60
+ }).onDelete("cascade"),
61
+ foreignKey({
62
+ columns: [table.customerId],
63
+ foreignColumns: [customers.id],
64
+ name: "core_customer_codes_customer_id_fkey"
65
+ }).onDelete("cascade"),
66
+ check("core_customer_codes_kind_check", inList(table.kind, CUSTOMER_LOGIN_KINDS)),
67
+ check("core_customer_codes_purpose_check", inList(table.purpose, CUSTOMER_CODE_PURPOSES))
68
+ ]);
69
+ /**
70
+ * A customer signed in to a plugin's app, held as a refresh token. The token
71
+ * rotates on every use; the previous one is kept, so using it again (it
72
+ * leaked) ends the session.
73
+ */
74
+ const customerSessions = pgTable("customer_sessions", {
75
+ id: text().primaryKey(),
76
+ orgId: orgIdColumn(),
77
+ customerId: text().notNull(),
78
+ /** The plugin whose app holds it. */
79
+ clientId: text().notNull(),
80
+ tokenHash: text().notNull(),
81
+ previousTokenHash: text(),
82
+ userAgent: text(),
83
+ ip: text(),
84
+ createdAt: createdAt(),
85
+ lastUsedAt: timestamptz().defaultNow().notNull(),
86
+ expiresAt: timestamptz().notNull(),
87
+ revokedAt: timestamptz()
88
+ }, (table) => [
89
+ index("core_customer_sessions_org_customer_idx").on(table.orgId, table.customerId),
90
+ foreignKey({
91
+ columns: [table.orgId],
92
+ foreignColumns: [organizations.id],
93
+ name: "core_customer_sessions_org_id_fkey"
94
+ }).onDelete("cascade"),
95
+ foreignKey({
96
+ columns: [table.customerId],
97
+ foreignColumns: [customers.id],
98
+ name: "core_customer_sessions_customer_id_fkey"
99
+ }).onDelete("cascade")
100
+ ]);
101
+ export { customerLogins, customerSessions };
@@ -1,15 +1,15 @@
1
- import { pgTable } from "../table.js";
1
+ import { CUSTOMER_SEXES, CUSTOMER_STATES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
2
3
  import { leadStages } from "./leadStages.js";
3
4
  import { organizations } from "./organizations.js";
4
5
  import { users } from "./users.js";
5
6
  import { check, date, foreignKey, index, jsonb, smallint, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
6
7
  import { sql } from "drizzle-orm";
7
8
  // ../db/src/schema/customers.ts
8
- const CUSTOMER_STATES = ["lead", "customer"];
9
9
  /** People the organisation works with, shared by every plugin. */
10
10
  const customers = pgTable("customers", {
11
11
  id: text().primaryKey(),
12
- orgId: text().notNull(),
12
+ orgId: orgIdColumn(),
13
13
  state: text({ enum: CUSTOMER_STATES }).default("customer").notNull(),
14
14
  firstName: text(),
15
15
  middleName: text(),
@@ -101,7 +101,7 @@ const customers = pgTable("customers", {
101
101
  foreignColumns: [users.id],
102
102
  name: "core_customers_owner_id_fkey"
103
103
  }).onDelete("set null"),
104
- check("core_customers_state_check", sql`${table.state} IN ('lead', 'customer')`),
105
- check("core_customers_sex_check", sql`${table.sex} IN (0, 1, 2, 9)`)
104
+ check("core_customers_state_check", inList(table.state, CUSTOMER_STATES)),
105
+ check("core_customers_sex_check", inList(table.sex, CUSTOMER_SEXES))
106
106
  ]);
107
- export { CUSTOMER_STATES, customers };
107
+ export { customers };
@@ -1,30 +1,25 @@
1
- import { pgTable } from "../table.js";
2
- import { CONTENT_TYPES, createdAt, updatedAt } from "./common.js";
1
+ import { ACTOR_TYPES, CONTENT_TYPES } from "../enums.js";
2
+ import { orgIdColumn, pgTable } from "../table.js";
3
+ import { createdAt } from "./common.js";
3
4
  import { organizations } from "./organizations.js";
4
- import { ACTOR_TYPES } from "./platform.js";
5
- import { bigint, bigserial, customType, foreignKey, index, integer, jsonb, primaryKey, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
5
+ import { foreignKey, index, integer, jsonb, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
6
6
  import { sql } from "drizzle-orm";
7
7
  // ../db/src/schema/events.ts
8
8
  const timestamptz = () => timestamp({
9
9
  withTimezone: true,
10
10
  mode: "date"
11
11
  });
12
- /** Postgres `xid8`: the id of the transaction that wrote a row. */
13
- const xid8 = customType({ dataType: () => "xid8" });
14
12
  /**
15
- * Every domain event, from core and plugins: the transactional outbox.
16
- * Written in the same transaction as the change (`core_publish_event()` for
17
- * plugins on this database, `POST /events` otherwise), so an event exists if
18
- * and only if its change was committed. Consumers read in commit order
19
- * (`tx_id`, `seq`) and turn events into timeline entries, notifications,
20
- * automation runs and webhooks.
13
+ * Every domain event, from core and plugins. Written in the same transaction
14
+ * as the change (`core_publish_event()` for plugins on this database,
15
+ * `POST /events` otherwise), so an event exists if and only if its change was
16
+ * committed. The insert trigger queues one job per consumer in that
17
+ * transaction too (`modules/events/consumers.ts`): timeline entries,
18
+ * notifications, automation runs, webhooks and plugin deliveries.
21
19
  */
22
20
  const events = pgTable("events", {
23
21
  id: text().primaryKey(),
24
- seq: bigserial({ mode: "number" }).notNull(),
25
- /** Transaction that wrote it: consumers only read finished transactions. */
26
- txId: xid8().default(sql`pg_current_xact_id()`).notNull(),
27
- orgId: text().notNull(),
22
+ orgId: orgIdColumn(),
28
23
  /** `core` or the plugin that published it. */
29
24
  source: text().notNull(),
30
25
  /** Namespaced by source: `core.customer.created`, `hr.leave.approved`. */
@@ -50,8 +45,6 @@ const events = pgTable("events", {
50
45
  depth: integer().default(0).notNull(),
51
46
  createdAt: createdAt()
52
47
  }, (table) => [
53
- uniqueIndex("core_events_seq_key").on(table.seq),
54
- index("core_events_commit_order_idx").on(table.txId, table.seq),
55
48
  index("core_events_org_created_idx").on(table.orgId, table.createdAt.desc(), table.id.desc()),
56
49
  index("core_events_subject_idx").on(table.orgId, table.subjectType, table.subjectId),
57
50
  uniqueIndex("core_events_dedupe_key").on(table.orgId, table.source, table.dedupeKey).where(sql`${table.dedupeKey} IS NOT NULL`),
@@ -61,45 +54,4 @@ const events = pgTable("events", {
61
54
  name: "core_events_org_id_fkey"
62
55
  }).onDelete("cascade")
63
56
  ]);
64
- pgTable("event_consumers", {
65
- name: text().primaryKey(),
66
- lastTxId: xid8().default(sql`'0'::xid8`).notNull(),
67
- lastSeq: bigint({ mode: "number" }).default(0).notNull(),
68
- updatedAt: updatedAt()
69
- });
70
- const EVENT_FAILURE_STATUSES = ["retrying", "dead"];
71
- /**
72
- * Events a consumer could not handle. The consumer's cursor moves on (one bad
73
- * event never blocks the stream); the event is retried here with backoff and,
74
- * after the last attempt, kept as `dead` until someone retries it by hand.
75
- */
76
- const eventFailures = pgTable("event_failures", {
77
- consumer: text().notNull(),
78
- eventId: text().notNull(),
79
- orgId: text().notNull(),
80
- status: text({ enum: EVENT_FAILURE_STATUSES }).default("retrying").notNull(),
81
- attempts: integer().default(1).notNull(),
82
- lastError: text().notNull(),
83
- /** When to try again; null once dead. */
84
- nextAttemptAt: timestamptz(),
85
- firstFailedAt: timestamptz().defaultNow().notNull(),
86
- updatedAt: updatedAt()
87
- }, (table) => [
88
- primaryKey({
89
- columns: [table.consumer, table.eventId],
90
- name: "core_event_failures_pkey"
91
- }),
92
- index("core_event_failures_due_idx").on(table.nextAttemptAt).where(sql`${table.status} = 'retrying'`),
93
- index("core_event_failures_org_idx").on(table.orgId, table.updatedAt.desc()),
94
- foreignKey({
95
- columns: [table.eventId],
96
- foreignColumns: [events.id],
97
- name: "core_event_failures_event_id_fkey"
98
- }).onDelete("cascade"),
99
- foreignKey({
100
- columns: [table.orgId],
101
- foreignColumns: [organizations.id],
102
- name: "core_event_failures_org_id_fkey"
103
- }).onDelete("cascade")
104
- ]);
105
- export { EVENT_FAILURE_STATUSES, eventFailures, events };
57
+ export { events };
@@ -1,4 +1,4 @@
1
- import { pgTable } from "../table.js";
1
+ import { orgIdColumn, pgTable } from "../table.js";
2
2
  import { companies } from "./companies.js";
3
3
  import { customers } from "./customers.js";
4
4
  import { fields } from "./fields.js";
@@ -24,7 +24,7 @@ const fieldValues = (name) => {
24
24
  const records = RECORDS[name];
25
25
  const t = `core_${name}`;
26
26
  return pgTable(name, {
27
- orgId: text().notNull(),
27
+ orgId: orgIdColumn(),
28
28
  recordId: text().notNull(),
29
29
  fieldId: text().notNull(),
30
30
  position: smallint().default(0).notNull(),
@@ -1,35 +1,17 @@
1
- import { pgTable } from "../table.js";
2
- import { FIELD_CONTENT_TYPES, createdAt, updatedAt } from "./common.js";
1
+ import { FIELD_CONTENT_TYPES, FIELD_TYPES, FIELD_VALIDATIONS } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
3
+ import { createdAt, updatedAt } from "./common.js";
3
4
  import { organizations } from "./organizations.js";
4
5
  import { boolean, check, foreignKey, index, integer, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
5
6
  import { sql } from "drizzle-orm";
6
7
  // ../db/src/schema/fields.ts
7
- const FIELD_TYPES = [
8
- "text",
9
- "textarea",
10
- "number",
11
- "date",
12
- "boolean",
13
- "select",
14
- "multiselect",
15
- "radio",
16
- "check",
17
- "phone",
18
- "list"
19
- ];
20
- /** Formats a text field's values must have. */
21
- const FIELD_VALIDATIONS = [
22
- "number",
23
- "email",
24
- "date"
25
- ];
26
8
  /**
27
9
  * Groups that properties of a record type are organised in (old erxes field
28
10
  * groups). Archiving a group hides it and its fields; values stay.
29
11
  */
30
12
  const fieldGroups = pgTable("field_groups", {
31
13
  id: text().primaryKey(),
32
- orgId: text().notNull(),
14
+ orgId: orgIdColumn(),
33
15
  contentType: text({ enum: FIELD_CONTENT_TYPES }).notNull(),
34
16
  name: text().notNull(),
35
17
  code: text().notNull(),
@@ -49,7 +31,7 @@ const fieldGroups = pgTable("field_groups", {
49
31
  foreignColumns: [organizations.id],
50
32
  name: "core_field_groups_org_id_fkey"
51
33
  }).onDelete("cascade"),
52
- check("core_field_groups_content_type_check", sql`${table.contentType} IN ('customer', 'company', 'product')`)
34
+ check("core_field_groups_content_type_check", inList(table.contentType, FIELD_CONTENT_TYPES))
53
35
  ]);
54
36
  /**
55
37
  * Custom fields ("properties") an organisation adds to customers, companies
@@ -57,7 +39,7 @@ const fieldGroups = pgTable("field_groups", {
57
39
  */
58
40
  const fields = pgTable("fields", {
59
41
  id: text().primaryKey(),
60
- orgId: text().notNull(),
42
+ orgId: orgIdColumn(),
61
43
  contentType: text({ enum: FIELD_CONTENT_TYPES }).notNull(),
62
44
  /** The group it is listed in; fields made before groups have none. */
63
45
  groupId: text(),
@@ -96,8 +78,8 @@ const fields = pgTable("fields", {
96
78
  foreignColumns: [fieldGroups.id],
97
79
  name: "core_fields_group_id_fkey"
98
80
  }).onDelete("set null"),
99
- check("core_fields_content_type_check", sql`${table.contentType} IN ('customer', 'company', 'product')`),
100
- check("core_fields_type_check", sql`${table.type} IN ('text', 'textarea', 'number', 'date', 'boolean', 'select', 'multiselect', 'radio', 'check', 'phone', 'list')`),
101
- check("core_fields_validation_check", sql`${table.validation} IS NULL OR ${table.validation} IN ('number', 'email', 'date')`)
81
+ check("core_fields_content_type_check", inList(table.contentType, FIELD_CONTENT_TYPES)),
82
+ check("core_fields_type_check", inList(table.type, FIELD_TYPES)),
83
+ check("core_fields_validation_check", sql`${table.validation} IS NULL OR ${inList(table.validation, FIELD_VALIDATIONS)}`)
102
84
  ]);
103
- export { FIELD_TYPES, FIELD_VALIDATIONS, fieldGroups, fields };
85
+ export { fieldGroups, fields };
@@ -1,26 +1,27 @@
1
- import { CONDITION_OPERATORS, CONTENT_TYPES, FIELD_CONTENT_TYPES, createdAt, updatedAt } from "./common.js";
1
+ import { createdAt, updatedAt } from "./common.js";
2
2
  import { companies, customerCompanies } from "./companies.js";
3
- import { CUSTOMER_STATES, customers } from "./customers.js";
4
- import { FIELD_TYPES, FIELD_VALIDATIONS, fieldGroups, fields } from "./fields.js";
3
+ import { customers } from "./customers.js";
4
+ import { fieldGroups, fields } from "./fields.js";
5
5
  import { organizations } from "./organizations.js";
6
6
  import { tags } from "./tags.js";
7
- import { USER_STATUSES, roles, users } from "./users.js";
8
- import { ACTOR_TYPES, FILE_STATUSES, NOTIFICATION_PRIORITIES, activities, auditLogs, files, notifications } from "./platform.js";
9
- import { EVENT_FAILURE_STATUSES, eventFailures, events } from "./events.js";
10
- import { PLUGIN_STATUSES, plugins } from "./plugins.js";
11
- import { JOB_STATUSES } from "./jobs.js";
12
- import { WEBHOOK_DELIVERY_STATUSES, WEBHOOK_STATUSES, webhookDeliveries, webhooks } from "./webhooks.js";
13
- import { AUTOMATION_STATUSES, RUN_STATUSES, RUN_STEP_STATUSES, automationRunSteps, automationRuns, automations } from "./automations.js";
14
- import { AUTH_TOKEN_PURPOSES } from "./authTokens.js";
7
+ import { roles, users } from "./users.js";
8
+ import { activities, auditLogs, files, notifications } from "./platform.js";
9
+ import { events } from "./events.js";
10
+ import { plugins } from "./plugins.js";
11
+ import { webhookDeliveries, webhooks } from "./webhooks.js";
12
+ import { automationRunSteps, automationRuns, automations } from "./automations.js";
13
+ import "./authTokens.js";
15
14
  import "./oauth.js";
16
- import { INVOICE_STATUSES, PAYMENT_GATEWAY_KINDS, REFUND_METHODS, REFUND_STATUSES, paymentGateways, paymentInvoices, paymentPluginSettings, paymentRefunds } from "./payments.js";
15
+ import { paymentGateways, paymentInvoices, paymentPluginSettings, paymentRefunds } from "./payments.js";
17
16
  import { trash } from "./trash.js";
18
- import { AUDIT_STREAM_FORMATS, AUDIT_STREAM_STATUSES, auditStreams } from "./auditStreams.js";
19
- import { ORGANIZATION_STATUSES } from "./saas.js";
20
- import { VIEW_RECORD_TYPES, views } from "./views.js";
21
- import { EXPORT_STATUSES, IMPORT_STATUSES, TRANSFER_RECORD_TYPES, exports, importRows, imports } from "./transfer.js";
17
+ import { auditStreams } from "./auditStreams.js";
18
+ import "./saas.js";
19
+ import { views } from "./views.js";
20
+ import { exports, importRows, imports } from "./transfer.js";
22
21
  import "./fieldValues.js";
23
- import { LEAD_STAGE_KINDS, leadStages } from "./leadStages.js";
24
- import { LEAD_EVENTS, leadEvents } from "./leads.js";
25
- import { PRODUCT_KINDS, PRODUCT_STATUSES, productCategories, products } from "./products.js";
26
- export { ACTOR_TYPES, AUDIT_STREAM_FORMATS, AUDIT_STREAM_STATUSES, AUTH_TOKEN_PURPOSES, AUTOMATION_STATUSES, CONDITION_OPERATORS, CONTENT_TYPES, CUSTOMER_STATES, EVENT_FAILURE_STATUSES, EXPORT_STATUSES, FIELD_CONTENT_TYPES, FIELD_TYPES, FIELD_VALIDATIONS, FILE_STATUSES, IMPORT_STATUSES, INVOICE_STATUSES, JOB_STATUSES, LEAD_EVENTS, LEAD_STAGE_KINDS, NOTIFICATION_PRIORITIES, ORGANIZATION_STATUSES, PAYMENT_GATEWAY_KINDS, PLUGIN_STATUSES, PRODUCT_KINDS, PRODUCT_STATUSES, REFUND_METHODS, REFUND_STATUSES, RUN_STATUSES, RUN_STEP_STATUSES, TRANSFER_RECORD_TYPES, USER_STATUSES, VIEW_RECORD_TYPES, WEBHOOK_DELIVERY_STATUSES, WEBHOOK_STATUSES, activities, auditLogs, auditStreams, automationRunSteps, automationRuns, automations, companies, createdAt, customerCompanies, customers, eventFailures, events, exports, fieldGroups, fields, files, importRows, imports, leadEvents, leadStages, notifications, organizations, paymentGateways, paymentInvoices, paymentPluginSettings, paymentRefunds, plugins, productCategories, products, roles, tags, trash, updatedAt, users, views, webhookDeliveries, webhooks };
22
+ import { leadStages } from "./leadStages.js";
23
+ import { leadEvents } from "./leads.js";
24
+ import { productCategories, products } from "./products.js";
25
+ import { customerLogins, customerSessions } from "./customerAuth.js";
26
+ import "./rateLimits.js";
27
+ export { activities, auditLogs, auditStreams, automationRunSteps, automationRuns, automations, companies, createdAt, customerCompanies, customerLogins, customerSessions, customers, events, exports, fieldGroups, fields, files, importRows, imports, leadEvents, leadStages, notifications, organizations, paymentGateways, paymentInvoices, paymentPluginSettings, paymentRefunds, plugins, productCategories, products, roles, tags, trash, updatedAt, users, views, webhookDeliveries, webhooks };
@@ -1,22 +1,17 @@
1
- import { pgTable } from "../table.js";
1
+ import { LEAD_STAGE_KINDS } 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, uniqueIndex } from "drizzle-orm/pg-core";
5
6
  import { sql } from "drizzle-orm";
6
7
  // ../db/src/schema/leadStages.ts
7
- /** `open`: still being worked; `qualified`: ready to become a customer; `disqualified`: not a fit (lost). */
8
- const LEAD_STAGE_KINDS = [
9
- "open",
10
- "qualified",
11
- "disqualified"
12
- ];
13
8
  /**
14
9
  * The stages a lead moves through, the organisation's own (New → Contacted →
15
10
  * Qualified …), in order. Every organisation starts with a default set.
16
11
  */
17
12
  const leadStages = pgTable("lead_stages", {
18
13
  id: text().primaryKey(),
19
- orgId: text().notNull(),
14
+ orgId: orgIdColumn(),
20
15
  name: text().notNull(),
21
16
  kind: text({ enum: LEAD_STAGE_KINDS }).default("open").notNull(),
22
17
  /** `#rrggbb` */
@@ -32,6 +27,6 @@ const leadStages = pgTable("lead_stages", {
32
27
  foreignColumns: [organizations.id],
33
28
  name: "core_lead_stages_org_id_fkey"
34
29
  }).onDelete("cascade"),
35
- check("core_lead_stages_kind_check", sql`${table.kind} IN ('open', 'qualified', 'disqualified')`)
30
+ check("core_lead_stages_kind_check", inList(table.kind, LEAD_STAGE_KINDS))
36
31
  ]);
37
- export { LEAD_STAGE_KINDS, leadStages };
32
+ export { leadStages };
@@ -1,16 +1,11 @@
1
- import { pgTable } from "../table.js";
1
+ import { 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(),
@@ -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 };
@@ -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 { 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(),
@@ -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 };