@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
package/dist/customers.js CHANGED
@@ -1,7 +1,9 @@
1
+ import { CUSTOMER_STATES } from "./db/src/enums.js";
1
2
  import { customers } from "./db/src/schema/customers.js";
2
3
  import { companies, customerCompanies } from "./db/src/schema/companies.js";
3
4
  import "./db/src/schema/index.js";
4
- import { CursorQuery, CustomFields, CustomFieldsPatch, EmailAddress, Id, IdList, ImageAddress, PhoneNumber, cursorPageOf, isoDate } from "./common.js";
5
+ import { CustomerSex } from "./values.js";
6
+ import { CursorQuery, CustomFields, CustomFieldsPatch, DESCRIPTION_MAX, EmailAddress, Id, IdList, ImageAddress, PhoneNumber, cursorPageOf, isoDate, listOf } from "./common.js";
5
7
  import { ContactState, LIFECYCLE_COLUMNS, LIFECYCLE_INPUT_COLUMNS, lifecycleInput, lifecycleRow } from "./leads.js";
6
8
  import { oc } from "@orpc/contract";
7
9
  import { createSelectSchema, createUpdateSchema } from "drizzle-zod";
@@ -9,13 +11,6 @@ import { z } from "zod";
9
11
  // src/customers.ts
10
12
  /** `lead`: not a customer yet (e.g. a form submission). */
11
13
  const CustomerState = ContactState;
12
- /** ISO/IEC 5218, as in erxes until now: 0 not known, 1 male, 2 female, 9 not applicable. */
13
- const CustomerSex = z.union([
14
- z.literal(0),
15
- z.literal(1),
16
- z.literal(2),
17
- z.literal(9)
18
- ]);
19
14
  /** A person the organisation works with; shared by every plugin. */
