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