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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/dist/activities.d.ts +1394 -139
  2. package/dist/activities.js +96 -40
  3. package/dist/audit.d.ts +1092 -48
  4. package/dist/audit.js +37 -23
  5. package/dist/auth.d.ts +2867 -61
  6. package/dist/auth.js +55 -25
  7. package/dist/automationRuns.d.ts +12 -13
  8. package/dist/automationRuns.js +15 -30
  9. package/dist/automations.d.ts +2474 -93
  10. package/dist/automations.js +56 -44
  11. package/dist/client.d.ts +30704 -0
  12. package/dist/client.js +62 -0
  13. package/dist/common.d.ts +28 -16
  14. package/dist/common.js +30 -22
  15. package/dist/companies.d.ts +1154 -44
  16. package/dist/companies.js +33 -17
  17. package/dist/contract/routes.json.js +1810 -0
  18. package/dist/customerAuth.d.ts +1661 -0
  19. package/dist/customerAuth.js +197 -0
  20. package/dist/customers.d.ts +1283 -65
  21. package/dist/customers.js +35 -25
  22. package/dist/{automationDefinition.d.ts → db/src/automationDefinition.d.ts} +4 -3
  23. package/dist/{automationDefinition.js → db/src/automationDefinition.js} +10 -32
  24. package/dist/db/src/enums.d.ts +8 -0
  25. package/dist/db/src/enums.js +334 -0
  26. package/dist/db/src/schema/auditStreams.js +6 -14
  27. package/dist/db/src/schema/authTokens.js +4 -6
  28. package/dist/db/src/schema/automations.d.ts +1 -0
  29. package/dist/db/src/schema/automations.js +15 -31
  30. package/dist/db/src/schema/common.d.ts +2 -6
  31. package/dist/db/src/schema/common.js +1 -24
  32. package/dist/db/src/schema/companies.js +6 -5
  33. package/dist/db/src/schema/customerAuth.js +101 -0
  34. package/dist/db/src/schema/customers.js +6 -6
  35. package/dist/db/src/schema/events.js +12 -60
  36. package/dist/db/src/schema/fieldValues.js +2 -2
  37. package/dist/db/src/schema/fields.js +10 -28
  38. package/dist/db/src/schema/index.d.ts +3 -0
  39. package/dist/db/src/schema/index.js +22 -20
  40. package/dist/db/src/schema/leadStages.js +5 -10
  41. package/dist/db/src/schema/leads.js +6 -11
  42. package/dist/db/src/schema/notes.js +92 -0
  43. package/dist/db/src/schema/oauth.js +3 -3
  44. package/dist/db/src/schema/payments.js +13 -23
  45. package/dist/db/src/schema/platform.js +11 -31
  46. package/dist/db/src/schema/plugins.js +10 -5
  47. package/dist/db/src/schema/products.js +7 -8
  48. package/dist/db/src/schema/rateLimits.js +7 -0
  49. package/dist/db/src/schema/saas.js +3 -9
  50. package/dist/db/src/schema/tags.js +6 -5
  51. package/dist/db/src/schema/transfer.js +13 -33
  52. package/dist/db/src/schema/trash.js +6 -7
  53. package/dist/db/src/schema/users.js +6 -10
  54. package/dist/db/src/schema/views.js +5 -13
  55. package/dist/db/src/schema/webhooks.js +7 -14
  56. package/dist/db/src/table.js +16 -2
  57. package/dist/errors.d.ts +516 -0
  58. package/dist/errors.js +218 -0
  59. package/dist/events.d.ts +585 -140
  60. package/dist/events.js +57 -42
  61. package/dist/fields.d.ts +1478 -66
  62. package/dist/fields.js +33 -47
  63. package/dist/files.d.ts +766 -62
  64. package/dist/files.js +34 -22
  65. package/dist/index.d.ts +25707 -902
  66. package/dist/index.js +22 -20
  67. package/dist/leads.d.ts +1108 -22
  68. package/dist/leads.js +39 -20
  69. package/dist/notifications.d.ts +691 -94
  70. package/dist/notifications.js +45 -91
  71. package/dist/organizations.d.ts +263 -5
  72. package/dist/organizations.js +21 -2
  73. package/dist/payments.d.ts +1794 -120
  74. package/dist/payments.js +58 -60
  75. package/dist/platform.d.ts +12 -20
  76. package/dist/platform.js +8 -21
  77. package/dist/plugins.d.ts +808 -71
  78. package/dist/plugins.js +66 -35
  79. package/dist/products.d.ts +1411 -53
  80. package/dist/products.js +40 -23
  81. package/dist/roles.d.ts +560 -8
  82. package/dist/roles.js +25 -11
  83. package/dist/routes.d.ts +8 -0
  84. package/dist/routes.js +13 -0
  85. package/dist/saas.d.ts +164 -8
  86. package/dist/saas.js +14 -29
  87. package/dist/tags.d.ts +344 -8
  88. package/dist/tags.js +16 -8
  89. package/dist/transfer.d.ts +1361 -36
  90. package/dist/transfer.js +47 -49
  91. package/dist/trash.d.ts +299 -23
  92. package/dist/trash.js +27 -20
  93. package/dist/users.d.ts +1233 -59
  94. package/dist/users.js +48 -39
  95. package/dist/values.d.ts +150 -0
  96. package/dist/values.js +139 -0
  97. package/dist/views.d.ts +404 -13
  98. package/dist/views.js +18 -28
  99. package/dist/webhooks.d.ts +791 -29
  100. package/dist/webhooks.js +36 -21
  101. package/package.json +9 -2
  102. package/dist/db/src/schema/jobs.js +0 -44
