@budibase/worker 2.1.38 → 2.1.40-alpha.0

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 (208) hide show
  1. package/__mocks__/aws-sdk.ts +15 -0
  2. package/__mocks__/oauth.ts +57 -0
  3. package/coverage/clover.xml +2153 -0
  4. package/coverage/coverage-final.json +78 -0
  5. package/coverage/lcov-report/base.css +224 -0
  6. package/coverage/lcov-report/block-navigation.js +87 -0
  7. package/coverage/lcov-report/favicon.png +0 -0
  8. package/coverage/lcov-report/index.html +431 -0
  9. package/coverage/lcov-report/prettify.css +1 -0
  10. package/coverage/lcov-report/prettify.js +2 -0
  11. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  12. package/coverage/lcov-report/sorter.js +196 -0
  13. package/coverage/lcov-report/src/api/controllers/global/auth.ts.html +925 -0
  14. package/coverage/lcov-report/src/api/controllers/global/configs.ts.html +1405 -0
  15. package/coverage/lcov-report/src/api/controllers/global/email.ts.html +196 -0
  16. package/coverage/lcov-report/src/api/controllers/global/index.html +236 -0
  17. package/coverage/lcov-report/src/api/controllers/global/license.ts.html +187 -0
  18. package/coverage/lcov-report/src/api/controllers/global/roles.ts.html +283 -0
  19. package/coverage/lcov-report/src/api/controllers/global/self.ts.html +577 -0
  20. package/coverage/lcov-report/src/api/controllers/global/templates.ts.html +316 -0
  21. package/coverage/lcov-report/src/api/controllers/global/users.ts.html +796 -0
  22. package/coverage/lcov-report/src/api/controllers/global/workspaces.ts.html +244 -0
  23. package/coverage/lcov-report/src/api/controllers/system/accounts.ts.html +148 -0
  24. package/coverage/lcov-report/src/api/controllers/system/environment.ts.html +124 -0
  25. package/coverage/lcov-report/src/api/controllers/system/index.html +191 -0
  26. package/coverage/lcov-report/src/api/controllers/system/migrations.ts.html +124 -0
  27. package/coverage/lcov-report/src/api/controllers/system/restore.ts.html +124 -0
  28. package/coverage/lcov-report/src/api/controllers/system/status.ts.html +133 -0
  29. package/coverage/lcov-report/src/api/controllers/system/tenants.ts.html +154 -0
  30. package/coverage/lcov-report/src/api/index.html +116 -0
  31. package/coverage/lcov-report/src/api/index.ts.html +583 -0
  32. package/coverage/lcov-report/src/api/routes/global/auth.ts.html +349 -0
  33. package/coverage/lcov-report/src/api/routes/global/configs.ts.html +457 -0
  34. package/coverage/lcov-report/src/api/routes/global/email.ts.html +193 -0
  35. package/coverage/lcov-report/src/api/routes/global/index.html +236 -0
  36. package/coverage/lcov-report/src/api/routes/global/license.ts.html +124 -0
  37. package/coverage/lcov-report/src/api/routes/global/roles.ts.html +133 -0
  38. package/coverage/lcov-report/src/api/routes/global/self.ts.html +139 -0
  39. package/coverage/lcov-report/src/api/routes/global/templates.ts.html +196 -0
  40. package/coverage/lcov-report/src/api/routes/global/users.ts.html +409 -0
  41. package/coverage/lcov-report/src/api/routes/global/workspaces.ts.html +196 -0
  42. package/coverage/lcov-report/src/api/routes/index.html +116 -0
  43. package/coverage/lcov-report/src/api/routes/index.ts.html +196 -0
  44. package/coverage/lcov-report/src/api/routes/system/accounts.ts.html +142 -0
  45. package/coverage/lcov-report/src/api/routes/system/environment.ts.html +109 -0
  46. package/coverage/lcov-report/src/api/routes/system/index.html +191 -0
  47. package/coverage/lcov-report/src/api/routes/system/migrations.ts.html +142 -0
  48. package/coverage/lcov-report/src/api/routes/system/restore.ts.html +109 -0
  49. package/coverage/lcov-report/src/api/routes/system/status.ts.html +109 -0
  50. package/coverage/lcov-report/src/api/routes/system/tenants.ts.html +124 -0
  51. package/coverage/lcov-report/src/api/routes/validation/index.html +131 -0
  52. package/coverage/lcov-report/src/api/routes/validation/index.ts.html +88 -0
  53. package/coverage/lcov-report/src/api/routes/validation/users.ts.html +235 -0
  54. package/coverage/lcov-report/src/constants/index.html +116 -0
  55. package/coverage/lcov-report/src/constants/index.ts.html +613 -0
  56. package/coverage/lcov-report/src/constants/templates/index.html +116 -0
  57. package/coverage/lcov-report/src/constants/templates/index.ts.html +316 -0
  58. package/coverage/lcov-report/src/db/index.html +116 -0
  59. package/coverage/lcov-report/src/db/index.ts.html +115 -0
  60. package/coverage/lcov-report/src/environment.ts.html +373 -0
  61. package/coverage/lcov-report/src/index.html +131 -0
  62. package/coverage/lcov-report/src/index.ts.html +394 -0
  63. package/coverage/lcov-report/src/middleware/cloudRestricted.ts.html +139 -0
  64. package/coverage/lcov-report/src/middleware/index.html +116 -0
  65. package/coverage/lcov-report/src/migrations/functions/globalInfoSyncUsers.ts.html +145 -0
  66. package/coverage/lcov-report/src/migrations/functions/index.html +116 -0
  67. package/coverage/lcov-report/src/migrations/index.html +116 -0
  68. package/coverage/lcov-report/src/migrations/index.ts.html +271 -0
  69. package/coverage/lcov-report/src/sdk/accounts/accounts.ts.html +262 -0
  70. package/coverage/lcov-report/src/sdk/accounts/index.html +131 -0
  71. package/coverage/lcov-report/src/sdk/accounts/index.ts.html +88 -0
  72. package/coverage/lcov-report/src/sdk/index.html +116 -0
  73. package/coverage/lcov-report/src/sdk/index.ts.html +106 -0
  74. package/coverage/lcov-report/src/sdk/users/events.ts.html +577 -0
  75. package/coverage/lcov-report/src/sdk/users/index.html +146 -0
  76. package/coverage/lcov-report/src/sdk/users/index.ts.html +88 -0
  77. package/coverage/lcov-report/src/sdk/users/users.ts.html +1903 -0
  78. package/coverage/lcov-report/src/tests/TestConfiguration.ts.html +1051 -0
  79. package/coverage/lcov-report/src/tests/api/accounts.ts.html +160 -0
  80. package/coverage/lcov-report/src/tests/api/auth.ts.html +220 -0
  81. package/coverage/lcov-report/src/tests/api/base.ts.html +133 -0
  82. package/coverage/lcov-report/src/tests/api/configs.ts.html +226 -0
  83. package/coverage/lcov-report/src/tests/api/email.ts.html +148 -0
  84. package/coverage/lcov-report/src/tests/api/environment.ts.html +130 -0
  85. package/coverage/lcov-report/src/tests/api/index.html +296 -0
  86. package/coverage/lcov-report/src/tests/api/index.ts.html +205 -0
  87. package/coverage/lcov-report/src/tests/api/migrations.ts.html +151 -0
  88. package/coverage/lcov-report/src/tests/api/restore.ts.html +127 -0
  89. package/coverage/lcov-report/src/tests/api/self.ts.html +163 -0
  90. package/coverage/lcov-report/src/tests/api/status.ts.html +121 -0
  91. package/coverage/lcov-report/src/tests/api/tenants.ts.html +130 -0
  92. package/coverage/lcov-report/src/tests/api/users.ts.html +511 -0
  93. package/coverage/lcov-report/src/tests/controllers.ts.html +100 -0
  94. package/coverage/lcov-report/src/tests/index.html +146 -0
  95. package/coverage/lcov-report/src/tests/index.ts.html +139 -0
  96. package/coverage/lcov-report/src/tests/mocks/email.js.html +115 -0
  97. package/coverage/lcov-report/src/tests/mocks/index.html +131 -0
  98. package/coverage/lcov-report/src/tests/mocks/index.ts.html +106 -0
  99. package/coverage/lcov-report/src/tests/structures/configs.ts.html +313 -0
  100. package/coverage/lcov-report/src/tests/structures/groups.ts.html +118 -0
  101. package/coverage/lcov-report/src/tests/structures/index.html +161 -0
  102. package/coverage/lcov-report/src/tests/structures/index.ts.html +151 -0
  103. package/coverage/lcov-report/src/tests/structures/users.ts.html +196 -0
  104. package/coverage/lcov-report/src/utilities/appService.ts.html +181 -0
  105. package/coverage/lcov-report/src/utilities/email.ts.html +850 -0
  106. package/coverage/lcov-report/src/utilities/fileSystem.ts.html +100 -0
  107. package/coverage/lcov-report/src/utilities/index.html +206 -0
  108. package/coverage/lcov-report/src/utilities/index.ts.html +112 -0
  109. package/coverage/lcov-report/src/utilities/redis.ts.html +424 -0
  110. package/coverage/lcov-report/src/utilities/templates.ts.html +232 -0
  111. package/coverage/lcov-report/src/utilities/users.ts.html +133 -0
  112. package/coverage/lcov.info +3958 -0
  113. package/jest.config.ts +29 -0
  114. package/package.json +13 -23
  115. package/scripts/localdomain.js +33 -7
  116. package/src/api/controllers/global/auth.ts +51 -43
  117. package/src/api/controllers/global/{configs.js → configs.ts} +119 -114
  118. package/src/api/controllers/global/{email.js → email.ts} +6 -5
  119. package/src/api/controllers/global/roles.ts +66 -0
  120. package/src/api/controllers/global/self.ts +2 -2
  121. package/src/api/controllers/global/users.ts +4 -2
  122. package/src/api/controllers/global/{workspaces.js → workspaces.ts} +15 -18
  123. package/src/api/controllers/system/{environment.js → environment.ts} +3 -2
  124. package/src/api/controllers/system/restore.ts +1 -1
  125. package/src/api/controllers/system/{status.js → status.ts} +4 -3
  126. package/src/api/controllers/system/tenants.ts +8 -51
  127. package/src/api/index.ts +31 -8
  128. package/src/api/routes/global/{auth.js → auth.ts} +31 -48
  129. package/src/api/routes/global/{configs.js → configs.ts} +22 -23
  130. package/src/api/routes/global/{email.js → email.ts} +9 -10
  131. package/src/api/routes/global/license.ts +1 -1
  132. package/src/api/routes/global/roles.ts +16 -0
  133. package/src/api/routes/global/self.ts +2 -2
  134. package/src/api/routes/global/templates.ts +4 -4
  135. package/src/api/routes/global/tests/auth.spec.ts +134 -64
  136. package/src/api/routes/global/tests/configs.spec.ts +34 -35
  137. package/src/api/routes/global/tests/email.spec.ts +4 -3
  138. package/src/api/routes/global/tests/license.spec.ts +31 -0
  139. package/src/api/routes/global/tests/realEmail.spec.ts +2 -3
  140. package/src/api/routes/global/tests/roles.spec.ts +27 -0
  141. package/src/api/routes/global/tests/self.spec.ts +3 -4
  142. package/src/api/routes/global/tests/templates.spec.ts +35 -0
  143. package/src/api/routes/global/tests/users.spec.ts +51 -43
  144. package/src/api/routes/global/tests/workspaces.spec.ts +29 -0
  145. package/src/api/routes/global/{users.js → users.ts} +26 -24
  146. package/src/api/routes/global/{workspaces.js → workspaces.ts} +9 -10
  147. package/src/api/routes/index.ts +2 -1
  148. package/src/api/routes/system/accounts.ts +1 -1
  149. package/src/api/routes/system/environment.ts +8 -0
  150. package/src/api/routes/system/migrations.ts +1 -1
  151. package/src/api/routes/system/restore.ts +1 -1
  152. package/src/api/routes/system/status.ts +8 -0
  153. package/src/api/routes/system/tenants.ts +13 -0
  154. package/src/api/routes/system/tests/accounts.spec.ts +5 -6
  155. package/src/api/routes/system/tests/environment.spec.ts +29 -0
  156. package/src/api/routes/system/tests/migrations.spec.ts +63 -0
  157. package/src/api/routes/system/tests/restore.spec.ts +36 -0
  158. package/src/api/routes/system/tests/status.spec.ts +48 -0
  159. package/src/api/routes/system/tests/tenants.spec.ts +61 -0
  160. package/src/api/routes/validation/users.ts +3 -3
  161. package/src/constants/{index.js → index.ts} +57 -64
  162. package/src/constants/templates/{index.js → index.ts} +23 -19
  163. package/src/db/index.ts +10 -0
  164. package/src/environment.ts +8 -6
  165. package/src/index.ts +15 -16
  166. package/src/middleware/{cloudRestricted.js → cloudRestricted.ts} +5 -4
  167. package/src/middleware/tests/tenancy.spec.ts +73 -0
  168. package/src/tests/TestConfiguration.ts +99 -50
  169. package/src/tests/api/accounts.ts +5 -8
  170. package/src/tests/api/auth.ts +3 -6
  171. package/src/tests/api/base.ts +16 -0
  172. package/src/tests/api/configs.ts +15 -8
  173. package/src/tests/api/email.ts +3 -6
  174. package/src/tests/api/environment.ts +15 -0
  175. package/src/tests/api/index.ts +15 -0
  176. package/src/tests/api/migrations.ts +22 -0
  177. package/src/tests/api/restore.ts +14 -0
  178. package/src/tests/api/self.ts +11 -6
  179. package/src/tests/api/status.ts +12 -0
  180. package/src/tests/api/tenants.ts +15 -0
  181. package/src/tests/api/users.ts +38 -6
  182. package/src/tests/controllers.ts +5 -0
  183. package/src/tests/index.ts +5 -1
  184. package/{scripts/jestSetup.js → src/tests/jestSetup.ts} +12 -3
  185. package/src/tests/structures/{configs.js → configs.ts} +11 -11
  186. package/src/tests/structures/index.ts +5 -3
  187. package/src/tests/structures/users.ts +1 -0
  188. package/src/utilities/appService.ts +32 -0
  189. package/src/utilities/{email.js → email.ts} +78 -45
  190. package/src/utilities/fileSystem.ts +5 -0
  191. package/src/utilities/{index.js → index.ts} +1 -1
  192. package/src/utilities/{redis.js → redis.ts} +29 -24
  193. package/src/utilities/templates.ts +49 -0
  194. package/src/utilities/users.ts +16 -0
  195. package/tsconfig.build.json +2 -3
  196. package/tsconfig.json +3 -1
  197. package/src/api/controllers/global/roles.js +0 -68
  198. package/src/api/routes/global/roles.js +0 -12
  199. package/src/api/routes/system/environment.js +0 -8
  200. package/src/api/routes/system/status.js +0 -8
  201. package/src/api/routes/system/tenants.js +0 -12
  202. package/src/db/index.js +0 -11
  203. package/src/tests/controllers.js +0 -7
  204. package/src/tests/structures/accounts.ts +0 -24
  205. package/src/utilities/appService.js +0 -33
  206. package/src/utilities/fileSystem.js +0 -5
  207. package/src/utilities/templates.js +0 -49
  208. package/src/utilities/users.js +0 -17
