diff options
Diffstat (limited to 'app/Controllers/Http/Dashboard/TransferController.ts')
-rw-r--r-- | app/Controllers/Http/Dashboard/TransferController.ts | 77 |
1 files changed, 34 insertions, 43 deletions
diff --git a/app/Controllers/Http/Dashboard/TransferController.ts b/app/Controllers/Http/Dashboard/TransferController.ts index b113e50..0296973 100644 --- a/app/Controllers/Http/Dashboard/TransferController.ts +++ b/app/Controllers/Http/Dashboard/TransferController.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; | 1 | import type { HttpContext } from '@adonisjs/core/http' |
2 | import { schema, validator } from '@ioc:Adonis/Core/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,58 +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 }: HttpContextContract) { | 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({ | 23 | public async import({ auth, request, response, session, view }: HttpContext) { |
24 | auth, | 24 | let file |
25 | request, | ||
26 | response, | ||
27 | session, | ||
28 | view, | ||
29 | }: HttpContextContract) { | ||
30 | let file; | ||
31 | try { | 25 | try { |
32 | file = await validator.validate({ | 26 | file = await validator.validate({ |
33 | schema: importSchema, | 27 | schema: importSchema, |
34 | data: JSON.parse(request.body().file), | 28 | data: JSON.parse(request.body().file), |
35 | }); | 29 | }) |
36 | } catch { | 30 | } catch { |
37 | session.flash({ | 31 | session.flash({ |
38 | message: 'Invalid Ferdium account file', | 32 | message: 'Invalid Ferdium account file', |
39 | }); | 33 | }) |
40 | 34 | ||
41 | return response.redirect('/user/transfer'); | 35 | return response.redirect('/user/transfer') |
42 | } | 36 | } |
43 | 37 | ||
44 | if (!file?.services || !file.workspaces) { | 38 | if (!file?.services || !file.workspaces) { |
45 | session.flash({ | 39 | session.flash({ |
46 | type: 'danger', | 40 | type: 'danger', |
47 | message: 'Invalid Ferdium account file (2)', | 41 | message: 'Invalid Ferdium account file (2)', |
48 | }); | 42 | }) |
49 | return response.redirect('/user/transfer'); | 43 | return response.redirect('/user/transfer') |
50 | } | 44 | } |
51 | 45 | ||
52 | const serviceIdTranslation = {}; | 46 | const serviceIdTranslation = {} |
53 | 47 | ||
54 | // Import services | 48 | // Import services |
55 | try { | 49 | try { |
56 | for (const service of file.services) { | 50 | for (const service of file.services) { |
57 | // Get new, unused uuid | 51 | // Get new, unused uuid |
58 | let serviceId; | 52 | let serviceId |
59 | do { | 53 | do { |
60 | serviceId = uuidv4(); | 54 | serviceId = uuidv4() |
61 | } while ( | 55 | } while ( |
62 | // 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 |
63 | (await Service.query().where('serviceId', serviceId)).length > 0 | 57 | (await Service.query().where('serviceId', serviceId)).length > 0 |
64 | ); | 58 | ) |
65 | 59 | ||
66 | // eslint-disable-next-line no-await-in-loop | 60 | // eslint-disable-next-line no-await-in-loop |
67 | await Service.create({ | 61 | await Service.create({ |
@@ -73,38 +67,37 @@ export default class TransferController { | |||
73 | typeof service.settings === 'string' | 67 | typeof service.settings === 'string' |
74 | ? service.settings | 68 | ? service.settings |
75 | : JSON.stringify(service.settings), | 69 | : JSON.stringify(service.settings), |
76 | }); | 70 | }) |
77 | 71 | ||
78 | // @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 '{}' |
79 | serviceIdTranslation[service.service_id || service.serviceId] = | 73 | serviceIdTranslation[service.service_id || service.serviceId] = serviceId |
80 | serviceId; | ||
81 | } | 74 | } |
82 | } catch (error) { | 75 | } catch (error) { |
83 | // eslint-disable-next-line no-console | 76 | // eslint-disable-next-line no-console |
84 | console.log(error); | 77 | console.log(error) |
85 | const errorMessage = `Could not import your services into our system.\nError: ${error}`; | 78 | const errorMessage = `Could not import your services into our system.\nError: ${error}` |
86 | return view.render('others/message', { | 79 | return view.render('others/message', { |
87 | heading: 'Error while importing', | 80 | heading: 'Error while importing', |
88 | text: errorMessage, | 81 | text: errorMessage, |
89 | }); | 82 | }) |
90 | } | 83 | } |
91 | 84 | ||
92 | // Import workspaces | 85 | // Import workspaces |
93 | try { | 86 | try { |
94 | for (const workspace of file.workspaces) { | 87 | for (const workspace of file.workspaces) { |
95 | let workspaceId; | 88 | let workspaceId |
96 | 89 | ||
97 | do { | 90 | do { |
98 | workspaceId = uuidv4(); | 91 | workspaceId = uuidv4() |
99 | } while ( | 92 | } while ( |
100 | // eslint-disable-next-line no-await-in-loop, unicorn/no-await-expression-member | 93 | // eslint-disable-next-line no-await-in-loop, unicorn/no-await-expression-member |
101 | (await Workspace.query().where('workspaceId', workspaceId)).length > 0 | 94 | (await Workspace.query().where('workspaceId', workspaceId)).length > 0 |
102 | ); | 95 | ) |
103 | 96 | ||
104 | const services = workspace.services.map( | 97 | const services = workspace.services.map( |
105 | // @ts-expect-error Parameter 'service' implicitly has an 'any' type. | 98 | // @ts-expect-error Parameter 'service' implicitly has an 'any' type. |
106 | service => serviceIdTranslation[service], | 99 | (service) => serviceIdTranslation[service] |
107 | ); | 100 | ) |
108 | 101 | ||
109 | // eslint-disable-next-line no-await-in-loop | 102 | // eslint-disable-next-line no-await-in-loop |
110 | await Workspace.create({ | 103 | await Workspace.create({ |
@@ -114,22 +107,20 @@ export default class TransferController { | |||
114 | order: workspace.order, | 107 | order: workspace.order, |
115 | services: JSON.stringify(services), | 108 | services: JSON.stringify(services), |
116 | data: | 109 | data: |
117 | typeof workspace.data === 'string' | 110 | typeof workspace.data === 'string' ? workspace.data : JSON.stringify(workspace.data), |
118 | ? workspace.data | 111 | }) |
119 | : JSON.stringify(workspace.data), | ||
120 | }); | ||
121 | } | 112 | } |
122 | } catch (error) { | 113 | } catch (error) { |
123 | const errorMessage = `Could not import your workspaces into our system.\nError: ${error}`; | 114 | const errorMessage = `Could not import your workspaces into our system.\nError: ${error}` |
124 | return view.render('others/message', { | 115 | return view.render('others/message', { |
125 | heading: 'Error while importing', | 116 | heading: 'Error while importing', |
126 | text: errorMessage, | 117 | text: errorMessage, |
127 | }); | 118 | }) |
128 | } | 119 | } |
129 | 120 | ||
130 | return view.render('others/message', { | 121 | return view.render('others/message', { |
131 | heading: 'Successfully imported', | 122 | heading: 'Successfully imported', |
132 | text: 'Your account has been imported, you can now login as usual!', | 123 | text: 'Your account has been imported, you can now login as usual!', |
133 | }); | 124 | }) |
134 | } | 125 | } |
135 | } | 126 | } |