@budibase/worker 2.1.32-alpha.8 → 2.1.33

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 (182) hide show
  1. package/package.json +23 -13
  2. package/{src/tests/jestSetup.ts → scripts/jestSetup.js} +3 -12
  3. package/scripts/localdomain.js +7 -33
  4. package/src/api/controllers/global/auth.ts +31 -35
  5. package/src/api/controllers/global/configs.js +17 -17
  6. package/src/api/controllers/global/self.ts +2 -2
  7. package/src/api/controllers/global/users.ts +1 -3
  8. package/src/api/controllers/system/{environment.ts → environment.js} +2 -3
  9. package/src/api/controllers/system/{status.ts → status.js} +3 -4
  10. package/src/api/controllers/system/tenants.ts +51 -8
  11. package/src/api/index.ts +6 -29
  12. package/src/api/routes/global/auth.js +39 -22
  13. package/src/api/routes/global/configs.js +9 -9
  14. package/src/api/routes/global/templates.ts +2 -2
  15. package/src/api/routes/global/tests/auth.spec.ts +64 -134
  16. package/src/api/routes/global/tests/configs.spec.ts +35 -34
  17. package/src/api/routes/global/tests/email.spec.ts +3 -4
  18. package/src/api/routes/global/tests/realEmail.spec.ts +3 -2
  19. package/src/api/routes/global/tests/self.spec.ts +4 -3
  20. package/src/api/routes/global/tests/users.spec.ts +43 -51
  21. package/src/api/routes/system/environment.js +8 -0
  22. package/src/api/routes/system/status.js +8 -0
  23. package/src/api/routes/system/tenants.js +12 -0
  24. package/src/api/routes/system/tests/accounts.spec.ts +6 -5
  25. package/src/constants/{index.ts → index.js} +64 -57
  26. package/src/constants/templates/index.js +2 -2
  27. package/src/db/index.js +11 -0
  28. package/src/index.ts +1 -1
  29. package/src/middleware/cloudRestricted.js +2 -2
  30. package/src/tests/TestConfiguration.ts +49 -98
  31. package/src/tests/api/accounts.ts +8 -5
  32. package/src/tests/api/auth.ts +6 -3
  33. package/src/tests/api/configs.ts +8 -15
  34. package/src/tests/api/email.ts +6 -3
  35. package/src/tests/api/index.ts +0 -15
  36. package/src/tests/api/self.ts +6 -11
  37. package/src/tests/api/users.ts +6 -38
  38. package/src/tests/index.ts +1 -5
  39. package/src/tests/structures/accounts.ts +24 -0
  40. package/src/tests/structures/configs.js +6 -6
  41. package/src/tests/structures/index.ts +2 -4
  42. package/src/tests/structures/users.ts +0 -1
  43. package/src/utilities/appService.js +3 -3
  44. package/src/utilities/{email.ts → email.js} +45 -78
  45. package/src/utilities/{redis.ts → redis.js} +24 -29
  46. package/src/utilities/templates.js +49 -0
  47. package/tsconfig.build.json +2 -2
  48. package/tsconfig.json +1 -3
  49. package/__mocks__/aws-sdk.ts +0 -15
  50. package/__mocks__/oauth.ts +0 -57
  51. package/coverage/clover.xml +0 -2160
  52. package/coverage/coverage-final.json +0 -78
  53. package/coverage/lcov-report/base.css +0 -224
  54. package/coverage/lcov-report/block-navigation.js +0 -87
  55. package/coverage/lcov-report/favicon.png +0 -0
  56. package/coverage/lcov-report/index.html +0 -431
  57. package/coverage/lcov-report/prettify.css +0 -1
  58. package/coverage/lcov-report/prettify.js +0 -2
  59. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  60. package/coverage/lcov-report/sorter.js +0 -196
  61. package/coverage/lcov-report/src/api/controllers/global/auth.ts.html +0 -913
  62. package/coverage/lcov-report/src/api/controllers/global/configs.js.html +0 -1390
  63. package/coverage/lcov-report/src/api/controllers/global/email.js.html +0 -193
  64. package/coverage/lcov-report/src/api/controllers/global/index.html +0 -236
  65. package/coverage/lcov-report/src/api/controllers/global/license.ts.html +0 -187
  66. package/coverage/lcov-report/src/api/controllers/global/roles.js.html +0 -289
  67. package/coverage/lcov-report/src/api/controllers/global/self.ts.html +0 -577
  68. package/coverage/lcov-report/src/api/controllers/global/templates.ts.html +0 -316
  69. package/coverage/lcov-report/src/api/controllers/global/users.ts.html +0 -796
  70. package/coverage/lcov-report/src/api/controllers/global/workspaces.js.html +0 -253
  71. package/coverage/lcov-report/src/api/controllers/system/accounts.ts.html +0 -148
  72. package/coverage/lcov-report/src/api/controllers/system/environment.ts.html +0 -124
  73. package/coverage/lcov-report/src/api/controllers/system/index.html +0 -191
  74. package/coverage/lcov-report/src/api/controllers/system/migrations.ts.html +0 -124
  75. package/coverage/lcov-report/src/api/controllers/system/restore.ts.html +0 -124
  76. package/coverage/lcov-report/src/api/controllers/system/status.ts.html +0 -133
  77. package/coverage/lcov-report/src/api/controllers/system/tenants.ts.html +0 -154
  78. package/coverage/lcov-report/src/api/index.html +0 -116
  79. package/coverage/lcov-report/src/api/index.ts.html +0 -583
  80. package/coverage/lcov-report/src/api/routes/global/auth.js.html +0 -349
  81. package/coverage/lcov-report/src/api/routes/global/configs.js.html +0 -460
  82. package/coverage/lcov-report/src/api/routes/global/email.js.html +0 -196
  83. package/coverage/lcov-report/src/api/routes/global/index.html +0 -236
  84. package/coverage/lcov-report/src/api/routes/global/license.ts.html +0 -124
  85. package/coverage/lcov-report/src/api/routes/global/roles.js.html +0 -121
  86. package/coverage/lcov-report/src/api/routes/global/self.ts.html +0 -139
  87. package/coverage/lcov-report/src/api/routes/global/templates.ts.html +0 -196
  88. package/coverage/lcov-report/src/api/routes/global/users.js.html +0 -403
  89. package/coverage/lcov-report/src/api/routes/global/workspaces.js.html +0 -199
  90. package/coverage/lcov-report/src/api/routes/index.html +0 -116
  91. package/coverage/lcov-report/src/api/routes/index.ts.html +0 -193
  92. package/coverage/lcov-report/src/api/routes/system/accounts.ts.html +0 -142
  93. package/coverage/lcov-report/src/api/routes/system/environment.ts.html +0 -109
  94. package/coverage/lcov-report/src/api/routes/system/index.html +0 -191
  95. package/coverage/lcov-report/src/api/routes/system/migrations.ts.html +0 -142
  96. package/coverage/lcov-report/src/api/routes/system/restore.ts.html +0 -109
  97. package/coverage/lcov-report/src/api/routes/system/status.ts.html +0 -109
  98. package/coverage/lcov-report/src/api/routes/system/tenants.ts.html +0 -124
  99. package/coverage/lcov-report/src/api/routes/validation/index.html +0 -131
  100. package/coverage/lcov-report/src/api/routes/validation/index.ts.html +0 -88
  101. package/coverage/lcov-report/src/api/routes/validation/users.ts.html +0 -235
  102. package/coverage/lcov-report/src/constants/index.html +0 -116
  103. package/coverage/lcov-report/src/constants/index.ts.html +0 -613
  104. package/coverage/lcov-report/src/constants/templates/index.html +0 -116
  105. package/coverage/lcov-report/src/constants/templates/index.js.html +0 -304
  106. package/coverage/lcov-report/src/db/index.html +0 -116
  107. package/coverage/lcov-report/src/db/index.ts.html +0 -115
  108. package/coverage/lcov-report/src/environment.ts.html +0 -364
  109. package/coverage/lcov-report/src/index.html +0 -131
  110. package/coverage/lcov-report/src/index.ts.html +0 -397
  111. package/coverage/lcov-report/src/middleware/cloudRestricted.js.html +0 -136
  112. package/coverage/lcov-report/src/middleware/index.html +0 -116
  113. package/coverage/lcov-report/src/migrations/functions/globalInfoSyncUsers.ts.html +0 -145
  114. package/coverage/lcov-report/src/migrations/functions/index.html +0 -116
  115. package/coverage/lcov-report/src/migrations/index.html +0 -116
  116. package/coverage/lcov-report/src/migrations/index.ts.html +0 -271
  117. package/coverage/lcov-report/src/sdk/accounts/accounts.ts.html +0 -262
  118. package/coverage/lcov-report/src/sdk/accounts/index.html +0 -131
  119. package/coverage/lcov-report/src/sdk/accounts/index.ts.html +0 -88
  120. package/coverage/lcov-report/src/sdk/index.html +0 -116
  121. package/coverage/lcov-report/src/sdk/index.ts.html +0 -106
  122. package/coverage/lcov-report/src/sdk/users/events.ts.html +0 -577
  123. package/coverage/lcov-report/src/sdk/users/index.html +0 -146
  124. package/coverage/lcov-report/src/sdk/users/index.ts.html +0 -88
  125. package/coverage/lcov-report/src/sdk/users/users.ts.html +0 -1903
  126. package/coverage/lcov-report/src/tests/TestConfiguration.ts.html +0 -1051
  127. package/coverage/lcov-report/src/tests/api/accounts.ts.html +0 -160
  128. package/coverage/lcov-report/src/tests/api/auth.ts.html +0 -220
  129. package/coverage/lcov-report/src/tests/api/base.ts.html +0 -133
  130. package/coverage/lcov-report/src/tests/api/configs.ts.html +0 -226
  131. package/coverage/lcov-report/src/tests/api/email.ts.html +0 -148
  132. package/coverage/lcov-report/src/tests/api/environment.ts.html +0 -130
  133. package/coverage/lcov-report/src/tests/api/index.html +0 -296
  134. package/coverage/lcov-report/src/tests/api/index.ts.html +0 -205
  135. package/coverage/lcov-report/src/tests/api/migrations.ts.html +0 -151
  136. package/coverage/lcov-report/src/tests/api/restore.ts.html +0 -127
  137. package/coverage/lcov-report/src/tests/api/self.ts.html +0 -163
  138. package/coverage/lcov-report/src/tests/api/status.ts.html +0 -121
  139. package/coverage/lcov-report/src/tests/api/tenants.ts.html +0 -130
  140. package/coverage/lcov-report/src/tests/api/users.ts.html +0 -511
  141. package/coverage/lcov-report/src/tests/controllers.js.html +0 -106
  142. package/coverage/lcov-report/src/tests/index.html +0 -146
  143. package/coverage/lcov-report/src/tests/index.ts.html +0 -139
  144. package/coverage/lcov-report/src/tests/mocks/email.js.html +0 -115
  145. package/coverage/lcov-report/src/tests/mocks/index.html +0 -131
  146. package/coverage/lcov-report/src/tests/mocks/index.ts.html +0 -106
  147. package/coverage/lcov-report/src/tests/structures/configs.js.html +0 -313
  148. package/coverage/lcov-report/src/tests/structures/groups.ts.html +0 -118
  149. package/coverage/lcov-report/src/tests/structures/index.html +0 -161
  150. package/coverage/lcov-report/src/tests/structures/index.ts.html +0 -151
  151. package/coverage/lcov-report/src/tests/structures/users.ts.html +0 -196
  152. package/coverage/lcov-report/src/utilities/appService.js.html +0 -184
  153. package/coverage/lcov-report/src/utilities/email.ts.html +0 -850
  154. package/coverage/lcov-report/src/utilities/fileSystem.js.html +0 -100
  155. package/coverage/lcov-report/src/utilities/index.html +0 -206
  156. package/coverage/lcov-report/src/utilities/index.js.html +0 -112
  157. package/coverage/lcov-report/src/utilities/redis.ts.html +0 -424
  158. package/coverage/lcov-report/src/utilities/templates.ts.html +0 -232
  159. package/coverage/lcov-report/src/utilities/users.js.html +0 -136
  160. package/coverage/lcov.info +0 -3986
  161. package/jest.config.ts +0 -29
  162. package/src/api/routes/global/tests/license.spec.ts +0 -31
  163. package/src/api/routes/global/tests/roles.spec.ts +0 -27
  164. package/src/api/routes/global/tests/templates.spec.ts +0 -35
  165. package/src/api/routes/global/tests/workspaces.spec.ts +0 -29
  166. package/src/api/routes/system/environment.ts +0 -8
  167. package/src/api/routes/system/status.ts +0 -8
  168. package/src/api/routes/system/tenants.ts +0 -13
  169. package/src/api/routes/system/tests/environment.spec.ts +0 -29
  170. package/src/api/routes/system/tests/migrations.spec.ts +0 -63
  171. package/src/api/routes/system/tests/restore.spec.ts +0 -36
  172. package/src/api/routes/system/tests/status.spec.ts +0 -48
  173. package/src/api/routes/system/tests/tenants.spec.ts +0 -61
  174. package/src/db/index.ts +0 -10
  175. package/src/middleware/tests/tenancy.spec.ts +0 -73
  176. package/src/tests/api/base.ts +0 -16
  177. package/src/tests/api/environment.ts +0 -15
  178. package/src/tests/api/migrations.ts +0 -22
  179. package/src/tests/api/restore.ts +0 -14
  180. package/src/tests/api/status.ts +0 -12
  181. package/src/tests/api/tenants.ts +0 -15
  182. package/src/utilities/templates.ts +0 -49
