1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
'use strict'
const User = use('App/Models/User');
const Service = use('App/Models/Service');
const uuid = require('uuid/v4');
class ServiceController {
// Create a new service for user
async create({
request,
response,
auth
}) {
try {
await auth.getUser()
} catch (error) {
return response.send('Missing or invalid api token')
}
const data = request.all();
// Get new, unused uuid
let serviceId;
do {
serviceId = uuid();
} while((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0)
const service = await Service.create({
userId: auth.user.id,
serviceId,
name: data.name,
recipeId: data.recipeId,
settings: JSON.stringify(data)
});
return response.send({
"data": {
userId: auth.user.id,
id: serviceId,
"isEnabled": true,
"isNotificationEnabled": true,
"isBadgeEnabled": true,
"isMuted": false,
"isDarkModeEnabled": "",
"spellcheckerLanguage": "",
"order": 1,
"customRecipe": false,
"hasCustomIcon": false,
"workspaces": [],
"iconUrl": null,
...data,
},
"status": ["created"]
})
}
// List all services a user has created
async list({
request,
response,
auth
}) {
try {
await auth.getUser()
} catch (error) {
return response.send('Missing or invalid api token')
}
const services = (await auth.user.services().fetch()).rows;
// Convert to array with all data Franz wants
const servicesArray = services.map(service => ({
"customRecipe": false,
"hasCustomIcon": false,
"isBadgeEnabled": true,
"isDarkModeEnabled": "",
"isEnabled": true,
"isMuted": false,
"isNotificationEnabled": true,
"order": 1,
"spellcheckerLanguage": "",
"workspaces": [],
"iconUrl": null,
...JSON.parse(service.settings),
"id": service.serviceId,
"name": service.name,
"recipeId": service.recipeId,
"userId": auth.user.id,
}))
return response.send(servicesArray)
}
}
module.exports = ServiceController
|