@@ -0,0 +1,197 @@
1
+ import { customers } from "./db/src/schema/customers.js";
2
+ import { customerLogins, customerSessions } from "./db/src/schema/customerAuth.js";
3
+ import "./db/src/schema/index.js";
4
+ import { COMMON_ERRORS, ERRORS } from "./errors.js";
5
+ import { EmailAddress, Id, ImageAddress, PhoneNumber, isoDate, listOf } from "./common.js";
6
+ import { PluginName } from "./plugins.js";
7
+ import { oc } from "@orpc/contract";
8
+ import { z } from "zod";
9
+ import { createSelectSchema, createUpdateSchema } from "drizzle-zod";
10
+ // src/customerAuth.ts
11
+ /** An email address or phone, with more fields. */
12
+ const addressWith = (fields) => z.discriminatedUnion("kind", [z.object({
13
+ kind: z.literal("email"),
14
+ value: EmailAddress,
15
+ ...fields
16
+ }), z.object({
17
+ kind: z.literal("phone"),
18
+ value: PhoneNumber,
19
+ ...fields
20
+ })]);
21
+ /**
22
+ * The organisation's subdomain, for apps that reach core at an address without
23
+ * one (mobile apps); the subdomain in core's address wins.
24
+ */
25
+ const organization = z.string().trim().toLowerCase().max(63).optional();
26
+ const Code = z.string().trim().regex(/^\d{6}$/, "the 6-digit code");
27
+ /** What a signed-in customer sees and edits of themselves. */
28
+ const CustomerProfile = createSelectSchema(customers, {
29
+ id: Id,
30
+ createdAt: isoDate
31
+ }).pick({
32
+ id: true,
33
+ firstName: true,
34
+ lastName: true,
35
+ avatarUrl: true,
36
+ /** `YYYY-MM-DD` */
37
+ birthDate: true,
38
+ primaryEmail: true,
39
+ primaryPhone: true,
40
+ createdAt: true
41
+ });
42
+ const CustomerProfileFields = createUpdateSchema(customers, {
43
+ firstName: (name) => name.trim().max(100),
44
+ lastName: (name) => name.trim().max(100),
45
+ avatarUrl: () => ImageAddress,
46
+ birthDate: (date) => date.regex(/^\d{4}-\d{2}-\d{2}$/, "YYYY-MM-DD")
47
+ }).pick({
48
+ firstName: true,
49
+ lastName: true,
50
+ avatarUrl: true,
51
+ birthDate: true
52
+ });
53
+ /** An email address or phone the customer signs in with. */
54
+ const CustomerLogin = createSelectSchema(customerLogins, {
55
+ id: Id,
56
+ createdAt: isoDate,
57
+ lastUsedAt: isoDate
58
+ }).pick({
59
+ id: true,
60
+ kind: true,
61
+ value: true,
62
+ createdAt: true,
63
+ lastUsedAt: true
64
+ });
65
+ /** Where the customer is signed in: one row per sign-in to an app. */
66
+ const CustomerSession = createSelectSchema(customerSessions, {
67
+ id: Id,
68
+ createdAt: isoDate,
69
+ lastUsedAt: isoDate
70
+ }).pick({
71
+ id: true,
72
+ /** The plugin whose app it is. */
73
+ clientId: true,
74
+ userAgent: true,
75
+ createdAt: true,
76
+ lastUsedAt: true
77
+ }).extend({ current: z.boolean() });
78
+ const CodeSent = z.object({ expiresInSeconds: z.int() });
79
+ /** The plugin whose customer app calls: its name, with `customerApp` in its manifest. */
80
+ const clientId = PluginName;
81
+ const RefreshToken = z.string().max(200);
82
+ /** The app's tokens: a 15-minute access token and a refresh token that rotates on each use. */
83
+ const CustomerTokens = z.object({
84
+ /** `Authorization: Bearer` for core's `/customer/*` and every plugin's `/customer/*`. */
85
+ accessToken: z.string(),
86
+ /** Trade it at `/customer-auth/refresh`; it lasts 90 days from its last use. */
87
+ refreshToken: z.string(),
88
+ /** Seconds the access token works. */
89
+ expiresIn: z.int()
90
+ });
91
+ /**
92
+ * Customers sign in to a plugin's app with a one-time code (decision 42): the
93
+ * app's own form calls these. Public; browsers only from the app's origins.
94
+ */
95
+ const { INVALID_CODE, INVALID_CREDENTIALS, INVALID_TOKEN, LAST_LOGIN, NOTHING_TO_RETRY, ORGANIZATION_REQUIRED, ORIGIN_NOT_ALLOWED, SUBJECT_REQUIRED, TOO_MANY_ATTEMPTS, TOO_MANY_CODES, UNKNOWN_APP } = ERRORS;
96
+ /** What these procedures answer with besides the common errors. */
97
+ const base = oc.errors({
98
+ ...COMMON_ERRORS,
99
+ INVALID_CODE,
100
+ INVALID_CREDENTIALS,
101
+ INVALID_TOKEN,
102
+ LAST_LOGIN,
103
+ NOTHING_TO_RETRY,
104
+ ORGANIZATION_REQUIRED,
105
+ ORIGIN_NOT_ALLOWED,
106
+ SUBJECT_REQUIRED,
107
+ TOO_MANY_ATTEMPTS,
108
+ TOO_MANY_CODES,
109
+ UNKNOWN_APP
110
+ });
111
+ const customerAuthContract = {
112
+ sendCode: base.route({
113
+ method: "POST",
114
+ path: "/customer-auth/code",
115
+ successStatus: 202,
116
+ summary: "Send a sign-in code to an email address or phone; the same answer whether or not it has an account. 400 UNKNOWN_APP, 403 ORIGIN_NOT_ALLOWED, 429 TOO_MANY_CODES"
117
+ }).input(addressWith({
118
+ clientId,
119
+ organization
120
+ })).output(CodeSent),
121
+ signIn: base.route({
122
+ method: "POST",
123
+ path: "/customer-auth/sign-in",
124
+ summary: "Sign in (or up) with the code and get the app's tokens. 400 INVALID_CODE (wrong, used, expired or 5 tries), 400 UNKNOWN_APP, 403 ORIGIN_NOT_ALLOWED"
125
+ }).input(addressWith({
126
+ clientId,
127
+ organization,
128
+ code: Code
129
+ })).output(CustomerTokens.extend({
130
+ customer: CustomerProfile,
131
+ created: z.boolean()
132
+ })),
133
+ refresh: base.route({
134
+ method: "POST",
135
+ path: "/customer-auth/refresh",
136
+ summary: "New tokens for a refresh token; the old one stops working, and using it again ends the session. 401 INVALID_TOKEN, 403 ORIGIN_NOT_ALLOWED"
137
+ }).input(z.object({
138
+ clientId,
139
+ refreshToken: RefreshToken
140
+ })).output(CustomerTokens),
141
+ signOut: base.route({
142
+ method: "POST",
143
+ path: "/customer-auth/sign-out",
144
+ summary: "End the refresh token's session; the answer is ok either way"
145
+ }).input(z.object({ refreshToken: RefreshToken })).output(z.object({ ok: z.literal(true) }))
146
+ };
147
+ /** The signed-in customer (`Authorization: Bearer` with a customer access token). */
148
+ const customerAccountContract = {
149
+ me: base.route({
150
+ method: "GET",
151
+ path: "/customer/me",
152
+ summary: "The signed-in customer's profile"
153
+ }).output(CustomerProfile),
154
+ update: base.route({
155
+ method: "PATCH",
156
+ path: "/customer/me",
157
+ summary: "Change name, picture or birth date"
158
+ }).input(CustomerProfileFields).output(CustomerProfile),
159
+ logins: base.route({
160
+ method: "GET",
161
+ path: "/customer/me/logins",
162
+ summary: "Addresses I sign in with"
163
+ }).output(listOf(CustomerLogin)),
164
+ addLogin: base.route({
165
+ method: "POST",
166
+ path: "/customer/me/logins",
167
+ successStatus: 202,
168
+ summary: "Send a code to a new address to sign in with. 409 TAKEN when it is already a login, 429 TOO_MANY_CODES"
169
+ }).input(addressWith({})).output(CodeSent),
170
+ confirmLogin: base.route({
171
+ method: "POST",
172
+ path: "/customer/me/logins/confirm",
173
+ successStatus: 201,
174
+ summary: "Add the address with its code. 400 INVALID_CODE, 409 TAKEN"
175
+ }).input(addressWith({ code: Code })).output(CustomerLogin),
176
+ removeLogin: base.route({
177
+ method: "DELETE",
178
+ path: "/customer/me/logins/{id}",
179
+ summary: "Stop signing in with an address. 409 LAST_LOGIN"
180
+ }).input(z.object({ id: Id })).output(z.object({ ok: z.literal(true) })),
181
+ sessions: base.route({
182
+ method: "GET",
183
+ path: "/customer/me/sessions",
184
+ summary: "Where I am signed in"
185
+ }).output(listOf(CustomerSession)),
186
+ revokeSession: base.route({
187
+ method: "DELETE",
188
+ path: "/customer/me/sessions/{id}",
189
+ summary: "Sign out one app"
190
+ }).input(z.object({ id: Id })).output(z.object({ ok: z.literal(true) })),
191
+ signOutEverywhere: base.route({
192
+ method: "POST",
193
+ path: "/customer/me/sign-out-everywhere",
194
+ summary: "Sign out of every app (access tokens end within 15 minutes)"
195
+ }).output(z.object({ ok: z.literal(true) }))
196
+ };
197
+ export { CustomerLogin, CustomerProfile, CustomerSession, CustomerTokens, customerAccountContract, customerAuthContract };