@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.
- package/dist/activities.d.ts +21 -14
- package/dist/activities.js +6 -10
- package/dist/audit.d.ts +13 -17
- package/dist/audit.js +9 -9
- package/dist/auth.d.ts +33 -35
- package/dist/auth.js +8 -7
- package/dist/automationDefinition.d.ts +4 -3
- package/dist/automationDefinition.js +9 -31
- package/dist/automationRuns.d.ts +6 -5
- package/dist/automationRuns.js +8 -22
- package/dist/automations.d.ts +35 -36
- package/dist/automations.js +15 -23
- package/dist/common.d.ts +29 -4
- package/dist/common.js +30 -10
- package/dist/companies.d.ts +34 -34
- package/dist/companies.js +6 -5
- package/dist/contract/routes.json.js +1794 -0
- package/dist/customerAuth.d.ts +222 -0
- package/dist/customerAuth.js +180 -0
- package/dist/customers.d.ts +51 -54
- package/dist/customers.js +7 -12
- package/dist/db/src/enums.d.ts +8 -0
- package/dist/db/src/enums.js +336 -0
- package/dist/db/src/schema/auditStreams.js +6 -14
- package/dist/db/src/schema/authTokens.js +4 -6
- package/dist/db/src/schema/automations.js +12 -28
- package/dist/db/src/schema/common.d.ts +2 -6
- package/dist/db/src/schema/common.js +1 -24
- package/dist/db/src/schema/companies.js +6 -5
- package/dist/db/src/schema/customerAuth.js +101 -0
- package/dist/db/src/schema/customers.js +6 -6
- package/dist/db/src/schema/events.js +12 -60
- package/dist/db/src/schema/fieldValues.js +2 -2
- package/dist/db/src/schema/fields.js +10 -28
- package/dist/db/src/schema/index.js +21 -20
- package/dist/db/src/schema/leadStages.js +5 -10
- package/dist/db/src/schema/leads.js +5 -10
- package/dist/db/src/schema/oauth.js +3 -3
- package/dist/db/src/schema/payments.js +12 -22
- package/dist/db/src/schema/platform.js +11 -26
- package/dist/db/src/schema/plugins.js +4 -4
- package/dist/db/src/schema/products.js +7 -8
- package/dist/db/src/schema/rateLimits.js +7 -0
- package/dist/db/src/schema/saas.js +3 -9
- package/dist/db/src/schema/tags.js +6 -5
- package/dist/db/src/schema/transfer.js +12 -32
- package/dist/db/src/schema/trash.js +6 -7
- package/dist/db/src/schema/users.js +6 -10
- package/dist/db/src/schema/views.js +5 -13
- package/dist/db/src/schema/webhooks.js +7 -14
- package/dist/db/src/table.js +16 -2
- package/dist/events.d.ts +19 -23
- package/dist/events.js +16 -18
- package/dist/fields.d.ts +22 -50
- package/dist/fields.js +6 -28
- package/dist/files.d.ts +10 -5
- package/dist/files.js +4 -3
- package/dist/index.d.ts +557 -321
- package/dist/index.js +17 -16
- package/dist/leads.d.ts +5 -3
- package/dist/leads.js +8 -7
- package/dist/notifications.d.ts +4 -48
- package/dist/notifications.js +7 -73
- package/dist/organizations.d.ts +3 -3
- package/dist/payments.d.ts +28 -58
- package/dist/payments.js +10 -34
- package/dist/platform.d.ts +1 -0
- package/dist/platform.js +2 -5
- package/dist/plugins.d.ts +10 -4
- package/dist/plugins.js +19 -9
- package/dist/products.d.ts +31 -36
- package/dist/products.js +8 -7
- package/dist/roles.d.ts +0 -3
- package/dist/roles.js +5 -5
- package/dist/routes.d.ts +8 -0
- package/dist/routes.js +13 -0
- package/dist/saas.d.ts +0 -6
- package/dist/saas.js +5 -27
- package/dist/tags.d.ts +4 -4
- package/dist/tags.js +3 -3
- package/dist/transfer.d.ts +62 -24
- package/dist/transfer.js +15 -36
- package/dist/trash.d.ts +23 -4
- package/dist/trash.js +5 -5
- package/dist/users.d.ts +27 -41
- package/dist/users.js +13 -26
- package/dist/values.d.ts +159 -0
- package/dist/values.js +144 -0
- package/dist/views.d.ts +8 -8
- package/dist/views.js +8 -21
- package/dist/webhooks.d.ts +26 -20
- package/dist/webhooks.js +18 -11
- package/package.json +4 -1
- package/dist/db/src/schema/jobs.js +0 -44
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
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 {
|
|
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:
|
|
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",
|
|
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:
|
|
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 {
|
|
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:
|
|
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",
|
|
105
|
-
check("core_customers_sex_check",
|
|
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 {
|
|
107
|
+
export { customers };
|
|
@@ -1,30 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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 {
|
|
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
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* (`
|
|
20
|
-
* automation runs and
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
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 {
|
|
2
|
-
import {
|
|
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:
|
|
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",
|
|
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:
|
|
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",
|
|
100
|
-
check("core_fields_type_check",
|
|
101
|
-
check("core_fields_validation_check", sql`${table.validation} IS NULL OR ${table.validation
|
|
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 {
|
|
85
|
+
export { fieldGroups, fields };
|
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createdAt, updatedAt } from "./common.js";
|
|
2
2
|
import { companies, customerCompanies } from "./companies.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
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 {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import
|
|
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 {
|
|
15
|
+
import { paymentGateways, paymentInvoices, paymentPluginSettings, paymentRefunds } from "./payments.js";
|
|
17
16
|
import { trash } from "./trash.js";
|
|
18
|
-
import {
|
|
19
|
-
import
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
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 {
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
|
|
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 {
|
|
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:
|
|
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",
|
|
30
|
+
check("core_lead_stages_kind_check", inList(table.kind, LEAD_STAGE_KINDS))
|
|
36
31
|
]);
|
|
37
|
-
export {
|
|
32
|
+
export { leadStages };
|
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
import {
|
|
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:
|
|
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",
|
|
52
|
+
check("core_lead_events_event_check", inList(table.event, LEAD_EVENTS))
|
|
58
53
|
]);
|
|
59
|
-
export {
|
|
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:
|
|
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:
|
|
30
|
+
orgId: orgIdColumn(),
|
|
31
31
|
clientId: text().notNull(),
|
|
32
32
|
userId: text().notNull(),
|
|
33
33
|
familyId: text().notNull(),
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
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:
|
|
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:
|
|
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",
|
|
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:
|
|
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",
|
|
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:
|
|
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",
|
|
124
|
-
check("core_payment_refunds_status_check",
|
|
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:
|
|
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 {
|
|
141
|
+
export { paymentGateways, paymentInvoices, paymentPluginSettings, paymentRefunds };
|