diff options
Diffstat (limited to 'app/Controllers/Http/Dashboard/ExportController.ts')
-rw-r--r-- | app/Controllers/Http/Dashboard/ExportController.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/app/Controllers/Http/Dashboard/ExportController.ts b/app/Controllers/Http/Dashboard/ExportController.ts new file mode 100644 index 0000000..7155eab --- /dev/null +++ b/app/Controllers/Http/Dashboard/ExportController.ts | |||
@@ -0,0 +1,56 @@ | |||
1 | import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; | ||
2 | |||
3 | // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
4 | function deepParseToJSON(obj: any): Record<string, unknown> { | ||
5 | if (typeof obj !== 'object' || obj === null) { | ||
6 | try { | ||
7 | // Try to parse the object as JSON | ||
8 | return JSON.parse(obj) as Record<string, unknown>; | ||
9 | } catch { | ||
10 | // If parsing fails, return the original value | ||
11 | return obj; | ||
12 | } | ||
13 | } | ||
14 | |||
15 | // If obj is an object, recursively parse its keys | ||
16 | if (Array.isArray(obj)) { | ||
17 | // If obj is an array, recursively parse each element | ||
18 | return obj.map(item => deepParseToJSON(item)) as unknown as Record< | ||
19 | string, | ||
20 | unknown | ||
21 | >; | ||
22 | } else { | ||
23 | // If obj is an object, recursively parse its keys | ||
24 | const parsedObj: Record<string, unknown> = {}; | ||
25 | for (const key in obj) { | ||
26 | if (obj.hasOwnProperty(key)) { | ||
27 | parsedObj[key] = deepParseToJSON(obj[key]); | ||
28 | } | ||
29 | } | ||
30 | return parsedObj; | ||
31 | } | ||
32 | } | ||
33 | |||
34 | export default class ExportController { | ||
35 | /** | ||
36 | * Display the export page | ||
37 | */ | ||
38 | public async show({ auth, response }: HttpContextContract) { | ||
39 | const user = auth.user!; | ||
40 | const services = await user.related('services').query(); | ||
41 | const workspaces = await user.related('workspaces').query(); | ||
42 | |||
43 | const exportData = { | ||
44 | username: user.username, | ||
45 | lastname: user.lastname, | ||
46 | mail: user.email, | ||
47 | services: deepParseToJSON(JSON.parse(JSON.stringify(services))), | ||
48 | workspaces: deepParseToJSON(JSON.parse(JSON.stringify(workspaces))), | ||
49 | }; | ||
50 | |||
51 | return response | ||
52 | .header('Content-Type', 'application/force-download') | ||
53 | .header('Content-disposition', 'attachment; filename=export.ferdium-data') | ||
54 | .send(exportData); | ||
55 | } | ||
56 | } | ||