@@ -1,28 +1,26 @@
1
- const {
2
- generateConfigID,
3
- getConfigParams,
4
- getScopedFullConfig,
5
- getAllApps,
6
- } = require("@budibase/backend-core/db")
7
- const { Configs } = require("../../../constants")
8
- const email = require("../../../utilities/email")
9
- const {
10
- upload,
11
- ObjectStoreBuckets,
12
- } = require("@budibase/backend-core/objectStore")
13
- const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
14
- const env = require("../../../environment")
15
- const { googleCallbackUrl, oidcCallbackUrl } = require("./auth")
16
- const {
17
- withCache,
18
- CacheKeys,
19
- bustCache,
1
+ import * as email from "../../../utilities/email"
2
+ import env from "../../../environment"
3
+ import { googleCallbackUrl, oidcCallbackUrl } from "./auth"
4
+ import {
5
+ events,
20
6
  cache,
21
- } = require("@budibase/backend-core/cache")
22
- const { events } = require("@budibase/backend-core")
23
- const { checkAnyUserExists } = require("../../../utilities/users")
24
-
25
- const getEventFns = async (db, config) => {
7
+ objectStore,
8
+ tenancy,
9
+ db as dbCore,
10
+ } from "@budibase/backend-core"
11
+ import { checkAnyUserExists } from "../../../utilities/users"
12
+ import {
13
+ Database,
14
+ Config as ConfigDoc,
15
+ ConfigType,
16
+ SSOType,
17
+ GoogleConfig,
18
+ OIDCConfig,
19
+ SettingsConfig,
20
+ BBContext,
21
+ } from "@budibase/types"
22
+
23
+ const getEventFns = async (db: Database, config: ConfigDoc) => {
26
24
  const fns = []
27
25
  const type = config.type
28
26
 
@@ -31,41 +29,45 @@ const getEventFns = async (db, config) => {
31
29
  existing = await db.get(config._id)
32
30
  }
33
31
 
32
+ const ssoType = type as SSOType
34
33
  if (!existing) {
35
34
  switch (config.type) {
36
- case Configs.SMTP: {
35
+ case ConfigType.SMTP: {
37
36
  fns.push(events.email.SMTPCreated)
38
37
  break
39
38
  }
40
- case Configs.GOOGLE: {
41
- fns.push(() => events.auth.SSOCreated(type))
42
- if (config.config.activated) {
43
- fns.push(() => events.auth.SSOActivated(type))
39
+ case ConfigType.GOOGLE: {
40
+ const googleCfg = config as GoogleConfig
41
+ fns.push(() => events.auth.SSOCreated(ssoType))
42
+ if (googleCfg.config.activated) {
43
+ fns.push(() => events.auth.SSOActivated(ssoType))
44
44
  }
45
45
  break
46
46
  }
47
- case Configs.OIDC: {
48
- fns.push(() => events.auth.SSOCreated(type))
49
- if (config.config.configs[0].activated) {
50
- fns.push(() => events.auth.SSOActivated(type))
47
+ case ConfigType.OIDC: {
48
+ const oidcCfg = config as OIDCConfig
49
+ fns.push(() => events.auth.SSOCreated(ssoType))
50
+ if (oidcCfg.config.configs[0].activated) {
51
+ fns.push(() => events.auth.SSOActivated(ssoType))
51
52
  }
52
53
  break
53
54
  }
54
- case Configs.SETTINGS: {
55
+ case ConfigType.SETTINGS: {
55
56
  // company
56
- const company = config.config.company
57
+ const settingsCfg = config as SettingsConfig
58
+ const company = settingsCfg.config.company
57
59
  if (company && company !== "Budibase") {
58
60
  fns.push(events.org.nameUpdated)
59
61
  }
60
62
 
61
63
  // logo
62
- const logoUrl = config.config.logoUrl
64
+ const logoUrl = settingsCfg.config.logoUrl
63
65
  if (logoUrl) {
64
66
  fns.push(events.org.logoUpdated)
65
67
  }
66
68
 
67
69
  // platform url
68
- const platformUrl = config.config.platformUrl
70
+ const platformUrl = settingsCfg.config.platformUrl
69
71
  if (
70
72
  platformUrl &&
71
73
  platformUrl !== "http://localhost:10000" &&
@@ -78,52 +80,55 @@ const getEventFns = async (db, config) => {
78
80
  }
79
81
  } else {
80
82
  switch (config.type) {
81
- case Configs.SMTP: {
83
+ case ConfigType.SMTP: {
82
84
  fns.push(events.email.SMTPUpdated)
83
85
  break
84
86
  }
85
- case Configs.GOOGLE: {
86
- fns.push(() => events.auth.SSOUpdated(type))
87
- if (!existing.config.activated && config.config.activated) {
88
- fns.push(() => events.auth.SSOActivated(type))
89
- } else if (existing.config.activated && !config.config.activated) {
90
- fns.push(() => events.auth.SSODeactivated(type))
87
+ case ConfigType.GOOGLE: {
88
+ const googleCfg = config as GoogleConfig
89
+ fns.push(() => events.auth.SSOUpdated(ssoType))
90
+ if (!existing.config.activated && googleCfg.config.activated) {
91
+ fns.push(() => events.auth.SSOActivated(ssoType))
92
+ } else if (existing.config.activated && !googleCfg.config.activated) {
93
+ fns.push(() => events.auth.SSODeactivated(ssoType))
91
94
  }
92
95
  break
93
96
  }
94
- case Configs.OIDC: {
95
- fns.push(() => events.auth.SSOUpdated(type))
97
+ case ConfigType.OIDC: {
98
+ const oidcCfg = config as OIDCConfig
99
+ fns.push(() => events.auth.SSOUpdated(ssoType))
96
100
  if (
97
101
  !existing.config.configs[0].activated &&
98
- config.config.configs[0].activated
102
+ oidcCfg.config.configs[0].activated
99
103
  ) {
100
- fns.push(() => events.auth.SSOActivated(type))
104
+ fns.push(() => events.auth.SSOActivated(ssoType))
101
105
  } else if (
102
106
  existing.config.configs[0].activated &&
103
- !config.config.configs[0].activated
107
+ !oidcCfg.config.configs[0].activated
104
108
  ) {
105
- fns.push(() => events.auth.SSODeactivated(type))
109
+ fns.push(() => events.auth.SSODeactivated(ssoType))
106
110
  }
107
111
  break
108
112
  }
109
- case Configs.SETTINGS: {
113
+ case ConfigType.SETTINGS: {
110
114
  // company
115
+ const settingsCfg = config as SettingsConfig
111
116
  const existingCompany = existing.config.company
112
- const company = config.config.company
117
+ const company = settingsCfg.config.company
113
118
  if (company && company !== "Budibase" && existingCompany !== company) {
114
119
  fns.push(events.org.nameUpdated)
115
120
  }
116
121
 
117
122
  // logo
118
123
  const existingLogoUrl = existing.config.logoUrl
119
- const logoUrl = config.config.logoUrl
124
+ const logoUrl = settingsCfg.config.logoUrl
120
125
  if (logoUrl && existingLogoUrl !== logoUrl) {
121
126
  fns.push(events.org.logoUpdated)
122
127
  }
123
128
 
124
129
  // platform url
125
130
  const existingPlatformUrl = existing.config.platformUrl
126
- const platformUrl = config.config.platformUrl
131
+ const platformUrl = settingsCfg.config.platformUrl
127
132
  if (
128
133
  platformUrl &&
129
134
  platformUrl !== "http://localhost:10000" &&
@@ -140,13 +145,13 @@ const getEventFns = async (db, config) => {
140
145
  return fns
141
146
  }
142
147
 
143
- exports.save = async function (ctx) {
144
- const db = getGlobalDB()
148
+ export async function save(ctx: BBContext) {
149
+ const db = tenancy.getGlobalDB()
145
150
  const { type, workspace, user, config } = ctx.request.body
146
151
  let eventFns = await getEventFns(db, ctx.request.body)
147
152
  // Config does not exist yet
148
153
  if (!ctx.request.body._id) {
149
- ctx.request.body._id = generateConfigID({
154
+ ctx.request.body._id = dbCore.generateConfigID({
150
155
  type,
151
156
  workspace,
152
157
  user,
@@ -155,18 +160,18 @@ exports.save = async function (ctx) {
155
160
  try {
156
161
  // verify the configuration
157
162
  switch (type) {
158
- case Configs.SMTP:
163
+ case ConfigType.SMTP:
159
164
  await email.verifyConfig(config)
160
165
  break
161
166
  }
162
- } catch (err) {
167
+ } catch (err: any) {
163
168
  ctx.throw(400, err)
164
169
  }
165
170
 
166
171
  try {
167
172
  const response = await db.put(ctx.request.body)
168
- await bustCache(CacheKeys.CHECKLIST)
169
- await bustCache(CacheKeys.ANALYTICS_ENABLED)
173
+ await cache.bustCache(cache.CacheKey.CHECKLIST)
174
+ await cache.bustCache(cache.CacheKey.ANALYTICS_ENABLED)
170
175
 
171
176
  for (const fn of eventFns) {
172
177
  await fn()
@@ -177,15 +182,15 @@ exports.save = async function (ctx) {
177
182
  _id: response.id,
178
183
  _rev: response.rev,
179
184
  }
180
- } catch (err) {
185
+ } catch (err: any) {
181
186
  ctx.throw(400, err)
182
187
  }
183
188
  }
184
189
 
185
- exports.fetch = async function (ctx) {
186
- const db = getGlobalDB()
190
+ export async function fetch(ctx: BBContext) {
191
+ const db = tenancy.getGlobalDB()
187
192
  const response = await db.allDocs(
188
- getConfigParams(
193
+ dbCore.getConfigParams(
189
194
  { type: ctx.params.type },
190
195
  {
191
196
  include_docs: true,
@@ -199,23 +204,23 @@ exports.fetch = async function (ctx) {
199
204
  * Gets the most granular config for a particular configuration type.
200
205
  * The hierarchy is type -> workspace -> user.
201
206
  */
202
- exports.find = async function (ctx) {
203
- const db = getGlobalDB()
207
+ export async function find(ctx: BBContext) {
208
+ const db = tenancy.getGlobalDB()
204
209
 
205
210
  const { userId, workspaceId } = ctx.query
206
211
  if (workspaceId && userId) {
207
- const workspace = await db.get(workspaceId)
212
+ const workspace = await db.get(workspaceId as string)
208
213
  const userInWorkspace = workspace.users.some(
209
- workspaceUser => workspaceUser === userId
214
+ (workspaceUser: any) => workspaceUser === userId
210
215
  )
211
- if (!ctx.user.admin && !userInWorkspace) {
216
+ if (!ctx.user!.admin && !userInWorkspace) {
212
217
  ctx.throw(400, `User is not in specified workspace: ${workspace}.`)
213
218
  }
214
219
  }
215
220
 
216
221
  try {
217
222
  // Find the config with the most granular scope based on context
218
- const scopedConfig = await getScopedFullConfig(db, {
223
+ const scopedConfig = await dbCore.getScopedFullConfig(db, {
219
224
  type: ctx.params.type,
220
225
  user: userId,
221
226
  workspace: workspaceId,
@@ -227,48 +232,48 @@ exports.find = async function (ctx) {
227
232
  // don't throw an error, there simply is nothing to return
228
233
  ctx.body = {}
229
234
  }
230
- } catch (err) {
231
- ctx.throw(err.status, err)
235
+ } catch (err: any) {
236
+ ctx.throw(err?.status || 400, err)
232
237
  }
233
238
  }
234
239
 
235
- exports.publicOidc = async function (ctx) {
236
- const db = getGlobalDB()
240
+ export async function publicOidc(ctx: BBContext) {
241
+ const db = tenancy.getGlobalDB()
237
242
  try {
238
243
  // Find the config with the most granular scope based on context
239
- const oidcConfig = await getScopedFullConfig(db, {
240
- type: Configs.OIDC,
244
+ const oidcConfig = await dbCore.getScopedFullConfig(db, {
245
+ type: ConfigType.OIDC,
241
246
  })
242
247
 
243
248
  if (!oidcConfig) {
244
249
  ctx.body = {}
245
250
  } else {
246
- ctx.body = oidcConfig.config.configs.map(config => ({
251
+ ctx.body = oidcConfig.config.configs.map((config: any) => ({
247
252
  logo: config.logo,
248
253
  name: config.name,
249
254
  uuid: config.uuid,
250
255
  }))
251
256
  }
252
- } catch (err) {
257
+ } catch (err: any) {
253
258
  ctx.throw(err.status, err)
254
259
  }
255
260
  }
256
261
 
257
- exports.publicSettings = async function (ctx) {
258
- const db = getGlobalDB()
262
+ export async function publicSettings(ctx: BBContext) {
263
+ const db = tenancy.getGlobalDB()
259
264
 
260
265
  try {
261
266
  // Find the config with the most granular scope based on context
262
- const publicConfig = await getScopedFullConfig(db, {
263
- type: Configs.SETTINGS,
267
+ const publicConfig = await dbCore.getScopedFullConfig(db, {
268
+ type: ConfigType.SETTINGS,
264
269
  })
265
270
 
266
- const googleConfig = await getScopedFullConfig(db, {
267
- type: Configs.GOOGLE,
271
+ const googleConfig = await dbCore.getScopedFullConfig(db, {
272
+ type: ConfigType.GOOGLE,
268
273
  })
269
274
 
270
- const oidcConfig = await getScopedFullConfig(db, {
271
- type: Configs.OIDC,
275
+ const oidcConfig = await dbCore.getScopedFullConfig(db, {
276
+ type: ConfigType.OIDC,
272
277
  })
273
278
 
274
279
  let config
@@ -301,12 +306,12 @@ exports.publicSettings = async function (ctx) {
301
306
  }
302
307
 
303
308
  ctx.body = config
304
- } catch (err) {
309
+ } catch (err: any) {
305
310
  ctx.throw(err.status, err)
306
311
  }
307
312
  }
308
313
 
309
- exports.upload = async function (ctx) {
314
+ export async function upload(ctx: BBContext) {
310
315
  if (ctx.request.files == null || ctx.request.files.file.length > 1) {
311
316
  ctx.throw(400, "One file must be uploaded.")
312
317
  }
@@ -315,19 +320,19 @@ exports.upload = async function (ctx) {
315
320
 
316
321
  let bucket
317
322
  if (env.SELF_HOSTED) {
318
- bucket = ObjectStoreBuckets.GLOBAL
323
+ bucket = objectStore.ObjectStoreBuckets.GLOBAL
319
324
  } else {
320
- bucket = ObjectStoreBuckets.GLOBAL_CLOUD
325
+ bucket = objectStore.ObjectStoreBuckets.GLOBAL_CLOUD
321
326
  }
322
327
 
323
328
  let key
324
329
  if (env.MULTI_TENANCY) {
325
- key = `${getTenantId()}/${type}/${name}`
330
+ key = `${tenancy.getTenantId()}/${type}/${name}`
326
331
  } else {
327
332
  key = `${type}/${name}`
328
333
  }
329
334
 
330
- await upload({
335
+ await objectStore.upload({
331
336
  bucket,
332
337
  filename: key,
333
338
  path: file.path,
@@ -336,11 +341,11 @@ exports.upload = async function (ctx) {
336
341
 
337
342
  // add to configuration structure
338
343
  // TODO: right now this only does a global level
339
- const db = getGlobalDB()
340
- let cfgStructure = await getScopedFullConfig(db, { type })
344
+ const db = tenancy.getGlobalDB()
345
+ let cfgStructure = await dbCore.getScopedFullConfig(db, { type })
341
346
  if (!cfgStructure) {
342
347
  cfgStructure = {
343
- _id: generateConfigID({ type }),
348
+ _id: dbCore.generateConfigID({ type }),
344
349
  config: {},
345
350
  }
346
351
  }
@@ -361,49 +366,49 @@ exports.upload = async function (ctx) {
361
366
  }
362
367
  }
363
368
 
364
- exports.destroy = async function (ctx) {
365
- const db = getGlobalDB()
369
+ export async function destroy(ctx: BBContext) {
370
+ const db = tenancy.getGlobalDB()
366
371
  const { id, rev } = ctx.params
367
372
  try {
368
373
  await db.remove(id, rev)
369
- await cache.delete(CacheKeys.CHECKLIST)
374
+ await cache.destroy(cache.CacheKey.CHECKLIST)
370
375
  ctx.body = { message: "Config deleted successfully" }
371
- } catch (err) {
376
+ } catch (err: any) {
372
377
  ctx.throw(err.status, err)
373
378
  }
374
379
  }
375
380
 
376
- exports.configChecklist = async function (ctx) {
377
- const db = getGlobalDB()
378
- const tenantId = getTenantId()
381
+ export async function configChecklist(ctx: BBContext) {
382
+ const db = tenancy.getGlobalDB()
383
+ const tenantId = tenancy.getTenantId()
379
384
 
380
385
  try {
381
- ctx.body = await withCache(
382
- CacheKeys.CHECKLIST,
386
+ ctx.body = await cache.withCache(
387
+ cache.CacheKey.CHECKLIST,
383
388
  env.CHECKLIST_CACHE_TTL,
384
389
  async () => {
385
390
  let apps = []
386
391
  if (!env.MULTI_TENANCY || tenantId) {
387
392
  // Apps exist
388
- apps = await getAllApps({ idsOnly: true, efficient: true })
393
+ apps = await dbCore.getAllApps({ idsOnly: true, efficient: true })
389
394
  }
390
395
 
391
396
  // They have set up SMTP
392
- const smtpConfig = await getScopedFullConfig(db, {
393
- type: Configs.SMTP,
397
+ const smtpConfig = await dbCore.getScopedFullConfig(db, {
398
+ type: ConfigType.SMTP,
394
399
  })
395
400
 
396
401
  // They have set up Google Auth
397
- const googleConfig = await getScopedFullConfig(db, {
398
- type: Configs.GOOGLE,
402
+ const googleConfig = await dbCore.getScopedFullConfig(db, {
403
+ type: ConfigType.GOOGLE,
399
404
  })
400
405
 
401
406
  // They have set up OIDC
402
- const oidcConfig = await getScopedFullConfig(db, {
403
- type: Configs.OIDC,
407
+ const oidcConfig = await dbCore.getScopedFullConfig(db, {
408
+ type: ConfigType.OIDC,
404
409
  })
405
410
 
406
- // They have set up an global user
411
+ // They have set up a global user
407
412
  const userExists = await checkAnyUserExists()
408
413
  return {
409
414
  apps: {
@@ -429,7 +434,7 @@ exports.configChecklist = async function (ctx) {
429
434
  }
430
435
  }
431
436
  )
432
- } catch (err) {
437
+ } catch (err: any) {
433
438
  ctx.throw(err.status, err)
434
439
  }
435
440
  }
@@ -1,7 +1,8 @@
1
- const { sendEmail } = require("../../../utilities/email")
2
- const { getGlobalDB } = require("@budibase/backend-core/tenancy")
1
+ import { sendEmail as sendEmailFn } from "../../../utilities/email"
2
+ import { tenancy } from "@budibase/backend-core"
3
+ import { BBContext } from "@budibase/types"
3
4
 
4
- exports.sendEmail = async ctx => {
5
+ export async function sendEmail(ctx: BBContext) {
5
6
  let {
6
7
  workspaceId,
7
8
  email,
@@ -16,10 +17,10 @@ exports.sendEmail = async ctx => {
16
17
  } = ctx.request.body
17
18
  let user
18
19
  if (userId) {
19
- const db = getGlobalDB()
20
+ const db = tenancy.getGlobalDB()
20
21
  user = await db.get(userId)
21
22
  }
22
- const response = await sendEmail(email, purpose, {
23
+ const response = await sendEmailFn(email, purpose, {
23
24
  workspaceId,
24
25
  user,
25
26
  contents,
@@ -0,0 +1,66 @@
1
+ import {
2
+ db as dbCore,
3
+ roles,
4
+ context,
5
+ cache,
6
+ tenancy,
7
+ } from "@budibase/backend-core"
8
+ import { BBContext, App } from "@budibase/types"
9
+ import { allUsers } from "../../../sdk/users"
10
+
11
+ export async function fetch(ctx: BBContext) {
12
+ const tenantId = ctx.user!.tenantId
13
+ // always use the dev apps as they'll be most up to date (true)
14
+ const apps = (await dbCore.getAllApps({ tenantId, all: true })) as App[]
15
+ const promises = []
16
+ for (let app of apps) {
17
+ // use dev app IDs
18
+ promises.push(roles.getAllRoles(app.appId))
19
+ }
20
+ const roleList = await Promise.all(promises)
21
+ const response: any = {}
22
+ for (let app of apps) {
23
+ const deployedAppId = dbCore.getProdAppID(app.appId)
24
+ response[deployedAppId] = {
25
+ roles: roleList.shift(),
26
+ name: app.name,
27
+ version: app.version,
28
+ url: app.url,
29
+ }
30
+ }
31
+ ctx.body = response
32
+ }
33
+
34
+ export async function find(ctx: BBContext) {
35
+ const appId = ctx.params.appId
36
+ await context.doInAppContext(dbCore.getDevAppID(appId), async () => {
37
+ const db = context.getAppDB()
38
+ const app = await db.get(dbCore.DocumentType.APP_METADATA)
39
+ ctx.body = {
40
+ roles: await roles.getAllRoles(),
41
+ name: app.name,
42
+ version: app.version,
43
+ url: app.url,
44
+ }
45
+ })
46
+ }
47
+
48
+ export async function removeAppRole(ctx: BBContext) {
49
+ const { appId } = ctx.params
50
+ const db = tenancy.getGlobalDB()
51
+ const users = await allUsers()
52
+ const bulk = []
53
+ const cacheInvalidations = []
54
+ for (let user of users) {
55
+ if (user.roles[appId]) {
56
+ cacheInvalidations.push(cache.user.invalidateUser(user._id))
57
+ delete user.roles[appId]
58
+ bulk.push(user)
59
+ }
60
+ }
61
+ await db.bulkDocs(bulk)
62
+ await Promise.all(cacheInvalidations)
63
+ ctx.body = {
64
+ message: "App role removed from all users",
65
+ }
66
+ }
@@ -73,12 +73,12 @@ export async function fetchAPIKey(ctx: any) {
73
73
  }
74
74
 
75
75
  const checkCurrentApp = (ctx: any) => {
76
- const appCookie = getCookie(ctx, constants.Cookies.CurrentApp)
76
+ const appCookie = getCookie(ctx, constants.Cookie.CurrentApp)
77
77
  if (appCookie && !tenancy.isUserInAppTenant(appCookie.appId)) {
78
78
  // there is a currentapp cookie from another tenant
79
79
  // remove the cookie as this is incompatible with the builder
80
80
  // due to builder and admin permissions being removed
81
- clearCookie(ctx, constants.Cookies.CurrentApp)
81
+ clearCookie(ctx, constants.Cookie.CurrentApp)
82
82
  }
83
83
  }
84
84
 
@@ -5,6 +5,7 @@ import {
5
5
  BulkUserRequest,
6
6
  BulkUserResponse,
7
7
  CloudAccount,
8
+ CreateAdminUserRequest,
8
9
  InviteUserRequest,
9
10
  InviteUsersRequest,
10
11
  SearchUsersRequest,
@@ -67,7 +68,8 @@ const parseBooleanParam = (param: any) => {
67
68
  }
68
69
 
69
70
  export const adminUser = async (ctx: any) => {
70
- const { email, password, tenantId } = ctx.request.body
71
+ const { email, password, tenantId } = ctx.request
72
+ .body as CreateAdminUserRequest
71
73
  await tenancy.doInTenant(tenantId, async () => {
72
74
  // account portal sends a pre-hashed password - honour param to prevent double hashing
73
75
  const hashPassword = parseBooleanParam(ctx.request.query.hashPassword)
@@ -102,7 +104,7 @@ export const adminUser = async (ctx: any) => {
102
104
  try {
103
105
  // always bust checklist beforehand, if an error occurs but can proceed, don't get
104
106
  // stuck in a cycle
105
- await cache.bustCache(cache.CacheKeys.CHECKLIST)
107
+ await cache.bustCache(cache.CacheKey.CHECKLIST)
106
108
  const finalUser = await sdk.users.save(user, {
107
109
  hashPassword,
108
110
  requirePassword,
@@ -1,16 +1,13 @@
1
- const {
2
- getWorkspaceParams,
3
- generateWorkspaceID,
4
- } = require("@budibase/backend-core/db")
5
- const { getGlobalDB } = require("@budibase/backend-core/tenancy")
1
+ import { tenancy, db as dbCore } from "@budibase/backend-core"
2
+ import { BBContext } from "@budibase/types"
6
3
 
7
- exports.save = async function (ctx) {
8
- const db = getGlobalDB()
4
+ export async function save(ctx: BBContext) {
5
+ const db = tenancy.getGlobalDB()
9
6
  const workspaceDoc = ctx.request.body
10
7
 
11
8
  // workspace does not exist yet
12
9
  if (!workspaceDoc._id) {
13
- workspaceDoc._id = generateWorkspaceID()
10
+ workspaceDoc._id = dbCore.generateWorkspaceID()
14
11
  }
15
12
 
16
13
  try {
@@ -19,38 +16,38 @@ exports.save = async function (ctx) {
19
16
  _id: response.id,
20
17
  _rev: response.rev,
21
18
  }
22
- } catch (err) {
19
+ } catch (err: any) {
23
20
  ctx.throw(err.status, err)
24
21
  }
25
22
  }
26
23
 
27
- exports.fetch = async function (ctx) {
28
- const db = getGlobalDB()
24
+ export async function fetch(ctx: BBContext) {
25
+ const db = tenancy.getGlobalDB()
29
26
  const response = await db.allDocs(
30
- getWorkspaceParams(undefined, {
27
+ dbCore.getWorkspaceParams(undefined, {
31
28
  include_docs: true,
32
29
  })
33
30
  )
34
31
  ctx.body = response.rows.map(row => row.doc)
35
32
  }
36
33
 
37
- exports.find = async function (ctx) {
38
- const db = getGlobalDB()
34
+ export async function find(ctx: BBContext) {
35
+ const db = tenancy.getGlobalDB()
39
36
  try {
40
37
  ctx.body = await db.get(ctx.params.id)
41
- } catch (err) {
38
+ } catch (err: any) {
42
39
  ctx.throw(err.status, err)
43
40
  }
44
41
  }
45
42
 
46
- exports.destroy = async function (ctx) {
47
- const db = getGlobalDB()
43
+ export async function destroy(ctx: BBContext) {
44
+ const db = tenancy.getGlobalDB()
48
45
  const { id, rev } = ctx.params
49
46
 
50
47
  try {
51
48
  await db.remove(id, rev)
52
49
  ctx.body = { message: "Workspace deleted successfully" }
53
- } catch (err) {
50
+ } catch (err: any) {
54
51
  ctx.throw(err.status, err)
55
52
  }
56
53
  }
@@ -1,6 +1,7 @@
1
- const env = require("../../../environment")
1
+ import { BBContext } from "@budibase/types"
2
+ import env from "../../../environment"
2
3
 
3
- exports.fetch = async ctx => {
4
+ export const fetch = async (ctx: BBContext) => {
4
5
  ctx.body = {
5
6
  multiTenancy: !!env.MULTI_TENANCY,
6
7
  cloud: !env.SELF_HOSTED,