@@ -1,5 +1,5 @@
1
1
  const fetch = require("node-fetch")
2
- const { Header } = require("@budibase/backend-core/constants")
2
+ const { Headers } = require("@budibase/backend-core/constants")
3
3
  const { getTenantId, isTenantIdSet } = require("@budibase/backend-core/tenancy")
4
4
  const { checkSlashesInUrl } = require("../utilities")
5
5
  const env = require("../environment")
@@ -9,9 +9,9 @@ async function makeAppRequest(url, method, body) {
9
9
  return
10
10
  }
11
11
  const request = { headers: {} }
12
- request.headers[Header.API_KEY] = env.INTERNAL_API_KEY
12
+ request.headers[Headers.API_KEY] = env.INTERNAL_API_KEY
13
13
  if (isTenantIdSet()) {
14
- request.headers[Header.TENANT_ID] = getTenantId()
14
+ request.headers[Headers.TENANT_ID] = getTenantId()
15
15
  }
16
16
  if (body) {
17
17
  request.headers["Content-Type"] = "application/json"
@@ -1,33 +1,15 @@
1
- import env from "../environment"
2
- import { EmailTemplatePurpose, TemplateType, Config } from "../constants"
3
- import { getTemplateByPurpose } from "../constants/templates"
4
- import { getSettingsTemplateContext } from "./templates"
5
- import { processString } from "@budibase/string-templates"
6
- import { getResetPasswordCode, getInviteCode } from "./redis"
7
- import { User, Database } from "@budibase/types"
8
- import { tenancy, db as dbCore } from "@budibase/backend-core"
9
1
  const nodemailer = require("nodemailer")
10
-
11
- type SendEmailOpts = {
12
- // workspaceId If finer grain controls being used then this will lookup config for workspace.
13
- workspaceId?: string
14
- // user If sending to an existing user the object can be provided, this is used in the context.
15
- user: User
16
- // from If sending from an address that is not what is configured in the SMTP config.
17
- from?: string
18
- // contents If sending a custom email then can supply contents which will be added to it.
19
- contents?: string
20
- // subject A custom subject can be specified if the config one is not desired.
21
- subject?: string
22
- // info Pass in a structure of information to be stored alongside the invitation.
23
- info?: any
24
- cc?: boolean
25
- bcc?: boolean
26
- automation?: boolean
27
- }
2
+ const env = require("../environment")
3
+ const { getScopedConfig } = require("@budibase/backend-core/db")
4
+ const { EmailTemplatePurpose, TemplateTypes, Configs } = require("../constants")
5
+ const { getTemplateByPurpose } = require("../constants/templates")
6
+ const { getSettingsTemplateContext } = require("./templates")
7
+ const { processString } = require("@budibase/string-templates")
8
+ const { getResetPasswordCode, getInviteCode } = require("../utilities/redis")
9
+ const { getGlobalDB } = require("@budibase/backend-core/tenancy")
28
10
 
29
11
  const TEST_MODE = false
30
- const TYPE = TemplateType.EMAIL
12
+ const TYPE = TemplateTypes.EMAIL
31
13
 
32
14
  const FULL_EMAIL_PURPOSES = [
33
15
  EmailTemplatePurpose.INVITATION,
@@ -36,8 +18,8 @@ const FULL_EMAIL_PURPOSES = [
36
18
  EmailTemplatePurpose.CUSTOM,
37
19
  ]
38
20
 
39
- function createSMTPTransport(config: any) {
40
- let options: any
21
+ function createSMTPTransport(config) {
22
+ let options
41
23
  let secure = config.secure
42
24
  // default it if not specified
43
25
  if (secure == null) {
@@ -70,15 +52,10 @@ function createSMTPTransport(config: any) {
70
52
  return nodemailer.createTransport(options)
71
53
  }
72
54
 
73
- async function getLinkCode(
74
- purpose: EmailTemplatePurpose,
75
- email: string,
76
- user: User,
77
- info: any = null
78
- ) {
55
+ async function getLinkCode(purpose, email, user, info = null) {
79
56
  switch (purpose) {
80
57
  case EmailTemplatePurpose.PASSWORD_RECOVERY:
81
- return getResetPasswordCode(user._id!, info)
58
+ return getResetPasswordCode(user._id, info)
82
59
  case EmailTemplatePurpose.INVITATION:
83
60
  return getInviteCode(email, info)
84
61
  default:
@@ -95,12 +72,7 @@ async function getLinkCode(
95
72
  * @param {string|null} contents if using a custom template can supply contents for context.
96
73
  * @return {Promise<string>} returns the built email HTML if all provided parameters were valid.
97
74
  */
98
- async function buildEmail(
99
- purpose: EmailTemplatePurpose,
100
- email: string,
101
- context: any,
102
- { user, contents }: any = {}
103
- ) {
75
+ async function buildEmail(purpose, email, context, { user, contents } = {}) {
104
76
  // this isn't a full email
105
77
  if (FULL_EMAIL_PURPOSES.indexOf(purpose) === -1) {
106
78
  throw `Unable to build an email of type ${purpose}`
@@ -141,19 +113,15 @@ async function buildEmail(
141
113
  * @param {boolean|null} automation Whether or not the configuration is being fetched for an email automation.
142
114
  * @return {Promise<object|null>} returns the SMTP configuration if it exists
143
115
  */
144
- async function getSmtpConfiguration(
145
- db: Database,
146
- workspaceId?: string,
147
- automation?: boolean
148
- ) {
149
- const params: any = {
150
- type: Config.SMTP,
116
+ async function getSmtpConfiguration(db, workspaceId = null, automation) {
117
+ const params = {
118
+ type: Configs.SMTP,
151
119
  }
152
120
  if (workspaceId) {
153
121
  params.workspace = workspaceId
154
122
  }
155
123
 
156
- const customConfig = await dbCore.getScopedConfig(db, params)
124
+ const customConfig = await getScopedConfig(db, params)
157
125
 
158
126
  if (customConfig) {
159
127
  return customConfig
@@ -178,12 +146,12 @@ async function getSmtpConfiguration(
178
146
  * Checks if a SMTP config exists based on passed in parameters.
179
147
  * @return {Promise<boolean>} returns true if there is a configuration that can be used.
180
148
  */
181
- export async function isEmailConfigured(workspaceId?: string) {
149
+ exports.isEmailConfigured = async (workspaceId = null) => {
182
150
  // when "testing" or smtp fallback is enabled simply return true
183
151
  if (TEST_MODE || env.SMTP_FALLBACK_ENABLED) {
184
152
  return true
185
153
  }
186
- const db = tenancy.getGlobalDB()
154
+ const db = getGlobalDB()
187
155
  const config = await getSmtpConfiguration(db, workspaceId)
188
156
  return config != null
189
157
  }
@@ -193,49 +161,48 @@ export async function isEmailConfigured(workspaceId?: string) {
193
161
  * send an email using it.
194
162
  * @param {string} email The email address to send to.
195
163
  * @param {string} purpose The purpose of the email being sent (e.g. reset password).
196
- * @param {object} opts The options for sending the email.
164
+ * @param {string|undefined} workspaceId If finer grain controls being used then this will lookup config for workspace.
165
+ * @param {object|undefined} user If sending to an existing user the object can be provided, this is used in the context.
166
+ * @param {string|undefined} from If sending from an address that is not what is configured in the SMTP config.
167
+ * @param {string|undefined} contents If sending a custom email then can supply contents which will be added to it.
168
+ * @param {string|undefined} subject A custom subject can be specified if the config one is not desired.
169
+ * @param {object|undefined} info Pass in a structure of information to be stored alongside the invitation.
170
+ * @param {boolean|undefined} disableFallback Prevent email being sent from SMTP fallback to avoid spam.
197
171
  * @return {Promise<object>} returns details about the attempt to send email, e.g. if it is successful; based on
198
172
  * nodemailer response.
199
173
  */
200
- export async function sendEmail(
201
- email: string,
202
- purpose: EmailTemplatePurpose,
203
- opts: SendEmailOpts
204
- ) {
205
- const db = tenancy.getGlobalDB()
206
- let config =
207
- (await getSmtpConfiguration(db, opts?.workspaceId, opts?.automation)) || {}
174
+ exports.sendEmail = async (
175
+ email,
176
+ purpose,
177
+ { workspaceId, user, from, contents, subject, info, cc, bcc, automation } = {}
178
+ ) => {
179
+ const db = getGlobalDB()
180
+ let config = (await getSmtpConfiguration(db, workspaceId, automation)) || {}
208
181
  if (Object.keys(config).length === 0 && !TEST_MODE) {
209
182
  throw "Unable to find SMTP configuration."
210
183
  }
211
184
  const transport = createSMTPTransport(config)
212
185
  // if there is a link code needed this will retrieve it
213
- const code = await getLinkCode(purpose, email, opts.user, opts?.info)
214
- let context
215
- if (code) {
216
- context = await getSettingsTemplateContext(purpose, code)
217
- }
186
+ const code = await getLinkCode(purpose, email, user, info)
187
+ const context = await getSettingsTemplateContext(purpose, code)
218
188
 
219
- let message: any = {
220
- from: opts?.from || config.from,
189
+ let message = {
190
+ from: from || config.from,
221
191
  html: await buildEmail(purpose, email, context, {
222
- user: opts?.user,
223
- contents: opts?.contents,
192
+ user,
193
+ contents,
224
194
  }),
225
195
  }
226
196
 
227
197
  message = {
228
198
  ...message,
229
199
  to: email,
230
- cc: opts?.cc,
231
- bcc: opts?.bcc,
200
+ cc: cc,
201
+ bcc: bcc,
232
202
  }
233
203
 
234
- if (opts?.subject || config.subject) {
235
- message.subject = await processString(
236
- opts?.subject || config.subject,
237
- context
238
- )
204
+ if (subject || config.subject) {
205
+ message.subject = await processString(subject || config.subject, context)
239
206
  }
240
207
  const response = await transport.sendMail(message)
241
208
  if (TEST_MODE) {
@@ -249,7 +216,7 @@ export async function sendEmail(
249
216
  * @param {object} config an SMTP configuration - this is based on the nodemailer API.
250
217
  * @return {Promise<boolean>} returns true if the configuration is valid.
251
218
  */
252
- export async function verifyConfig(config: any) {
219
+ exports.verifyConfig = async config => {
253
220
  const transport = createSMTPTransport(config)
254
221
  await transport.verify()
255
222
  }
@@ -1,35 +1,36 @@
1
- import { redis, utils } from "@budibase/backend-core"
1
+ const { Client, utils } = require("@budibase/backend-core/redis")
2
+ const { newid } = require("@budibase/backend-core/utils")
2
3
 
3
- function getExpirySecondsForDB(db: string) {
4
+ function getExpirySecondsForDB(db) {
4
5
  switch (db) {
5
- case redis.utils.Databases.PW_RESETS:
6
+ case utils.Databases.PW_RESETS:
6
7
  // a hour
7
8
  return 3600
8
- case redis.utils.Databases.INVITATIONS:
9
+ case utils.Databases.INVITATIONS:
9
10
  // a day
10
11
  return 86400
11
12
  }
12
13
  }
13
14
 
14
- let pwResetClient: any, invitationClient: any
15
+ let pwResetClient, invitationClient
15
16
 
16
- function getClient(db: string) {
17
+ function getClient(db) {
17
18
  switch (db) {
18
- case redis.utils.Databases.PW_RESETS:
19
+ case utils.Databases.PW_RESETS:
19
20
  return pwResetClient
20
- case redis.utils.Databases.INVITATIONS:
21
+ case utils.Databases.INVITATIONS:
21
22
  return invitationClient
22
23
  }
23
24
  }
24
25
 
25
- async function writeACode(db: string, value: any) {
26
+ async function writeACode(db, value) {
26
27
  const client = await getClient(db)
27
- const code = utils.newid()
28
+ const code = newid()
28
29
  await client.store(code, value, getExpirySecondsForDB(db))
29
30
  return code
30
31
  }
31
32
 
32
- async function getACode(db: string, code: string, deleteCode = true) {
33
+ async function getACode(db, code, deleteCode = true) {
33
34
  const client = await getClient(db)
34
35
  const value = await client.get(code)
35
36
  if (!value) {
@@ -41,9 +42,9 @@ async function getACode(db: string, code: string, deleteCode = true) {
41
42
  return value
42
43
  }
43
44
 
44
- export async function init() {
45
- pwResetClient = new redis.Client(redis.utils.Databases.PW_RESETS)
46
- invitationClient = new redis.Client(redis.utils.Databases.INVITATIONS)
45
+ exports.init = async () => {
46
+ pwResetClient = new Client(utils.Databases.PW_RESETS)
47
+ invitationClient = new Client(utils.Databases.INVITATIONS)
47
48
  await pwResetClient.init()
48
49
  await invitationClient.init()
49
50
  }
@@ -51,7 +52,7 @@ export async function init() {
51
52
  /**
52
53
  * make sure redis connection is closed.
53
54
  */
54
- export async function shutdown() {
55
+ exports.shutdown = async () => {
55
56
  if (pwResetClient) await pwResetClient.finish()
56
57
  if (invitationClient) await invitationClient.finish()
57
58
  console.log("Redis shutdown")
@@ -64,8 +65,8 @@ export async function shutdown() {
64
65
  * @param {object} info Info about the user/the reset process.
65
66
  * @return {Promise<string>} returns the code that was stored to redis.
66
67
  */
67
- export async function getResetPasswordCode(userId: string, info: any) {
68
- return writeACode(redis.utils.Databases.PW_RESETS, { userId, info })
68
+ exports.getResetPasswordCode = async (userId, info) => {
69
+ return writeACode(utils.Databases.PW_RESETS, { userId, info })
69
70
  }
70
71
 
71
72
  /**
@@ -74,12 +75,9 @@ export async function getResetPasswordCode(userId: string, info: any) {
74
75
  * @param {boolean} deleteCode If the code is used/finished with this will delete it - defaults to true.
75
76
  * @return {Promise<string>} returns the user ID if it is found
76
77
  */
77
- export async function checkResetPasswordCode(
78
- resetCode: string,
79
- deleteCode = true
80
- ) {
78
+ exports.checkResetPasswordCode = async (resetCode, deleteCode = true) => {
81
79
  try {
82
- return getACode(redis.utils.Databases.PW_RESETS, resetCode, deleteCode)
80
+ return getACode(utils.Databases.PW_RESETS, resetCode, deleteCode)
83
81
  } catch (err) {
84
82
  throw "Provided information is not valid, cannot reset password - please try again."
85
83
  }
@@ -91,8 +89,8 @@ export async function checkResetPasswordCode(
91
89
  * @param {object|null} info Information to be carried along with the invitation.
92
90
  * @return {Promise<string>} returns the code that was stored to redis.
93
91
  */
94
- export async function getInviteCode(email: string, info: any) {
95
- return writeACode(redis.utils.Databases.INVITATIONS, { email, info })
92
+ exports.getInviteCode = async (email, info) => {
93
+ return writeACode(utils.Databases.INVITATIONS, { email, info })
96
94
  }
97
95
 
98
96
  /**
@@ -101,12 +99,9 @@ export async function getInviteCode(email: string, info: any) {
101
99
  * @param {boolean} deleteCode whether or not the code should be deleted after retrieval - defaults to true.
102
100
  * @return {Promise<object>} If the code is valid then an email address will be returned.
103
101
  */
104
- export async function checkInviteCode(
105
- inviteCode: string,
106
- deleteCode: boolean = true
107
- ) {
102
+ exports.checkInviteCode = async (inviteCode, deleteCode = true) => {
108
103
  try {
109
- return getACode(redis.utils.Databases.INVITATIONS, inviteCode, deleteCode)
104
+ return getACode(utils.Databases.INVITATIONS, inviteCode, deleteCode)
110
105
  } catch (err) {
111
106
  throw "Invitation is not valid or has expired, please request a new one."
112
107
  }
@@ -0,0 +1,49 @@
1
+ const { getScopedConfig } = require("@budibase/backend-core/db")
2
+ const {
3
+ Configs,
4
+ InternalTemplateBindings,
5
+ LOGO_URL,
6
+ EmailTemplatePurpose,
7
+ } = require("../constants")
8
+ const { checkSlashesInUrl } = require("./index")
9
+ const {
10
+ getGlobalDB,
11
+ addTenantToUrl,
12
+ } = require("@budibase/backend-core/tenancy")
13
+ const BASE_COMPANY = "Budibase"
14
+
15
+ exports.getSettingsTemplateContext = async (purpose, code = null) => {
16
+ const db = getGlobalDB()
17
+ // TODO: use more granular settings in the future if required
18
+ let settings = (await getScopedConfig(db, { type: Configs.SETTINGS })) || {}
19
+ const URL = settings.platformUrl
20
+ const context = {
21
+ [InternalTemplateBindings.LOGO_URL]:
22
+ checkSlashesInUrl(`${URL}/${settings.logoUrl}`) || LOGO_URL,
23
+ [InternalTemplateBindings.PLATFORM_URL]: URL,
24
+ [InternalTemplateBindings.COMPANY]: settings.company || BASE_COMPANY,
25
+ [InternalTemplateBindings.DOCS_URL]:
26
+ settings.docsUrl || "https://docs.budibase.com/",
27
+ [InternalTemplateBindings.LOGIN_URL]: checkSlashesInUrl(
28
+ addTenantToUrl(`${URL}/login`)
29
+ ),
30
+ [InternalTemplateBindings.CURRENT_DATE]: new Date().toISOString(),
31
+ [InternalTemplateBindings.CURRENT_YEAR]: new Date().getFullYear(),
32
+ }
33
+ // attach purpose specific context
34
+ switch (purpose) {
35
+ case EmailTemplatePurpose.PASSWORD_RECOVERY:
36
+ context[InternalTemplateBindings.RESET_CODE] = code
37
+ context[InternalTemplateBindings.RESET_URL] = checkSlashesInUrl(
38
+ addTenantToUrl(`${URL}/builder/auth/reset?code=${code}`)
39
+ )
40
+ break
41
+ case EmailTemplatePurpose.INVITATION:
42
+ context[InternalTemplateBindings.INVITE_CODE] = code
43
+ context[InternalTemplateBindings.INVITE_URL] = checkSlashesInUrl(
44
+ addTenantToUrl(`${URL}/builder/invite?code=${code}`)
45
+ )
46
+ break
47
+ }
48
+ return context
49
+ }
@@ -20,7 +20,7 @@
20
20
  "node_modules",
21
21
  "dist",
22
22
  "src/tests",
23
- "**/*.spec.js",
24
- "**/*.spec.ts"
23
+ "**/*.spec.ts",
24
+ "**/*.spec.js"
25
25
  ]
26
26
  }
package/tsconfig.json CHANGED
@@ -8,8 +8,7 @@
8
8
  "paths": {
9
9
  "@budibase/types": ["../types/src"],
10
10
  "@budibase/backend-core": ["../backend-core/src"],
11
- "@budibase/backend-core/*": ["../backend-core/*"],
12
- "@budibase/pro": ["../../../budibase-pro/packages/pro/src"]
11
+ "@budibase/backend-core/*": ["../backend-core/*"]
13
12
  }
14
13
  },
15
14
  "ts-node": {
@@ -18,7 +17,6 @@
18
17
  "references": [
19
18
  { "path": "../types" },
20
19
  { "path": "../backend-core" },
21
- { "path": "../../../budibase-pro/packages/pro" }
22
20
  ],
23
21
  "include": [
24
22
  "src/**/*",
@@ -1,15 +0,0 @@
1
- const mockS3 = {
2
- headBucket: jest.fn().mockReturnThis(),
3
- deleteObject: jest.fn().mockReturnThis(),
4
- deleteObjects: jest.fn().mockReturnThis(),
5
- createBucket: jest.fn().mockReturnThis(),
6
- listObjects: jest.fn().mockReturnThis(),
7
- promise: jest.fn().mockReturnThis(),
8
- catch: jest.fn(),
9
- }
10
-
11
- const AWS = {
12
- S3: jest.fn(() => mockS3),
13
- }
14
-
15
- export default AWS
@@ -1,57 +0,0 @@
1
- import * as jwt from "jsonwebtoken"
2
-
3
- const mockOAuth2 = {
4
- getOAuthAccessToken: (code: string, p: any, cb: any) => {
5
- const err = null
6
- const accessToken = "access_token"
7
- const refreshToken = "refresh_token"
8
-
9
- const exp = new Date()
10
- exp.setDate(exp.getDate() + 1)
11
-
12
- const iat = new Date()
13
- iat.setDate(iat.getDate() - 1)
14
-
15
- const claims = {
16
- iss: "test",
17
- sub: "sub",
18
- aud: "clientId",
19
- exp: exp.getTime() / 1000,
20
- iat: iat.getTime() / 1000,
21
- email: "oauth@example.com",
22
- }
23
-
24
- const idToken = jwt.sign(claims, "secret")
25
-
26
- const params = {
27
- id_token: idToken,
28
- }
29
- return cb(err, accessToken, refreshToken, params)
30
- },
31
- _request: (
32
- method: string,
33
- url: string,
34
- headers: any,
35
- postBody: any,
36
- accessToken: string,
37
- cb: any
38
- ) => {
39
- const err = null
40
- const body = {
41
- sub: "sub",
42
- user_id: "userId",
43
- name: "OAuth",
44
- family_name: "2",
45
- given_name: "OAuth",
46
- middle_name: "",
47
- }
48
- const res = {}
49
- return cb(err, JSON.stringify(body), res)
50
- },
51
- }
52
-
53
- const oauth = {
54
- OAuth2: jest.fn(() => mockOAuth2),
55
- }
56
-
57
- export = oauth