20
15
  const Customer = createSelectSchema(customers, {
21
16
  id: Id,
@@ -66,7 +61,7 @@ const CustomerFields = createUpdateSchema(customers, {
66
61
  position: (value) => value.trim().max(200),
67
62
  department: (value) => value.trim().max(200),
68
63
  code: (code) => code.trim().max(100),
69
- description: (value) => value.trim().max(5e3),
64
+ description: (value) => value.trim().max(DESCRIPTION_MAX),
70
65
  ownerId: () => Id,
71
66
  ...lifecycleInput
72
67
  }).pick({
@@ -159,7 +154,7 @@ const customersContract = {
159
154
  primaryPhone: z.string().trim().min(3).max(40).optional(),
160
155
  firstName: z.string().trim().max(100).optional(),
161
156
  lastName: z.string().trim().max(100).optional(),
162
- state: z.enum(["lead", "customer"]).optional(),
157
+ state: z.enum(CUSTOMER_STATES).optional(),
163
158
  /** For a new one: where they came from (default: the plugin, or `manual`). */
164
159
  source: CustomerFields.shape.source,
165
160
  sourceDetails: CustomerFields.shape.sourceDetails
@@ -201,7 +196,7 @@ const customersContract = {
201
196
  method: "GET",
202
197
  path: "/customers/{id}/companies",
203
198
  summary: "The customer's companies"
204
- }).input(ID).output(z.object({ data: z.array(CompanyRef) })),
199
+ }).input(ID).output(listOf(CompanyRef)),
205
200
  linkCompany: oc.route({
206
201
  method: "PUT",
207
202
  path: "/customers/{id}/companies/{companyId}",
@@ -218,4 +213,4 @@ const customersContract = {
218
213
  companyId: Id
219
214
  })).output(z.object({ ok: z.literal(true) }))
220
215
  };
221
- export { CompanyRef, ContactLink, ContactLinkInput, Customer, CustomerFields, CustomerSex, CustomerState, customersContract };
216
+ export { CompanyRef, ContactLink, ContactLinkInput, Customer, CustomerFields, CustomerState, customersContract };
@@ -0,0 +1,8 @@
1
+ /** `all`: every condition holds; `any`: one does. */
2
+ export declare const CONDITION_MATCHES: readonly ['all', 'any'];
3
+ export declare const CONDITION_OPERATORS: readonly ['eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'contains', 'not_contains', 'starts_with', 'in', 'not_in', 'exists', 'not_exists',
4
+ /** Dates relative to now; the value is `{ amount, unit }` (days, weeks, months). */
5
+ 'within_last', 'more_than_ago', 'within_next',
6
+ /** The day and month fall within the next `amount` days (birthdays, anniversaries), whatever the year. */
7
+ 'anniversary_within'];
8
+ export declare const DELAY_UNITS: readonly ['minutes', 'hours', 'days'];
@@ -0,0 +1,336 @@
1
+ // ../db/src/enums.ts
2
+ /**
3
+ * Every value list core stores, accepts or reads from its settings, one `as const` array
4
+ * each. The tables (`text({ enum })`, `inList` checks), the contract (`z.enum`) and the
5
+ * config all read them from here; `erxes/no-inline-enum` fails a list spelled out anywhere
6
+ * else. This file imports nothing, so a browser can load an enum without the tables or drizzle.
7
+ */
8
+ /** The contact records: what tags, trash, webhooks and automation subjects refer to. */
9
+ const CONTENT_TYPES = ["customer", "company"];
10
+ /** An automation or plugin action's subject: a contact record, or either. */
11
+ const AUTOMATION_SUBJECTS = [...CONTENT_TYPES, "any"];
12
+ /** Records custom fields attach to: contacts, and the shared product catalog. */
13
+ const FIELD_CONTENT_TYPES = [...CONTENT_TYPES, "product"];
14
+ const CUSTOMER_STATES = ["lead", "customer"];
15
+ /** ISO/IEC 5218: not known, male, female, not applicable. */
16
+ const CUSTOMER_SEXES = [
17
+ 0,
18
+ 1,
19
+ 2,
20
+ 9
21
+ ];
22
+ /** The fields no two customers share. A 409 `CUSTOMER_EXISTS` names the one that was taken. */
23
+ const CUSTOMER_UNIQUE_FIELDS = ["primaryEmail", "primaryPhone"];
24
+ /** `open`: still being worked; `qualified`: ready to become a customer; `disqualified`: not a fit (lost). */
25
+ const LEAD_STAGE_KINDS = [
26
+ "open",
27
+ "qualified",
28
+ "disqualified"
29
+ ];
30
+ const LEAD_EVENTS = [
31
+ "created",
32
+ "stage",
33
+ "converted",
34
+ "reopened"
35
+ ];
36
+ const PRODUCT_KINDS = ["goods", "service"];
37
+ const PRODUCT_STATUSES = ["active", "archived"];
38
+ /** Lists views belong to: customers, leads (people), companies, company leads, products. */
39
+ const VIEW_RECORD_TYPES = [
40
+ "customer",
41
+ "lead",
42
+ "company",
43
+ "company_lead",
44
+ "product"
45
+ ];
46
+ const ACTIVITY_TARGETS = ["customer", "company"];
47
+ /** Which activities a record's timeline shows. */
48
+ const TIMELINE_KINDS = [
49
+ "all",
50
+ "notes",
51
+ "changes",
52
+ "plugins"
53
+ ];
54
+ const SORT_DIRECTIONS = ["asc", "desc"];
55
+ const PAGE_DIRECTIONS = ["forward", "backward"];
56
+ /** Whether a page starts at the cursor's record or after it. */
57
+ const CURSOR_MODES = ["inclusive", "exclusive"];
58
+ /** A yes/no query parameter or setting, as text. */
59
+ const BOOLEAN_STRINGS = ["true", "false"];
60
+ /** How a list filter's values compare. */
61
+ const FILTER_FIELD_TYPES = [
62
+ "text",
63
+ "number",
64
+ "date",
65
+ "boolean",
66
+ "select",
67
+ "member",
68
+ "tags"
69
+ ];
70
+ /** Where a list filter comes from, for grouping in the picker. */
71
+ const FILTER_FIELD_GROUPS = [
72
+ "fields",
73
+ "lead",
74
+ "custom",
75
+ "company",
76
+ "activity"
77
+ ];
78
+ const FIELD_TYPES = [
79
+ "text",
80
+ "textarea",
81
+ "number",
82
+ "date",
83
+ "boolean",
84
+ "select",
85
+ "multiselect",
86
+ "radio",
87
+ "check",
88
+ "phone",
89
+ "list"
90
+ ];
91
+ /** `all`: every condition holds; `any`: one does. */
92
+ const CONDITION_MATCHES = ["all", "any"];
93
+ /** The units of a relative date condition's `{ amount, unit }`. */
94
+ const RELATIVE_DATE_UNITS = [
95
+ "days",
96
+ "weeks",
97
+ "months"
98
+ ];
99
+ /** Formats a text field's values must have. */
100
+ const FIELD_VALIDATIONS = [
101
+ "number",
102
+ "email",
103
+ "date"
104
+ ];
105
+ const CONDITION_OPERATORS = [
106
+ "eq",
107
+ "neq",
108
+ "gt",
109
+ "gte",
110
+ "lt",
111
+ "lte",
112
+ "contains",
113
+ "not_contains",
114
+ "starts_with",
115
+ "in",
116
+ "not_in",
117
+ "exists",
118
+ "not_exists",
119
+ "within_last",
120
+ "more_than_ago",
121
+ "within_next",
122
+ "anniversary_within"
123
+ ];
124
+ const USER_STATUSES = [
125
+ "invited",
126
+ "active",
127
+ "disabled"
128
+ ];
129
+ const AUTH_TOKEN_PURPOSES = ["invite", "password_reset"];
130
+ /** The languages core's emails and example data come in. */
131
+ const LANGUAGES = ["en", "mn"];
132
+ /** How a member gets one kind of notification. */
133
+ const NOTIFICATION_DELIVERIES = [
134
+ "email",
135
+ "inbox",
136
+ "off"
137
+ ];
138
+ /** How a customer signs in: a code sent to an email address or a phone. */
139
+ const CUSTOMER_LOGIN_KINDS = ["email", "phone"];
140
+ /** What a sign-in code is for: signing in (or up), or adding a login to a signed-in customer. */
141
+ const CUSTOMER_CODE_PURPOSES = ["sign_in", "add_login"];
142
+ const ORGANIZATION_STATUSES = [
143
+ "active",
144
+ "suspended",
145
+ "inactive"
146
+ ];
147
+ const PLUGIN_TIERS = [
148
+ "free",
149
+ "plus",
150
+ "pro"
151
+ ];
152
+ const PLUGIN_STATUSES = ["active", "disabled"];
153
+ /** A plugin's billing status on erxes.io: `past_due` while Stripe retries, `canceling` until the period ends. */
154
+ const ENTITLEMENT_STATUSES = [
155
+ "active",
156
+ "past_due",
157
+ "canceling"
158
+ ];
159
+ /**
160
+ * What a plugin may do here: `full`; `warning` (payment retrying or ending
161
+ * at period end: everything still works); `read_only` (it left the plan:
162
+ * reads and exports for the grace period); `off` (not in the plan).
163
+ */
164
+ const PLUGIN_ACCESS = [
165
+ "full",
166
+ "warning",
167
+ "read_only",
168
+ "off"
169
+ ];
170
+ /**
171
+ * Who did something: a team member, a plugin (by name, e.g. `hr`), a signed-in
172
+ * customer, or core itself (`system`: cleanups, imports). Ids only: names are
173
+ * resolved when shown.
174
+ */
175
+ const ACTOR_TYPES = [
176
+ "user",
177
+ "plugin",
178
+ "customer",
179
+ "system"
180
+ ];
181
+ /** `deleting`: removed, waiting for storage to confirm the bytes are gone (retried until it does). */
182
+ const FILE_STATUSES = [
183
+ "pending",
184
+ "ready",
185
+ "deleting"
186
+ ];
187
+ /** Where files are kept. */
188
+ const FILE_STORAGES = ["local", "s3"];
189
+ /** How a browser gets live updates: a stream, or polling when streams are off. */
190
+ const LIVE_MODES = ["live", "polling"];
191
+ const NOTIFICATION_PRIORITIES = ["normal", "high"];
192
+ const AUDIT_STREAM_FORMATS = [
193
+ "erxes",
194
+ "datadog",
195
+ "splunk"
196
+ ];
197
+ const AUDIT_STREAM_STATUSES = ["active", "paused"];
198
+ /** Where a new audit stream starts: new entries only, or the whole log. */
199
+ const AUDIT_STREAM_STARTS = ["now", "beginning"];
200
+ const EVENT_FAILURE_STATUSES = ["retrying", "dead"];
201
+ const WEBHOOK_STATUSES = ["active", "disabled"];
202
+ const WEBHOOK_DELIVERY_STATUSES = [
203
+ "pending",
204
+ "succeeded",
205
+ "failed"
206
+ ];
207
+ const TRANSFER_RECORD_TYPES = [
208
+ "customer",
209
+ "lead",
210
+ "company"
211
+ ];
212
+ const IMPORT_STATUSES = [
213
+ "parsing",
214
+ "mapping",
215
+ "checking",
216
+ "checked",
217
+ "running",
218
+ "done",
219
+ "failed",
220
+ "undone"
221
+ ];
222
+ const EXPORT_STATUSES = [
223
+ "running",
224
+ "done",
225
+ "failed"
226
+ ];
227
+ const EXPORT_FORMATS = ["csv", "xlsx"];
228
+ /** The columns an import maps to: the record's fields, custom fields, links to other records. */
229
+ const TRANSFER_FIELD_GROUPS = [
230
+ "fields",
231
+ "custom",
232
+ "relations"
233
+ ];
234
+ const IMPORT_MODES = [
235
+ "create",
236
+ "update",
237
+ "upsert"
238
+ ];
239
+ /** The column an import finds an existing record by. */
240
+ const IMPORT_MATCH_KEYS = [
241
+ "email",
242
+ "phone",
243
+ "code",
244
+ "id",
245
+ "name"
246
+ ];
247
+ /** What an import does with one row. */
248
+ const IMPORT_ROW_ACTIONS = [
249
+ "create",
250
+ "update",
251
+ "skip",
252
+ "error"
253
+ ];
254
+ const AUTOMATION_STATUSES = [
255
+ "draft",
256
+ "active",
257
+ "paused"
258
+ ];
259
+ const FINISHED_RUN_STATUSES = [
260
+ "succeeded",
261
+ "failed",
262
+ "cancelled"
263
+ ];
264
+ const RUN_STATUSES = [
265
+ "running",
266
+ "waiting",
267
+ ...FINISHED_RUN_STATUSES
268
+ ];
269
+ const RUN_STEP_STATUSES = [
270
+ "succeeded",
271
+ "failed",
272
+ "skipped"
273
+ ];
274
+ /** Whether a record may enter an automation again: every time, once, or once a day. */
275
+ const REENTRY_RULES = [
276
+ "always",
277
+ "once",
278
+ "daily"
279
+ ];
280
+ const DELAY_UNITS = [
281
+ "minutes",
282
+ "hours",
283
+ "days"
284
+ ];
285
+ /** How often a scheduled trigger fires. */
286
+ const SCHEDULE_INTERVALS = [
287
+ "hour",
288
+ "day",
289
+ "week",
290
+ "month"
291
+ ];
292
+ /** How the values of a path conditions and placeholders read compare. */
293
+ const AUTOMATION_FIELD_TYPES = [
294
+ "text",
295
+ "number",
296
+ "date",
297
+ "boolean",
298
+ "list",
299
+ "member",
300
+ "tags",
301
+ "select"
302
+ ];
303
+ /** The inputs an action's form shows. */
304
+ const ACTION_FIELD_TYPES = [
305
+ "text",
306
+ "longtext",
307
+ "number",
308
+ "boolean",
309
+ "date",
310
+ "select",
311
+ "members",
312
+ "tags",
313
+ "json",
314
+ "custom_field",
315
+ "lead_stage",
316
+ "recipients"
317
+ ];
318
+ const PAYMENT_GATEWAY_KINDS = ["qpay", "test"];
319
+ const PAYMENT_GATEWAY_STATUSES = ["active", "disabled"];
320
+ /** How a gateway refunds: any part, only the whole payment, or not at all. */
321
+ const GATEWAY_REFUND_KINDS = [
322
+ "partial",
323
+ "full",
324
+ "none"
325
+ ];
326
+ const INVOICE_STATUSES = [
327
+ "pending",
328
+ "paid",
329
+ "refunded",
330
+ "cancelled",
331
+ "expired",
332
+ "failed"
333
+ ];
334
+ const REFUND_STATUSES = ["succeeded", "failed"];
335
+ const REFUND_METHODS = ["gateway", "manual"];
336
+ export { ACTION_FIELD_TYPES, ACTIVITY_TARGETS, ACTOR_TYPES, AUDIT_STREAM_FORMATS, AUDIT_STREAM_STARTS, AUDIT_STREAM_STATUSES, AUTH_TOKEN_PURPOSES, AUTOMATION_FIELD_TYPES, AUTOMATION_STATUSES, AUTOMATION_SUBJECTS, BOOLEAN_STRINGS, CONDITION_MATCHES, CONDITION_OPERATORS, CONTENT_TYPES, CURSOR_MODES, CUSTOMER_CODE_PURPOSES, CUSTOMER_LOGIN_KINDS, CUSTOMER_SEXES, CUSTOMER_STATES, CUSTOMER_UNIQUE_FIELDS, DELAY_UNITS, ENTITLEMENT_STATUSES, EVENT_FAILURE_STATUSES, EXPORT_FORMATS, EXPORT_STATUSES, FIELD_CONTENT_TYPES, FIELD_TYPES, FIELD_VALIDATIONS, FILE_STATUSES, FILE_STORAGES, FILTER_FIELD_GROUPS, FILTER_FIELD_TYPES, FINISHED_RUN_STATUSES, GATEWAY_REFUND_KINDS, IMPORT_MATCH_KEYS, IMPORT_MODES, IMPORT_ROW_ACTIONS, IMPORT_STATUSES, INVOICE_STATUSES, LANGUAGES, LEAD_EVENTS, LEAD_STAGE_KINDS, LIVE_MODES, NOTIFICATION_DELIVERIES, NOTIFICATION_PRIORITIES, ORGANIZATION_STATUSES, PAGE_DIRECTIONS, PAYMENT_GATEWAY_KINDS, PAYMENT_GATEWAY_STATUSES, PLUGIN_ACCESS, PLUGIN_STATUSES, PLUGIN_TIERS, PRODUCT_KINDS, PRODUCT_STATUSES, REENTRY_RULES, REFUND_METHODS, REFUND_STATUSES, RELATIVE_DATE_UNITS, RUN_STATUSES, RUN_STEP_STATUSES, SCHEDULE_INTERVALS, SORT_DIRECTIONS, TIMELINE_KINDS, TRANSFER_FIELD_GROUPS, TRANSFER_RECORD_TYPES, USER_STATUSES, VIEW_RECORD_TYPES, WEBHOOK_DELIVERY_STATUSES, WEBHOOK_STATUSES };
@@ -1,19 +1,13 @@
1
- import { pgTable } from "../table.js";
1
+ import { AUDIT_STREAM_FORMATS, AUDIT_STREAM_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 } from "drizzle-orm/pg-core";
5
- import { sql } from "drizzle-orm";
6
6
  // ../db/src/schema/auditStreams.ts
7
7
  const timestamptz = () => timestamp({
8
8
  withTimezone: true,
9
9
  mode: "date"
10
10
  });
11
- const AUDIT_STREAM_FORMATS = [
12
- "erxes",
13
- "datadog",
14
- "splunk"
15
- ];
16
- const AUDIT_STREAM_STATUSES = ["active", "paused"];
17
11
  /**
18
12
  * Sends an organisation's audit log, in chain order, to a log service as it
19
13
  * is written (SIEM, Datadog, Splunk, any HTTPS endpoint). `cursor_seq` is
@@ -22,7 +16,7 @@ const AUDIT_STREAM_STATUSES = ["active", "paused"];
22
16
  */
23
17
  const auditStreams = pgTable("audit_streams", {
24
18
  id: text().primaryKey(),
25
- orgId: text().notNull(),
19
+ orgId: orgIdColumn(),
26
20
  name: text().notNull(),
27
21
  url: text().notNull(),
28
22
  format: text({ enum: AUDIT_STREAM_FORMATS }).default("erxes").notNull(),
@@ -32,8 +26,6 @@ const auditStreams = pgTable("audit_streams", {
32
26
  authHeaderValueSealed: text(),
33
27
  status: text({ enum: AUDIT_STREAM_STATUSES }).default("active").notNull(),
34
28
  cursorSeq: bigint({ mode: "number" }).default(0).notNull(),
35
- /** The delivery loop that owns the stream: resuming starts a new one and the old one stops. */
36
- chainId: text(),
37
29
  lastDeliveredAt: timestamptz(),
38
30
  lastError: text(),
39
31
  consecutiveFailures: integer().default(0).notNull(),
@@ -42,12 +34,12 @@ const auditStreams = pgTable("audit_streams", {
42
34
  updatedAt: updatedAt()
43
35
  }, (table) => [
44
36
  index("core_audit_streams_org_idx").on(table.orgId),
45
- check("core_audit_streams_format_check", sql`${table.format} IN ('erxes', 'datadog', 'splunk')`),
46
- check("core_audit_streams_status_check", sql`${table.status} IN ('active', 'paused')`),
37
+ check("core_audit_streams_format_check", inList(table.format, AUDIT_STREAM_FORMATS)),
38
+ check("core_audit_streams_status_check", inList(table.status, AUDIT_STREAM_STATUSES)),
47
39
  foreignKey({
48
40
  columns: [table.orgId],
49
41
  foreignColumns: [organizations.id],
50
42
  name: "core_audit_streams_org_id_fkey"
51
43
  }).onDelete("cascade")
52
44
  ]);
53
- export { AUDIT_STREAM_FORMATS, AUDIT_STREAM_STATUSES, auditStreams };
45
+ export { auditStreams };
@@ -1,17 +1,16 @@
1
- import { pgTable } from "../table.js";
1
+ import { AUTH_TOKEN_PURPOSES } from "../enums.js";
2
+ import { inList, orgIdColumn, pgTable } from "../table.js";
2
3
  import { createdAt } from "./common.js";
3
4
  import { organizations } from "./organizations.js";
4
5
  import { check, foreignKey, index, text, timestamp } from "drizzle-orm/pg-core";
5
- import { sql } from "drizzle-orm";
6
6
  // ../db/src/schema/authTokens.ts
7
7
  const timestamptz = () => timestamp({
8
8
  withTimezone: true,
9
9
  mode: "date"
10
10
  });
11
- const AUTH_TOKEN_PURPOSES = ["invite", "password_reset"];
12
11
  pgTable("auth_tokens", {
13
12
  id: text().primaryKey(),
14
- orgId: text().notNull(),
13
+ orgId: orgIdColumn(),
15
14
  userId: text().notNull(),
16
15
  purpose: text({ enum: AUTH_TOKEN_PURPOSES }).notNull(),
17
16
  secretHash: text().notNull(),
@@ -20,11 +19,10 @@ pgTable("auth_tokens", {
20
19
  createdAt: createdAt()
21
20
  }, (table) => [
22
21
  index("core_auth_tokens_user_idx").on(table.orgId, table.userId, table.purpose),
23
- check("core_auth_tokens_purpose_check", sql`${table.purpose} IN ('invite', 'password_reset')`),
22
+ check("core_auth_tokens_purpose_check", inList(table.purpose, AUTH_TOKEN_PURPOSES)),
24
23
  foreignKey({
25
24
  columns: [table.orgId],
26
25
  foreignColumns: [organizations.id],
27
26
  name: "core_auth_tokens_org_id_fkey"
28
27
  }).onDelete("cascade")
29
28
  ]);
30
- export { AUTH_TOKEN_PURPOSES };
@@ -1,5 +1,6 @@
1
- import { pgTable } from "../table.js";
2
- import { CONTENT_TYPES, createdAt, updatedAt } from "./common.js";
1
+ import { AUTOMATION_STATUSES, CONTENT_TYPES, FINISHED_RUN_STATUSES, RUN_STATUSES, RUN_STEP_STATUSES } 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 { check, foreignKey, index, integer, jsonb, primaryKey, text, timestamp, uniqueIndex } from "drizzle-orm/pg-core";
5
6
  import { sql } from "drizzle-orm";
@@ -8,23 +9,6 @@ const timestamptz = () => timestamp({
8
9
  withTimezone: true,
9
10
  mode: "date"
10
11
  });
11
- const AUTOMATION_STATUSES = [
12
- "draft",
13
- "active",
14
- "paused"
15
- ];
16
- const RUN_STATUSES = [
17
- "running",
18
- "waiting",
19
- "succeeded",
20
- "failed",
21
- "cancelled"
22
- ];
23
- const RUN_STEP_STATUSES = [
24
- "succeeded",
25
- "failed",
26
- "skipped"
27
- ];
28
12
  /**
29
13
  * An automation: a trigger (event type + conditions) and steps, validated by
30
14
  * the contract's `AutomationDefinition`. `definition` is the working copy;
@@ -32,7 +16,7 @@ const RUN_STEP_STATUSES = [
32
16
  */
33
17
  const automations = pgTable("automations", {
34
18
  id: text().primaryKey(),
35
- orgId: text().notNull(),
19
+ orgId: orgIdColumn(),
36
20
  name: text().notNull(),
37
21
  description: text(),
38
22
  status: text({ enum: AUTOMATION_STATUSES }).default("draft").notNull(),
@@ -53,7 +37,7 @@ const automations = pgTable("automations", {
53
37
  index("core_automations_trigger_idx").on(table.orgId, table.triggerEvent).where(sql`${table.status} = 'active'`),
54
38
  index("core_automations_due_idx").on(table.nextRunAt).where(sql`${table.status} = 'active'`),
55
39
  uniqueIndex("core_automations_org_id_id_key").on(table.orgId, table.id),
56
- check("core_automations_status_check", sql`${table.status} IN ('draft', 'active', 'paused')`),
40
+ check("core_automations_status_check", inList(table.status, AUTOMATION_STATUSES)),
57
41
  foreignKey({
58
42
  columns: [table.orgId],
59
43
  foreignColumns: [organizations.id],
@@ -61,7 +45,7 @@ const automations = pgTable("automations", {
61
45
  }).onDelete("cascade")
62
46
  ]);
63
47
  pgTable("automation_versions", {
64
- orgId: text().notNull(),
48
+ orgId: orgIdColumn(),
65
49
  automationId: text().notNull(),
66
50
  version: integer().notNull(),
67
51
  definition: jsonb().$type().notNull(),
@@ -78,7 +62,7 @@ pgTable("automation_versions", {
78
62
  /** One trip of a customer or company (or none) through an automation. */
79
63
  const automationRuns = pgTable("automation_runs", {
80
64
  id: text().primaryKey(),
81
- orgId: text().notNull(),
65
+ orgId: orgIdColumn(),
82
66
  automationId: text().notNull(),
83
67
  version: integer().notNull(),
84
68
  eventId: text().notNull(),
@@ -106,9 +90,9 @@ const automationRuns = pgTable("automation_runs", {
106
90
  index("core_automation_runs_automation_idx").on(table.orgId, table.automationId, table.startedAt.desc()),
107
91
  index("core_automation_runs_subject_idx").on(table.automationId, table.subjectId, table.startedAt.desc()),
108
92
  index("core_automation_runs_waiting_idx").on(table.orgId, table.subjectId).where(sql`${table.status} = 'waiting'`),
109
- index("core_automation_runs_prunable_idx").on(table.finishedAt).where(sql`${table.status} IN ('succeeded', 'failed', 'cancelled') AND ${table.context} <> '{}'::jsonb`),
93
+ index("core_automation_runs_prunable_idx").on(table.finishedAt).where(sql`${inList(table.status, FINISHED_RUN_STATUSES)} AND ${table.context} <> '{}'::jsonb`),
110
94
  uniqueIndex("core_automation_runs_org_id_id_key").on(table.orgId, table.id),
111
- check("core_automation_runs_status_check", sql`${table.status} IN ('running', 'waiting', 'succeeded', 'failed', 'cancelled')`),
95
+ check("core_automation_runs_status_check", inList(table.status, RUN_STATUSES)),
112
96
  foreignKey({
113
97
  columns: [table.orgId, table.automationId],
114
98
  foreignColumns: [automations.orgId, automations.id],
@@ -118,7 +102,7 @@ const automationRuns = pgTable("automation_runs", {
118
102
  /** What each step of a run did: input, output or error. */
119
103
  const automationRunSteps = pgTable("automation_run_steps", {
120
104
  id: text().primaryKey(),
121
- orgId: text().notNull(),
105
+ orgId: orgIdColumn(),
122
106
  runId: text().notNull(),
123
107
  stepId: text().notNull(),
124
108
  type: text().notNull(),
@@ -131,11 +115,11 @@ const automationRunSteps = pgTable("automation_run_steps", {
131
115
  finishedAt: timestamptz().defaultNow().notNull()
132
116
  }, (table) => [
133
117
  index("core_automation_run_steps_run_idx").on(table.runId, table.finishedAt),
134
- check("core_automation_run_steps_status_check", sql`${table.status} IN ('succeeded', 'failed', 'skipped')`),
118
+ check("core_automation_run_steps_status_check", inList(table.status, RUN_STEP_STATUSES)),
135
119
  foreignKey({
136
120
  columns: [table.orgId, table.runId],
137
121
  foreignColumns: [automationRuns.orgId, automationRuns.id],
138
122
  name: "core_automation_run_steps_run_fkey"
139
123
  }).onDelete("cascade")
140
124
  ]);
141
- export { AUTOMATION_STATUSES, RUN_STATUSES, RUN_STEP_STATUSES, automationRunSteps, automationRuns, automations };
125
+ export { automationRunSteps, automationRuns, automations };
@@ -1,8 +1,4 @@
1
- export declare const CONDITION_OPERATORS: readonly ['eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'contains', 'not_contains', 'starts_with', 'in', 'not_in', 'exists', 'not_exists',
2
- /** Dates relative to now; the value is `{ amount, unit }` (days, weeks, months). */
3
- 'within_last', 'more_than_ago', 'within_next',
4
- /** The day and month fall within the next `amount` days (birthdays, anniversaries), whatever the year. */
5
- 'anniversary_within'];
1
+ import { CONDITION_MATCHES, CONDITION_OPERATORS } from "../enums.js";
6
2
  export type ConditionOperator = (typeof CONDITION_OPERATORS)[number];
7
3
  /** One rule of the condition language that view filters and automations store. */
8
4
  export interface ConditionRule {
@@ -13,6 +9,6 @@ export interface ConditionRule {
13
9
  }
14
10
  /** Rules (or nested groups) that must all, or any, hold. */
15
11
  export interface ConditionGroup {
16
- match: 'all' | 'any';
12
+ match: (typeof CONDITION_MATCHES)[number];
17
13
  rules: (ConditionRule | ConditionGroup)[];
18
14
  }
@@ -1,28 +1,5 @@
1
1
  import { timestamp } from "drizzle-orm/pg-core";
2
2
  // ../db/src/schema/common.ts
3
- /** Records that tags and custom fields attach to. */
4
- const CONTENT_TYPES = ["customer", "company"];
5
- /** Records custom fields attach to: contacts, and the shared product catalog. */
6
- const FIELD_CONTENT_TYPES = [...CONTENT_TYPES, "product"];
7
- const CONDITION_OPERATORS = [
8
- "eq",
9
- "neq",
10
- "gt",
11
- "gte",
12
- "lt",
13
- "lte",
14
- "contains",
15
- "not_contains",
16
- "starts_with",
17
- "in",
18
- "not_in",
19
- "exists",
20
- "not_exists",
21
- "within_last",
22
- "more_than_ago",
23
- "within_next",
24
- "anniversary_within"
25
- ];
26
3
  const createdAt = () => timestamp({
27
4
  withTimezone: true,
28
5
  mode: "date"
@@ -31,4 +8,4 @@ const updatedAt = () => timestamp({
31
8
  withTimezone: true,
32
9
  mode: "date"
33
10
  }).defaultNow().notNull();
34
- export { CONDITION_OPERATORS, CONTENT_TYPES, FIELD_CONTENT_TYPES, createdAt, updatedAt };
11
+ export { createdAt, updatedAt };