diff options
author | 2024-02-10 18:37:40 -0700 | |
---|---|---|
committer | 2024-02-13 06:59:44 +0530 | |
commit | e1c47572a6235fd8fd20af888ac3a11c7ae1369d (patch) | |
tree | 2dccff36a441916d7014037cef3f7ce84a790cad /app/Controllers/Http/Dashboard/TransferController.ts | |
parent | refactor: project maintenance (diff) | |
download | ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.tar.gz ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.tar.zst ferdium-server-e1c47572a6235fd8fd20af888ac3a11c7ae1369d.zip |
updates
Diffstat (limited to 'app/Controllers/Http/Dashboard/TransferController.ts')
-rw-r--r-- | app/Controllers/Http/Dashboard/TransferController.ts | 67 |
1 files changed, 35 insertions, 32 deletions
diff --git a/app/Controllers/Http/Dashboard/TransferController.ts b/app/Controllers/Http/Dashboard/TransferController.ts index 0296973..ab50bcf 100644 --- a/app/Controllers/Http/Dashboard/TransferController.ts +++ b/app/Controllers/Http/Dashboard/TransferController.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | import type { HttpContext } from '@adonisjs/core/http' | 1 | import type { HttpContext } from '@adonisjs/core/http'; |
2 | import { schema, validator } from '@adonisjs/validator' | 2 | import { schema, validator } from '@adonisjs/validator'; |
3 | import Service from '#app/Models/Service' | 3 | import Service from '#app/Models/Service'; |
4 | import Workspace from '#app/Models/Workspace' | 4 | import Workspace from '#app/Models/Workspace'; |
5 | import { v4 as uuidv4 } from 'uuid' | 5 | import { v4 as uuidv4 } from 'uuid'; |
6 | 6 | ||
7 | const importSchema = schema.create({ | 7 | const importSchema = schema.create({ |
8 | username: schema.string(), | 8 | username: schema.string(), |
@@ -10,52 +10,52 @@ const importSchema = schema.create({ | |||
10 | mail: schema.string(), | 10 | mail: schema.string(), |
11 | services: schema.array().anyMembers(), | 11 | services: schema.array().anyMembers(), |
12 | workspaces: schema.array().anyMembers(), | 12 | workspaces: schema.array().anyMembers(), |
13 | }) | 13 | }); |
14 | 14 | ||
15 | export default class TransferController { | 15 | export default class TransferController { |
16 | /** | 16 | /** |
17 | * Display the transfer page | 17 | * Display the transfer page |
18 | */ | 18 | */ |
19 | public async show({ view }: HttpContext) { | 19 | public async show({ view }: HttpContext) { |
20 | return view.render('dashboard/transfer') | 20 | return view.render('dashboard/transfer'); |
21 | } | 21 | } |
22 | 22 | ||
23 | public async import({ auth, request, response, session, view }: HttpContext) { | 23 | public async import({ auth, request, response, session, view }: HttpContext) { |
24 | let file | 24 | let file; |
25 | try { | 25 | try { |
26 | file = await validator.validate({ | 26 | file = await validator.validate({ |
27 | schema: importSchema, | 27 | schema: importSchema, |
28 | data: JSON.parse(request.body().file), | 28 | data: JSON.parse(request.body().file), |
29 | }) | 29 | }); |
30 | } catch { | 30 | } catch { |
31 | session.flash({ | 31 | session.flash({ |
32 | message: 'Invalid Ferdium account file', | 32 | message: 'Invalid Ferdium account file', |
33 | }) | 33 | }); |
34 | 34 | ||
35 | return response.redirect('/user/transfer') | 35 | return response.redirect('/user/transfer'); |
36 | } | 36 | } |
37 | 37 | ||
38 | if (!file?.services || !file.workspaces) { | 38 | if (!file?.services || !file.workspaces) { |
39 | session.flash({ | 39 | session.flash({ |
40 | type: 'danger', | 40 | type: 'danger', |
41 | message: 'Invalid Ferdium account file (2)', | 41 | message: 'Invalid Ferdium account file (2)', |
42 | }) | 42 | }); |
43 | return response.redirect('/user/transfer') | 43 | return response.redirect('/user/transfer'); |
44 | } | 44 | } |
45 | 45 | ||
46 | const serviceIdTranslation = {} | 46 | const serviceIdTranslation = {}; |
47 | 47 | ||
48 | // Import services | 48 | // Import services |
49 | try { | 49 | try { |
50 | for (const service of file.services) { | 50 | for (const service of file.services) { |
51 | // Get new, unused uuid | 51 | // Get new, unused uuid |
52 | let serviceId | 52 | let serviceId; |
53 | do { | 53 | do { |
54 | serviceId = uuidv4() | 54 | serviceId = uuidv4(); |
55 | } while ( | 55 | } while ( |
56 | // eslint-disable-next-line no-await-in-loop, unicorn/no-await-expression-member | 56 | // eslint-disable-next-line no-await-in-loop, unicorn/no-await-expression-member |
57 | (await Service.query().where('serviceId', serviceId)).length > 0 | 57 | (await Service.query().where('serviceId', serviceId)).length > 0 |
58 | ) | 58 | ); |
59 | 59 | ||
60 | // eslint-disable-next-line no-await-in-loop | 60 | // eslint-disable-next-line no-await-in-loop |
61 | await Service.create({ | 61 | await Service.create({ |
@@ -67,37 +67,38 @@ export default class TransferController { | |||
67 | typeof service.settings === 'string' | 67 | typeof service.settings === 'string' |
68 | ? service.settings | 68 | ? service.settings |
69 | : JSON.stringify(service.settings), | 69 | : JSON.stringify(service.settings), |
70 | }) | 70 | }); |
71 | 71 | ||
72 | // @ts-expect-error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' | 72 | // @ts-expect-error Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' |
73 | serviceIdTranslation[service.service_id || service.serviceId] = serviceId | 73 | serviceIdTranslation[service.service_id || service.serviceId] = |
74 | serviceId; | ||
74 | } | 75 | } |
75 | } catch (error) { | 76 | } catch (error) { |
76 | // eslint-disable-next-line no-console | 77 | // eslint-disable-next-line no-console |
77 | console.log(error) | 78 | console.log(error); |
78 | const errorMessage = `Could not import your services into our system.\nError: ${error}` | 79 | const errorMessage = `Could not import your services into our system.\nError: ${error}`; |
79 | return view.render('others/message', { | 80 | return view.render('others/message', { |
80 | heading: 'Error while importing', | 81 | heading: 'Error while importing', |
81 | text: errorMessage, | 82 | text: errorMessage, |
82 | }) | 83 | }); |
83 | } | 84 | } |
84 | 85 | ||
85 | // Import workspaces | 86 | // Import workspaces |
86 | try { | 87 | try { |
87 | for (const workspace of file.workspaces) { | 88 | for (const workspace of file.workspaces) { |
88 | let workspaceId | 89 | let workspaceId; |
89 | 90 | ||
90 | do { | 91 | do { |
91 | workspaceId = uuidv4() | 92 | workspaceId = uuidv4(); |
92 | } while ( | 93 | } while ( |
93 | // eslint-disable-next-line no-await-in-loop, unicorn/no-await-expression-member | 94 | // eslint-disable-next-line no-await-in-loop, unicorn/no-await-expression-member |
94 | (await Workspace.query().where('workspaceId', workspaceId)).length > 0 | 95 | (await Workspace.query().where('workspaceId', workspaceId)).length > 0 |
95 | ) | 96 | ); |
96 | 97 | ||
97 | const services = workspace.services.map( | 98 | const services = workspace.services.map( |
98 | // @ts-expect-error Parameter 'service' implicitly has an 'any' type. | 99 | // @ts-expect-error Parameter 'service' implicitly has an 'any' type. |
99 | (service) => serviceIdTranslation[service] | 100 | service => serviceIdTranslation[service], |
100 | ) | 101 | ); |
101 | 102 | ||
102 | // eslint-disable-next-line no-await-in-loop | 103 | // eslint-disable-next-line no-await-in-loop |
103 | await Workspace.create({ | 104 | await Workspace.create({ |
@@ -107,20 +108,22 @@ export default class TransferController { | |||
107 | order: workspace.order, | 108 | order: workspace.order, |
108 | services: JSON.stringify(services), | 109 | services: JSON.stringify(services), |
109 | data: | 110 | data: |
110 | typeof workspace.data === 'string' ? workspace.data : JSON.stringify(workspace.data), | 111 | typeof workspace.data === 'string' |
111 | }) | 112 | ? workspace.data |
113 | : JSON.stringify(workspace.data), | ||
114 | }); | ||
112 | } | 115 | } |
113 | } catch (error) { | 116 | } catch (error) { |
114 | const errorMessage = `Could not import your workspaces into our system.\nError: ${error}` | 117 | const errorMessage = `Could not import your workspaces into our system.\nError: ${error}`; |
115 | return view.render('others/message', { | 118 | return view.render('others/message', { |
116 | heading: 'Error while importing', | 119 | heading: 'Error while importing', |
117 | text: errorMessage, | 120 | text: errorMessage, |
118 | }) | 121 | }); |
119 | } | 122 | } |
120 | 123 | ||
121 | return view.render('others/message', { | 124 | return view.render('others/message', { |
122 | heading: 'Successfully imported', | 125 | heading: 'Successfully imported', |
123 | text: 'Your account has been imported, you can now login as usual!', | 126 | text: 'Your account has been imported, you can now login as usual!', |
124 | }) | 127 | }); |
125 | } | 128 | } |
126 | } | 129 | } |