diff options
author | 2021-09-06 13:05:15 +0530 | |
---|---|---|
committer | 2021-09-06 13:05:15 +0530 | |
commit | c938548dc54a890ab9190f4642c281f72ef9ce29 (patch) | |
tree | 65c3d02553d9a3992adea0e2eac198295e3ede9b /src/internal-server/app/Controllers/Http/UserController.js | |
parent | docs: add Tsakatac as a contributor for bug (#1873) (diff) | |
download | ferdium-app-c938548dc54a890ab9190f4642c281f72ef9ce29.tar.gz ferdium-app-c938548dc54a890ab9190f4642c281f72ef9ce29.tar.zst ferdium-app-c938548dc54a890ab9190f4642c281f72ef9ce29.zip |
fix: Import previously exported `ferdi.data` file (with workspaces) threw error (#1874)
Diffstat (limited to 'src/internal-server/app/Controllers/Http/UserController.js')
-rw-r--r-- | src/internal-server/app/Controllers/Http/UserController.js | 144 |
1 files changed, 66 insertions, 78 deletions
diff --git a/src/internal-server/app/Controllers/Http/UserController.js b/src/internal-server/app/Controllers/Http/UserController.js index 57aea6fc1..994dcc0dc 100644 --- a/src/internal-server/app/Controllers/Http/UserController.js +++ b/src/internal-server/app/Controllers/Http/UserController.js | |||
@@ -30,6 +30,19 @@ const apiRequest = (url, route, method, auth) => new Promise((resolve, reject) = | |||
30 | 30 | ||
31 | const LOGIN_SUCCESS_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJGZXJkaSBJbnRlcm5hbCBTZXJ2ZXIiLCJpYXQiOjE1NzEwNDAyMTUsImV4cCI6MjUzMzk1NDE3ODQ0LCJhdWQiOiJnZXRmZXJkaS5jb20iLCJzdWIiOiJmZXJkaUBsb2NhbGhvc3QiLCJ1c2VySWQiOiIxIn0.9_TWFGp6HROv8Yg82Rt6i1-95jqWym40a-HmgrdMC6M'; | 31 | const LOGIN_SUCCESS_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJGZXJkaSBJbnRlcm5hbCBTZXJ2ZXIiLCJpYXQiOjE1NzEwNDAyMTUsImV4cCI6MjUzMzk1NDE3ODQ0LCJhdWQiOiJnZXRmZXJkaS5jb20iLCJzdWIiOiJmZXJkaUBsb2NhbGhvc3QiLCJ1c2VySWQiOiIxIn0.9_TWFGp6HROv8Yg82Rt6i1-95jqWym40a-HmgrdMC6M'; |
32 | 32 | ||
33 | const DEFAULT_USER_DATA = { | ||
34 | accountType: 'individual', | ||
35 | beta: false, | ||
36 | email: '', | ||
37 | emailValidated: true, | ||
38 | features: {}, | ||
39 | firstname: 'Ferdi', | ||
40 | id: '82c1cf9d-ab58-4da2-b55e-aaa41d2142d8', | ||
41 | isSubscriptionOwner: true, | ||
42 | lastname: 'Application', | ||
43 | locale: DEFAULT_APP_SETTINGS.fallbackLocale, | ||
44 | }; | ||
45 | |||
33 | class UserController { | 46 | class UserController { |
34 | // Register a new user | 47 | // Register a new user |
35 | async signup({ | 48 | async signup({ |
@@ -83,16 +96,7 @@ class UserController { | |||
83 | const settings = typeof user.settings === 'string' ? JSON.parse(user.settings) : user.settings; | 96 | const settings = typeof user.settings === 'string' ? JSON.parse(user.settings) : user.settings; |
84 | 97 | ||
85 | return response.send({ | 98 | return response.send({ |
86 | accountType: 'individual', | 99 | ...DEFAULT_USER_DATA, |
87 | beta: false, | ||
88 | email: '', | ||
89 | emailValidated: true, | ||
90 | features: {}, | ||
91 | firstname: 'Ferdi', | ||
92 | id: '82c1cf9d-ab58-4da2-b55e-aaa41d2142d8', | ||
93 | isSubscriptionOwner: true, | ||
94 | lastname: 'Application', | ||
95 | locale: DEFAULT_APP_SETTINGS.fallbackLocale, | ||
96 | ...settings || {}, | 100 | ...settings || {}, |
97 | }); | 101 | }); |
98 | } | 102 | } |
@@ -118,16 +122,7 @@ class UserController { | |||
118 | 122 | ||
119 | return response.send({ | 123 | return response.send({ |
120 | data: { | 124 | data: { |
121 | accountType: 'individual', | 125 | ...DEFAULT_USER_DATA, |
122 | beta: false, | ||
123 | email: '', | ||
124 | emailValidated: true, | ||
125 | features: {}, | ||
126 | firstname: 'Ferdi', | ||
127 | id: '82c1cf9d-ab58-4da2-b55e-aaa41d2142d8', | ||
128 | isSubscriptionOwner: true, | ||
129 | lastname: 'Application', | ||
130 | locale: DEFAULT_APP_SETTINGS.fallbackLocale, | ||
131 | ...newSettings, | 126 | ...newSettings, |
132 | }, | 127 | }, |
133 | status: [ | 128 | status: [ |
@@ -216,20 +211,7 @@ class UserController { | |||
216 | const services = await apiRequest(server, 'me/services', 'GET', token); | 211 | const services = await apiRequest(server, 'me/services', 'GET', token); |
217 | 212 | ||
218 | for (const service of services) { | 213 | for (const service of services) { |
219 | // Get new, unused uuid | 214 | await this._createAndCacheService(service, serviceIdTranslation); // eslint-disable-line no-await-in-loop |
220 | let serviceId; | ||
221 | do { | ||
222 | serviceId = uuid(); | ||
223 | } while ((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | ||
224 | |||
225 | await Service.create({ // eslint-disable-line no-await-in-loop | ||
226 | serviceId, | ||
227 | name: service.name, | ||
228 | recipeId: service.recipeId, | ||
229 | settings: JSON.stringify(service), | ||
230 | }); | ||
231 | |||
232 | serviceIdTranslation[service.id] = serviceId; | ||
233 | } | 215 | } |
234 | } catch (e) { | 216 | } catch (e) { |
235 | const errorMessage = `Could not import your services into our system.\nError: ${e}`; | 217 | const errorMessage = `Could not import your services into our system.\nError: ${e}`; |
@@ -241,20 +223,7 @@ class UserController { | |||
241 | const workspaces = await apiRequest(server, 'workspace', 'GET', token); | 223 | const workspaces = await apiRequest(server, 'workspace', 'GET', token); |
242 | 224 | ||
243 | for (const workspace of workspaces) { | 225 | for (const workspace of workspaces) { |
244 | let workspaceId; | 226 | await this._createWorkspace(workspace, serviceIdTranslation); // eslint-disable-line no-await-in-loop |
245 | do { | ||
246 | workspaceId = uuid(); | ||
247 | } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | ||
248 | |||
249 | const services = workspace.services.map(service => serviceIdTranslation[service]); | ||
250 | |||
251 | await Workspace.create({ // eslint-disable-line no-await-in-loop | ||
252 | workspaceId, | ||
253 | name: workspace.name, | ||
254 | order: workspace.order, | ||
255 | services: JSON.stringify(services), | ||
256 | data: JSON.stringify({}), | ||
257 | }); | ||
258 | } | 227 | } |
259 | } catch (e) { | 228 | } catch (e) { |
260 | const errorMessage = `Could not import your workspaces into our system.\nError: ${e}`; | 229 | const errorMessage = `Could not import your workspaces into our system.\nError: ${e}`; |
@@ -313,20 +282,7 @@ class UserController { | |||
313 | // Import services | 282 | // Import services |
314 | try { | 283 | try { |
315 | for (const service of file.services) { | 284 | for (const service of file.services) { |
316 | // Get new, unused uuid | 285 | await this._createAndCacheService(service, serviceIdTranslation); // eslint-disable-line no-await-in-loop |
317 | let serviceId; | ||
318 | do { | ||
319 | serviceId = uuid(); | ||
320 | } while ((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | ||
321 | |||
322 | await Service.create({ // eslint-disable-line no-await-in-loop | ||
323 | serviceId, | ||
324 | name: service.name, | ||
325 | recipeId: service.recipeId, | ||
326 | settings: JSON.stringify(service.settings), | ||
327 | }); | ||
328 | |||
329 | serviceIdTranslation[service.id] = serviceId; | ||
330 | } | 286 | } |
331 | } catch (e) { | 287 | } catch (e) { |
332 | const errorMessage = `Could not import your services into our system.\nError: ${e}`; | 288 | const errorMessage = `Could not import your services into our system.\nError: ${e}`; |
@@ -336,22 +292,7 @@ class UserController { | |||
336 | // Import workspaces | 292 | // Import workspaces |
337 | try { | 293 | try { |
338 | for (const workspace of file.workspaces) { | 294 | for (const workspace of file.workspaces) { |
339 | let workspaceId; | 295 | await this._createWorkspace(workspace, serviceIdTranslation); // eslint-disable-line no-await-in-loop |
340 | do { | ||
341 | workspaceId = uuid(); | ||
342 | } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | ||
343 | |||
344 | const services = (workspace.services && typeof (workspace.services) === 'object') ? | ||
345 | workspace.services.map((service) => serviceIdTranslation[service]) : | ||
346 | []; | ||
347 | |||
348 | await Workspace.create({ // eslint-disable-line no-await-in-loop | ||
349 | workspaceId, | ||
350 | name: workspace.name, | ||
351 | order: workspace.order, | ||
352 | services: JSON.stringify(services), | ||
353 | data: JSON.stringify(workspace.data), | ||
354 | }); | ||
355 | } | 296 | } |
356 | } catch (e) { | 297 | } catch (e) { |
357 | const errorMessage = `Could not import your workspaces into our system.\nError: ${e}`; | 298 | const errorMessage = `Could not import your workspaces into our system.\nError: ${e}`; |
@@ -360,6 +301,53 @@ class UserController { | |||
360 | 301 | ||
361 | return response.send('Your account has been imported.'); | 302 | return response.send('Your account has been imported.'); |
362 | } | 303 | } |
304 | |||
305 | async _createWorkspace(workspace, serviceIdTranslation) { | ||
306 | let newWorkspaceId; | ||
307 | do { | ||
308 | newWorkspaceId = uuid(); | ||
309 | } while ((await Workspace.query().where('workspaceId', newWorkspaceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | ||
310 | |||
311 | if (workspace.services && typeof (workspace.services) === 'string' && workspace.services.length > 0) { | ||
312 | workspace.services = JSON.parse(workspace.services); | ||
313 | } | ||
314 | const services = (workspace.services && typeof (workspace.services) === 'object') ? | ||
315 | workspace.services.map(oldServiceId => serviceIdTranslation[oldServiceId]) : | ||
316 | []; | ||
317 | if (workspace.data && typeof (workspace.data) === 'string' && workspace.data.length > 0) { | ||
318 | workspace.data = JSON.parse(workspace.data); | ||
319 | } | ||
320 | |||
321 | await Workspace.create({ | ||
322 | workspaceId: newWorkspaceId, | ||
323 | name: workspace.name, | ||
324 | order: workspace.order, | ||
325 | services: JSON.stringify(services), | ||
326 | data: JSON.stringify(workspace.data || {}), | ||
327 | }); | ||
328 | } | ||
329 | |||
330 | async _createAndCacheService(service, serviceIdTranslation) { | ||
331 | // Get new, unused uuid | ||
332 | let newServiceId; | ||
333 | do { | ||
334 | newServiceId = uuid(); | ||
335 | } while ((await Service.query().where('serviceId', newServiceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | ||
336 | |||
337 | // store the old serviceId as the key for future lookup | ||
338 | serviceIdTranslation[service.serviceId] = newServiceId; | ||
339 | |||
340 | if (service.settings && typeof (service.settings) === 'string' && service.settings.length > 0) { | ||
341 | service.settings = JSON.parse(service.settings); | ||
342 | } | ||
343 | |||
344 | await Service.create({ | ||
345 | serviceId: newServiceId, | ||
346 | name: service.name, | ||
347 | recipeId: service.recipeId, | ||
348 | settings: JSON.stringify(service.settings || {}), | ||
349 | }); | ||
350 | } | ||
363 | } | 351 | } |
364 | 352 | ||
365 | module.exports = UserController; | 353 | module.exports = UserController; |