@budibase/worker 3.37.4 → 3.38.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/worker",
3
3
  "email": "hi@budibase.com",
4
- "version": "3.37.4",
4
+ "version": "3.38.0",
5
5
  "description": "Budibase background service",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -80,5 +80,5 @@
80
80
  "supertest": "6.3.3",
81
81
  "timekeeper": "2.2.0"
82
82
  },
83
- "gitHead": "cde1ea48fb34a1f21307a047066170830f41f2f2"
83
+ "gitHead": "a16eac4b5fd97bb1f8a1e5b04b25ddbd17f1cf44"
84
84
  }
@@ -23,31 +23,30 @@ export async function unlockTenant(tenantId: string) {
23
23
  }
24
24
 
25
25
  export async function setActivation(tenantId: string, active: boolean) {
26
- const db = tenancy.getTenantDB(tenantId)
27
- const settingsConfig = await db.tryGet<SettingsConfig>(
28
- configs.generateConfigID(ConfigType.SETTINGS)
29
- )
30
- if (!settingsConfig?.config) {
31
- throw new Error(
32
- `Cannot set activation. Settings config not found for tenant ${tenantId}`
33
- )
34
- }
26
+ const { db, settingsConfig } = await getOrCreateSettingsConfig(tenantId)
35
27
  settingsConfig.config.active = active
36
28
  await db.put(settingsConfig)
37
29
  }
38
30
 
39
31
  async function setLock(tenantId: string, lockReason?: LockReason) {
32
+ const { db, settingsConfig } = await getOrCreateSettingsConfig(tenantId)
33
+ settingsConfig.config.lockedBy = lockReason
34
+ await db.put(settingsConfig)
35
+ }
36
+
37
+ async function getOrCreateSettingsConfig(tenantId: string) {
40
38
  const db = tenancy.getTenantDB(tenantId)
41
- const settingsConfig = await db.tryGet<SettingsConfig>(
39
+ const settingsConfig = (await db.tryGet<SettingsConfig>(
42
40
  configs.generateConfigID(ConfigType.SETTINGS)
43
- )
44
- if (!settingsConfig?.config) {
45
- throw new Error(
46
- `Cannot lock. Settings config not found for tenant ${tenantId}`
47
- )
41
+ )) || {
42
+ _id: configs.generateConfigID(ConfigType.SETTINGS),
43
+ type: ConfigType.SETTINGS,
44
+ config: {},
48
45
  }
49
- settingsConfig.config.lockedBy = lockReason
50
- await db.put(settingsConfig)
46
+
47
+ settingsConfig.config ||= {}
48
+
49
+ return { db, settingsConfig }
51
50
  }
52
51
 
53
52
  async function removeGlobalDB(tenantId: string) {
@@ -93,19 +93,23 @@ describe("tenants", () => {
93
93
  })
94
94
  })
95
95
 
96
- it("should throw error when settings config not found", async () => {
97
- const tenantId = structures.tenant.id()
96
+ it("should create settings config when not found", async () => {
98
97
  const lockReason = LockReason.FREE_TIER
99
98
 
100
99
  mockDb.tryGet.mockResolvedValue(null)
101
100
 
102
- await expect(lockTenant(tenantId, lockReason)).rejects.toThrow(
103
- `Cannot lock. Settings config not found for tenant ${tenantId}`
104
- )
101
+ await lockTenant(structures.tenant.id(), lockReason)
102
+
103
+ expect(mockDb.put).toHaveBeenCalledWith({
104
+ _id: "config_settings",
105
+ type: ConfigType.SETTINGS,
106
+ config: {
107
+ lockedBy: lockReason,
108
+ },
109
+ })
105
110
  })
106
111
 
107
- it("should throw error when settings config has no config property", async () => {
108
- const tenantId = structures.tenant.id()
112
+ it("should initialise config property when missing", async () => {
109
113
  const lockReason = LockReason.FREE_TIER
110
114
 
111
115
  const settingsConfig = {
@@ -115,9 +119,14 @@ describe("tenants", () => {
115
119
 
116
120
  mockDb.tryGet.mockResolvedValue(settingsConfig)
117
121
 
118
- await expect(lockTenant(tenantId, lockReason)).rejects.toThrow(
119
- `Cannot lock. Settings config not found for tenant ${tenantId}`
120
- )
122
+ await lockTenant(structures.tenant.id(), lockReason)
123
+
124
+ expect(mockDb.put).toHaveBeenCalledWith({
125
+ ...settingsConfig,
126
+ config: {
127
+ lockedBy: lockReason,
128
+ },
129
+ })
121
130
  })
122
131
  })
123
132
 
@@ -149,14 +158,18 @@ describe("tenants", () => {
149
158
  })
150
159
  })
151
160
 
152
- it("should throw error when settings config not found", async () => {
153
- const tenantId = structures.tenant.id()
154
-
161
+ it("should create settings config when not found", async () => {
155
162
  mockDb.tryGet.mockResolvedValue(null)
156
163
 
157
- await expect(unlockTenant(tenantId)).rejects.toThrow(
158
- `Cannot lock. Settings config not found for tenant ${tenantId}`
159
- )
164
+ await unlockTenant(structures.tenant.id())
165
+
166
+ expect(mockDb.put).toHaveBeenCalledWith({
167
+ _id: "config_settings",
168
+ type: ConfigType.SETTINGS,
169
+ config: {
170
+ lockedBy: undefined,
171
+ },
172
+ })
160
173
  })
161
174
  })
162
175
 
@@ -213,19 +226,21 @@ describe("tenants", () => {
213
226
  })
214
227
  })
215
228
 
216
- it("should throw error when settings config not found", async () => {
217
- const tenantId = structures.tenant.id()
218
-
229
+ it("should create settings config when not found", async () => {
219
230
  mockDb.tryGet.mockResolvedValue(null)
220
231
 
221
- await expect(setActivation(tenantId, true)).rejects.toThrow(
222
- `Cannot set activation. Settings config not found for tenant ${tenantId}`
223
- )
224
- })
232
+ await setActivation(structures.tenant.id(), true)
225
233
 
226
- it("should throw error when settings config has no config property", async () => {
227
- const tenantId = structures.tenant.id()
234
+ expect(mockDb.put).toHaveBeenCalledWith({
235
+ _id: "config_settings",
236
+ type: ConfigType.SETTINGS,
237
+ config: {
238
+ active: true,
239
+ },
240
+ })
241
+ })
228
242
 
243
+ it("should initialise config property when missing", async () => {
229
244
  const settingsConfig = {
230
245
  _id: "config_settings",
231
246
  type: ConfigType.SETTINGS,
@@ -233,9 +248,14 @@ describe("tenants", () => {
233
248
 
234
249
  mockDb.tryGet.mockResolvedValue(settingsConfig)
235
250
 
236
- await expect(setActivation(tenantId, true)).rejects.toThrow(
237
- `Cannot set activation. Settings config not found for tenant ${tenantId}`
238
- )
251
+ await setActivation(structures.tenant.id(), true)
252
+
253
+ expect(mockDb.put).toHaveBeenCalledWith({
254
+ ...settingsConfig,
255
+ config: {
256
+ active: true,
257
+ },
258
+ })
239
259
  })
240
260
  })
241
261
  })